Skip to content

Commit

Permalink
tests & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijay Ramesh committed Jul 20, 2019
1 parent e2a89fc commit 5b50f29
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 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!
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:
Add `.github/main.workflow` with the following:

```
workflow "PR Lint Action" {
workflow "Lint your PRs" {
on = "pull_request"
resolves = "PR Lint Action"
}
Expand Down Expand Up @@ -49,4 +49,8 @@ PASS ./index.test.js
✓ 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)
```
```
## Contributing
If you have other things worth automatically checking for in your PRs, please submit a pull request.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Toolkit.run(
async tools => {
const repoInfo = {
owner: tools.context.payload.repository.owner.login,
repo: tools.context.payload.repository.name
repo: tools.context.payload.repository.name,
ref: tools.context.payload.pull_request.head.ref
}

const config = {
...defaults,
...(await getConfig(tools.github, CONFIG_FILENAME, repoInfo))
Expand All @@ -26,15 +28,15 @@ Toolkit.run(
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
tools.context.payload.pull_request.head.ref.toLowerCase() :
tools.context.payload.pull_request.head.ref

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')
tools.log('PR title ' + title + ' does not contain approved project')
return false
}
}
Expand All @@ -45,7 +47,7 @@ Toolkit.run(
// 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')
tools.log('PR branch ' + head_branch + ' does not contain an approved project')
return false
}
}
Expand Down
15 changes: 14 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('pr-lint-action', () => {
it('fails if check_title is true and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))


Expand All @@ -44,6 +45,7 @@ describe('pr-lint-action', () => {
it('passes if check_title is false and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('branch.yml'))


Expand All @@ -57,6 +59,7 @@ describe('pr-lint-action', () => {
it('passes if check_title is true and title matches', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))


Expand All @@ -70,6 +73,7 @@ describe('pr-lint-action', () => {
it('fails if check_branch is true and branch does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('branch.yml'))


Expand All @@ -83,6 +87,7 @@ describe('pr-lint-action', () => {
it('passes if check_branch is false and branch does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))


Expand All @@ -96,6 +101,7 @@ describe('pr-lint-action', () => {
it('passes if check_branch is true and branch matches', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('branch.yml'))


Expand All @@ -109,6 +115,7 @@ describe('pr-lint-action', () => {
it('fails if check_branch and check_title is true and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))


Expand All @@ -122,6 +129,7 @@ describe('pr-lint-action', () => {
it('fails if check_branch and check_title is true and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))

tools.context.payload = pullRequestOpenedFixture(bad_title_and_good_branch)
Expand All @@ -134,6 +142,7 @@ describe('pr-lint-action', () => {
it('passes if check_branch and check_title is true and both match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))


Expand All @@ -146,6 +155,7 @@ describe('pr-lint-action', () => {
it('passes if ignore_case and lower case title/branch', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))


Expand All @@ -159,6 +169,7 @@ describe('pr-lint-action', () => {
it('fails if not ignore_case and lower case title/branch', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('no-ignore-case.yml'))


Expand Down Expand Up @@ -206,7 +217,9 @@ function pullRequestOpenedFixture({ title, ref_name }) {
pull_request: {
number: 1,
title: title,
head_ref_name: ref_name
head: {
ref: ref_name
}
},
repository: {
name: 'pr-lint-action-test',
Expand Down
3 changes: 2 additions & 1 deletion utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const CONFIG_PATH = '.github'
/**
* @returns {Promise<Object.<string, string | string[]>>}
*/
module.exports = async function getConfig(github, fileName, { owner, repo }) {
module.exports = async function getConfig(github, fileName, { owner, repo, ref }) {
try {
const response = await github.repos.getContents({
owner,
repo,
ref: ref,
path: path.posix.join(CONFIG_PATH, fileName)
})

Expand Down

0 comments on commit 5b50f29

Please sign in to comment.