UNPKG

2.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.processQueryResult = exports.buildSearchQuery = void 0;
4const tslib_1 = require("tslib");
5const endent_1 = tslib_1.__importDefault(require("endent"));
6/**
7 * Generate a GitHub graphql query to find all the commits related
8 * to a PR.
9 */
10function buildSearchQuery(owner, project, commits) {
11 const repo = `${owner}/${project}`;
12 const query = commits.reduce((q, commit) => {
13 const subQuery = `repo:${repo} ${commit}`;
14 return endent_1.default `
15 ${q}
16
17 hash_${commit}: search(query: "${subQuery}", type: ISSUE, first: 10) {
18 edges {
19 node {
20 ... on PullRequest {
21 number
22 state
23 body
24 headRefName
25 headRepositoryOwner {
26 login
27 }
28 labels(first: 10) {
29 edges {
30 node {
31 name
32 }
33 }
34 }
35 }
36 }
37 }
38 }
39 `;
40 }, "");
41 if (!query) {
42 return;
43 }
44 return `{
45 ${query}
46 rateLimit {
47 limit
48 cost
49 remaining
50 resetAt
51 }
52 }`;
53}
54exports.buildSearchQuery = buildSearchQuery;
55/** Use the graphql query result to fill in more information about a commit */
56function processQueryResult({ owner, sha, result, commitsWithoutPR, prereleaseBranches, }) {
57 const hash = sha.split("hash_")[1];
58 const commit = commitsWithoutPR.find((commitWithoutPR) => commitWithoutPR.hash === hash);
59 if (!commit) {
60 return;
61 }
62 // When matching SHA to PR only take merged into account. You can have
63 // multiple open PRs with the same commits, such as in a rebase.
64 const prs = result.edges.filter((edge) => edge.node.state === "MERGED");
65 const isInPrerelease = result.edges.filter((edge) => prereleaseBranches.includes(edge.node.headRefName) &&
66 edge.node.headRepositoryOwner.login === owner);
67 if (prs.length) {
68 const pr = prs[0].node;
69 const labels = pr.labels
70 ? pr.labels.edges.map((edge) => edge.node)
71 : [];
72 commit.pullRequest = {
73 number: pr.number,
74 body: pr.body,
75 };
76 commit.labels = [...labels.map((label) => label.name), ...commit.labels];
77 }
78 else if (!result.edges.length || isInPrerelease.length) {
79 commit.labels = ["pushToBaseBranch", ...commit.labels];
80 }
81 commit.subject = commit.subject.split("\n")[0];
82 return commit;
83}
84exports.processQueryResult = processQueryResult;
85//# sourceMappingURL=match-sha-to-pr.js.map
\No newline at end of file