From 73ab069f5e3e1020f658d5a5d0b9f1558f6b0100 Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Tue, 29 Nov 2022 13:47:04 -0500 Subject: [PATCH 1/6] update Stack Overflow plugin to support API Access Token Signed-off-by: Connor Younglund --- plugins/stack-overflow-backend/README.md | 1 + .../search/StackOverflowQuestionsCollatorFactory.ts | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/plugins/stack-overflow-backend/README.md b/plugins/stack-overflow-backend/README.md index a35a6678cb..65fa50e062 100644 --- a/plugins/stack-overflow-backend/README.md +++ b/plugins/stack-overflow-backend/README.md @@ -23,6 +23,7 @@ If you have a private stack overflow instance you will need to supply an API key stackoverflow: baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance apiKey: $STACK_OVERFLOW_API_KEY + apiAccessToken: $STACK_OVERFLOW_API_ACCESS_TOKEN ``` ## Areas of Responsibility diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 1aa0e5d300..68aa83cdeb 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts @@ -52,6 +52,7 @@ export type StackOverflowQuestionsCollatorFactoryOptions = { baseUrl?: string; maxPage?: number; apiKey?: string; + apiAccessToken?: string; requestParams: StackOverflowQuestionsRequestParams; logger: Logger; }; @@ -67,6 +68,7 @@ export class StackOverflowQuestionsCollatorFactory protected requestParams: StackOverflowQuestionsRequestParams; private readonly baseUrl: string | undefined; private readonly apiKey: string | undefined; + private readonly apiAccessToken: string | undefined; private readonly maxPage: number | undefined; private readonly logger: Logger; public readonly type: string = 'stack-overflow'; @@ -74,6 +76,7 @@ export class StackOverflowQuestionsCollatorFactory private constructor(options: StackOverflowQuestionsCollatorFactoryOptions) { this.baseUrl = options.baseUrl; this.apiKey = options.apiKey; + this.apiAccessToken = options.apiAccessToken; this.maxPage = options.maxPage; this.requestParams = options.requestParams; this.logger = options.logger.child({ documentType: this.type }); @@ -84,6 +87,7 @@ export class StackOverflowQuestionsCollatorFactory options: StackOverflowQuestionsCollatorFactoryOptions, ) { const apiKey = config.getOptionalString('stackoverflow.apiKey'); + const apiAccessToken = config.getOptionalString('stackoverflow.apiAccessToken'); const baseUrl = config.getOptionalString('stackoverflow.baseUrl') || 'https://api.stackexchange.com/2.2'; @@ -93,6 +97,7 @@ export class StackOverflowQuestionsCollatorFactory baseUrl, maxPage, apiKey, + apiAccessToken, }); } @@ -138,6 +143,11 @@ export class StackOverflowQuestionsCollatorFactory } const res = await fetch( `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, + this.apiAccessToken ? { + headers: { + 'X-API-Access-Token': this.apiAccessToken, + }, + } : undefined, ); const data = await res.json(); From fd0ca6f447ecfc58855d90c7a90682568662d57f Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Tue, 29 Nov 2022 13:58:51 -0500 Subject: [PATCH 2/6] add changeset Signed-off-by: Connor Younglund --- .changeset/short-turtles-dream.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/short-turtles-dream.md diff --git a/.changeset/short-turtles-dream.md b/.changeset/short-turtles-dream.md new file mode 100644 index 0000000000..9192f9137c --- /dev/null +++ b/.changeset/short-turtles-dream.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-stack-overflow-backend': minor +--- + +Added option to supply API Access Token From ea857549b53fb065ae5ab772fbde40e5f8b24d4a Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Tue, 29 Nov 2022 14:18:23 -0500 Subject: [PATCH 3/6] format with Prettier Signed-off-by: Connor Younglund --- .../StackOverflowQuestionsCollatorFactory.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 68aa83cdeb..ebbad99d0a 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts @@ -87,7 +87,9 @@ export class StackOverflowQuestionsCollatorFactory options: StackOverflowQuestionsCollatorFactoryOptions, ) { const apiKey = config.getOptionalString('stackoverflow.apiKey'); - const apiAccessToken = config.getOptionalString('stackoverflow.apiAccessToken'); + const apiAccessToken = config.getOptionalString( + 'stackoverflow.apiAccessToken', + ); const baseUrl = config.getOptionalString('stackoverflow.baseUrl') || 'https://api.stackexchange.com/2.2'; @@ -143,11 +145,13 @@ export class StackOverflowQuestionsCollatorFactory } const res = await fetch( `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, - this.apiAccessToken ? { - headers: { - 'X-API-Access-Token': this.apiAccessToken, - }, - } : undefined, + this.apiAccessToken + ? { + headers: { + 'X-API-Access-Token': this.apiAccessToken, + }, + } + : undefined, ); const data = await res.json(); From 6756292c2499ea6de1f96dffba442c056120bd92 Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Tue, 29 Nov 2022 14:52:42 -0500 Subject: [PATCH 4/6] generate updated api-report.md Signed-off-by: Connor Younglund --- plugins/stack-overflow-backend/api-report.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/stack-overflow-backend/api-report.md b/plugins/stack-overflow-backend/api-report.md index 11bfbaadc9..f6c7530de9 100644 --- a/plugins/stack-overflow-backend/api-report.md +++ b/plugins/stack-overflow-backend/api-report.md @@ -43,6 +43,7 @@ export type StackOverflowQuestionsCollatorFactoryOptions = { baseUrl?: string; maxPage?: number; apiKey?: string; + apiAccessToken?: string; requestParams: StackOverflowQuestionsRequestParams; logger: Logger; }; From 1c5ee676163d3fea923e62cc953a86547431d743 Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Fri, 2 Dec 2022 15:56:15 -0500 Subject: [PATCH 5/6] address PR feedback Signed-off-by: Connor Younglund --- .changeset/short-turtles-dream.md | 4 ++-- plugins/stack-overflow-backend/README.md | 6 ++++-- plugins/stack-overflow-backend/config.d.ts | 8 +++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.changeset/short-turtles-dream.md b/.changeset/short-turtles-dream.md index 9192f9137c..022ef35f58 100644 --- a/.changeset/short-turtles-dream.md +++ b/.changeset/short-turtles-dream.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-stack-overflow-backend': minor +'@backstage/plugin-stack-overflow-backend': patch --- -Added option to supply API Access Token +Added option to supply API Access Token. This is required in addition to an API key when trying to access the data for a private Stack Overflow Team. diff --git a/plugins/stack-overflow-backend/README.md b/plugins/stack-overflow-backend/README.md index 65fa50e062..0d88ea98d1 100644 --- a/plugins/stack-overflow-backend/README.md +++ b/plugins/stack-overflow-backend/README.md @@ -15,9 +15,11 @@ stackoverflow: baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance ``` -### Stack Overflow for Teams (private stack overflow instance) +### Stack Overflow for Teams -If you have a private stack overflow instance you will need to supply an API key as well. You can read more about how to set this up by going to [Stack Overflows Help Page](https://stackoverflow.help/en/articles/4385859-stack-overflow-for-teams-api) +If you have an private Stack Overflow instance and/or a private Stack Overflow Team you will need to supply an API key. You can read more about how to set this up by going to [Stack Overflow's Help Page](https://stackoverflow.help/en/articles/4385859-stack-overflow-for-teams-api). + +A private Stack Overflow Team requires both an API key and an API Access Token. Step 3 ("Generate an Access Token via OAuth") from the previously mentioned Help Page explains the process for generating an API Access Token with no expiration. ```yaml stackoverflow: diff --git a/plugins/stack-overflow-backend/config.d.ts b/plugins/stack-overflow-backend/config.d.ts index 617524f0a3..54e517c5d8 100644 --- a/plugins/stack-overflow-backend/config.d.ts +++ b/plugins/stack-overflow-backend/config.d.ts @@ -25,9 +25,15 @@ export interface Config { baseUrl?: string; /** - * The api key to authenticate to Stack Overflow API + * The API key to authenticate to Stack Overflow API * @visibility secret */ apiKey?: string; + + /** + * The API Access Token to authenticate to Stack Overflow API + * @visibility secret + */ + apiAccessToken?: string; }; } From fa7dacc5c7d8b689b2571e6754126db5424b85a2 Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Fri, 2 Dec 2022 15:58:46 -0500 Subject: [PATCH 6/6] an --> a Signed-off-by: Connor Younglund --- plugins/stack-overflow-backend/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stack-overflow-backend/README.md b/plugins/stack-overflow-backend/README.md index 0d88ea98d1..4bcc755404 100644 --- a/plugins/stack-overflow-backend/README.md +++ b/plugins/stack-overflow-backend/README.md @@ -17,7 +17,7 @@ stackoverflow: ### Stack Overflow for Teams -If you have an private Stack Overflow instance and/or a private Stack Overflow Team you will need to supply an API key. You can read more about how to set this up by going to [Stack Overflow's Help Page](https://stackoverflow.help/en/articles/4385859-stack-overflow-for-teams-api). +If you have a private Stack Overflow instance and/or a private Stack Overflow Team you will need to supply an API key. You can read more about how to set this up by going to [Stack Overflow's Help Page](https://stackoverflow.help/en/articles/4385859-stack-overflow-for-teams-api). A private Stack Overflow Team requires both an API key and an API Access Token. Step 3 ("Generate an Access Token via OAuth") from the previously mentioned Help Page explains the process for generating an API Access Token with no expiration.