-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
215 lines (173 loc) · 8.34 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const core = require('@actions/core')
const github = require('@actions/github')
async function run() {
try {
// -----
// --------------- GET DATA
// ----------
// Get octokit
const githubToken = core.getInput('github-token')
const octokit = github.getOctokit(githubToken)
// Get context
const eventName = github.context.eventName
const payload = github.context.payload
const owner = payload.repository.owner.login
const repo = payload.repository.name
const ref = payload.ref
const pullRequest = payload.pull_request
// -----
// --------------- GET INPUT
// ----------
const commitMessageBodyMaxLength = parseInt(core.getInput('commit-message-body-max-length'))
const commitMessageBodyMinLength = parseInt(core.getInput('commit-message-body-min-length'))
const commitMessageSubjectMaxLength = parseInt(core.getInput('commit-message-subject-max-length'))
const commitMessageSubjectMinLength = parseInt(core.getInput('commit-message-subject-min-length'))
const prohibitBlankLinesCmBody = (core.getInput('prohibit-blank-lines-cm-body') == 'true')
const prohibitUnknownCommitAuthors = (core.getInput('prohibit-unknown-commit-authors') == 'true')
const prohibitUnknownCommitCommitters = (core.getInput('prohibit-unknown-commit-committers') == 'true')
const prohibitUnsignedCommits = (core.getInput('prohibit-unsigned-commits') == 'true')
const regexBranchName = RegExp(core.getInput('re-branch-name'))
const regexCommitAuthorEmail = RegExp(core.getInput('re-commit-author-email'))
const regexCommitAuthorName = RegExp(core.getInput('re-commit-author-name'))
const regexCommitCommitterEmail = RegExp(core.getInput('re-commit-committer-email'))
const regexCommitCommitterName = RegExp(core.getInput('re-commit-committer-name'))
const regexCommitMessageBody = RegExp(core.getInput('re-commit-message-body'), 's')
const regexCommitMessageSplit = RegExp(core.getInput('re-commit-message-split'), 's')
const regexCommitMessageSubject = RegExp(core.getInput('re-commit-message-subject'))
const regexPullRequestTitle = RegExp(core.getInput('re-pull-request-title'))
const regexTagName = RegExp(core.getInput('re-tag-name'))
// -----
// --------------- CHECK PULL REQUEST
// ----------
if (eventName === "pull_request") {
// Print pull request title
core.info(`Pull request title: ${pullRequest.title}`)
// Check pull request title
if (!regexPullRequestTitle.test(pullRequest.title))
core.setFailed("Pull Request title does not match regex")
}
// -----
// --------------- CHECK BRANCH
// ----------
// Get branch name
let branchName = ""
if (eventName === "pull_request") {
branchName = pullRequest.head.ref
} else if (eventName === "push" && ref.startsWith("refs/heads")) {
branchName = ref.replace("refs/heads/", "")
}
if (branchName !== "") {
// Print branch name
core.info(`Branch name: ${branchName}`)
// Check branch name
if (!regexBranchName.test(branchName))
core.setFailed("Branch name does not match regex")
}
// -----
// --------------- CHECK TAG
// ----------
if (eventName === "push" && ref.startsWith("refs/tags")) {
// Get tag name
const tagName = ref.replace("refs/tags/", "")
// Print tag name
core.info(`Tag name: ${tagName}`)
// Check tag name
if (!regexTagName.test(tagName))
core.setFailed("Tag name does not match regex")
}
// -----
// --------------- CHECK COMMITS
// ----------
// Get commits
let commits = []
if (eventName === "pull_request") {
// Get all commits from pull request
const { data } = await octokit.rest.pulls.listCommits({
owner: owner,
repo: repo,
pull_number: pullRequest.number
})
// Set commits
commits = data
} else if (eventName === "push") {
// Get pushed commits
let commitPromises = []
payload.commits.forEach(commit => {
commitPromises.push(octokit.rest.repos.getCommit({
owner: owner,
repo: repo,
ref: commit.id
}))
})
// Append commits
for await (const { data: commit } of commitPromises) {
commits.push(commit)
}
}
// Check all commits
commits.forEach(commit => {
// Skip merge commits
if (commit.parents && commit.parents.length > 1) {
core.info(`Merge commit detected: ${commit.sha}`);
return;
}
// Split commit message
let matches = regexCommitMessageSplit.exec(commit.commit.message)
let commitMessageSubject = matches[1]
let commitMessageBody = matches[2]
console.log('-----')
core.info(`Commit hash: ${commit.sha}`)
core.info(`Commit author email: ${commit.commit.author.email}`)
core.info(`Commit author name: ${commit.commit.author.name}`)
core.info(`Commit author GitHub account: ${commit.author == null ? undefined : commit.author.login}`)
core.info(`Commit committer email: ${commit.commit.committer.email}`)
core.info(`Commit committer name: ${commit.commit.committer.name}`)
core.info(`Commit committer GitHub account: ${commit.committer == null ? undefined : commit.committer.login}`)
core.info(`Commit has valid signature: ${commit.commit.verification.verified}`)
core.info(`Commit message subject: ${commitMessageSubject}`)
core.info(`Commit message body: ${commitMessageBody}`)
// Check commit author
if (prohibitUnknownCommitAuthors && commit.author == null)
core.setFailed(`Commit author does not exist on GitHub (${commit.sha.substr(0, 7)})`)
if (!regexCommitAuthorEmail.test(commit.commit.author.email))
core.setFailed(`Commit author email does not match regex (${commit.sha.substr(0, 7)})`)
if (!regexCommitAuthorName.test(commit.commit.author.name))
core.setFailed(`Commit author name does not match regex (${commit.sha.substr(0, 7)})`)
// Check commit committer
if (prohibitUnknownCommitCommitters && commit.committer == null)
core.setFailed(`Commit committer does not exist on GitHub (${commit.sha.substr(0, 7)})`)
if (!regexCommitCommitterEmail.test(commit.commit.committer.email))
core.setFailed(`Commit committer email does not match regex (${commit.sha.substr(0, 7)})`)
if (!regexCommitCommitterName.test(commit.commit.committer.name))
core.setFailed(`Commit committer name does not match regex (${commit.sha.substr(0, 7)})`)
// Check for valid signature
if (prohibitUnsignedCommits && !commit.commit.verification.verified)
core.setFailed(`Commit has no valid signature (${commit.sha.substr(0, 7)})`)
// Check commit message subject
if (commitMessageSubjectMinLength != -1 && commitMessageSubject.length < commitMessageSubjectMinLength)
core.setFailed(`Commit message subject is too short (${commit.sha.substr(0, 7)})`)
if (commitMessageSubjectMaxLength != -1 && commitMessageSubject.length > commitMessageSubjectMaxLength)
core.setFailed(`Commit message subject is too long (${commit.sha.substr(0, 7)})`)
if (!regexCommitMessageSubject.test(commitMessageSubject))
core.setFailed(`Commit message subject does not match regex (${commit.sha.substr(0, 7)})`)
// Check commit message body
if (commitMessageBody != null) {
commitMessageBody.split("\n").forEach(function (line, index) {
if (line.length == 0) {
if (prohibitBlankLinesCmBody)
core.setFailed(`Blank lines are not allowed in commit message body; line ${(index+1).toString()} (${commit.sha.substr(0, 7)})`)
} else if (commitMessageBodyMinLength != -1 && line.length < commitMessageBodyMinLength) {
core.setFailed(`Commit message body line ${(index+1).toString()} is too short (${commit.sha.substr(0, 7)})`)
}
if (commitMessageBodyMaxLength != -1 && line.length > commitMessageBodyMaxLength)
core.setFailed(`Commit message body line ${(index+1).toString()} is too long (${commit.sha.substr(0, 7)})`)
})
if (!regexCommitMessageBody.test(commitMessageBody))
core.setFailed(`Commit message body does not match regex (${commit.sha.substr(0, 7)})`)
}
})
} catch (error) {
core.setFailed(error.message);
}
}
run()