Skip to content

Commit

Permalink
Merge pull request #4 from movableink/mn/neutralize-old-checks
Browse files Browse the repository at this point in the history
Update prior clubhouse checks to neutral
  • Loading branch information
mnutt authored Oct 25, 2019
2 parents aae5a93 + bd46592 commit a78c61c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ A GitHub Action that verifies your pull request contains a reference to a Clubho
Add `.github/workflows/lint.yaml` with the following:

```yaml
name: PR Lint
name: Clubhouse
on:
pull_request:
types: [opened, edited, reopened, synchronize]

jobs:
ch_lint_pr:
name: Clubhouse
name: Check for story ID
runs-on: ubuntu-latest
steps:
- uses: movableink/pr-clubhouse-lint-action@release
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// When using a release branch, we have node_modules vendored. But on other
// branches we don't, so when we're testing we want to optionally just run npm
// to install dependencies
const fs = require('fs');
if (!fs.existsSync(`${__dirname}/node_modules`)) {
const child_process = require('child_process');
child_process.execSync('npm install', { stdio: [0, 1, 2], cwd: __dirname });
}

const github = require('@actions/github');
const action = require('./lib/action');

action(github.context);
const octokit = new github.GitHub(process.env.GITHUB_TOKEN);

action(github.context, octokit);
32 changes: 28 additions & 4 deletions lib/action.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const core = require('@actions/core');

module.exports = function(context) {
// Matches [ch49555] and ch49555/branch-name
const clubhouseRegex = /(\[ch\d+\])|(ch\d+\/)/;
// Matches [ch49555] and ch49555/branch-name
const clubhouseRegex = /(\[ch\d+\])|(ch\d+\/)/;

// We need to be able to identify old checks to neutralize them,
// unfortunately the only way is to name them with one of these:
const jobNames = ['Clubhouse', 'Check for story ID'];

module.exports = async function(context, api) {
const { repository, pull_request } = context.payload;

const repoInfo = {
Expand All @@ -25,7 +29,7 @@ module.exports = function(context) {

const passed = toSearch.some(line => {
const linePassed = !!line.match(clubhouseRegex);
core.warning(`Searching ${line}...${linePassed}`);
console.log(`Searching ${line}...${linePassed}`);
return linePassed;
});

Expand All @@ -34,4 +38,24 @@ module.exports = function(context) {
if (!passed) {
core.setFailed('PR Linting Failed');
}

if (process.env.GITHUB_TOKEN) {
// If there are any previously failed CH checks, set them to neutral
// since we want this check to override those.
const checkList = await api.checks.listForRef(repoInfo);
const { check_runs } = checkList.data;

const clubhouseChecks = check_runs.filter(r => jobNames.includes(r.name));
const completedChecks = clubhouseChecks.filter(r => r.status === 'completed');

for (let check of completedChecks) {
console.log(`Updating ${check.id} check to neutral status`);

await api.checks.update({
...repoInfo,
check_run_id: check.id,
conclusion: 'neutral'
});
}
}
};
126 changes: 101 additions & 25 deletions lib/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ jest.mock('@actions/core');
const core = require('@actions/core');
const action = require('./action');

process.env.GITHUB_TOKEN = 'fake_token';

describe('pr-lint-action', () => {
let api = {};
let context = {};

const bad_title_and_branch = {
Expand Down Expand Up @@ -47,39 +50,112 @@ describe('pr-lint-action', () => {
core.setFailed = jest.fn();
});

it('fails if missing from title and branch and body', async () => {
context.payload = pullRequestOpenedFixture(bad_title_and_branch);
describe('checking status', () => {
beforeEach(() => {
api.checks = {
listForRef: () => ({ data: { check_runs: [] } }),
update: () => {}
};
});

await action(context);
expect(core.setFailed).toHaveBeenCalledWith('PR Linting Failed');
expect.assertions(1);
});
it('fails if missing from title and branch and body', async () => {
context.payload = pullRequestOpenedFixture(bad_title_and_branch);

it('passes if branch matches', async () => {
context.payload = pullRequestOpenedFixture(bad_title_and_good_branch);
await action(context, api);
expect(core.setFailed).toHaveBeenCalledWith('PR Linting Failed');
expect.assertions(1);
});

await action(context);
expect(console.log).toHaveBeenCalledWith('Passed clubhouse number check: true');
expect(core.setFailed).not.toHaveBeenCalled();
expect.assertions(2);
});
it('passes if branch matches', async () => {
context.payload = pullRequestOpenedFixture(bad_title_and_good_branch);

await action(context, api);
expect(console.log).toHaveBeenCalledWith('Passed clubhouse number check: true');
expect(core.setFailed).not.toHaveBeenCalled();
expect.assertions(2);
});

it('passes if title matches', async () => {
context.payload = pullRequestOpenedFixture(good_title_and_branch);

it('passes if title matches', async () => {
context.payload = pullRequestOpenedFixture(good_title_and_branch);
await action(context, api);
expect(console.log).toHaveBeenCalledWith('Passed clubhouse number check: true');
expect(core.setFailed).not.toHaveBeenCalled();
expect.assertions(2);
});

await action(context);
expect(console.log).toHaveBeenCalledWith('Passed clubhouse number check: true');
expect(core.setFailed).not.toHaveBeenCalled();
expect.assertions(2);
it('passes if body matches', async () => {
context.payload = pullRequestOpenedFixture(good_body);

await action(context, api);
expect(console.log).toHaveBeenCalledWith('Passed clubhouse number check: true');
expect(core.setFailed).not.toHaveBeenCalled();
expect.assertions(2);
});
});

it('passes if body matches', async () => {
context.payload = pullRequestOpenedFixture(good_body);
describe('clearing old checks', () => {
beforeEach(() => {
api.checks = {
listForRef: () => ({
data: {
check_runs: [
{
// this is us, ignore
id: 1,
name: 'Clubhouse',
status: 'in_progress',
conclusion: 'neutral'
},
{
id: 2,
name: 'Clubhouse',
status: 'completed',
conclusion: 'failure'
},
{
id: 3,
name: 'Clubhouse',
status: 'completed',
conclusion: 'failure'
},
{
// this is travis, ignore
id: 4,
name: 'Travis',
status: 'completed',
conclusion: 'failure'
}
]
}
}),
update: jest.fn()
};
});

it('updates the previous failed check to neutral', async () => {
context.payload = pullRequestOpenedFixture(good_body);

await action(context, api);

expect(api.checks.update).toHaveBeenCalledTimes(2);

expect(api.checks.update).toHaveBeenNthCalledWith(1, {
check_run_id: 2,
owner: 'movableink',
repo: 'pr-clubhouse-lint-action-test',
ref: 'fix_things',
conclusion: 'neutral'
});

await action(context);
expect(console.log).toHaveBeenCalledWith('Passed clubhouse number check: true');
expect(core.setFailed).not.toHaveBeenCalled();
expect.assertions(2);
expect(api.checks.update).toHaveBeenNthCalledWith(2, {
check_run_id: 3,
owner: 'movableink',
repo: 'pr-clubhouse-lint-action-test',
ref: 'fix_things',
conclusion: 'neutral'
});
});
});
});

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

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

0 comments on commit a78c61c

Please sign in to comment.