@@ -7,7 +7,6 @@ import debugFactory from "debug";
77import parseGithubUrl from "./parse-github-url.js" ;
88import resolveConfig from "./resolve-config.js" ;
99import { toOctokitOptions } from "./octokit.js" ;
10- import getSearchQueries from "./get-search-queries.js" ;
1110import getSuccessComment from "./get-success-comment.js" ;
1211import findSRIssues from "./find-sr-issues.js" ;
1312import { 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+ }
0 commit comments