Skip to content

Commit

Permalink
fix(github): remove deleted issue from issues cache (#33349)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Jan 8, 2025
1 parent 974a8a8 commit 5282f7c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
51 changes: 51 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,6 @@ export async function getIssueList(): Promise<Issue[]> {
}

export async function getIssue(number: number): Promise<Issue | null> {
// istanbul ignore if
if (config.hasIssuesEnabled === false) {
return null;
}
Expand All @@ -1246,8 +1245,12 @@ export async function getIssue(number: number): Promise<Issue | null> {
);
GithubIssueCache.updateIssue(issue);
return issue;
} catch (err) /* istanbul ignore next */ {
} catch (err) {
logger.debug({ err, number }, 'Error getting issue');
if (err.response?.statusCode === 410) {
logger.debug(`Issue #${number} has been deleted`);
GithubIssueCache.deleteIssue(number);
}
return null;
}
}
Expand Down
26 changes: 26 additions & 0 deletions lib/modules/platform/github/issue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 7 additions & 0 deletions lib/modules/platform/github/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5282f7c

Please sign in to comment.