refactor(dev-infra): use graphql to query PRs in merge tooling (#41459)

Migrate to use graqhql to query for PR information during the validation and
preperation portions of the pr merge tooling.

PR Close #41459
This commit is contained in:
Joey Perrott 2021-04-06 07:43:21 -07:00 committed by atscott
parent 18d4446dc6
commit 2d3539f4df
3 changed files with 78 additions and 35 deletions

View File

@ -3396,7 +3396,7 @@ function loadAndValidatePullRequest(_a, prNumber, ignoreNonFatalFailures) {
if (prData === null) { if (prData === null) {
return [2 /*return*/, PullRequestFailure.notFound()]; return [2 /*return*/, PullRequestFailure.notFound()];
} }
labels = prData.labels.map(function (l) { return l.name; }); labels = prData.labels.nodes.map(function (l) { return l.name; });
if (!labels.some(function (name) { return matchesPattern(name, config.mergeReadyLabel); })) { if (!labels.some(function (name) { return matchesPattern(name, config.mergeReadyLabel); })) {
return [2 /*return*/, PullRequestFailure.notMergeReady()]; return [2 /*return*/, PullRequestFailure.notMergeReady()];
} }
@ -3412,36 +3412,34 @@ function loadAndValidatePullRequest(_a, prNumber, ignoreNonFatalFailures) {
} }
throw error; throw error;
} }
return [4 /*yield*/, git.github.repos.getCombinedStatusForRef(tslib.__assign(tslib.__assign({}, git.remoteParams), { ref: prData.head.sha }))]; state = prData.commits.nodes.slice(-1)[0].commit.status.state;
case 2: if (state === 'FAILURE' && !ignoreNonFatalFailures) {
state = (_b.sent()).data.state;
if (state === 'failure' && !ignoreNonFatalFailures) {
return [2 /*return*/, PullRequestFailure.failingCiJobs()]; return [2 /*return*/, PullRequestFailure.failingCiJobs()];
} }
if (state === 'pending' && !ignoreNonFatalFailures) { if (state === 'PENDING' && !ignoreNonFatalFailures) {
return [2 /*return*/, PullRequestFailure.pendingCiJobs()]; return [2 /*return*/, PullRequestFailure.pendingCiJobs()];
} }
githubTargetBranch = prData.base.ref; githubTargetBranch = prData.baseRefOid;
requiredBaseSha = config.requiredBaseCommits && config.requiredBaseCommits[githubTargetBranch]; requiredBaseSha = config.requiredBaseCommits && config.requiredBaseCommits[githubTargetBranch];
needsCommitMessageFixup = !!config.commitMessageFixupLabel && needsCommitMessageFixup = !!config.commitMessageFixupLabel &&
labels.some(function (name) { return matchesPattern(name, config.commitMessageFixupLabel); }); labels.some(function (name) { return matchesPattern(name, config.commitMessageFixupLabel); });
hasCaretakerNote = !!config.caretakerNoteLabel && hasCaretakerNote = !!config.caretakerNoteLabel &&
labels.some(function (name) { return matchesPattern(name, config.caretakerNoteLabel); }); labels.some(function (name) { return matchesPattern(name, config.caretakerNoteLabel); });
_b.label = 3; _b.label = 2;
case 3: case 2:
_b.trys.push([3, 5, , 6]); _b.trys.push([2, 4, , 5]);
return [4 /*yield*/, getBranchesFromTargetLabel(targetLabel, githubTargetBranch)]; return [4 /*yield*/, getBranchesFromTargetLabel(targetLabel, githubTargetBranch)];
case 4: case 3:
targetBranches = _b.sent(); targetBranches = _b.sent();
return [3 /*break*/, 6]; return [3 /*break*/, 5];
case 5: case 4:
error_1 = _b.sent(); error_1 = _b.sent();
if (error_1 instanceof InvalidTargetBranchError || error_1 instanceof InvalidTargetLabelError) { if (error_1 instanceof InvalidTargetBranchError || error_1 instanceof InvalidTargetLabelError) {
return [2 /*return*/, new PullRequestFailure(error_1.failureMessage)]; return [2 /*return*/, new PullRequestFailure(error_1.failureMessage)];
} }
throw error_1; throw error_1;
case 6: return [2 /*return*/, { case 5: return [2 /*return*/, {
url: prData.html_url, url: prData.url,
prNumber: prNumber, prNumber: prNumber,
labels: labels, labels: labels,
requiredBaseSha: requiredBaseSha, requiredBaseSha: requiredBaseSha,
@ -3450,24 +3448,44 @@ function loadAndValidatePullRequest(_a, prNumber, ignoreNonFatalFailures) {
hasCaretakerNote: hasCaretakerNote, hasCaretakerNote: hasCaretakerNote,
targetBranches: targetBranches, targetBranches: targetBranches,
title: prData.title, title: prData.title,
commitCount: prData.commits, commitCount: prData.commits.nodes.length,
}]; }];
} }
}); });
}); });
} }
/* GraphQL schema for the response body the requested PR. */
var PR_SCHEMA$2 = {
url: typedGraphqlify.types.string,
number: typedGraphqlify.types.number,
commits: typedGraphqlify.params({ first: 100 }, {
nodes: [{
commit: {
status: {
state: typedGraphqlify.types.oneOf(['FAILURE', 'PENDING', 'SUCCESS']),
},
message: typedGraphqlify.types.string,
},
}],
}),
baseRefOid: typedGraphqlify.types.string,
title: typedGraphqlify.types.string,
labels: typedGraphqlify.params({ first: 100 }, {
nodes: [{
name: typedGraphqlify.types.string,
}]
}),
};
/** Fetches a pull request from Github. Returns null if an error occurred. */ /** Fetches a pull request from Github. Returns null if an error occurred. */
function fetchPullRequestFromGithub(git, prNumber) { function fetchPullRequestFromGithub(git, prNumber) {
return tslib.__awaiter(this, void 0, void 0, function () { return tslib.__awaiter(this, void 0, void 0, function () {
var result, e_1; var e_1;
return tslib.__generator(this, function (_a) { return tslib.__generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
_a.trys.push([0, 2, , 3]); _a.trys.push([0, 2, , 3]);
return [4 /*yield*/, git.github.pulls.get(tslib.__assign(tslib.__assign({}, git.remoteParams), { pull_number: prNumber }))]; return [4 /*yield*/, getPr(PR_SCHEMA$2, prNumber, git)];
case 1: case 1: return [2 /*return*/, _a.sent()];
result = _a.sent();
return [2 /*return*/, result.data];
case 2: case 2:
e_1 = _a.sent(); e_1 = _a.sent();
// If the pull request could not be found, we want to return `null` so // If the pull request could not be found, we want to return `null` so
@ -4315,7 +4333,7 @@ var MergeCommandModule = {
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
/* GraphQL schema for the response body for each pending PR. */ /* GraphQL schema for the response body for each pending PR. */
const PR_SCHEMA$2 = { const PR_SCHEMA$3 = {
state: typedGraphqlify.types.string, state: typedGraphqlify.types.string,
maintainerCanModify: typedGraphqlify.types.boolean, maintainerCanModify: typedGraphqlify.types.boolean,
viewerDidAuthor: typedGraphqlify.types.boolean, viewerDidAuthor: typedGraphqlify.types.boolean,
@ -4353,7 +4371,7 @@ function rebasePr(prNumber, githubToken, config = getConfig()) {
*/ */
const previousBranchOrRevision = git.getCurrentBranchOrRevision(); const previousBranchOrRevision = git.getCurrentBranchOrRevision();
/* Get the PR information from Github. */ /* Get the PR information from Github. */
const pr = yield getPr(PR_SCHEMA$2, prNumber, git); const pr = yield getPr(PR_SCHEMA$3, prNumber, git);
const headRefName = pr.headRef.name; const headRefName = pr.headRef.name;
const baseRefName = pr.baseRef.name; const baseRefName = pr.baseRef.name;
const fullHeadRef = `${pr.headRef.repository.nameWithOwner}:${headRefName}`; const fullHeadRef = `${pr.headRef.repository.nameWithOwner}:${headRefName}`;

View File

@ -24,6 +24,7 @@ ts_library(
"@npm//@types/semver", "@npm//@types/semver",
"@npm//@types/yargs", "@npm//@types/yargs",
"@npm//chalk", "@npm//chalk",
"@npm//typed-graphqlify",
], ],
) )

View File

@ -6,9 +6,10 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import * as Octokit from '@octokit/rest'; import {params, types as graphQLTypes} from 'typed-graphqlify';
import {GitClient} from '../../utils/git/index'; import {GitClient} from '../../utils/git/index';
import {getPr} from '../../utils/github';
import {TargetLabel} from './config'; import {TargetLabel} from './config';
import {PullRequestFailure} from './failures'; import {PullRequestFailure} from './failures';
@ -53,7 +54,7 @@ export async function loadAndValidatePullRequest(
return PullRequestFailure.notFound(); return PullRequestFailure.notFound();
} }
const labels = prData.labels.map(l => l.name); const labels = prData.labels.nodes.map(l => l.name);
if (!labels.some(name => matchesPattern(name, config.mergeReadyLabel))) { if (!labels.some(name => matchesPattern(name, config.mergeReadyLabel))) {
return PullRequestFailure.notMergeReady(); return PullRequestFailure.notMergeReady();
@ -72,17 +73,16 @@ export async function loadAndValidatePullRequest(
throw error; throw error;
} }
const {data: {state}} =
await git.github.repos.getCombinedStatusForRef({...git.remoteParams, ref: prData.head.sha});
if (state === 'failure' && !ignoreNonFatalFailures) { const state = prData.commits.nodes.slice(-1)[0].commit.status.state;
if (state === 'FAILURE' && !ignoreNonFatalFailures) {
return PullRequestFailure.failingCiJobs(); return PullRequestFailure.failingCiJobs();
} }
if (state === 'pending' && !ignoreNonFatalFailures) { if (state === 'PENDING' && !ignoreNonFatalFailures) {
return PullRequestFailure.pendingCiJobs(); return PullRequestFailure.pendingCiJobs();
} }
const githubTargetBranch = prData.base.ref; const githubTargetBranch = prData.baseRefOid;
const requiredBaseSha = const requiredBaseSha =
config.requiredBaseCommits && config.requiredBaseCommits[githubTargetBranch]; config.requiredBaseCommits && config.requiredBaseCommits[githubTargetBranch];
const needsCommitMessageFixup = !!config.commitMessageFixupLabel && const needsCommitMessageFixup = !!config.commitMessageFixupLabel &&
@ -105,7 +105,7 @@ export async function loadAndValidatePullRequest(
} }
return { return {
url: prData.html_url, url: prData.url,
prNumber, prNumber,
labels, labels,
requiredBaseSha, requiredBaseSha,
@ -114,16 +114,40 @@ export async function loadAndValidatePullRequest(
hasCaretakerNote, hasCaretakerNote,
targetBranches, targetBranches,
title: prData.title, title: prData.title,
commitCount: prData.commits, commitCount: prData.commits.nodes.length,
}; };
} }
/* GraphQL schema for the response body the requested PR. */
const PR_SCHEMA = {
url: graphQLTypes.string,
number: graphQLTypes.number,
commits: params({first: 100}, {
nodes: [{
commit: {
status: {
state: graphQLTypes.oneOf(['FAILURE' as const, 'PENDING' as const, 'SUCCESS' as const]),
},
message: graphQLTypes.string,
},
}],
}),
baseRefOid: graphQLTypes.string,
title: graphQLTypes.string,
labels: params({first: 100}, {
nodes: [{
name: graphQLTypes.string,
}]
}),
};
/** Fetches a pull request from Github. Returns null if an error occurred. */ /** Fetches a pull request from Github. Returns null if an error occurred. */
async function fetchPullRequestFromGithub( async function fetchPullRequestFromGithub(
git: GitClient, prNumber: number): Promise<Octokit.PullsGetResponse|null> { git: GitClient, prNumber: number): Promise<typeof PR_SCHEMA|null> {
try { try {
const result = await git.github.pulls.get({...git.remoteParams, pull_number: prNumber}); return await getPr(PR_SCHEMA, prNumber, git);
return result.data;
} catch (e) { } catch (e) {
// If the pull request could not be found, we want to return `null` so // If the pull request could not be found, we want to return `null` so
// that the error can be handled gracefully. // that the error can be handled gracefully.