forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
63 lines (55 loc) · 2.04 KB
/
commit-message.yaml
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
name: 'Commit Message Check'
on:
pull_request_target:
types:
- opened
- reopened
- synchronize
jobs:
check-commit-messages:
name: Check Commit Messages
runs-on: ubuntu-latest
steps:
- name: Block mentions
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const commits = await github.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const mentionRegex = /(^| )@([a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38})/gmi;
let mentioningCommitShas = [];
for (const commit of commits.data) {
while (match = mentionRegex.exec(commit.commit.message)) {
const mention = match[2];
// check if detected mention is actually a github username
try {
const user = await github.users.getByUsername({
username: mention,
});
if (user.data.login == mention) {
mentioningCommitShas.push(commit.sha);
}
} catch (err) {
if (err.status != 404) {
throw err;
}
}
}
}
if (mentioningCommitShas.length == 0) {
core.info("no commit messages with mentions detected");
return;
}
errMsg = `This PR contains commits including \`@mentions\` in the commit message: ${mentioningCommitShas.join(", ")}\n`+
`Please remove the mentions from the commit messages to prevent notifying the mentioned users every time the commit is pushed to some fork of this repository.`;
await github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `@${context.payload.pull_request.user.login} ` + errMsg
});
core.setFailed(errMsg);