From 3a7deeef9208a776d70693c72518fea79f4647d4 Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Thu, 4 Jan 2024 10:35:27 +0100 Subject: [PATCH 1/3] fix: api returns questions with relevant tags Signed-off-by: Tommy Le --- plugins/stack-overflow/src/api/StackOverflowClient.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/stack-overflow/src/api/StackOverflowClient.ts b/plugins/stack-overflow/src/api/StackOverflowClient.ts index f598f912bd..d2f95c5236 100644 --- a/plugins/stack-overflow/src/api/StackOverflowClient.ts +++ b/plugins/stack-overflow/src/api/StackOverflowClient.ts @@ -46,6 +46,7 @@ export class StackOverflowClient implements StackOverflowApi { }): Promise { const params = qs.stringify(options.requestParams, { addQueryPrefix: true, + arrayFormat: 'repeat', }); const response = await fetch(`${this.baseUrl}/questions${params}`); const data = await response.json(); From c1bc3318b48d43e1d39af72044cee488b83c5cf4 Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Thu, 4 Jan 2024 10:41:33 +0100 Subject: [PATCH 2/3] add changeset Signed-off-by: Tommy Le --- .changeset/polite-meals-hug.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/polite-meals-hug.md diff --git a/.changeset/polite-meals-hug.md b/.changeset/polite-meals-hug.md new file mode 100644 index 0000000000..664f450c6d --- /dev/null +++ b/.changeset/polite-meals-hug.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-stack-overflow': patch +--- + +Fixes a bug that made the API return questions not related to the tags provided From 108c28f5ba2fdc2539decc23e7cf21a36ea5ca0b Mon Sep 17 00:00:00 2001 From: Tommy Le Date: Tue, 9 Jan 2024 10:40:25 +0100 Subject: [PATCH 3/3] add test Signed-off-by: Tommy Le --- .../src/api/StackOverflowClient.test.ts | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/plugins/stack-overflow/src/api/StackOverflowClient.test.ts b/plugins/stack-overflow/src/api/StackOverflowClient.test.ts index 6574771350..e5967b6d45 100644 --- a/plugins/stack-overflow/src/api/StackOverflowClient.test.ts +++ b/plugins/stack-overflow/src/api/StackOverflowClient.test.ts @@ -27,19 +27,34 @@ const backstageQuestions: StackOverflowQuestion[] = [ { title: 'What is it?', link: 'https://example.com:9191/questions/1', - tags: ['asdf'], + tags: ['backstage'], owner: { who: 'me' }, answer_count: 3, }, { title: 'Is it?', link: 'https://example.com:9191/questions/2', - tags: ['asdf'], + tags: ['backstage'], owner: { who: 'me' }, answer_count: 4, }, ]; +const vimQuestions: StackOverflowQuestion[] = [ + { + title: 'How do I exit vim?', + link: 'https://example.com:9191/questions/3', + tags: ['vim'], + owner: { who: 'me' }, + answer_count: 5, + }, +]; + +const questionMap: Record = { + backstage: backstageQuestions, + vim: vimQuestions, +}; + describe('StackOverflowClient', () => { setupRequestMockHandlers(server); @@ -48,19 +63,15 @@ describe('StackOverflowClient', () => { const setupHandlers = () => { server.use( rest.get(`${mockBaseUrl}/questions`, (req, res, ctx) => { - return res( - ctx.json({ - items: - req.url.searchParams.get('tagged') === 'backstage' - ? backstageQuestions - : [], - }), - ); + const taggedParam = req.url.searchParams.get('tagged'); + const questions = taggedParam ? questionMap[taggedParam] || [] : []; + + return res(ctx.json({ items: questions })); }), ); }; - it('list questions should return all questions', async () => { + it('list questions should return all questions with the provided tag', async () => { setupHandlers(); const client = StackOverflowClient.fromConfig( new ConfigReader({ @@ -70,10 +81,17 @@ describe('StackOverflowClient', () => { }), ); - const responseQuestions = await client.listQuestions({ + const bsQuestions = await client.listQuestions({ requestParams: { tagged: 'backstage' }, }); - expect(responseQuestions.length).toEqual(2); - expect(responseQuestions).toEqual(backstageQuestions); + + const vQuestions = await client.listQuestions({ + requestParams: { tagged: 'vim' }, + }); + + expect(bsQuestions.length).toEqual(2); + expect(bsQuestions).toEqual(backstageQuestions); + expect(vQuestions.length).toEqual(1); + expect(vQuestions).toEqual(vimQuestions); }); });