Skip to content

Commit 3d4cfe1

Browse files
authored
Merge pull request #8 from aschbacd/feature-support-push-events
Feature support push events
2 parents e1de1b9 + 871781e commit 3d4cfe1

File tree

6 files changed

+124
-26
lines changed

6 files changed

+124
-26
lines changed

README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,41 @@ Create the file `.github/workflows/linters.yaml` in your repository and add the
1313
name: Linters
1414

1515
on:
16-
pull_request:
16+
pull_request: {}
17+
push: {}
1718

1819
jobs:
1920
gitlint:
2021
runs-on: ubuntu-latest
2122
name: GitLint
2223
steps:
2324
- name: Lint commits, branches, and pull requests
24-
uses: aschbacd/[email protected]
25+
uses: aschbacd/[email protected]
26+
```
27+
28+
### Example configuration for Jira
29+
30+
If you want to allow Jira issue ids at the beginning of the commit message / pull request title you
31+
can use the following configuration. With this configuration commit messages / pull request titles
32+
like `[GLA-1] Add sample file` as well as `Add sample file` will be valid.
33+
34+
```yaml
35+
name: Linters
36+
37+
on:
38+
pull_request: {}
39+
push: {}
40+
41+
jobs:
42+
gitlint:
43+
runs-on: ubuntu-latest
44+
name: GitLint
45+
steps:
46+
- name: Lint commits, branches, and pull requests
47+
uses: aschbacd/[email protected]
48+
with:
49+
re-commit-message-subject: ^(\[[A-Z]+\-[0-9]+\] )?[A-Z].*((?!\.).)$
50+
re-pull-request-title: ^(\[[A-Z]+\-[0-9]+\] )?[A-Z].*((?!\.).)$
2551
```
2652

2753
## Customization
@@ -48,6 +74,7 @@ The following input keys can be used in your GitHub Actions workflow (shown abov
4874
| re-commit-message-split | Regex used to split commit message subject and body (DotAll) | `([^\n]*)(?:\n\n(.*))?` |
4975
| re-commit-message-subject | Regex used to check commit message subject | `^[A-Z].*((?!\\.).)$` |
5076
| re-pull-request-title | Regex used to check pull request title | `^[A-Z].*((?!\\.).)$` |
77+
| re-tag-name | Regex used to check tag name | `.*` |
5178

5279
## Resources
5380

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ inputs:
7676
description: "Regular expression to check pull request title"
7777
required: false
7878
default: "^[A-Z].*((?!\\.).)$"
79+
re-tag-name:
80+
description: "Regular expression to check tag name"
81+
required: false
82+
default: ".*"
7983
runs:
8084
using: "node12"
8185
main: "index.js"

index.js

+87-20
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ async function run() {
1212
const githubToken = core.getInput('github-token')
1313
const octokit = github.getOctokit(githubToken)
1414

15-
// Get data
16-
const { data: pullRequest } = await octokit.rest.pulls.get({
17-
owner: github.context.payload.repository.owner.login,
18-
repo: github.context.payload.repository.name,
19-
pull_number: github.context.payload.pull_request.number
20-
})
21-
const { data: commits } = await octokit.rest.pulls.listCommits({
22-
owner: github.context.payload.repository.owner.login,
23-
repo: github.context.payload.repository.name,
24-
pull_number: github.context.payload.pull_request.number
25-
})
15+
// Get context
16+
const eventName = github.context.eventName
17+
const payload = github.context.payload
18+
const owner = payload.repository.owner.login
19+
const repo = payload.repository.name
20+
21+
const ref = payload.ref
22+
const pullRequest = payload.pull_request
2623

2724
// -----
2825
// --------------- GET INPUT
@@ -47,21 +44,91 @@ async function run() {
4744
const regexCommitMessageSplit = RegExp(core.getInput('re-commit-message-split'), 's')
4845
const regexCommitMessageSubject = RegExp(core.getInput('re-commit-message-subject'))
4946
const regexPullRequestTitle = RegExp(core.getInput('re-pull-request-title'))
47+
const regexTagName = RegExp(core.getInput('re-tag-name'))
48+
49+
// -----
50+
// --------------- CHECK PULL REQUEST
51+
// ----------
52+
53+
if (eventName === "pull_request") {
54+
// Print pull request title
55+
core.info(`Pull request title: ${pullRequest.title}`)
56+
57+
// Check pull request title
58+
if (!regexPullRequestTitle.test(pullRequest.title))
59+
core.setFailed("Pull Request title does not match regex")
60+
}
5061

5162
// -----
52-
// --------------- CHECK DATA
63+
// --------------- CHECK BRANCH
5364
// ----------
5465

55-
core.info(`Pull request title: ${pullRequest.title}`)
56-
core.info(`Branch name: ${pullRequest.head.ref}`)
66+
// Get branch name
67+
let branchName = ""
68+
if (eventName === "pull_request") {
69+
branchName = pullRequest.head.ref
70+
} else if (eventName === "push" && ref.startsWith("refs/heads")) {
71+
branchName = ref.replace("refs/heads/", "")
72+
}
5773

58-
// Check pull request title
59-
if (!regexPullRequestTitle.test(pullRequest.title))
60-
core.setFailed("Pull Request title does not match regex")
74+
if (branchName !== "") {
75+
// Print branch name
76+
core.info(`Branch name: ${branchName}`)
6177

62-
// Check branch name
63-
if (!regexBranchName.test(pullRequest.head.ref))
64-
core.setFailed("Branch name does not match regex")
78+
// Check branch name
79+
if (!regexBranchName.test(branchName))
80+
core.setFailed("Branch name does not match regex")
81+
}
82+
83+
// -----
84+
// --------------- CHECK TAG
85+
// ----------
86+
87+
if (eventName === "push" && ref.startsWith("refs/tags")) {
88+
// Get tag name
89+
const tagName = ref.replace("refs/tags/", "")
90+
91+
// Print tag name
92+
core.info(`Tag name: ${tagName}`)
93+
94+
// Check tag name
95+
if (!regexTagName.test(tagName))
96+
core.setFailed("Tag name does not match regex")
97+
}
98+
99+
// -----
100+
// --------------- CHECK COMMITS
101+
// ----------
102+
103+
// Get commits
104+
let commits = []
105+
if (eventName === "pull_request") {
106+
// Get all commits from pull request
107+
const { data } = await octokit.rest.pulls.listCommits({
108+
owner: owner,
109+
repo: repo,
110+
pull_number: pullRequest.number
111+
})
112+
113+
// Set commits
114+
commits = data
115+
} else if (eventName === "push") {
116+
// Get pushed commits
117+
let commitPromises = []
118+
119+
payload.commits.forEach(commit => {
120+
commitPromises.push(octokit.rest.repos.getCommit({
121+
owner: owner,
122+
repo: repo,
123+
ref: commit.id
124+
}))
125+
})
126+
127+
// Append commits
128+
for await (const { data: commit } of commitPromises) {
129+
commits.push(commit)
130+
}
131+
}
65132

66133
// Check all commits
67134
commits.forEach(commit => {

node_modules/.package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gitlint-action",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "This GitHub Action ensures that your naming conventions for commits, branches, and pull requests are being respected.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)