Skip to content

Commit be394cf

Browse files
babblebeygr2m
andauthored
fix: replace github search api with graphql in success lifecycle method (#857)
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
1 parent a669d1d commit be394cf

File tree

5 files changed

+377
-288
lines changed

5 files changed

+377
-288
lines changed

lib/get-search-queries.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/success.js

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import debugFactory from "debug";
77
import parseGithubUrl from "./parse-github-url.js";
88
import resolveConfig from "./resolve-config.js";
99
import { toOctokitOptions } from "./octokit.js";
10-
import getSearchQueries from "./get-search-queries.js";
1110
import getSuccessComment from "./get-success-comment.js";
1211
import findSRIssues from "./find-sr-issues.js";
1312
import { RELEASE_NAME } from "./definitions/constants.js";
@@ -65,44 +64,38 @@ export default async function success(pluginConfig, context, { Octokit }) {
6564
const releaseInfos = releases.filter((release) => Boolean(release.name));
6665
const shas = commits.map(({ hash }) => hash);
6766

68-
const searchQueries = getSearchQueries(
69-
`repo:${owner}/${repo}+type:pr+is:merged`,
70-
shas,
71-
).map(
72-
async (q) =>
73-
(await octokit.request("GET /search/issues", { q })).data.items,
67+
const { repository } = await octokit.graphql(
68+
buildAssociatedPRsQuery(shas),
69+
{ owner, repo },
7470
);
75-
76-
const searchQueriesResults = await Promise.all(searchQueries);
77-
const uniqueSearchQueriesResults = uniqBy(
78-
flatten(searchQueriesResults),
79-
"number",
71+
const associatedPRs = Object.values(repository).map(
72+
(item) => item.associatedPullRequests.nodes,
8073
);
81-
const prs = await pFilter(
82-
uniqueSearchQueriesResults,
83-
async ({ number }) => {
84-
const commits = await octokit.paginate(
85-
"GET /repos/{owner}/{repo}/pulls/{pull_number}/commits",
86-
{
87-
owner,
88-
repo,
89-
pull_number: number,
90-
},
91-
);
92-
const matchingCommit = commits.find(({ sha }) => shas.includes(sha));
93-
if (matchingCommit) return matchingCommit;
9474

95-
const { data: pullRequest } = await octokit.request(
96-
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
97-
{
98-
owner,
99-
repo,
100-
pull_number: number,
101-
},
102-
);
103-
return shas.includes(pullRequest.merge_commit_sha);
104-
},
105-
);
75+
const uniqueAssociatedPRs = uniqBy(flatten(associatedPRs), "number");
76+
77+
const prs = await pFilter(uniqueAssociatedPRs, async ({ number }) => {
78+
const commits = await octokit.paginate(
79+
"GET /repos/{owner}/{repo}/pulls/{pull_number}/commits",
80+
{
81+
owner,
82+
repo,
83+
pull_number: number,
84+
},
85+
);
86+
const matchingCommit = commits.find(({ sha }) => shas.includes(sha));
87+
if (matchingCommit) return matchingCommit;
88+
89+
const { data: pullRequest } = await octokit.request(
90+
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
91+
{
92+
owner,
93+
repo,
94+
pull_number: number,
95+
},
96+
);
97+
return shas.includes(pullRequest.merge_commit_sha);
98+
});
10699

107100
debug(
108101
"found pull requests: %O",
@@ -250,3 +243,32 @@ export default async function success(pluginConfig, context, { Octokit }) {
250243
throw new AggregateError(errors);
251244
}
252245
}
246+
247+
/**
248+
* Builds GraphQL query for fetching associated PRs to a list of commit hash (sha)
249+
* @param {Array<string>} shas
250+
* @returns {string}
251+
*/
252+
export function buildAssociatedPRsQuery(shas) {
253+
return `#graphql
254+
query getAssociatedPRs($owner: String!, $repo: String!) {
255+
repository(owner: $owner, name: $repo) {
256+
${shas
257+
.map((sha) => {
258+
return `commit${sha.slice(0, 6)}: object(oid: "${sha}") {
259+
...on Commit {
260+
associatedPullRequests(first: 100) {
261+
nodes {
262+
url
263+
number
264+
body
265+
}
266+
}
267+
}
268+
}`;
269+
})
270+
.join("")}
271+
}
272+
}
273+
`;
274+
}

test/get-search-queries.test.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/integration.test.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,17 @@ test("Comment and add labels on PR included in the releases", async (t) => {
432432
repeat: 2,
433433
},
434434
)
435-
.getOnce(
436-
`https://api.github.local/search/issues?q=${encodeURIComponent(
437-
`repo:${owner}/${repo}`,
438-
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
439-
"is:merged",
440-
)}+${commits.map((commit) => commit.hash).join("+")}`,
441-
{ items: prs },
442-
)
435+
.postOnce("https://api.github.local/graphql", {
436+
data: {
437+
repository: {
438+
commit123: {
439+
associatedPullRequests: {
440+
nodes: [prs[0]],
441+
},
442+
},
443+
},
444+
},
445+
})
443446
.getOnce(
444447
`https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`,
445448
[{ sha: commits[0].hash }],
@@ -649,14 +652,17 @@ test("Verify, release and notify success", async (t) => {
649652
{ html_url: releaseUrl },
650653
{ body: { draft: false } },
651654
)
652-
.getOnce(
653-
`https://api.github.local/search/issues?q=${encodeURIComponent(
654-
`repo:${owner}/${repo}`,
655-
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
656-
"is:merged",
657-
)}+${commits.map((commit) => commit.hash).join("+")}`,
658-
{ items: prs },
659-
)
655+
.postOnce("https://api.github.local/graphql", {
656+
data: {
657+
repository: {
658+
commit123: {
659+
associatedPullRequests: {
660+
nodes: [prs[0]],
661+
},
662+
},
663+
},
664+
},
665+
})
660666
.getOnce(
661667
`https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`,
662668
[{ sha: commits[0].hash }],
@@ -686,7 +692,6 @@ test("Verify, release and notify success", async (t) => {
686692
browser_download_url: otherAssetUrl,
687693
},
688694
)
689-
690695
.postOnce(
691696
`https://api.github.local/repos/${owner}/${repo}/issues/1/comments`,
692697
{
@@ -800,14 +805,17 @@ test("Verify, update release and notify success", async (t) => {
800805
},
801806
},
802807
)
803-
.getOnce(
804-
`https://api.github.local/search/issues?q=${encodeURIComponent(
805-
`repo:${owner}/${repo}`,
806-
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
807-
"is:merged",
808-
)}+${commits.map((commit) => commit.hash).join("+")}`,
809-
{ items: prs },
810-
)
808+
.postOnce("https://api.github.local/graphql", {
809+
data: {
810+
repository: {
811+
commit123: {
812+
associatedPullRequests: {
813+
nodes: [prs[0]],
814+
},
815+
},
816+
},
817+
},
818+
})
811819
.getOnce(
812820
`https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`,
813821
[{ sha: commits[0].hash }],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy