1+ const addIssueToCurrentMilestone = async ( context , req ) => {
2+ if ( req . body . action !== 'closed' ) return "Nothing to do here: PR has not been closed"
3+ if ( req . body . pull_request . merged !== 'true' ) return "Nothing to do here: PR has been closed, but not by merging"
4+
5+ const owner = 'git-for-windows'
6+ const repo = 'git'
7+ const sender = req . body . sender . login
8+
9+ const getToken = ( ( ) => {
10+ let token
11+
12+ const get = async ( ) => {
13+ const getInstallationIdForRepo = require ( './get-installation-id-for-repo' )
14+ const installationId = await getInstallationIdForRepo ( context , owner , repo )
15+ const getInstallationAccessToken = require ( './get-installation-access-token' )
16+ return await getInstallationAccessToken ( context , installationId )
17+ }
18+
19+ return async ( ) => token || ( token = await get ( ) )
20+ } ) ( )
21+
22+ const isAllowed = async ( login ) => {
23+ if ( login === 'gitforwindowshelper[bot]' ) return true
24+ const getCollaboratorPermissions = require ( './get-collaborator-permissions' )
25+ const token = await getToken ( )
26+ const permission = await getCollaboratorPermissions ( context , token , owner , repo , login )
27+ return [ 'ADMIN' , 'MAINTAIN' , 'WRITE' ] . includes ( permission . toString ( ) )
28+ }
29+
30+ if ( ! await isAllowed ( sender ) ) throw new Error ( `${ sender } is not allowed to do that` )
31+
32+ const githubApiRequest = require ( './github-api-request' )
33+
34+ const candidates = req . body . pull_request . body . match ( / (?: [ C c ] l o s e s | [ F f ] i x e s ) (?: h t t p s : \/ \/ g i t h u b \. c o m \/ g i t - f o r - w i n d o w s \/ g i t \/ i s s u e s \/ | # ) ( \d + ) / )
35+
36+ if ( candidates . length !== 1 ) throw new Error ( `Expected 1 candidate issue, got ${ candidates . length } ` )
37+
38+ const { getCurrentMilestone } = require ( './GitForWindowsHelper/milestones' )
39+ const current = await getCurrentMilestone ( console , token , owner , repo )
40+
41+ const issueNumber = candidates [ 0 ]
42+ const issue = await githubApiRequest ( context , await getToken ( ) , 'GET' , `/repos/${ owner } /${ repo } /issues/${ issueNumber } ` )
43+
44+ if ( issue . labels . length > 0 ) {
45+ for ( const label of issue . labels ) {
46+ if ( label . name === "component-update" ) {
47+ await githubApiRequest ( context , await getToken ( ) , 'PATCH' , `/repos/${ owner } /${ repo } /issues/${ issueNumber } ` , {
48+ milestone : current . id
49+ } )
50+
51+ return `Added issue ${ issueNumber } to milestone "Next release"`
52+ }
53+ }
54+ }
55+
56+ throw new Error ( `Issue ${ issueNumber } isn't a component update` )
57+ }
58+
59+
60+ module . exports = {
61+ addIssueToCurrentMilestone,
62+ }
0 commit comments