@@ -143,6 +143,29 @@ module.exports = async ({ github, context, core, dry }) => {
143143 return users [ id ]
144144 }
145145
146+ // Same for teams
147+ const teams = { }
148+ function getTeam ( id ) {
149+ if ( ! teams [ id ] ) {
150+ teams [ id ] = github
151+ . request ( {
152+ method : 'GET' ,
153+ url : '/organizations/{orgId}/team/{id}' ,
154+ // TODO: Make this work without pull_requests payloads
155+ orgId : context . payload . pull_request . base . user . id ,
156+ id,
157+ } )
158+ . then ( ( resp ) => resp . data )
159+ . catch ( ( e ) => {
160+ // Team may have been deleted
161+ if ( e . status === 404 ) return null
162+ throw e
163+ } )
164+ }
165+
166+ return teams [ id ]
167+ }
168+
146169 async function handlePullRequest ( { item, stats, events } ) {
147170 const log = ( k , v ) => core . info ( `PR #${ item . number } - ${ k } : ${ v } ` )
148171
@@ -175,17 +198,49 @@ module.exports = async ({ github, context, core, dry }) => {
175198 } )
176199
177200 // Check for any human reviews other than the PR author, GitHub actions and other GitHub apps.
178- // Accounts could be deleted as well, so don't count them.
179201 const reviews = (
180- await github . paginate ( github . rest . pulls . listReviews , {
181- ...context . repo ,
182- pull_number,
183- } )
184- ) . filter (
202+ await github . graphql (
203+ `query($owner: String!, $repo: String!, $pr: Int!) {
204+ repository(owner: $owner, name: $repo) {
205+ pullRequest(number: $pr) {
206+ # Unlikely that there's ever more than 100 reviews, so let's not bother,
207+ # but once https://github.com/actions/github-script/issues/309 is resolved,
208+ # it would be easy to enable pagination.
209+ reviews(first: 100) {
210+ nodes {
211+ state
212+ user: author {
213+ # Only get users, no bots
214+ ... on User {
215+ login
216+ # Set the id field in the resulting JSON to GraphQL's databaseId
217+ # databaseId in GraphQL-land is the same as id in REST-land
218+ id: databaseId
219+ }
220+ }
221+ onBehalfOf(first: 100) {
222+ nodes {
223+ slug
224+ }
225+ }
226+ }
227+ }
228+ }
229+ }
230+ }` ,
231+ {
232+ owner : context . repo . owner ,
233+ repo : context . repo . repo ,
234+ pr : pull_number ,
235+ } ,
236+ )
237+ ) . repository . pullRequest . reviews . nodes . filter (
185238 ( r ) =>
186- r . user &&
187- ! r . user . login . endsWith ( '[bot]' ) &&
188- r . user . type !== 'Bot' &&
239+ // The `... on User` makes it such that .login only exists for users,
240+ // but we still need to filter the others out.
241+ // Accounts could be deleted as well, so don't count them.
242+ r . user ?. login &&
243+ // Also exclude author reviews, can't request their review in any case
189244 r . user . id !== pull_request . user ?. id ,
190245 )
191246
@@ -354,6 +409,16 @@ module.exports = async ({ github, context, core, dry }) => {
354409 if ( e . code !== 'ENOENT' ) throw e
355410 }
356411
412+ let team_maintainers = [ ]
413+ try {
414+ team_maintainers = Object . keys (
415+ JSON . parse ( await readFile ( `${ pull_number } /teams.json` , 'utf-8' ) ) ,
416+ ) . map ( ( id ) => parseInt ( id ) )
417+ } catch ( e ) {
418+ // Older artifacts don't have the teams.json, yet.
419+ if ( e . code !== 'ENOENT' ) throw e
420+ }
421+
357422 // We set this label earlier already, but the current PR state can be very different
358423 // after handleReviewers has requested reviews, so update it in this case to prevent
359424 // this label from flip-flopping.
@@ -366,14 +431,15 @@ module.exports = async ({ github, context, core, dry }) => {
366431 pull_request,
367432 reviews,
368433 // TODO: Use maintainer map instead of the artifact.
369- maintainers : Object . keys (
434+ user_maintainers : Object . keys (
370435 JSON . parse (
371436 await readFile ( `${ pull_number } /maintainers.json` , 'utf-8' ) ,
372437 ) ,
373438 ) . map ( ( id ) => parseInt ( id ) ) ,
439+ team_maintainers,
374440 owners,
375- getTeamMembers,
376441 getUser,
442+ getTeam,
377443 } )
378444 }
379445 }
0 commit comments