Skip to content

Commit 3944b17

Browse files
authored
fix: catch FileNotFound error when building changeset (#1306)
* test: add tests for building changeset * fix: catch FileNotFound error when building changeset
1 parent 73f0260 commit 3944b17

2 files changed

Lines changed: 145 additions & 3 deletions

File tree

src/github.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import {Octokit} from '@octokit/rest';
2020
import {request} from '@octokit/request';
2121
import {graphql} from '@octokit/graphql';
2222
import {RequestError} from '@octokit/request-error';
23-
import {GitHubAPIError, DuplicateReleaseError} from './errors';
23+
import {
24+
GitHubAPIError,
25+
DuplicateReleaseError,
26+
FileNotFoundError,
27+
} from './errors';
2428

2529
const MAX_ISSUE_BODY_SIZE = 65536;
2630
export const GH_API_URL = 'https://api.github.com';
@@ -1034,7 +1038,7 @@ export class GitHub {
10341038
defaultBranch
10351039
);
10361040
} catch (err) {
1037-
if (err.status !== 404) throw err;
1041+
if (!(err instanceof FileNotFoundError)) throw err;
10381042
// if the file is missing and create = false, just continue
10391043
// to the next update, otherwise create the file.
10401044
if (!update.createIfMissing) {

test/github.ts

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ import {PullRequest} from '../src/pull-request';
2727
import {TagName} from '../src/util/tag-name';
2828
import {Version} from '../src/version';
2929
import assert = require('assert');
30-
import {DuplicateReleaseError, GitHubAPIError} from '../src/errors';
30+
import {
31+
DuplicateReleaseError,
32+
GitHubAPIError,
33+
FileNotFoundError,
34+
} from '../src/errors';
3135
import {fail} from 'assert';
36+
import {PullRequestBody} from '../src/util/pull-request-body';
37+
import {PullRequestTitle} from '../src/util/pull-request-title';
38+
import * as codeSuggester from 'code-suggester';
39+
import {RawContent} from '../src/updaters/raw-content';
3240

3341
const fixturesPath = './test/fixtures';
3442
const sandbox = sinon.createSandbox();
@@ -776,4 +784,134 @@ describe('GitHub', () => {
776784
);
777785
});
778786
});
787+
788+
describe('createReleasePullRequest', () => {
789+
it('should update file', async () => {
790+
const createPullRequestStub = sandbox
791+
.stub(codeSuggester, 'createPullRequest')
792+
.resolves(1);
793+
sandbox
794+
.stub(github, 'getFileContentsOnBranch')
795+
.withArgs('existing-file', 'main')
796+
.resolves({
797+
sha: 'abc123',
798+
content: 'somecontent',
799+
parsedContent: 'somecontent',
800+
mode: '100644',
801+
});
802+
sandbox.stub(github, 'getPullRequest').withArgs(1).resolves({
803+
title: 'created title',
804+
headBranchName: 'release-please--branches--main',
805+
baseBranchName: 'main',
806+
number: 1,
807+
body: 'some body',
808+
labels: [],
809+
files: [],
810+
});
811+
const pullRequest = await github.createReleasePullRequest(
812+
{
813+
title: PullRequestTitle.ofTargetBranch('main'),
814+
body: new PullRequestBody([]),
815+
labels: [],
816+
headRefName: 'release-please--branches--main',
817+
draft: false,
818+
updates: [
819+
{
820+
path: 'existing-file',
821+
createIfMissing: false,
822+
updater: new RawContent('some content'),
823+
},
824+
],
825+
},
826+
'main'
827+
);
828+
expect(pullRequest.number).to.eql(1);
829+
sinon.assert.calledOnce(createPullRequestStub);
830+
const changes = createPullRequestStub.getCall(0).args[1];
831+
expect(changes).to.not.be.undefined;
832+
expect(changes!.size).to.eql(1);
833+
expect(changes!.get('existing-file')).to.not.be.undefined;
834+
});
835+
it('should handle missing files', async () => {
836+
const createPullRequestStub = sandbox
837+
.stub(codeSuggester, 'createPullRequest')
838+
.resolves(1);
839+
sandbox
840+
.stub(github, 'getFileContentsOnBranch')
841+
.withArgs('missing-file', 'main')
842+
.rejects(new FileNotFoundError('missing-file'));
843+
sandbox.stub(github, 'getPullRequest').withArgs(1).resolves({
844+
title: 'created title',
845+
headBranchName: 'release-please--branches--main',
846+
baseBranchName: 'main',
847+
number: 1,
848+
body: 'some body',
849+
labels: [],
850+
files: [],
851+
});
852+
const pullRequest = await github.createReleasePullRequest(
853+
{
854+
title: PullRequestTitle.ofTargetBranch('main'),
855+
body: new PullRequestBody([]),
856+
labels: [],
857+
headRefName: 'release-please--branches--main',
858+
draft: false,
859+
updates: [
860+
{
861+
path: 'missing-file',
862+
createIfMissing: false,
863+
updater: new RawContent('some content'),
864+
},
865+
],
866+
},
867+
'main'
868+
);
869+
expect(pullRequest.number).to.eql(1);
870+
sinon.assert.calledOnce(createPullRequestStub);
871+
const changes = createPullRequestStub.getCall(0).args[1];
872+
expect(changes).to.not.be.undefined;
873+
expect(changes!.size).to.eql(0);
874+
});
875+
it('should create missing file', async () => {
876+
const createPullRequestStub = sandbox
877+
.stub(codeSuggester, 'createPullRequest')
878+
.resolves(1);
879+
sandbox
880+
.stub(github, 'getFileContentsOnBranch')
881+
.withArgs('missing-file', 'main')
882+
.rejects(new FileNotFoundError('missing-file'));
883+
sandbox.stub(github, 'getPullRequest').withArgs(1).resolves({
884+
title: 'created title',
885+
headBranchName: 'release-please--branches--main',
886+
baseBranchName: 'main',
887+
number: 1,
888+
body: 'some body',
889+
labels: [],
890+
files: [],
891+
});
892+
const pullRequest = await github.createReleasePullRequest(
893+
{
894+
title: PullRequestTitle.ofTargetBranch('main'),
895+
body: new PullRequestBody([]),
896+
labels: [],
897+
headRefName: 'release-please--branches--main',
898+
draft: false,
899+
updates: [
900+
{
901+
path: 'missing-file',
902+
createIfMissing: true,
903+
updater: new RawContent('some content'),
904+
},
905+
],
906+
},
907+
'main'
908+
);
909+
expect(pullRequest.number).to.eql(1);
910+
sinon.assert.calledOnce(createPullRequestStub);
911+
const changes = createPullRequestStub.getCall(0).args[1];
912+
expect(changes).to.not.be.undefined;
913+
expect(changes!.size).to.eql(1);
914+
expect(changes!.get('missing-file')).to.not.be.undefined;
915+
});
916+
});
779917
});

0 commit comments

Comments
 (0)