Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for local config file #77

Merged
merged 5 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
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