Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aschbacd committed Dec 13, 2020
0 parents commit deb7aa8
Show file tree
Hide file tree
Showing 382 changed files with 147,506 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Dominik Aschbacher

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# GitLint Action

This GitHub Action ensures that your naming conventions for commits, branches, and pull requests
are being respected. GitLint uses min. and max. character counts as well as regular expressions
to check against the data. It is recommended to add a workflow that runs when a pull request is
created or when it gets updated.

## Usage

Create the file `.github/workflows/gitlint.yaml` in your repository and add the following workflow:

```yaml
name: GitLint

on:
pull_request:

jobs:
gitlint:
runs-on: ubuntu-latest
name: GitLint
steps:
- name: Lint commits, branches, and pull requests
uses: aschbacd/gitlint-action@v1.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
commit-message-body-max-length: 72
commit-message-subject-max-length: 50
prohibit-unknown-commit-authors: true
prohibit-unknown-commit-committers: true
re-commit-message-subject: "^[A-Z].*((?!\\.).)$"
re-pull-request-title: "^[A-Z].*((?!\\.).)$"
```
## Customization
The following input keys can be used in your GitHub Actions workflow (shown above).
| Key | Description | Default | Recommended |
| ---------------------------------- | ------------------------------------------------------------ | ----------------------- | ----------------------------- |
| commit-message-body-max-length | Max. characters for line in commit message body | -1 (disabled) | 72 |
| commit-message-body-min-length | Min. characters for line in commit message body | -1 (disabled) | -1 (disabled) |
| commit-message-subject-max-length | Max. characters for commit message subject | -1 (disabled) | 50 |
| commit-message-subject-min-length | Min. characters for commit message subject | -1 (disabled) | -1 (disabled) |
| github-token | Token used to authenticate against GitHub api | `-` | `${{ secrets.GITHUB_TOKEN }}` |
| prohibit-unknown-commit-authors | Commit author must be GitHub user | `false` | `true` |
| prohibit-unknown-commit-committers | Commit committer must be GitHub user | `false` | `true` |
| prohibit-unsigned-commits | Commits without a valid signature are invalid | `false` | `false` |
| re-branch-name | Regex used to check branch name | `.*` | `[a-z]+\/.+` |
| re-commit-author-email | Regex used to check commit author email | `.*` | `.*` |
| re-commit-author-name | Regex used to check commit author name | `.*` | `.*` |
| re-commit-committer-email | Regex used to check commit committer email | `.*` | `.*` |
| re-commit-committer-name | Regex used to check commit committer name | `.*` | `.*` |
| re-commit-message-body | Regex used to check commit message body (DotAll) | `.*` | `.*` |
| re-commit-message-split | Regex used to split commit message subject and body (DotAll) | `([^\n]*)(?:\n\n(.*))?` | `([^\n]*)(?:\n\n(.*))?` |
| re-commit-message-subject | Regex used to check commit message subject | `.*` | `^[A-Z].*((?!\.).)$` |
| re-pull-request-title | Regex used to check pull request title | `.*` | `^[A-Z].*((?!\.).)$` |

## Resources

If you want to learn more about Git commits check out [this section](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)
of the Pro Git book.
73 changes: 73 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: "GitLint Action"
description: "Check if naming conventions for commits, branches, and PRs are respected"
inputs:
commit-message-body-max-length:
description: "Max. length of commit message body"
required: false
default: -1
commit-message-body-min-length:
description: "Min. length of commit message body"
required: false
default: -1
commit-message-subject-max-length:
description: "Max. length of commit message subject"
required: false
default: -1
commit-message-subject-min-length:
description: "Min. length of commit message subject"
required: false
default: -1
github-token:
description: "GitHub token used to access api"
required: true
prohibit-unknown-commit-authors:
description: "Prohibit commit authors that are not known to GitHub"
required: false
default: false
prohibit-unknown-commit-committers:
description: "Prohibit commit committers that are not known to GitHub"
required: false
default: false
prohibit-unsigned-commits:
description: "Prohibit commits without a valid signature"
required: false
default: false
re-branch-name:
description: "Regular expression to check branch name"
required: false
default: ".*"
re-commit-author-email:
description: "Regular expression to check commit author email"
required: false
default: ".*"
re-commit-author-name:
description: "Regular expression to check commit author name"
required: false
default: ".*"
re-commit-committer-email:
description: "Regular expression to check commit author email"
required: false
default: ".*"
re-commit-committer-name:
description: "Regular expression to check commit author name"
required: false
default: ".*"
re-commit-message-body:
description: "Regular expression to check commit message body"
required: false
default: ".*"
re-commit-message-split:
description: "Regular expression to split commit message into subject and body"
required: false
default: "([^\n]*)(?:\n\n(.*))?"
re-commit-message-subject:
description: "Regular expression to check commit message subject"
required: false
default: ".*"
re-pull-request-title:
description: "Regular expression to check pull request title"
required: false
default: ".*"
runs:
using: "node12"
main: "index.js"
137 changes: 137 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
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 data
const { data: pullRequest } = await octokit.pulls.get({
owner: github.context.payload.repository.owner.login,
repo: github.context.payload.repository.name,
pull_number: github.context.payload.pull_request.number
})
const { data: commits } = await octokit.pulls.listCommits({
owner: github.context.payload.repository.owner.login,
repo: github.context.payload.repository.name,
pull_number: github.context.payload.pull_request.number
})

// -----
// --------------- 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 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'))

// -----
// --------------- CHECK DATA
// ----------

core.info(`Pull request title: ${pullRequest.title}`)
core.info(`Branch name: ${pullRequest.head.ref}`)

// Check pull request title
if (!regexPullRequestTitle.test(pullRequest.title))
core.setFailed("Pull Request title does not match regex")

// Check branch name
if (!regexBranchName.test(pullRequest.head.ref))
core.setFailed("Branch name does not match regex")

// Check all commits
commits.forEach(commit => {
// 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}`)
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}`)
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 (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()
9 changes: 9 additions & 0 deletions node_modules/@actions/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit deb7aa8

Please sign in to comment.