-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch-cleanup.js
67 lines (59 loc) · 1.88 KB
/
branch-cleanup.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
const Octokit = require('@octokit/rest')
const token = process.env.GITHUB_TOKEN
const client = new Octokit({ auth: `token ${token}` })
const data = JSON.parse(process.env.EVENT_JSON)
async function main() {
const ref = data.pull_request.head.ref
const owner = data.pull_request.head.repo.owner.login
const repo = data.pull_request.head.repo.name
if (!data.pull_request.merged) {
console.log(`Skipping: branch "${ref}" has not been merged`)
return
}
const repoInfo = await client.repos.get({ owner, repo })
const defaultBranch = repoInfo.data.default_branch
if (defaultBranch === ref) {
console.log(`Skipping: cannot delete default branch "${ref}"`)
return
}
const branchInfo = await client.repos.getBranch({ owner, repo, branch: ref })
if (branchInfo.data.is_protected) {
console.log(`Skipping: cannot delete protected branch "${ref}"`)
return
}
const pullsWithRefAsBase = await client.pulls.list({
owner,
repo,
state: 'open',
base: ref
})
if (pullsWithRefAsBase.data.length) {
console.log(
`Skipping: ${ref} is the base of PR ${pullsWithRefAsBase.data[0].number}`
)
return
}
console.log(`Deleting branch "${ref}" for ${owner}/${repo}...`)
try {
const deleteResponse = await client.git.deleteRef({
owner,
repo,
ref: `heads/${ref}`
})
console.log(`Branch ${ref} deleted`)
if (deleteResponse.status === 422) {
console.log(`The branch ${ref} was already deleted`)
} else if (deleteResponse.status === 204) {
} else {
console.log('Something unexpected happened!')
console.log(
`status: ${deleteResponse.status}, response body: ${deleteResponse.data}`
)
}
} catch (error) {
console.log('Something unexpected happened!')
console.log(error.message)
}
}
// module.exports is only required for the async tests to function
module.exports = main()