-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmerged.js
122 lines (96 loc) · 3.16 KB
/
merged.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const r = require('rexrex')
const getConfig = require('../config')
const { executeAction } = require('../util')
const { DELETE_BRANCH, TAG } = require('../constants')
const analytics = require('../analytics')
const digit = r.capture(r.extra(r.matchers.NUMBER))
const pattern = r.and(r.capture(r.repeat('v', 0, 1)), digit, '\\.', digit, '\\.', digit)
module.exports.deleteBranch = () => async (context) => {
const thread = context.payload.pull_request
if (thread.merged !== true) return
// Don't delete branches from 'unknown repository's
if (!thread.head.repo) return
// Don't delete branches from forks
if (thread.head.repo.fork === true) return
const config = await getConfig(context)
if (!Array.isArray(config.merges)) return
await Promise.allSettled(
config.merges.map(async (c) => {
const action = c.action || c
return executeAction(action, {
[DELETE_BRANCH]: () => {
return context.github.git
.deleteRef(context.repo({ ref: `heads/${thread.head.ref}` }))
.catch((e) => {
// TODO this is because GitHub has already deleted the reference
if (e.message !== 'Reference does not exist') {
throw e
}
})
},
})
})
)
}
module.exports.createTag = () => async (context) => {
const thread = context.payload.pull_request
if (thread.merged !== true) return
// Only create tags on "master"
if (thread.base.ref !== context.payload.repository.default_branch) return
const isMajor = thread.labels.find(({ name }) => name.toLowerCase().includes('major'))
const isMinor = thread.labels.find(({ name }) => name.toLowerCase().includes('minor'))
const isPatch = thread.labels.find(({ name }) => name.toLowerCase().includes('patch'))
if (!(isMajor || isMinor || isPatch)) {
const config = await getConfig(context)
const isAutoPatch =
Array.isArray(config.merges) &&
config.merges.find((c) => {
const value = c.action || c
return typeof value === 'string' && value.trim().toLowerCase() === TAG
})
if (!isAutoPatch) return
}
const { data } = await context.github.repos.listTags(context.repo())
if (!data || !data[0]) return
const REX = r.regex(pattern)
const matchedTag = data.find((d) => REX.exec(d.name))
if (!matchedTag) return
const match = REX.exec(matchedTag.name)
const v = {
v: match[1] || '',
major: Number(match[2]),
minor: Number(match[3]),
patch: Number(match[4]),
}
let tag
if (isMajor) {
tag = `${v.v}${v.major + 1}.0.0`
} else if (isMinor) {
tag = `${v.v}${v.major}.${v.minor + 1}.0`
} else {
tag = `${v.v}${v.major}.${v.minor}.${v.patch + 1}`
}
const sha = thread.merge_commit_sha
await context.github.git.createTag(
context.repo({
tag,
type: 'commit',
message: `${thread.title} (#${thread.number})`,
object: sha,
})
)
await context.github.git.createRef(
context.repo({
ref: `refs/tags/${tag}`,
sha,
})
)
analytics.track({
userId: context.payload.installation.id,
event: `Tag created`,
properties: context.repo({
tag,
sha,
}),
})
}