1+ const getCurrentMilestone = async ( context , token , owner , repo ) => {
2+ return await getMilestoneByName ( context , token , owner , repo , 'Next release' )
3+ }
4+
5+ const getMilestoneByName = async ( context , token , owner , repo , name ) => {
6+ const githubApiRequest = require ( './github-api-request' )
7+ const milestones = await githubApiRequest ( context , token , 'GET' , `/repos/${ owner } /${ repo } /milestones?state=open` )
8+ if ( milestones . length === 2 ) {
9+ const filtered = milestones . filter ( m => m . title !== name )
10+ if ( filtered . length === 1 ) milestones . splice ( 0 , 2 , filtered )
11+ }
12+ if ( milestones . length !== 1 ) throw new Error ( `Expected one milestone, got ${ milestones . length } ` )
13+ return milestones [ 0 ]
14+ }
15+
16+ const closeMilestone = async ( context , token , owner , repo , milestoneNumber , dueOn ) => {
17+ const githubApiRequest = require ( './github-api-request' )
18+ const payload = {
19+ state : 'closed'
20+ }
21+ if ( dueOn ) payload . due_on = dueOn
22+ await githubApiRequest ( context , token , 'PATCH' , `/repos/${ owner } /${ repo } /milestones/${ milestoneNumber } ` , payload )
23+ }
24+
25+ const renameMilestone = async ( context , token , owner , repo , milestoneNumber , newName ) => {
26+ const githubApiRequest = require ( './github-api-request' )
27+ const payload = {
28+ title : newName
29+ }
30+ await githubApiRequest ( context , token , 'PATCH' , `/repos/${ owner } /${ repo } /milestones/${ milestoneNumber } ` , payload )
31+ }
32+
33+ const openNextReleaseMilestone = async ( context , token , owner , repo ) => {
34+ const githubApiRequest = require ( './github-api-request' )
35+ const milestones = await githubApiRequest ( context , token , 'GET' , `/repos/${ owner } /${ repo } /milestones?state=open` )
36+ const filtered = milestones . filter ( m => m . title === 'Next release' )
37+ if ( filtered . length === 1 ) return filtered [ 0 ]
38+
39+ return await githubApiRequest ( context , token , 'POST' , `/repos/${ owner } /${ repo } /milestones` , {
40+ title : 'Next release'
41+ } )
42+ }
43+
44+ module . exports = {
45+ getCurrentMilestone,
46+ getMilestoneByName,
47+ closeMilestone,
48+ renameMilestone,
49+ openNextReleaseMilestone
50+ }
0 commit comments