Skip to content

Commit

Permalink
Merge pull request #3 from Logitech/ddevassy/default_configfiles
Browse files Browse the repository at this point in the history
Updated default yml config to bypass the external config files
  • Loading branch information
ddevassy authored Apr 13, 2020
2 parents 8a6426c + 506a997 commit c163426
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 31 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

A GitHub Action that verifies your pull request contains a reference to a ticket. You can use this to (optionally) check:

* The PR title contains `[PROJ-1234]`
* The branch name contains `PROJ-1234` or `PROJ_1234`
* Each commit contains `[PROJ-1234]`
* The PR title contains `[VC-1234]`
* The branch name contains `VC-1234` or `VC_1234`
* Each commit contains `[VC-1234]`



## Usage

Add `.github/workflows/main.yml` with the following:
Add `.github/workflows/jiracheck.yml` with the following:

```
name: PR Lint
Expand All @@ -19,20 +19,20 @@ jobs:
pr_lint:
runs-on: ubuntu-latest
steps:
- uses: vijaykramesh/pr-lint-action@v1.0
- uses: Logitech/vc-pr-jira-action@v1.2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

## Configuration

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

For example:

```yml
projects: ['PROJ', 'ABC']
tickets: ['VC', 'JIRA']
check_title: true
check_branch: true
check_commits: true
Expand All @@ -41,7 +41,7 @@ ignore_case: true
## Testing
Run `jest test` to test:
Run `jest test` or `npm test` to test:

```
PASS ./index.test.js
Expand Down
2 changes: 1 addition & 1 deletion fixtures/all.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projects: ['PROJ']
tickets: ['VC']
check_title: true
check_branch: true
check_commits: true
Expand Down
2 changes: 1 addition & 1 deletion fixtures/branch.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projects: ['PROJ']
tickets: ['VC']
check_title: false
check_branch: true
ignore_case: true
2 changes: 1 addition & 1 deletion fixtures/commits.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projects: ['PROJ']
tickets: ['VC']
check_title: false
check_branch: false
check_commits: true
Expand Down
2 changes: 1 addition & 1 deletion fixtures/no-ignore-case.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projects: ['PROJ']
tickets: ['VC']
check_title: true
check_branch: true
ignore_case: false
2 changes: 1 addition & 1 deletion fixtures/title.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projects: ['PROJ']
tickets: ['VC']
check_title: true
check_branch: false
ignore_case: true
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { Toolkit } = require('actions-toolkit')
const getConfig = require('./utils/config')

const CONFIG_FILENAME = 'pr-lint.yml'
const CONFIG_FILENAME = 'jira_config.yml'

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

Toolkit.run(
Expand All @@ -34,43 +34,43 @@ Toolkit.run(
pull_request.head.ref.toLowerCase() :
pull_request.head.ref

const projects = config.projects.map(project => config.ignore_case ? project.toLowerCase() : project)
const tickets = config.tickets.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(createWrappedProjectRegex(project)))) {
tools.log('PR title ' + title + ' does not contain approved project')
// check the title matches [VC-1234] somewhere
if (!tickets.some(project => title.match(createWrappedProjectRegex(project)))) {
tools.log('PR title ' + title + ' does not contain approved Jiras')
return false
}
}
return true
})()

const branch_passed = (() => {
// check the branch matches PROJECT-1234 or PROJECT_1234 somewhere
// check the branch matches VC-1234 or VC_1234 somewhere
if (config.check_branch) {
if (!projects.some(project => head_branch.match(createProjectRegex(project)))) {
tools.log('PR branch ' + head_branch + ' does not contain an approved project')
if (!tickets.some(project => head_branch.match(createProjectRegex(project)))) {
tools.log('PR branch ' + head_branch + ' does not contain an approved Jiras')
return false
}
}
return true
})()

const commits_passed = await (async () => {
// check the branch matches PROJECT-1234 or PROJECT_1234 somewhere
// check the branch matches VC-1234 or VC_1234 somewhere
if (config.check_commits) {
const listCommitsParams = {
owner: repository.owner.login,
repo: repository.name,
pull_number: pull_request.number
}
const commitsInPR = (await tools.github.pulls.listCommits(listCommitsParams)).data
const failedCommits = findFailedCommits(projects, commitsInPR, config.ignore_case);
const failedCommits = findFailedCommits(tickets, commitsInPR, config.ignore_case);

if(failedCommits.length) {
failedCommits.forEach(
failedCommit => tools.log('Commit message \'' + failedCommit + '\' does not contain an approved project')
failedCommit => tools.log('Commit message \'' + failedCommit + '\' does not contain an approved Jiras')
)
return false
}
Expand Down Expand Up @@ -100,9 +100,9 @@ Toolkit.run(
{ event: ['pull_request.opened', 'pull_request.edited', 'pull_request.synchronize'], secrets: ['GITHUB_TOKEN'] }
)

function findFailedCommits(projects, commitsInPR, ignoreCase) {
function findFailedCommits(tickets, commitsInPR, ignoreCase) {
const failedCommits = [];
projects.forEach(project => {
tickets.forEach(project => {
commitsInPR.forEach(commit => {
const commitMessage = ignoreCase ? commit.commit.message.toLowerCase() : commit.commit.message
if (!commitMessage.match(createProjectRegex(project))) {
Expand Down
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Toolkit } = require('actions-toolkit')

nock.disableNetConnect()

describe('pr-lint-action', () => {
describe('jira-check-action', () => {
let action, tools

// Mock Toolkit.run to define `action` so we can call it
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "pr-lint-action",
"name": "jira-check-action",
"private": true,
"main": "index.js",
"scripts": {
"start": "node ./index.js"
},
"dependencies": {
"test": "jest",
"actions-toolkit": "^2.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit c163426

Please sign in to comment.