forked from dawidd6/action-delete-branch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
77 lines (65 loc) · 2.64 KB
/
main.js
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
const core = require('@actions/core')
const github = require('@actions/github')
async function main() {
try {
const token = core.getInput("github_token", { required: true })
const numbers = core.getInput("numbers")
const owner = core.getInput("owner")
const repository = core.getInput("repository")
const branches = core.getInput("branches")
const exclude = core.getInput("exclude")
const prefix = core.getInput("prefix")
const suffix = core.getInput("suffix")
const soft_fail = core.getInput("soft_fail")
const client = github.getOctokit(token)
let branchesToDelete = branches ? branches.split(",") : []
// get all branches from repository
const response = await client.repos.listBranches({
...github.context.repo
})
console.log("==> Found " + response.data.length + " branches")
for (const branch of response.data) {
branchesToDelete.push(branch.name)
}
console.log("==> Branches to delete: " + branchesToDelete)
if (numbers) {
for (const number of numbers.split(",")) {
const pull = await client.pulls.get({
...github.context.repo,
pull_number: number
})
branchesToDelete.push(pull.data.head.ref)
}
}
console.log("==> Branches to delete: " + branchesToDelete)
console.log("==> Number to delete: " + branchesToDelete.length)
let ownerOfRepository = owner ? owner : github.context.repo.owner
let repositoryContainingBranches = repository ? repository : github.context.repo.repo
for (let branch of branchesToDelete) {
if (prefix)
branch = prefix + branch
if (suffix)
branch = branch + suffix
if (exclude && exclude.includes(branch)) {
return;
}
console.log("==> Deleting \"" + ownerOfRepository + "/" + repositoryContainingBranches + "/" + branch + "\" branch")
try {
await client.git.deleteRef({
owner: ownerOfRepository,
repo: repositoryContainingBranches,
ref: "heads/" + branch
})
} catch (error) {
const shouldFailSoftly = (soft_fail === 'true');
if (shouldFailSoftly)
core.warning(error.message)
else
core.setFailed(error.message)
}
}
} catch (error) {
core.setFailed(error.message)
}
}
main()