From 3fa1b533f1e12923e2f2985386df7558610c0159 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Tue, 31 Dec 2024 16:18:36 +0530 Subject: [PATCH 1/3] remove deleted issue from cache --- lib/modules/platform/github/index.ts | 7 ++++++- lib/modules/platform/github/issue.ts | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 93488565ba630a..5dc0a5a3a6a626 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -1247,7 +1247,12 @@ export async function getIssue(number: number): Promise { GithubIssueCache.updateIssue(issue); return issue; } catch (err) /* istanbul ignore next */ { - logger.debug({ err, number }, 'Error getting issue'); + logger.debug({ err, number: number }, 'Error getting issue'); + // istanbul ignore if + if (err.message === 'Response Code 410(Gone)') { + logger.debug(`Issue #${number} has been deleted`); + GithubIssueCache.deleteIssue(number); + } return null; } } diff --git a/lib/modules/platform/github/issue.ts b/lib/modules/platform/github/issue.ts index 1cc85151777233..11a5d050dceceb 100644 --- a/lib/modules/platform/github/issue.ts +++ b/lib/modules/platform/github/issue.ts @@ -85,6 +85,13 @@ export class GithubIssueCache { } } + static deleteIssue(number: number): void { + const cacheData = this.data; + if (cacheData) { + delete cacheData[number]; + } + } + /** * At the moment of repo initialization, repository cache is not available. * What we can do is to store issues for later reconciliation. From 71b07eab4f8a34f5943cc25b5a7f809c773f7137 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Tue, 31 Dec 2024 16:48:33 +0530 Subject: [PATCH 2/3] fix: ci issues --- lib/modules/platform/github/index.ts | 2 +- lib/modules/platform/github/issue.spec.ts | 26 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 5dc0a5a3a6a626..064e29cb2cf208 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -1247,7 +1247,7 @@ export async function getIssue(number: number): Promise { GithubIssueCache.updateIssue(issue); return issue; } catch (err) /* istanbul ignore next */ { - logger.debug({ err, number: number }, 'Error getting issue'); + logger.debug({ err, number }, 'Error getting issue'); // istanbul ignore if if (err.message === 'Response Code 410(Gone)') { logger.debug(`Issue #${number} has been deleted`); diff --git a/lib/modules/platform/github/issue.spec.ts b/lib/modules/platform/github/issue.spec.ts index 749364ddf5ad4e..8a9e9bd1cb82a4 100644 --- a/lib/modules/platform/github/issue.spec.ts +++ b/lib/modules/platform/github/issue.spec.ts @@ -159,6 +159,32 @@ describe('modules/platform/github/issue', () => { }); }); + it('removes particular issue from the cache', () => { + cache.platform = { + github: { + issuesCache: { + '1': { + number: 1, + body: 'body-1', + state: 'open', + title: 'title-1', + lastModified: '2020-01-01T00:00:00.000Z', + }, + }, + }, + }; + + GithubIssueCache.deleteIssue(1); + + expect(cache).toEqual({ + platform: { + github: { + issuesCache: {}, + }, + }, + }); + }); + it('reconciles cache', () => { cache.platform = { github: { From f6087c3d277a440c4abe9a7af5bb6a1fe0d64750 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Tue, 7 Jan 2025 05:49:20 +0530 Subject: [PATCH 3/3] Apply Suggestions --- lib/modules/platform/github/index.spec.ts | 51 +++++++++++++++++++++++ lib/modules/platform/github/index.ts | 6 +-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts index eb133f6ecae99b..9eaf850b78dce7 100644 --- a/lib/modules/platform/github/index.spec.ts +++ b/lib/modules/platform/github/index.spec.ts @@ -1531,6 +1531,57 @@ describe('modules/platform/github/index', () => { }); }); + describe('getIssue()', () => { + it('returns null if issues disabled', async () => { + const scope = httpMock.scope(githubApiHost); + initRepoMock(scope, 'some/repo', { hasIssuesEnabled: false }); + await github.initRepo({ repository: 'some/repo' }); + const res = await github.getIssue(1); + expect(res).toBeNull(); + }); + + it('returns issue', async () => { + const scope = httpMock.scope(githubApiHost); + initRepoMock(scope, 'some/repo'); + const issue = { + number: 1, + state: 'open', + title: 'title-1', + body: 'body-1', + }; + scope + .get('/repos/some/repo/issues/1') + .reply(200, { ...issue, updated_at: '2022-01-01T00:00:00Z' }); + await github.initRepo({ repository: 'some/repo' }); + const res = await github.getIssue(1); + expect(res).toMatchObject({ + ...issue, + lastModified: '2022-01-01T00:00:00Z', + }); + }); + + it('returns null if issue not found', async () => { + const scope = httpMock.scope(githubApiHost); + initRepoMock(scope, 'some/repo'); + scope.get('/repos/some/repo/issues/1').reply(404); + await github.initRepo({ repository: 'some/repo' }); + const res = await github.getIssue(1); + expect(res).toBeNull(); + }); + + it('logs debug message if issue deleted', async () => { + const scope = httpMock.scope(githubApiHost); + initRepoMock(scope, 'some/repo'); + scope.get('/repos/some/repo/issues/1').reply(410); + await github.initRepo({ repository: 'some/repo' }); + const res = await github.getIssue(1); + expect(res).toBeNull(); + expect(logger.logger.debug).toHaveBeenCalledWith( + 'Issue #1 has been deleted', + ); + }); + }); + describe('findIssue()', () => { it('returns null if no issue', async () => { httpMock diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 064e29cb2cf208..8551653ce0dac3 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -1231,7 +1231,6 @@ export async function getIssueList(): Promise { } export async function getIssue(number: number): Promise { - // istanbul ignore if if (config.hasIssuesEnabled === false) { return null; } @@ -1246,10 +1245,9 @@ export async function getIssue(number: number): Promise { ); GithubIssueCache.updateIssue(issue); return issue; - } catch (err) /* istanbul ignore next */ { + } catch (err) { logger.debug({ err, number }, 'Error getting issue'); - // istanbul ignore if - if (err.message === 'Response Code 410(Gone)') { + if (err.response?.statusCode === 410) { logger.debug(`Issue #${number} has been deleted`); GithubIssueCache.deleteIssue(number); }