WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 5b50f29

Browse files
author
Vijay Ramesh
committed
tests & docs
1 parent e2a89fc commit 5b50f29

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# pr-lint-action
22

3-
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!
3+
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.
44

55
## Usage
66

7-
Add `.github/pr-lint.workflow` with the following:
7+
Add `.github/main.workflow` with the following:
88

99
```
10-
workflow "PR Lint Action" {
10+
workflow "Lint your PRs" {
1111
on = "pull_request"
1212
resolves = "PR Lint Action"
1313
}
@@ -49,4 +49,8 @@ PASS ./index.test.js
4949
✓ passes if check_branch and check_title is true and both match (58ms)
5050
✓ passes if ignore_case and lower case title/branch (55ms)
5151
✓ fails if not ignore_case and lower case title/branch (109ms)
52-
```
52+
```
53+
54+
## Contributing
55+
56+
If you have other things worth automatically checking for in your PRs, please submit a pull request.

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ Toolkit.run(
1414
async tools => {
1515
const repoInfo = {
1616
owner: tools.context.payload.repository.owner.login,
17-
repo: tools.context.payload.repository.name
17+
repo: tools.context.payload.repository.name,
18+
ref: tools.context.payload.pull_request.head.ref
1819
}
20+
1921
const config = {
2022
...defaults,
2123
...(await getConfig(tools.github, CONFIG_FILENAME, repoInfo))
@@ -26,15 +28,15 @@ Toolkit.run(
2628
tools.context.payload.pull_request.title
2729

2830
const head_branch = config.ignore_case ?
29-
tools.context.payload.pull_request.head_ref_name.toLowerCase() :
30-
tools.context.payload.pull_request.head_ref_name
31+
tools.context.payload.pull_request.head.ref.toLowerCase() :
32+
tools.context.payload.pull_request.head.ref
3133

3234
const projects = config.projects.map(project => config.ignore_case ? project.toLowerCase() : project)
3335
const title_passed = (() => {
3436
if (config.check_title) {
3537
// check the title matches [PROJECT-1234] somewhere
3638
if (!projects.some(project => title.match(new RegExp('\\[' + project + '-\\d*\\]')))) {
37-
tools.log('PR title does not contain approved project')
39+
tools.log('PR title ' + title + ' does not contain approved project')
3840
return false
3941
}
4042
}
@@ -45,7 +47,7 @@ Toolkit.run(
4547
// check the branch matches PROJECT-1234 or PROJECT_1234 somewhere
4648
if (config.check_branch) {
4749
if (!projects.some(project => head_branch.match(new RegExp(project + '[-_]\\d*')))) {
48-
tools.log('PR branch does not contain an approved project')
50+
tools.log('PR branch ' + head_branch + ' does not contain an approved project')
4951
return false
5052
}
5153
}

index.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('pr-lint-action', () => {
3131
it('fails if check_title is true and title does not match', async () => {
3232
nock('https://api.github.com')
3333
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
34+
.query(true)
3435
.reply(200, configFixture('title.yml'))
3536

3637

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

4951

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

6265

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

7579

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

8893

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

101107

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

114121

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

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

139148

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

151161

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

164175

@@ -206,7 +217,9 @@ function pullRequestOpenedFixture({ title, ref_name }) {
206217
pull_request: {
207218
number: 1,
208219
title: title,
209-
head_ref_name: ref_name
220+
head: {
221+
ref: ref_name
222+
}
210223
},
211224
repository: {
212225
name: 'pr-lint-action-test',

utils/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const CONFIG_PATH = '.github'
66
/**
77
* @returns {Promise<Object.<string, string | string[]>>}
88
*/
9-
module.exports = async function getConfig(github, fileName, { owner, repo }) {
9+
module.exports = async function getConfig(github, fileName, { owner, repo, ref }) {
1010
try {
1111
const response = await github.repos.getContents({
1212
owner,
1313
repo,
14+
ref: ref,
1415
path: path.posix.join(CONFIG_PATH, fileName)
1516
})
1617

0 commit comments

Comments
 (0)