Skip to content

Commit

Permalink
Added option for local config file (#77)
Browse files Browse the repository at this point in the history
* Added option for local config file

* Updated readme

* Moved error message to new file constants.js

* Updated README per suggestion

* Minor typo fix

Co-authored-by: Brian Duffey <bduff9@gmail.com>
  • Loading branch information
bduffey-pe and bduff9 committed Dec 9, 2022
1 parent b5e8187 commit 3fa3f27
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
parserOptions: {
ecmaVersion: 2018,
ecmaVersion: 2020,
},
env: {
es6: true,
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
config: .github/reviewers.yml # Config file location override
# Look for config locally during run instead of in repo.
# For instance, if you'd like to use a config file stored in external storage,
# you can fetch it before you run this action, then let this action pick it up with `use_local: true`.
# This defaults to false if not specified.
# See https://github.com/necojackarc/auto-request-review/issues/76 for more details.
use_local: true
```

### (Optional) GitHub Personal Access Token
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ inputs:
config:
required: false
default: '.github/auto_request_review.yml'
use_local:
required: false
default: 'false'
runs:
using: 'node16'
main: 'dist/index.js'
57 changes: 50 additions & 7 deletions dist/index.js

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

5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const LOCAL_FILE_MISSING = 'Local file missing';

module.exports = {
LOCAL_FILE_MISSING,
};
38 changes: 31 additions & 7 deletions src/github.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const core = require('@actions/core');
const fs = require('fs');
const github = require('@actions/github');
const partition = require('lodash/partition');
const yaml = require('yaml');
const { LOCAL_FILE_MISSING } = require('./constants');

class PullRequest {
// ref: https://developer.github.com/v3/pulls/#get-a-pull-request
Expand Down Expand Up @@ -36,15 +38,32 @@ async function fetch_config() {
const context = get_context();
const octokit = get_octokit();
const config_path = get_config_path();
const useLocal = get_use_local();
let content = '';

const { data: response_body } = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: config_path,
ref: context.ref,
});
if (!useLocal) {
const { data: response_body } = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: config_path,
ref: context.ref,
});

content = Buffer.from(response_body.content, response_body.encoding).toString();
} else {
try {
content = fs.readFileSync(config_path).toString();

if (!content) {
throw new Error();
}
} catch (error) {
core.debug(`Error when reading local file: ${error}`);

throw new Error(LOCAL_FILE_MISSING);
}
}

const content = Buffer.from(response_body.content, response_body.encoding).toString();
return yaml.parse(content);
}

Expand Down Expand Up @@ -98,6 +117,7 @@ async function assign_reviewers(reviewers) {
let context_cache;
let token_cache;
let config_path_cache;
let use_local_cache;
let octokit_cache;

function get_context() {
Expand All @@ -112,6 +132,10 @@ function get_config_path() {
return config_path_cache || (config_path_cache = core.getInput('config'));
}

function get_use_local() {
return use_local_cache ?? (use_local_cache = core.getInput('use_local') === 'true');
}

function get_octokit() {
if (octokit_cache) {
return octokit_cache;
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const core = require('@actions/core');
const { LOCAL_FILE_MISSING } = require('./constants');
const github = require('./github'); // Don't destructure this object to stub with sinon in tests

const {
Expand All @@ -24,6 +25,12 @@ async function run() {
core.warning('No configuration file is found in the base branch; terminating the process');
return;
}

if (error.message === LOCAL_FILE_MISSING) {
core.warning('No configuration file is found locally; terminating the process');
return;
}

throw error;
}

Expand Down

0 comments on commit 3fa3f27

Please sign in to comment.