forked from dawidd6/action-delete-branch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
60 lines (50 loc) · 2.03 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
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 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(",") : []
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)
}
}
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
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()