-
Notifications
You must be signed in to change notification settings - Fork 55
/
delete-merged-branch.js
32 lines (27 loc) · 1.18 KB
/
delete-merged-branch.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
module.exports = async (context) => {
const config = await context.config('delete-merged-branch-config.yml', { exclude: [] })
const headRepoId = context.payload.pull_request.head.repo.id
const baseRepoId = context.payload.pull_request.base.repo.id
const owner = context.payload.repository.owner.login
const repo = context.payload.repository.name
const branchName = context.payload.pull_request.head.ref
const ref = `heads/${branchName}`
if (headRepoId !== baseRepoId) {
context.log.info(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
return
}
if (config.exclude.some((rule) => new RegExp(`^${rule.split('*').join('.*')}$`).test(branchName))) {
context.log.info(`Branch ${branchName} excluded. Keeping ${context.payload.pull_request.head.label}`)
return
}
if (!context.payload.pull_request.merged) {
context.log.info(`PR was closed but not merged. Keeping ${owner}/${repo}/${ref}`)
return
}
try {
await context.github.gitdata.deleteReference({ owner, repo, ref })
context.log.info(`Successfully deleted ${owner}/${repo}/${ref}`)
} catch (e) {
context.log.warn(e, `Failed to delete ${owner}/${repo}/${ref}`)
}
}