@@ -7,7 +7,6 @@ import debugFactory from "debug";
7
7
import parseGithubUrl from "./parse-github-url.js" ;
8
8
import resolveConfig from "./resolve-config.js" ;
9
9
import { toOctokitOptions } from "./octokit.js" ;
10
- import getSearchQueries from "./get-search-queries.js" ;
11
10
import getSuccessComment from "./get-success-comment.js" ;
12
11
import findSRIssues from "./find-sr-issues.js" ;
13
12
import { RELEASE_NAME } from "./definitions/constants.js" ;
@@ -65,44 +64,38 @@ export default async function success(pluginConfig, context, { Octokit }) {
65
64
const releaseInfos = releases . filter ( ( release ) => Boolean ( release . name ) ) ;
66
65
const shas = commits . map ( ( { hash } ) => hash ) ;
67
66
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 } ,
74
70
) ;
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 ,
80
73
) ;
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 ;
94
74
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
+ } ) ;
106
99
107
100
debug (
108
101
"found pull requests: %O" ,
@@ -250,3 +243,32 @@ export default async function success(pluginConfig, context, { Octokit }) {
250
243
throw new AggregateError ( errors ) ;
251
244
}
252
245
}
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