Skip to content

Commit

Permalink
fix(github): catch fork list failures gracefully (#18933)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored Nov 16, 2022
1 parent 7adc861 commit 9562cf4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
31 changes: 28 additions & 3 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ describe('modules/platform/github/index', () => {
scope: httpMock.Scope,
repository: string,
forkExisted: boolean,
forkResult = 200,
forkDefaulBranch = 'master'
): void {
scope
Expand Down Expand Up @@ -293,7 +294,7 @@ describe('modules/platform/github/index', () => {
// getForks
.get(`/repos/${repository}/forks?per_page=100`)
.reply(
200,
forkResult,
forkExisted
? [
{
Expand Down Expand Up @@ -335,7 +336,7 @@ describe('modules/platform/github/index', () => {
const repo = 'some/repo';
const branch = 'master';
const scope = httpMock.scope(githubApiHost);
forkInitRepoMock(scope, repo, false, branch);
forkInitRepoMock(scope, repo, false, 200, branch);
scope.get('/user').reply(404);
await expect(
github.initRepo({
Expand All @@ -345,6 +346,30 @@ describe('modules/platform/github/index', () => {
).rejects.toThrow(REPOSITORY_CANNOT_FORK);
});

it('throws when listing forks with 404', async () => {
const repo = 'some/repo';
const scope = httpMock.scope(githubApiHost);
forkInitRepoMock(scope, repo, false, 404);
await expect(
github.initRepo({
repository: 'some/repo',
forkToken: 'true',
})
).rejects.toThrow(REPOSITORY_CANNOT_FORK);
});

it('throws when listing forks with 500', async () => {
const repo = 'some/repo';
const scope = httpMock.scope(githubApiHost);
forkInitRepoMock(scope, repo, false, 500);
await expect(
github.initRepo({
repository: 'some/repo',
forkToken: 'true',
})
).rejects.toThrow(REPOSITORY_CANNOT_FORK);
});

it('throws when error creating fork', async () => {
const repo = 'some/repo';
const scope = httpMock.scope(githubApiHost);
Expand Down Expand Up @@ -378,7 +403,7 @@ describe('modules/platform/github/index', () => {

it('detects fork default branch mismatch', async () => {
const scope = httpMock.scope(githubApiHost);
forkInitRepoMock(scope, 'some/repo', true, 'not_master');
forkInitRepoMock(scope, 'some/repo', true, 200, 'not_master');
scope.get('/user').reply(200, {
login: 'forked',
});
Expand Down
31 changes: 20 additions & 11 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,26 @@ export async function listForks(
token: string,
repository: string
): Promise<GhRestRepo[]> {
// Get list of existing repos
const url = `repos/${repository}/forks?per_page=100`;
const repos = (
await githubApi.getJson<GhRestRepo[]>(url, {
token,
paginate: true,
pageLimit: 100,
})
).body;
logger.debug(`Found ${repos.length} forked repo(s)`);
return repos;
try {
// Get list of existing repos
const url = `repos/${repository}/forks?per_page=100`;
const repos = (
await githubApi.getJson<GhRestRepo[]>(url, {
token,
paginate: true,
pageLimit: 100,
})
).body;
logger.debug(`Found ${repos.length} forked repo(s)`);
return repos;
} catch (err) {
if (err.statusCode === 404) {
logger.debug('Cannot list repo forks - it is likely private');
} else {
logger.debug({ err }, 'Unknown error listing repository forks');
}
throw new Error(REPOSITORY_CANNOT_FORK);
}
}

export async function findFork(
Expand Down

0 comments on commit 9562cf4

Please sign in to comment.