Skip to content

Commit

Permalink
refactor(ng-dev/pr): use common fetch pr function in ng-dev pr discov…
Browse files Browse the repository at this point in the history
…er-new-conflicts (#242)

Use the common fetch pr functions to get the pr from github in the discover-new-conflicts command.

PR Close #242
  • Loading branch information
josephperrott committed Sep 27, 2021
1 parent 2c6f847 commit 6777242
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
10 changes: 8 additions & 2 deletions github-actions/slash-commands/main.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion ng-dev/pr/common/fetch-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import {AuthenticatedGitClient} from '../../utils/git/authenticated-git-client';
import {getPr} from '../../utils/github';
import {getPendingPrs, getPr} from '../../utils/github';
import {params, types as graphqlTypes, onUnion} from 'typed-graphqlify';
import {
MergeableState,
CheckConclusionState,
StatusState,
PullRequestState,
Expand All @@ -29,6 +30,8 @@ export const PR_SCHEMA = {
isDraft: graphqlTypes.boolean,
state: graphqlTypes.custom<PullRequestState>(),
number: graphqlTypes.number,
mergeable: graphqlTypes.custom<MergeableState>(),
updatedAt: graphqlTypes.string,
// Only the last 100 commits from a pull request are obtained as we likely will never see a pull
// requests with more than 100 commits.
commits: params(
Expand Down Expand Up @@ -108,6 +111,13 @@ export async function fetchPullRequestFromGithub(
return await getPr(PR_SCHEMA, prNumber, git);
}

/** Fetches a pull request from Github. Returns null if an error occurred. */
export async function fetchPendingPullRequestsFromGithub(
git: AuthenticatedGitClient,
): Promise<PullRequestFromGithub[] | null> {
return await getPendingPrs(PR_SCHEMA, git);
}

/**
* Gets the statuses for a commit from a pull requeste, using a consistent interface for both
* status and checks results.
Expand Down
2 changes: 1 addition & 1 deletion ng-dev/pr/discover-new-conflicts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ ts_library(
],
visibility = ["//ng-dev:__subpackages__"],
deps = [
"//ng-dev/pr/common",
"//ng-dev/utils",
"@npm//@types/cli-progress",
"@npm//@types/node",
"@npm//@types/yargs",
"@npm//typed-graphqlify",
],
)
51 changes: 13 additions & 38 deletions ng-dev/pr/discover-new-conflicts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,14 @@
*/

import {Bar} from 'cli-progress';
import {types as graphqlTypes} from 'typed-graphqlify';

import {error, info} from '../../utils/console';
import {AuthenticatedGitClient} from '../../utils/git/authenticated-git-client';
import {GitCommandError} from '../../utils/git/git-client';
import {getPendingPrs} from '../../utils/github';

/* Graphql schema for the response body for each pending PR. */
const PR_SCHEMA = {
headRef: {
name: graphqlTypes.string,
repository: {
url: graphqlTypes.string,
nameWithOwner: graphqlTypes.string,
},
},
baseRef: {
name: graphqlTypes.string,
repository: {
url: graphqlTypes.string,
nameWithOwner: graphqlTypes.string,
},
},
updatedAt: graphqlTypes.string,
number: graphqlTypes.number,
mergeable: graphqlTypes.string,
title: graphqlTypes.string,
};

/* Pull Request response from Github Graphql query */
type RawPullRequest = typeof PR_SCHEMA;

/** Convert raw Pull Request response from Github to usable Pull Request object. */
function processPr(pr: RawPullRequest) {
return {...pr, updatedAt: new Date(pr.updatedAt).getTime()};
}

/* Pull Request object after processing, derived from the return type of the processPr function. */
type PullRequest = ReturnType<typeof processPr>;
import {
fetchPendingPullRequestsFromGithub,
PullRequestFromGithub,
} from '../common/fetch-pull-request';

/** Name of a temporary local branch that is used for checking conflicts. **/
const tempWorkingBranch = '__NgDevRepoBaseAfterChange__';
Expand All @@ -66,11 +35,17 @@ export async function discoverNewConflictsForPr(newPrNumber: number, updatedAfte
/* Progress bar to indicate progress. */
const progressBar = new Bar({format: `[{bar}] ETA: {eta}s | {value}/{total}`});
/* PRs which were found to be conflicting. */
const conflicts: Array<PullRequest> = [];
const conflicts: Array<PullRequestFromGithub> = [];

info(`Requesting pending PRs from Github`);
/** List of PRs from github currently known as mergable. */
const allPendingPRs = (await getPendingPrs(PR_SCHEMA, git)).map(processPr);
const allPendingPRs = await fetchPendingPullRequestsFromGithub(git);

if (allPendingPRs === null) {
error('Unable to find any pending PRs in the repository');
process.exit(1);
}

/** The PR which is being checked against. */
const requestedPr = allPendingPRs.find((pr) => pr.number === newPrNumber);
if (requestedPr === undefined) {
Expand All @@ -88,7 +63,7 @@ export async function discoverNewConflictsForPr(newPrNumber: number, updatedAfte
// PRs which either have not been processed or are determined as mergable by Github
pr.mergeable !== 'CONFLICTING' &&
// PRs updated after the provided date
pr.updatedAt >= updatedAfter
new Date(pr.updatedAt).getTime() >= updatedAfter
);
});
info(`Retrieved ${allPendingPRs.length} total pending PRs`);
Expand Down

0 comments on commit 6777242

Please sign in to comment.