UNPKG

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