diff --git a/src/github.ts b/src/github.ts index d99e61b16..29f1d52b4 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1169,6 +1169,20 @@ export class GitHub { draft: !!options?.draft, labels: pullRequest.labels, }); + if (prNumber === 0) { + this.logger.warn( + 'no code changes detected, skipping pull request creation' + ); + return { + headBranchName: pullRequest.headBranchName, + baseBranchName: targetBranch, + number: 0, + title: pullRequest.title, + body: pullRequest.body, + labels: pullRequest.labels, + files: [], + }; + } return await this.getPullRequest(prNumber); } ); diff --git a/test/github.ts b/test/github.ts index 5bb64d62d..242bf17b8 100644 --- a/test/github.ts +++ b/test/github.ts @@ -1142,6 +1142,33 @@ describe('GitHub', () => { }); }); + describe('createPullRequest', () => { + it('should not call getPullRequest when no code changes detected', async () => { + const createPullRequestStub = sandbox + .stub(codeSuggester, 'createPullRequest') + .resolves(0); + const getPullRequestStub = sandbox.stub(github, 'getPullRequest'); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const pullRequest = await (github as any).createPullRequest( + { + headBranchName: 'release-please--branches--main', + baseBranchName: 'main', + title: 'Release v1.0.0', + body: 'Release body', + labels: ['release-please'], + }, + 'main', + 'commit message', + [] + ); + + expect(pullRequest.number).to.eql(0); + sinon.assert.calledOnce(createPullRequestStub); + sinon.assert.notCalled(getPullRequestStub); + }); + }); + describe('buildChangeSet', () => { it('should merge updates for the same file', async () => { const manifestPath = '.release-please-manifest.json';