From 713be06b2bd26a2460732ca2e7d523aab0aa9821 Mon Sep 17 00:00:00 2001 From: Matt Mulligan Date: Thu, 19 May 2022 14:43:41 -0400 Subject: [PATCH] Added apikey config for StackOverflow backend Signed-off-by: Matt Mulligan --- .changeset/few-monkeys-grab.md | 5 +++++ plugins/stack-overflow-backend/CHANGELOG.md | 6 ++++++ plugins/stack-overflow-backend/README.md | 10 ++++++++++ plugins/stack-overflow-backend/package.json | 2 +- .../StackOverflowQuestionsCollatorFactory.ts | 15 +++++++++++++-- 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/few-monkeys-grab.md diff --git a/.changeset/few-monkeys-grab.md b/.changeset/few-monkeys-grab.md new file mode 100644 index 0000000000..b841445b68 --- /dev/null +++ b/.changeset/few-monkeys-grab.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-stack-overflow-backend': patch +--- + +Added an apiKey configuration to app-config.yaml to prevent encoding the api key when passed to a private stack overflow instance diff --git a/plugins/stack-overflow-backend/CHANGELOG.md b/plugins/stack-overflow-backend/CHANGELOG.md index 1483589528..bca947fecb 100644 --- a/plugins/stack-overflow-backend/CHANGELOG.md +++ b/plugins/stack-overflow-backend/CHANGELOG.md @@ -1,5 +1,11 @@ # @backstage/plugin-stack-overflow-backend +## 0.1.2 + +### Patch Changes + +- Added apiKey configuration to prevent encoding the key value + ## 0.1.1 ### Patch Changes diff --git a/plugins/stack-overflow-backend/README.md b/plugins/stack-overflow-backend/README.md index f2eaf660c2..62115b889c 100644 --- a/plugins/stack-overflow-backend/README.md +++ b/plugins/stack-overflow-backend/README.md @@ -15,6 +15,16 @@ stackoverflow: baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance ``` +### Stack Overflow for Teams (private stack overflow instance) + +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) + +```yaml +stackoverflow: + baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance + apiKey: $STACK_OVERFLOW_API_KEY +``` + ## Areas of Responsibility This stack overflow backend plugin is primarily responsible for the following: diff --git a/plugins/stack-overflow-backend/package.json b/plugins/stack-overflow-backend/package.json index dd842ba01e..c4416be8b6 100644 --- a/plugins/stack-overflow-backend/package.json +++ b/plugins/stack-overflow-backend/package.json @@ -1,6 +1,6 @@ { "name": "@backstage/plugin-stack-overflow-backend", - "version": "0.1.1", + "version": "0.1.2", "main": "src/index.ts", "types": "src/index.ts", "license": "Apache-2.0", diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 98fc86c2ea..55de71b7c7 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; requestParams: StackOverflowQuestionsRequestParams; logger: Logger; + config: Config; }; /** @@ -65,12 +66,14 @@ export class StackOverflowQuestionsCollatorFactory protected requestParams: StackOverflowQuestionsRequestParams; private readonly baseUrl: string | undefined; private readonly logger: Logger; + private readonly config: Config; public readonly type: string = 'stack-overflow'; private constructor(options: StackOverflowQuestionsCollatorFactoryOptions) { this.baseUrl = options.baseUrl; this.requestParams = options.requestParams; this.logger = options.logger; + this.config = options.config; } static fromConfig( @@ -80,7 +83,11 @@ export class StackOverflowQuestionsCollatorFactory const baseUrl = config.getOptionalString('stackoverflow.baseUrl') || 'https://api.stackexchange.com/2.2'; - return new StackOverflowQuestionsCollatorFactory({ ...options, baseUrl }); + return new StackOverflowQuestionsCollatorFactory({ + ...options, + baseUrl, + config, + }); } async getCollator() { @@ -93,12 +100,16 @@ export class StackOverflowQuestionsCollatorFactory `No stackoverflow.baseUrl configured in your app-config.yaml`, ); } + const params = qs.stringify(this.requestParams, { arrayFormat: 'comma', addQueryPrefix: true, }); - const res = await fetch(`${this.baseUrl}/questions${params}`); + const apiKey = this.config.getOptionalString('stackoverflow.apiKey'); + const apiKeyParam = apiKey ? `${params ? '&' : '?'}key=${apiKey}` : ''; + + const res = await fetch(`${this.baseUrl}/questions${params}${apiKeyParam}`); const data = await res.json(); for (const question of data.items) {