fix(backend-github): add fallback for diff errors/warnings (#3558)

This commit is contained in:
Erez Rokah 2020-04-07 15:42:24 +03:00 committed by GitHub
parent 4afbbdd8a9
commit 1705c79a92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -535,30 +535,68 @@ export default class API {
} }
} }
async retrieveMetadata(contentKey: string) { async getPullRequestCommits(number: number) {
const { collection, slug } = this.parseContentKey(contentKey); if (number === MOCK_PULL_REQUEST) {
const branch = branchFromContentKey(contentKey); return [];
const pullRequest = await this.getBranchPullRequest(branch); }
const { files: diffs } = await this.getDifferences(this.branch, pullRequest.head.sha); try {
const commits: Octokit.PullsListCommitsResponseItem[] = await this.request(
`${this.originRepoURL}/pulls/${number}/commits`,
);
return commits;
} catch (e) {
console.log(e);
return [];
}
}
matchingEntriesFromDiffs(diffs: Octokit.ReposCompareCommitsResponseFilesItem[]) {
// media files don't have a patch attribute, except svg files // media files don't have a patch attribute, except svg files
const matchingEntries = diffs const matchingEntries = diffs
.filter(d => d.patch && !d.filename.endsWith('.svg')) .filter(d => d.patch && !d.filename.endsWith('.svg'))
.map(f => ({ path: f.filename, newFile: f.status === 'added' })); .map(f => ({ path: f.filename, newFile: f.status === 'added' }));
return matchingEntries;
}
async retrieveMetadata(contentKey: string) {
const { collection, slug } = this.parseContentKey(contentKey);
const branch = branchFromContentKey(contentKey);
const pullRequest = await this.getBranchPullRequest(branch);
const { files: diffs } = await this.getDifferences(this.branch, pullRequest.head.sha);
const matchingEntries = this.matchingEntriesFromDiffs(diffs);
let entry = matchingEntries[0];
if (matchingEntries.length <= 0) { if (matchingEntries.length <= 0) {
console.error( // this can happen if there is an empty diff for some reason
'Unable to locate entry from diff', // we traverse the commits history to infer the entry
JSON.stringify({ branch, pullRequest, diffs, matchingEntries }), const commits = await this.getPullRequestCommits(pullRequest.number);
); for (const commit of commits) {
throw new EditorialWorkflowError('content is not under editorial workflow', true); const { files: diffs } = await this.getDifferences(this.branch, commit.sha);
const matchingEntries = this.matchingEntriesFromDiffs(diffs);
entry = matchingEntries[0];
if (entry) {
break;
}
}
if (!entry) {
console.error(
'Unable to locate entry from diff',
JSON.stringify({ branch, pullRequest, diffs, matchingEntries }),
);
throw new EditorialWorkflowError('content is not under editorial workflow', true);
}
} else if (matchingEntries.length > 1) { } else if (matchingEntries.length > 1) {
console.warn( // this only works for folder collections
`Expected 1 matching entry from diff, but received '${matchingEntries.length}'`, const entryBySlug = matchingEntries.filter(e => e.path.includes(slug))[0];
JSON.stringify({ branch, pullRequest, diffs, matchingEntries }), entry = entryBySlug || entry;
); if (!entryBySlug) {
console.warn(
`Expected 1 matching entry from diff, but received '${matchingEntries.length}'. Matched '${entry.path}'`,
JSON.stringify({ branch, pullRequest, diffs, matchingEntries }),
);
}
} }
const entry = matchingEntries[0];
const { path, newFile } = entry; const { path, newFile } = entry;
const mediaFiles = diffs const mediaFiles = diffs