Skip to content

Commit

Permalink
Added rules for PR titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Puthraya committed Nov 6, 2019
0 parents commit 1e4e2c9
Show file tree
Hide file tree
Showing 374 changed files with 72,667 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: main
# This workflow is triggered on pushes to the repository.
on: [pull_request]

jobs:
build:
# Job name is Greeting
name: Check
# This job runs on Linux
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: '10.x' # This step prints an output (time) from the previous step's action.
- run: npm install
- name: Validate
run: node index.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 GitHub, Inc. and contributors

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.
22 changes: 22 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Branch naming rules
<img alt="GitHub Actions status" src="https://github.com/deepakputhraya/action-pr-title/workflows/main/badge.svg">

Github action to enforce Pull Request title conventions

## Usage

See [action.yml](./action.yml)

```yaml
steps:
- uses: deepakputhraya/action-pr-title@master
with:
regex: '([a-z])+\/([a-z])+' # Regex the title should match.
allowed_prefixes: 'feature,stable,fix' # title should start with the given prefix
prefix_case_sensitive: false # title prefix are case insensitive
min_length: 5 # Min length of the title
max_length: 20 # Max length of the title
```
## License
The scripts and documentation in this project are released under the [MIT License](./LICENSE)
26 changes: 26 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: 'Pull Request title rules'
description: 'Github action to enforce Pull Request title conventions'
author: 'deepakputhraya'
inputs:
regex:
description: 'Regex to validate the pull request title'
default: '.+'
allowed_prefixes:
description: 'Comma separated list of prefix allowed to be used in title. eg: feature,hotfix,JIRA-'
default: ''
prefix_case_sensitive:
description: 'Are the allowed prefixes case sensitive?'
default: false
min_length:
description: 'Min length of title'
default: 1
max_length:
description: 'Max length of title. -1 to ignore the rule'
default: -1

runs:
using: 'node12'
main: 'index.js'
branding:
icon: 'alert-triangle'
color: 'gray-dark'
61 changes: 61 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const core = require('@actions/core');
const github = require('@actions/github');

const validEvent = ['pull_request'];

function validateTitlePrefix(title, prefix, caseSensitive) {
if (!caseSensitive) {
prefix = prefix.toLowerCase();
title = prefix.toLowerCase();
}
return title.startsWith(prefix);
}

async function run() {
try {
const eventName = github.context.eventName;
core.info(`Event name: ${eventName}`);
if (validEvent.indexOf(eventName) < 0) {
core.setFailed(`Invalid event: ${eventName}`);
return;
}

const title = github.context.payload.title;
core.info(`Pull Request title: "${title}"`);
// Check if title pass regex
const regex = RegExp(core.getInput('regex'));
core.info(`Regex: ${regex}`);
if (!regex.test(title)) {
core.setFailed(`Pull Request title "${title}" failed to pass match regex - ${regex}`);
return
}

// Check min length
const minLen = parseInt(core.getInput('min_length'));
if (title.length < minLen) {
core.setFailed(`Pull Request title "${title}" is smaller than min length specified - ${minLen}`);
return
}

// Check max length
const maxLen = parseInt(core.getInput('max_length'));
if (maxLen > 0 && title.length > maxLen) {
core.setFailed(`Pull Request title "${title}" is greater than max length specified - ${maxLen}`);
return
}

// Check if title starts with a prefix
const prefixes = core.getInput('allowed_prefixes');
const prefixCaseSensitive = (core.getInput('prefix_case_sensitive') === 'true');
core.info(`Allowed Prefixes: ${prefixes}`);
if (prefixes.length > 0 && !prefixes.split(',').some((el) => validateTitlePrefix(title, el, prefixCaseSensitive))) {
core.setFailed(`Pull Request title "${title}" did not match any of the prefixes - ${prefixes}`);
return
}

} catch (error) {
core.setFailed(error.message);
}
}

run();
1 change: 1 addition & 0 deletions node_modules/.bin/semver

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

1 change: 1 addition & 0 deletions node_modules/.bin/which

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

107 changes: 107 additions & 0 deletions node_modules/@actions/core/README.md

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

16 changes: 16 additions & 0 deletions node_modules/@actions/core/lib/command.d.ts

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

66 changes: 66 additions & 0 deletions node_modules/@actions/core/lib/command.js

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

1 change: 1 addition & 0 deletions node_modules/@actions/core/lib/command.js.map

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

Loading

0 comments on commit 1e4e2c9

Please sign in to comment.