Skip to content

Commit

Permalink
fix: handle pull request files returned as null (#1651)
Browse files Browse the repository at this point in the history
* test: add failing test for graphQL not returning file nodes

* fix: handle pull request files returned as null

* fix pagination handling
  • Loading branch information
chingor13 committed Sep 22, 2022
1 parent 7d4abfb commit 4cea3dd
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
33 changes: 33 additions & 0 deletions __snapshots__/github.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ export class GitHub {
return pr.mergeCommit && pr.mergeCommit.oid === graphCommit.sha;
});
if (pullRequest) {
const files = pullRequest.files.nodes.map(node => node.path);
const files = (pullRequest.files?.nodes || []).map(node => node.path);
commit.pullRequest = {
sha: commit.sha,
number: pullRequest.number,
Expand All @@ -484,7 +484,7 @@ export class GitHub {
labels: pullRequest.labels.nodes.map(node => node.name),
files,
};
if (pullRequest.files.pageInfo?.hasNextPage && options.backfillFiles) {
if (pullRequest.files?.pageInfo?.hasNextPage && options.backfillFiles) {
this.logger.info(
`PR #${pullRequest.number} has many files, backfilling`
);
Expand Down
61 changes: 61 additions & 0 deletions test/fixtures/commits-since-no-files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"repository": {
"ref": {
"target": {
"history": {
"nodes": [
{
"associatedPullRequests": {
"nodes": [
{
"number": 7,
"title": "feat: feature that will be plain merged",
"baseRefName": "main",
"headRefName": "feature-branch-plain-merge",
"labels": {
"nodes": []
},
"body": "",
"mergeCommit": {
"oid": "e6daec403626c9987c7af0d97b34f324cd84320a"
},
"files": null
}
]
},
"sha": "e6daec403626c9987c7af0d97b34f324cd84320a",
"message": "Merge pull request #7 from chingor13/feature-branch-plain-merge\n\nfeat: feature that will be plain merged"
},
{
"associatedPullRequests": {
"nodes": [
{
"number": 7,
"title": "feat: feature that will be plain merged",
"baseRefName": "main",
"headRefName": "feature-branch-plain-merge",
"labels": {
"nodes": []
},
"body": "",
"mergeCommit": {
"oid": "b29149f890e6f76ee31ed128585744d4c598924c"
},
"files": {
"nodes": []
}
}
]
},
"sha": "b29149f890e6f76ee31ed128585744d4c598924c",
"message": "feat: feature-branch-plain-merge commit 2"
}
],
"pageInfo": {
"hasNextPage": false
}
}
}
}
}
}
23 changes: 23 additions & 0 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import * as codeSuggester from 'code-suggester';
import {RawContent} from '../src/updaters/raw-content';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {HttpProxyAgent} from 'http-proxy-agent';
import {Commit} from '../src/commit';

const fixturesPath = './test/fixtures';
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -542,6 +543,28 @@ describe('GitHub', () => {
});
});

describe('mergeCommitIterator', () => {
it('handles merged pull requests without files', async () => {
const graphql = JSON.parse(
readFileSync(
resolve(fixturesPath, 'commits-since-no-files.json'),
'utf8'
)
);
req.post('/graphql').reply(200, {
data: graphql,
});
const generator = github.mergeCommitIterator('main');
const commits: Commit[] = [];
for await (const commit of generator) {
commits.push(commit);
}
expect(commits).lengthOf(2);
snapshot(commits!);
req.done();
});
});

describe('getCommitFiles', () => {
it('fetches the list of files', async () => {
req
Expand Down

0 comments on commit 4cea3dd

Please sign in to comment.