From ea5631a8b2281a97005d099903f3d442e68c0d9a Mon Sep 17 00:00:00 2001 From: Matt Mulligan Date: Wed, 22 Jun 2022 10:55:28 -0400 Subject: [PATCH] updated the frontend plugin to accept apikey config Signed-off-by: Matt Mulligan --- .changeset/friendly-sheep-flash.md | 5 +++++ plugins/stack-overflow/README.md | 10 ++++++++++ plugins/stack-overflow/config.d.ts | 6 ++++++ .../src/home/StackOverflowQuestions/Content.tsx | 17 +++++++++++++++-- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .changeset/friendly-sheep-flash.md diff --git a/.changeset/friendly-sheep-flash.md b/.changeset/friendly-sheep-flash.md new file mode 100644 index 0000000000..27fd383f82 --- /dev/null +++ b/.changeset/friendly-sheep-flash.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-stack-overflow': patch +--- + +Added API key configuration to bypass encoding diff --git a/plugins/stack-overflow/README.md b/plugins/stack-overflow/README.md index 96a8f524e3..637afd0494 100644 --- a/plugins/stack-overflow/README.md +++ b/plugins/stack-overflow/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 frontend plugin is primarily responsible for the following: diff --git a/plugins/stack-overflow/config.d.ts b/plugins/stack-overflow/config.d.ts index 811893231e..5ea62be2bd 100644 --- a/plugins/stack-overflow/config.d.ts +++ b/plugins/stack-overflow/config.d.ts @@ -24,5 +24,11 @@ export interface Config { * @visibility frontend */ baseUrl: string; + + /** + * The api key to authenticate to Stack Overflow API + * @visibility backend + */ + apiKey: string; }; } diff --git a/plugins/stack-overflow/src/home/StackOverflowQuestions/Content.tsx b/plugins/stack-overflow/src/home/StackOverflowQuestions/Content.tsx index fbc3a90a9d..8b954f5cf4 100644 --- a/plugins/stack-overflow/src/home/StackOverflowQuestions/Content.tsx +++ b/plugins/stack-overflow/src/home/StackOverflowQuestions/Content.tsx @@ -42,6 +42,7 @@ import { export const Content = (props: StackOverflowQuestionsContentProps) => { const { requestParams } = props; const configApi = useApi(configApiRef); + const apiKey = configApi.getOptionalString('stackoverflow.apiKey'); const baseUrl = configApi.getOptionalString('stackoverflow.baseUrl') || 'https://api.stackexchange.com/2.2'; @@ -49,8 +50,20 @@ export const Content = (props: StackOverflowQuestionsContentProps) => { const { value, loading, error } = useAsync(async (): Promise< StackOverflowQuestion[] > => { - const params = qs.stringify(requestParams, { addQueryPrefix: true }); - const response = await fetch(`${baseUrl}/questions${params}`); + try { + if (Object.keys(requestParams).indexOf('key') >= 0) { + delete requestParams.key; + } + } catch (e) { + // console.log("Failed to remove key from params"); + } + + const params = qs.stringify(requestParams, { + arrayFormat: 'comma', + addQueryPrefix: true, + }); + const apiKeyParam = apiKey ? `${params ? '&' : '?'}key=${apiKey}` : ''; + const response = await fetch(`${baseUrl}/questions${params}${apiKeyParam}`); const data = await response.json(); return data.items; }, []);