Skip to content

Commit 62d4634

Browse files
committed
- added stale pull dry run clean up
1 parent fd04b3e commit 62d4634

File tree

4 files changed

+8869
-30
lines changed

4 files changed

+8869
-30
lines changed

.infraway/config.yaml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ events:
44
pull_request: "deploy" # or "ignore", deploy by default
55
commit: "deploy" # or "ignore", deploy by default
66

7+
stale_pull_cleanup:
8+
enabled: true
9+
duration: "7 days"
710

811
deploy:
912
- name: repo-name1

index.js

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { Buffer } = require('buffer');
22
const yaml = require('js-yaml');
33
const bluebird = require('bluebird');
4+
const { sub } = require("date-fns");
45

56
const getConfigFile = async (context, owner) => {
67
const repoPaths = {
@@ -74,6 +75,49 @@ const getLatestCommitInPullRequest = async (context, owner, repo, pullNumber) =>
7475
return pull.data.head.sha;
7576
};
7677

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+
77121
const configMap = {};
78122
const configMapPromise = {};
79123
const getConfig = (owner) => configMap[owner];
@@ -92,6 +136,11 @@ const syncConfig = async (context, owner) => {
92136
}
93137
}
94138

139+
const sync = async (app, context, owner) => {
140+
await syncConfig(context, owner);
141+
await syncStale(app, context, owner);
142+
};
143+
95144
const getDeployPayloads = async (context, { owner, repo, pullNumber, sha = '' }, components = [], action = '', logger = null) => {
96145
const config = getConfig(owner);
97146
const componentsMap = new Map(components.map((c) => [c.component, c]));
@@ -154,7 +203,7 @@ const getDeployPayloads = async (context, { owner, repo, pullNumber, sha = '' },
154203
environment,
155204
values: values || component,
156205
domain: `${environment}.${domain}`,
157-
action: 'deploy'
206+
action: 'deploy',
158207
}];
159208
}, Promise.resolve([]));
160209

@@ -262,8 +311,7 @@ const deleteDeployments = async (app, context, owner, payloads) => {
262311
* @param {import('probot').Probot} app
263312
*/
264313
module.exports = (app) => {
265-
// Your code here
266-
app.log.info("Yay, the app was loaded!");
314+
app.log.info("Bot started!");
267315
app.on(
268316
"issue_comment.created",
269317
async (context) => {
@@ -286,7 +334,7 @@ module.exports = (app) => {
286334
const pullNumber = context.payload.issue.html_url.indexOf('/pull/')
287335
? context.payload.issue.number : null;
288336

289-
await syncConfig(context, owner);
337+
await sync(context, owner);
290338

291339
if (!pullNumber) {
292340
app.log.info('Cannot find pull request. Deploy dismissed.');
@@ -322,7 +370,7 @@ module.exports = (app) => {
322370
} = context.payload;
323371
app.log.info('push');
324372
app.log.info({ owner, repo, sha, ctx });
325-
await syncConfig(context, owner);
373+
await sync(context, owner);
326374

327375
const config = getConfig(owner);
328376
const { commit: commitEvent } = config.events || { commit: 'deploy' };
@@ -356,7 +404,7 @@ module.exports = (app) => {
356404
}
357405
app.log.info(`pull_request.${action}`);
358406
app.log.info({ owner, repo, ctx, pullNumber });
359-
await syncConfig(context, owner);
407+
await sync(context, owner);
360408

361409
const config = getConfig(owner);
362410
const { pull_request: prEvent } = config.events || { pull_request: 'deploy' };

0 commit comments

Comments
 (0)