forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-commit.js
118 lines (105 loc) · 2.7 KB
/
validate-commit.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
'use strict'
import pkg from "@octokit/action";
const { Octokit } = pkg;
import ValidateCommit from "core-validate-commit";
import { readFile } from "fs/promises";
const octokit = new Octokit();
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
const eventPayload = JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH));
const { data: checkRun } = await octokit.checks.create({
repo,
owner,
'name': 'Validate Commit',
'head_sha': eventPayload.pull_request.head.sha
});
const { repository } = await octokit.graphql(
`query($owner:String!, $repo:String!, $pr:Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
commits(last: 250) {
nodes {
commit {
oid,
message
}
}
}
}
}
}`,
{
owner: owner,
repo: repo,
pr: eventPayload.number
}
);
const validate = new ValidateCommit({ 'validate-metadata': false })
const errors = []
let shouldExit = false;
process.on('beforeExit', () => {
if (shouldExit) {
return
}
shouldExit = true
console.log(errors)
if (errors.length === 0) {
octokit.checks.update({
repo,
owner,
check_run_id: checkRun.id,
'status': 'completed',
'conclusion': 'success',
'output': {
'title': "Valid Commit",
'summary': "",
'annotations': errors
}
}).then(() => console.log('done'))
return
}
const summary = `Commits listed below don't follow our [commit guidelines](https://goo.gl/p2fr5Q):
${errors.map(e => `- [${e.sha} ${e.title}] ${e.message}`).join('\n')}`;
octokit.checks.update({
repo,
owner,
check_run_id: checkRun.id,
'status': 'completed',
'conclusion': 'neutral',
'output': {
'title': "Invalid Commit Message",
summary,
'annotations': errors
}
}).then(() => console.log('done'))
});
validate.on('message', ({ data, commit }) => {
const { message, level } = data;
if (level === 'pass' || level === 'skip') {
return;
}
const { title, sha } = commit
errors.push({
path: sha,
start_line: 1,
end_line: 1,
annotation_level: level === 'fail' ? 'failure' : 'warning',
message,
title,
raw_details: commit._rawStr
});
})
const skippable = [
'fixup!', '!fixup', '[fixup]', 'fixup:', 'fixup ',
'squash!', '!squash', '[squash]', 'squash:', 'squash ',
]
for (const { commit } of repository.pullRequest.commits.nodes) {
if (skippable.some(s => commit.message.startsWith(s))) {
continue
}
const { headers, data } = await octokit.git.getCommit({
"owner": owner,
"repo": repo,
"commit_sha": commit.oid
})
validate.lint(data)
}