From 4c7e1fafc86b2ac302d01cc188e5f6f1f99e366c Mon Sep 17 00:00:00 2001 From: MattIPv4 Date: Tue, 27 Oct 2026 00:10:52 +0000 Subject: [PATCH] fix: handle renamed branch refs for signed commits --- __test__/github-helper.unit.test.ts | 82 +++++++++++++++++++++++++++++ src/github-helper.ts | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 __test__/github-helper.unit.test.ts diff --git a/__test__/github-helper.unit.test.ts b/__test__/github-helper.unit.test.ts new file mode 100644 index 0000000000..3cc897517a --- /dev/null +++ b/__test__/github-helper.unit.test.ts @@ -0,0 +1,82 @@ +jest.mock('../src/octokit-client', () => ({ + Octokit: class {}, + retryOptions: {}, + throttleOptions: {} +})) +jest.mock('@octokit/request-error', () => ({ + RequestError: class extends Error {} +})) +jest.mock('p-limit', () => ({ + __esModule: true, + default: () => (fn: () => unknown) => fn() +})) + +import {GitHubHelper} from '../src/github-helper' +import type {Commit, GitCommandManager} from '../src/git-command-manager' + +describe('GitHubHelper signed commit refs', () => { + const createHelper = (returnedBranchName: string) => { + const octokit = { + rest: { + repos: { + getBranch: jest + .fn() + .mockResolvedValue({data: {name: returnedBranchName}}) + }, + git: { + updateRef: jest.fn().mockResolvedValue({}), + createRef: jest.fn().mockResolvedValue({}) + } + } + } + const helper = new GitHubHelper('github.com', '') + Object.defineProperty(helper, 'octokit', {value: octokit}) + return {helper, octokit} + } + + const git = {} as GitCommandManager + const baseCommit = {sha: 'base-sha', tree: 'tree-sha'} as Commit + + test('creates a ref when getBranch redirects to a renamed branch', async () => { + const {helper, octokit} = createHelper('renamed-topic') + + await helper.pushSignedCommits( + git, + [], + baseCommit, + '', + 'owner/repo', + 'topic' + ) + + expect(octokit.rest.git.createRef).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + sha: 'base-sha', + ref: 'refs/heads/topic' + }) + expect(octokit.rest.git.updateRef).not.toHaveBeenCalled() + }) + + test('updates the ref when getBranch returns the requested branch', async () => { + const {helper, octokit} = createHelper('topic') + + await helper.pushSignedCommits( + git, + [], + baseCommit, + '', + 'owner/repo', + 'topic' + ) + + expect(octokit.rest.git.updateRef).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + sha: 'base-sha', + ref: 'heads/topic', + force: true + }) + expect(octokit.rest.git.createRef).not.toHaveBeenCalled() + }) +}) diff --git a/src/github-helper.ts b/src/github-helper.ts index 26ef65cb87..7cd07795bf 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -441,7 +441,7 @@ export class GitHubHelper { branch: branch }) .then( - () => true, + ({data}) => data.name === branch, () => false )