Skip to content

Commit

Permalink
Improve test coverage (#22)
Browse files Browse the repository at this point in the history
* Add sinon

* Add coverage badge to README

* Add tests for github module

* Add tests for the main function

* Suppress logger messages

* Build the action
  • Loading branch information
necojackarc committed Aug 30, 2020
1 parent 5e8e1a4 commit f442e81
Show file tree
Hide file tree
Showing 11 changed files with 598 additions and 186 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Auto Request Review

![CI](https://github.com/necojackarc/auto-request-review/workflows/CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/necojackarc/auto-request-review/badge.svg?branch=master)](https://coveralls.io/github/necojackarc/auto-request-review?branch=master)

A GitHub Action automatically requests review of a pull request based on files changes and/or groups the author belongs to 🤖

Expand Down
30 changes: 18 additions & 12 deletions dist/index.js

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

121 changes: 120 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "request-review-based-on-files",
"name": "auto-request-review",
"version": "0.1.2",
"description": "GitHub Action that automatically requests review of a pull request based on files changed",
"main": "index.js",
Expand All @@ -23,7 +23,8 @@
"chai": "^4.2.0",
"eslint": "^7.7.0",
"mocha": "^8.1.2",
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"sinon": "^9.0.3"
},
"nyc": {
"include": [
Expand Down
8 changes: 8 additions & 0 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,17 @@ function get_octokit() {
return octokit_cache = github.getOctokit(token);
}

function clear_cache() {
context_cache = undefined;
token_cache = undefined;
config_path_cache = undefined;
octokit_cache = undefined;
}

module.exports = {
get_pull_request,
fetch_config,
fetch_changed_files,
assign_reviewers,
clear_cache,
};
20 changes: 9 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
'use strict';

const core = require('@actions/core');

const {
get_pull_request,
fetch_config,
fetch_changed_files,
assign_reviewers,
} = require('./github');
const github = require('./github'); // Don't destructure this object to stub with sinon in tests

const {
fetch_other_group_members,
Expand All @@ -21,7 +15,7 @@ async function run() {
let config;

try {
config = await fetch_config();
config = await github.fetch_config();
} catch (error) {
if (error.status === 404) {
core.warning('No configuration file is found in the base branch; terminating the process');
Expand All @@ -30,15 +24,15 @@ async function run() {
throw error;
}

const { title, is_draft, author } = get_pull_request();
const { title, is_draft, author } = github.get_pull_request();

if (!should_request_review({ title, is_draft, config })) {
core.info('Matched the ignoring rules; terminating the process');
return;
}

core.info('Fetching changed files in the pull request');
const changed_files = await fetch_changed_files();
const changed_files = await github.fetch_changed_files();

core.info('Identifying reviewers based on the changed files and the configuration');
const reviewers_based_on_files = identify_reviewers({ config, changed_files, excludes: [ author ] });
Expand All @@ -54,7 +48,11 @@ async function run() {
}

core.info(`Requesting review to ${reviewers.join(', ')}`);
await assign_reviewers(reviewers);
await github.assign_reviewers(reviewers);
}

run().catch((error) => core.setFailed(error));

module.exports = {
run,
};
15 changes: 15 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const core = require('@actions/core');
const sinon = require('sinon');

before(function() {
// Don't show logs in tests
sinon.stub(core, 'info');
sinon.stub(core, 'warning');
});

after(function() {
core.info.restore();
core.warning.restore();
});
Loading

0 comments on commit f442e81

Please sign in to comment.