Skip to content

Commit

Permalink
fix: replace github search api with graphql in success lifecycle meth…
Browse files Browse the repository at this point in the history
…od (#857)

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
  • Loading branch information
babblebey and gr2m committed Jul 1, 2024
1 parent a669d1d commit be394cf
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 288 deletions.
16 changes: 0 additions & 16 deletions lib/get-search-queries.js

This file was deleted.

94 changes: 58 additions & 36 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import debugFactory from "debug";
import parseGithubUrl from "./parse-github-url.js";
import resolveConfig from "./resolve-config.js";
import { toOctokitOptions } from "./octokit.js";
import getSearchQueries from "./get-search-queries.js";
import getSuccessComment from "./get-success-comment.js";
import findSRIssues from "./find-sr-issues.js";
import { RELEASE_NAME } from "./definitions/constants.js";
Expand Down Expand Up @@ -65,44 +64,38 @@ export default async function success(pluginConfig, context, { Octokit }) {
const releaseInfos = releases.filter((release) => Boolean(release.name));
const shas = commits.map(({ hash }) => hash);

const searchQueries = getSearchQueries(
`repo:${owner}/${repo}+type:pr+is:merged`,
shas,
).map(
async (q) =>
(await octokit.request("GET /search/issues", { q })).data.items,
const { repository } = await octokit.graphql(
buildAssociatedPRsQuery(shas),
{ owner, repo },
);

const searchQueriesResults = await Promise.all(searchQueries);
const uniqueSearchQueriesResults = uniqBy(
flatten(searchQueriesResults),
"number",
const associatedPRs = Object.values(repository).map(
(item) => item.associatedPullRequests.nodes,
);
const prs = await pFilter(
uniqueSearchQueriesResults,
async ({ number }) => {
const commits = await octokit.paginate(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/commits",
{
owner,
repo,
pull_number: number,
},
);
const matchingCommit = commits.find(({ sha }) => shas.includes(sha));
if (matchingCommit) return matchingCommit;

const { data: pullRequest } = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
{
owner,
repo,
pull_number: number,
},
);
return shas.includes(pullRequest.merge_commit_sha);
},
);
const uniqueAssociatedPRs = uniqBy(flatten(associatedPRs), "number");

const prs = await pFilter(uniqueAssociatedPRs, async ({ number }) => {
const commits = await octokit.paginate(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/commits",
{
owner,
repo,
pull_number: number,
},
);
const matchingCommit = commits.find(({ sha }) => shas.includes(sha));
if (matchingCommit) return matchingCommit;

const { data: pullRequest } = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
{
owner,
repo,
pull_number: number,
},
);
return shas.includes(pullRequest.merge_commit_sha);
});

debug(
"found pull requests: %O",
Expand Down Expand Up @@ -250,3 +243,32 @@ export default async function success(pluginConfig, context, { Octokit }) {
throw new AggregateError(errors);
}
}

/**
* Builds GraphQL query for fetching associated PRs to a list of commit hash (sha)
* @param {Array<string>} shas
* @returns {string}
*/
export function buildAssociatedPRsQuery(shas) {
return `#graphql
query getAssociatedPRs($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
${shas
.map((sha) => {
return `commit${sha.slice(0, 6)}: object(oid: "${sha}") {
...on Commit {
associatedPullRequests(first: 100) {
nodes {
url
number
body
}
}
}
}`;
})
.join("")}
}
}
`;
}
41 changes: 0 additions & 41 deletions test/get-search-queries.test.js

This file was deleted.

58 changes: 33 additions & 25 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,17 @@ test("Comment and add labels on PR included in the releases", async (t) => {
repeat: 2,
},
)
.getOnce(
`https://api.github.local/search/issues?q=${encodeURIComponent(
`repo:${owner}/${repo}`,
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
"is:merged",
)}+${commits.map((commit) => commit.hash).join("+")}`,
{ items: prs },
)
.postOnce("https://api.github.local/graphql", {
data: {
repository: {
commit123: {
associatedPullRequests: {
nodes: [prs[0]],
},
},
},
},
})
.getOnce(
`https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`,
[{ sha: commits[0].hash }],
Expand Down Expand Up @@ -649,14 +652,17 @@ test("Verify, release and notify success", async (t) => {
{ html_url: releaseUrl },
{ body: { draft: false } },
)
.getOnce(
`https://api.github.local/search/issues?q=${encodeURIComponent(
`repo:${owner}/${repo}`,
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
"is:merged",
)}+${commits.map((commit) => commit.hash).join("+")}`,
{ items: prs },
)
.postOnce("https://api.github.local/graphql", {
data: {
repository: {
commit123: {
associatedPullRequests: {
nodes: [prs[0]],
},
},
},
},
})
.getOnce(
`https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`,
[{ sha: commits[0].hash }],
Expand Down Expand Up @@ -686,7 +692,6 @@ test("Verify, release and notify success", async (t) => {
browser_download_url: otherAssetUrl,
},
)

.postOnce(
`https://api.github.local/repos/${owner}/${repo}/issues/1/comments`,
{
Expand Down Expand Up @@ -800,14 +805,17 @@ test("Verify, update release and notify success", async (t) => {
},
},
)
.getOnce(
`https://api.github.local/search/issues?q=${encodeURIComponent(
`repo:${owner}/${repo}`,
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
"is:merged",
)}+${commits.map((commit) => commit.hash).join("+")}`,
{ items: prs },
)
.postOnce("https://api.github.local/graphql", {
data: {
repository: {
commit123: {
associatedPullRequests: {
nodes: [prs[0]],
},
},
},
},
})
.getOnce(
`https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`,
[{ sha: commits[0].hash }],
Expand Down
Loading

0 comments on commit be394cf

Please sign in to comment.