-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
127 lines (107 loc) · 3.32 KB
/
index.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
123
124
125
126
127
// Checks API example
// See: https://developer.github.com/v3/checks/ to learn more
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Application} app
*/
module.exports = app => {
app.log('Semantic PR Title app loaded!')
app.on([
'pull_request.opened',
'pull_request.reopened',
'check_run.rerequested',
'pull_request.edited'
], async context => {
let conclusion = 'failure'
let message = ''
context.log.info('context', JSON.stringify(context, null, 2))
const { payload } = context
const action = payload.action
let pr,
headSha,
headBranch
switch (action) {
case 'rerequested':
const prs = payload.check_run.check_suite.pull_requests
pr = prs && prs.length ? prs[0] : null
headSha = payload.check_run.check_suite.head_sha
headBranch = payload.check_run.check_suite.head_branch
break
case 'reopened':
case 'edited':
case 'opened':
pr = payload.pull_request
headSha = pr.head.sha
headBranch = pr.head.ref
break
default:
context.log.info(`action "${action}" not recognized.`)
break
}
// -----------------------------------
// start the check
// -----------------------------------
const check = (await context.github.checks.create(context.repo({
name: 'Validate Title',
head_branch: headBranch,
head_sha: headSha,
status: 'in_progress',
started_at: new Date(),
output: {
title: 'Checking PR title!',
summary: 'Let\'s make sure the PR title is valid.'
}
}))).data
const {
repo,
owner
} = await context.repo()
try {
if (!pr) {
throw new Error(`No PRs found for delivery #<a target="_blank" href="https://github.com/settings/apps/semantic-pr-title/advanced#${context.id}">${context.id}</a>.`)
}
let { title } = pr
if (!title) {
const prQuery = {
owner,
repo,
number: pr.number
}
const fullPr = (await context.github.pullRequests.get(prQuery)).data
if (!fullPr) {
throw new Error(`Error: could not find pull request for query "${JSON.stringify(prQuery, null, 2)}".`)
}
title = fullPr.title
}
const config = await context.config('semantic-pr-title.yml', {
REGEX: '(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\\([a-z0-9\\s]+\\))?(:\\s)([a-z0-9\\s]+)'
})
const regex = new RegExp(config.REGEX, 'i')
context.log.info(`REGEX is "${config.REGEX}"`)
message = `PR Title "${title}" does not match regex "${config.REGEX}".`
if (regex.test(title) === true) {
conclusion = 'success'
message = `PR title "${title}" is valid!`
}
context.log(`message: ${message}`)
} catch (err) {
message = `${err}`
app.log.error(err)
}
// -----------------------------------
// update check
// -----------------------------------
return context.github.checks.update({
repo,
owner,
check_run_id: check.id,
status: 'completed',
completed_at: new Date(),
conclusion,
output: {
title: conclusion === 'success' ? 'Check succeeded!' : 'Check failed!',
summary: message
}
})
})
}