Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijay Ramesh committed Jul 19, 2019
0 parents commit e2a89fc
Show file tree
Hide file tree
Showing 13 changed files with 6,115 additions and 0 deletions.
89 changes: 89 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:slim

# Labels for GitHub to read your action
LABEL "com.github.actions.name"="PR Lint Action"
LABEL "com.github.actions.description"="Lint PRs to ensure they contain a ticket and more!"
LABEL "com.github.actions.icon"="book-open"
LABEL "com.github.actions.color"="blue"

# Copy the package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm ci

# Copy the rest of your action's code
COPY . .

# Run `node /index.js`
ENTRYPOINT ["node", "/index.js"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2019 Vijay Ramesh

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.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# pr-lint-action

A GitHub Action that verifies your pull request contains a reference to a ticket. It will optionally check the PR title contains `[PROJ-1234]` and the branch contains PROJ-1234 or PROJ_1234. This helps ensure every PR gets mapped to a ticket in Jira!

## Usage

Add `.github/pr-lint.workflow` with the following:

```
workflow "PR Lint Action" {
on = "pull_request"
resolves = "PR Lint Action"
}
action "PR Lint Action" {
uses = "vijaykramesh/pr-lint-action@master"
secrets = ["GITHUB_TOKEN"]
}
```

## Configuration

Configure by creating a `.github/pr-lint.yml` file:

For example:

```yml
projects: ['PROJ', 'ABC']
check_title: true
check_branch: true
ignore_case: true
```
## Testing
Run `jest test` to test:

```
PASS ./index.test.js
pr-lint-action
✓ fails if check_title is true and title does not match (106ms)
✓ passes if check_title is false and title does not match (67ms)
✓ passes if check_title is true and title matches (61ms)
✓ fails if check_branch is true and branch does not match (57ms)
✓ passes if check_branch is false and branch does not match (59ms)
✓ passes if check_branch is true and branch matches (57ms)
✓ fails if check_branch and check_title is true and title does not match (59ms)
✓ fails if check_branch and check_title is true and title does not match (59ms)
✓ passes if check_branch and check_title is true and both match (58ms)
✓ passes if ignore_case and lower case title/branch (55ms)
✓ fails if not ignore_case and lower case title/branch (109ms)
```
4 changes: 4 additions & 0 deletions fixtures/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
projects: ['PROJ']
check_title: true
check_branch: true
ignore_case: true
4 changes: 4 additions & 0 deletions fixtures/branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
projects: ['PROJ']
check_title: false
check_branch: true
ignore_case: true
4 changes: 4 additions & 0 deletions fixtures/no-ignore-case.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
projects: ['PROJ']
check_title: true
check_branch: true
ignore_case: false
4 changes: 4 additions & 0 deletions fixtures/title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
projects: ['PROJ']
check_title: true
check_branch: false
ignore_case: true
64 changes: 64 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { Toolkit } = require('actions-toolkit')
const getConfig = require('./utils/config')

const CONFIG_FILENAME = 'pr-lint.yml'

const defaults = {
projects: ['PROJ'],
check_title: true,
check_branch: false,
ignore_case: false
}

Toolkit.run(
async tools => {
const repoInfo = {
owner: tools.context.payload.repository.owner.login,
repo: tools.context.payload.repository.name
}
const config = {
...defaults,
...(await getConfig(tools.github, CONFIG_FILENAME, repoInfo))
}

const title = config.ignore_case ?
tools.context.payload.pull_request.title.toLowerCase() :
tools.context.payload.pull_request.title

const head_branch = config.ignore_case ?
tools.context.payload.pull_request.head_ref_name.toLowerCase() :
tools.context.payload.pull_request.head_ref_name

const projects = config.projects.map(project => config.ignore_case ? project.toLowerCase() : project)
const title_passed = (() => {
if (config.check_title) {
// check the title matches [PROJECT-1234] somewhere
if (!projects.some(project => title.match(new RegExp('\\[' + project + '-\\d*\\]')))) {
tools.log('PR title does not contain approved project')
return false
}
}
return true
})()

const branch_passed = (() => {
// check the branch matches PROJECT-1234 or PROJECT_1234 somewhere
if (config.check_branch) {
if (!projects.some(project => head_branch.match(new RegExp(project + '[-_]\\d*')))) {
tools.log('PR branch does not contain an approved project')
return false
}
}
return true
})()

const statuses = [title_passed, branch_passed]

if (statuses.some(status => status === false )){
tools.exit.failure("PR Linting Failed")
} else {
tools.exit.success()
}
},
{ event: ['pull_request.opened', 'pull_request.edited', 'pull_request.synchronize'], secrets: ['GITHUB_TOKEN'] }
)
Loading

0 comments on commit e2a89fc

Please sign in to comment.