Skip to content

Commit 6b00c9f

Browse files
Merge branch 'semantic-release:master' into master
2 parents b8c3442 + be394cf commit 6b00c9f

File tree

6 files changed

+440
-352
lines changed

6 files changed

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

0 commit comments

Comments
 (0)