1
1
const { Buffer } = require ( 'buffer' ) ;
2
2
const yaml = require ( 'js-yaml' ) ;
3
3
const bluebird = require ( 'bluebird' ) ;
4
+ const { sub } = require ( "date-fns" ) ;
4
5
5
6
const getConfigFile = async ( context , owner ) => {
6
7
const repoPaths = {
@@ -74,6 +75,49 @@ const getLatestCommitInPullRequest = async (context, owner, repo, pullNumber) =>
74
75
return pull . data . head . sha ;
75
76
} ;
76
77
78
+ const getStalePulls = async ( context , owner ) => {
79
+ const {
80
+ deploy,
81
+ stale_pull_cleanup : cleanupPolicy ,
82
+ } = getConfig ( owner ) ;
83
+ if ( ! cleanupPolicy . enabled ) {
84
+ return ;
85
+ }
86
+ const duration = ( cleanupPolicy . duration || '7 days' ) . split ( ' ' ) ;
87
+ const filterDate = sub ( new Date ( ) , { [ duration [ 1 ] ] : parseInt ( duration [ 0 ] , 10 ) } ) ;
88
+ const repos = deploy . map ( ( d ) => d . name ) ;
89
+ return repos . reduce ( async ( acc , repo ) => {
90
+ const foundPulls = await context . octokit . pulls . list ( {
91
+ owner,
92
+ repo,
93
+ state : 'open' ,
94
+ sort : 'updated' ,
95
+ direction : 'asc' ,
96
+ } ) ;
97
+ acc . push (
98
+ ...foundPulls
99
+ . filter ( ( p ) => new Date ( p . updated_at ) < filterDate )
100
+ . map ( ( p ) => ( { pullNumber : p . number , owner, repo } ) )
101
+ ) ;
102
+ } , [ ] ) ;
103
+ }
104
+
105
+ const stalePromise = { } ;
106
+ const syncStale = async ( app , context , owner ) => {
107
+ if ( ! stalePromise [ owner ] ) {
108
+ stalePromise [ owner ] = new Promise ( ( resolve , reject ) => {
109
+ setInterval ( async ( ) => {
110
+ const pulls = await getStalePulls ( context , owner ) ;
111
+ await pulls . reduce ( async ( _ , { owner, repo, pullNumber } ) => {
112
+ app . log . info ( `Cleaning up resources for stale pull: ${ owner } /${ repo } /pull/${ pullNumber } ` ) ;
113
+ // const payloads = await getDeletePayloads(context, { owner, repo, pullNumber });
114
+ // await deleteDeployments(app, context, owner, payloads);
115
+ } , Promise . resolve ( ) ) ;
116
+ } , 20 * 60 * 1000 ) ;
117
+ } ) ;
118
+ }
119
+ }
120
+
77
121
const configMap = { } ;
78
122
const configMapPromise = { } ;
79
123
const getConfig = ( owner ) => configMap [ owner ] ;
@@ -92,6 +136,11 @@ const syncConfig = async (context, owner) => {
92
136
}
93
137
}
94
138
139
+ const sync = async ( app , context , owner ) => {
140
+ await syncConfig ( context , owner ) ;
141
+ await syncStale ( app , context , owner ) ;
142
+ } ;
143
+
95
144
const getDeployPayloads = async ( context , { owner, repo, pullNumber, sha = '' } , components = [ ] , action = '' , logger = null ) => {
96
145
const config = getConfig ( owner ) ;
97
146
const componentsMap = new Map ( components . map ( ( c ) => [ c . component , c ] ) ) ;
@@ -154,7 +203,7 @@ const getDeployPayloads = async (context, { owner, repo, pullNumber, sha = '' },
154
203
environment,
155
204
values : values || component ,
156
205
domain : `${ environment } .${ domain } ` ,
157
- action : 'deploy'
206
+ action : 'deploy' ,
158
207
} ] ;
159
208
} , Promise . resolve ( [ ] ) ) ;
160
209
@@ -262,8 +311,7 @@ const deleteDeployments = async (app, context, owner, payloads) => {
262
311
* @param {import('probot').Probot } app
263
312
*/
264
313
module . exports = ( app ) => {
265
- // Your code here
266
- app . log . info ( "Yay, the app was loaded!" ) ;
314
+ app . log . info ( "Bot started!" ) ;
267
315
app . on (
268
316
"issue_comment.created" ,
269
317
async ( context ) => {
@@ -286,7 +334,7 @@ module.exports = (app) => {
286
334
const pullNumber = context . payload . issue . html_url . indexOf ( '/pull/' )
287
335
? context . payload . issue . number : null ;
288
336
289
- await syncConfig ( context , owner ) ;
337
+ await sync ( context , owner ) ;
290
338
291
339
if ( ! pullNumber ) {
292
340
app . log . info ( 'Cannot find pull request. Deploy dismissed.' ) ;
@@ -322,7 +370,7 @@ module.exports = (app) => {
322
370
} = context . payload ;
323
371
app . log . info ( 'push' ) ;
324
372
app . log . info ( { owner, repo, sha, ctx } ) ;
325
- await syncConfig ( context , owner ) ;
373
+ await sync ( context , owner ) ;
326
374
327
375
const config = getConfig ( owner ) ;
328
376
const { commit : commitEvent } = config . events || { commit : 'deploy' } ;
@@ -356,7 +404,7 @@ module.exports = (app) => {
356
404
}
357
405
app . log . info ( `pull_request.${ action } ` ) ;
358
406
app . log . info ( { owner, repo, ctx, pullNumber } ) ;
359
- await syncConfig ( context , owner ) ;
407
+ await sync ( context , owner ) ;
360
408
361
409
const config = getConfig ( owner ) ;
362
410
const { pull_request : prEvent } = config . events || { pull_request : 'deploy' } ;
0 commit comments