From a0ac7496ad6f15de0be262c28a18d354c95e1815 Mon Sep 17 00:00:00 2001 From: Chris Hawkins Date: Wed, 29 Mar 2023 16:59:30 +0100 Subject: [PATCH] Support for 2.3 teamName Signed-off-by: Chris Hawkins --- ...ckOverflowQuestionsCollatorFactory.test.ts | 44 +++++++++++++++++++ .../StackOverflowQuestionsCollatorFactory.ts | 27 ++++++++++-- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.test.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.test.ts index bdc5be125c..5d2f65923c 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.test.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.test.ts @@ -139,5 +139,49 @@ describe('StackOverflowQuestionsCollatorFactory', () => { expect(documents).toHaveLength(mockOverrideQuestion.items.length); }); + + it('uses API key when provided', async () => { + worker.use( + rest.get('http://stack.overflow.override/questions', (req, res, ctx) => + req.url.searchParams.get('key') === 'abcdefg' + ? res(ctx.status(200), ctx.json(mockOverrideQuestion)) + : res(ctx.status(401), ctx.json({})), + ), + ); + const factory = StackOverflowQuestionsCollatorFactory.fromConfig(config, { + logger, + baseUrl: 'http://stack.overflow.override', + apiKey: 'abcdefg', + requestParams: defaultOptions.requestParams, + }); + const collator = await factory.getCollator(); + + const pipeline = TestPipeline.fromCollator(collator); + const { documents } = await pipeline.execute(); + + expect(documents).toHaveLength(mockOverrideQuestion.items.length); + }); + + it('uses teamName when provided', async () => { + worker.use( + rest.get('http://stack.overflow.override/questions', (req, res, ctx) => + req.url.searchParams.get('team') === 'abcdefg' + ? res(ctx.status(200), ctx.json(mockOverrideQuestion)) + : res(ctx.status(401), ctx.json({})), + ), + ); + const factory = StackOverflowQuestionsCollatorFactory.fromConfig(config, { + logger, + baseUrl: 'http://stack.overflow.override', + teamName: 'abcdefg', + requestParams: defaultOptions.requestParams, + }); + const collator = await factory.getCollator(); + + const pipeline = TestPipeline.fromCollator(collator); + const { documents } = await pipeline.execute(); + + expect(documents).toHaveLength(mockOverrideQuestion.items.length); + }); }); }); diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 807d6637c1..ccded6b266 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts @@ -53,6 +53,7 @@ export type StackOverflowQuestionsCollatorFactoryOptions = { maxPage?: number; apiKey?: string; apiAccessToken?: string; + teamName?: string; requestParams: StackOverflowQuestionsRequestParams; logger: Logger; }; @@ -69,6 +70,7 @@ export class StackOverflowQuestionsCollatorFactory private readonly baseUrl: string | undefined; private readonly apiKey: string | undefined; private readonly apiAccessToken: string | undefined; + private readonly teamName: string | undefined; private readonly maxPage: number | undefined; private readonly logger: Logger; public readonly type: string = 'stack-overflow'; @@ -77,6 +79,7 @@ export class StackOverflowQuestionsCollatorFactory this.baseUrl = options.baseUrl; this.apiKey = options.apiKey; this.apiAccessToken = options.apiAccessToken; + this.teamName = options.teamName; this.maxPage = options.maxPage; this.requestParams = options.requestParams; this.logger = options.logger.child({ documentType: this.type }); @@ -90,15 +93,18 @@ export class StackOverflowQuestionsCollatorFactory const apiAccessToken = config.getOptionalString( 'stackoverflow.apiAccessToken', ); + const pat = config.getOptionalString('stackoverflow.pat'); + const teamName = config.getOptionalString('stackoverflow.teamName'); const baseUrl = config.getOptionalString('stackoverflow.baseUrl') || - 'https://api.stackexchange.com/2.2'; + 'https://api.stackexchange.com/2.3'; const maxPage = options.maxPage || 100; return new StackOverflowQuestionsCollatorFactory({ baseUrl, maxPage, apiKey, apiAccessToken, + teamName, ...options, }); } @@ -114,10 +120,16 @@ export class StackOverflowQuestionsCollatorFactory ); } + if (this.apiKey && this.teamName) { + this.logger.debug( + 'Both stackoverflow.apiKey and stackoverflow.teamName configured in your app-config.yaml, apiKey must be removed before teamName will be used', + ); + } + try { if (Object.keys(this.requestParams).indexOf('key') >= 0) { this.logger.warn( - 'The API Key should be passed as a seperate param to bypass encoding', + 'The API Key should be passed as a separate param to bypass encoding', ); delete this.requestParams.key; } @@ -134,6 +146,15 @@ export class StackOverflowQuestionsCollatorFactory ? `${params ? '&' : '?'}key=${this.apiKey}` : ''; + const teamParam = this.teamName + ? `${params ? '&' : '?'}team=${this.teamName}` + : ''; + + // PAT change requires team name as a parameter + const requestUrl = this.apiKey + ? `${this.baseUrl}/questions${params}${apiKeyParam}` + : `${this.baseUrl}/questions${params}${teamParam}`; + let hasMorePages = true; let page = 1; while (hasMorePages) { @@ -144,7 +165,7 @@ export class StackOverflowQuestionsCollatorFactory break; } const res = await fetch( - `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, + `${requestUrl}&page=${page}`, this.apiAccessToken ? { headers: {