-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdelete-stale-branches.js
More file actions
78 lines (76 loc) · 2.29 KB
/
delete-stale-branches.js
File metadata and controls
78 lines (76 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
paginateAllBranches
} from "../main-qxfdnkb5.js";
import {
getDefaultBranch
} from "../main-p1105nb1.js";
import {
paginateAllOpenPullRequests
} from "../main-zd3p3dtn.js";
import {
SECONDS_IN_A_DAY
} from "../main-9c2herm2.js";
import {
require_bluebird
} from "../main-ttmzs6m5.js";
import {
HelperInputs
} from "../main-8h70j5cy.js";
import {
octokit
} from "../main-4c5nddsb.js";
import {
context
} from "../main-6avxv4a6.js";
import"../main-9m3k9gt0.js";
import {
info
} from "../main-q70tmm6g.js";
import {
__toESM
} from "../main-wckvcay0.js";
// src/helpers/delete-stale-branches.ts
var import_bluebird = __toESM(require_bluebird(), 1);
class DeleteStaleBranches extends HelperInputs {
}
var deleteStaleBranches = async ({ days = "30" } = {}) => {
const openPullRequests = await paginateAllOpenPullRequests();
const openPullRequestBranches = new Set(openPullRequests.map((pr) => pr.head.ref));
const unprotectedBranches = await paginateAllBranches({ protectedBranches: false });
const defaultBranch = await getDefaultBranch();
const featureBranchesWithNoOpenPullRequest = unprotectedBranches.filter(({ name }) => !openPullRequestBranches.has(name) && name !== defaultBranch);
const branchesWithUpdatedDates = await import_bluebird.map(featureBranchesWithNoOpenPullRequest, async ({ name, commit: { sha } }) => {
const {
data: {
committer: { date }
}
} = await octokit.git.getCommit({
commit_sha: sha,
...context.repo
});
return {
name,
date
};
}, { concurrency: 5 });
const branchesToDelete = branchesWithUpdatedDates.filter(({ date }) => branchIsTooOld(date, days)).map(({ name }) => name);
await import_bluebird.map(branchesToDelete, async (branch) => {
info(`Deleting branch ${branch}...`);
await octokit.git.deleteRef({
ref: `heads/${branch}`,
...context.repo
});
}, { concurrency: 5 });
};
var branchIsTooOld = (dateLastUpdated, daysThreshold) => {
const lastUpdated = new Date(dateLastUpdated);
const now = Date.now();
const timeSinceLastUpdated = now - lastUpdated.getTime();
const threshold = Number(daysThreshold) * SECONDS_IN_A_DAY;
return timeSinceLastUpdated > threshold;
};
export {
deleteStaleBranches,
DeleteStaleBranches
};
//# debugId=1F67AFDE1629D57364756E2164756E21