Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(github): Remote branch existence check (#23480)
- Loading branch information
Showing
5 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as httpMock from '../../../../test/http-mock'; | ||
import { remoteBranchExists } from './branch'; | ||
|
||
describe('modules/platform/github/branch', () => { | ||
it('should return true if the branch exists', async () => { | ||
httpMock | ||
.scope('https://api.github.com') | ||
.head('/repos/my/repo/git/refs/heads/renovate/foobar/') | ||
.reply(404) | ||
.head('/repos/my/repo/git/refs/heads/renovate/foobar') | ||
.reply(200, { ref: 'renovate/foobar' }); | ||
|
||
const result = await remoteBranchExists('my/repo', 'renovate/foobar'); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if the branch does not exist', async () => { | ||
httpMock | ||
.scope('https://api.github.com') | ||
.head('/repos/my/repo/git/refs/heads/renovate/foobar/') | ||
.reply(404) | ||
.head('/repos/my/repo/git/refs/heads/renovate/foobar') | ||
.reply(404); | ||
|
||
const result = await remoteBranchExists('my/repo', 'renovate/foobar'); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should throw an error for nested branches', async () => { | ||
httpMock | ||
.scope('https://api.github.com') | ||
.head('/repos/my/repo/git/refs/heads/renovate/foobar/') | ||
.reply(200); | ||
|
||
await expect( | ||
remoteBranchExists('my/repo', 'renovate/foobar') | ||
).rejects.toThrow( | ||
`Trying to create a branch 'renovate/foobar' while it's the part of nested branch` | ||
); | ||
}); | ||
|
||
it('should throw an error if the request fails for any other reason', async () => { | ||
httpMock | ||
.scope('https://api.github.com') | ||
.head('/repos/my/repo/git/refs/heads/renovate/foobar/') | ||
.reply(500, { message: 'Something went wrong' }); | ||
|
||
await expect( | ||
remoteBranchExists('my/repo', 'renovate/foobar') | ||
).rejects.toThrow('external-host-error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { HttpError } from '../../../util/http'; | ||
import { Result } from '../../../util/result'; | ||
import { githubApi } from './common'; | ||
|
||
function headRef(repo: string, branchName: string): Promise<boolean> { | ||
return Result.wrap( | ||
githubApi.headJson(`/repos/${repo}/git/refs/heads/${branchName}`, { | ||
memCache: false, | ||
}) | ||
) | ||
.transform(() => true) | ||
.catch((err) => { | ||
if (err instanceof HttpError && err.response?.statusCode === 404) { | ||
return Result.ok(false); | ||
} | ||
|
||
return Result.err(err); | ||
}) | ||
.unwrapOrThrow(); | ||
} | ||
|
||
export async function remoteBranchExists( | ||
repo: string, | ||
branchName: string | ||
): Promise<boolean> { | ||
const refNested = `${branchName}/`; | ||
const isNested = await headRef(repo, refNested); | ||
if (isNested) { | ||
const message = `Trying to create a branch '${branchName}' while it's the part of nested branch`; | ||
throw new Error(message); | ||
} | ||
|
||
return headRef(repo, branchName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters