Skip to content

Commit

Permalink
Merge acdc6f6 into b5e8187
Browse files Browse the repository at this point in the history
  • Loading branch information
bduffey-pe committed Dec 7, 2022
2 parents b5e8187 + acdc6f6 commit aeabe0f
Show file tree
Hide file tree
Showing 6 changed files with 77 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
config: .github/reviewers.yml # Config file location override
use_local: true # Look for config locally during run instead of in repo
```

### (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'
43 changes: 36 additions & 7 deletions dist/index.js

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

37 changes: 30 additions & 7 deletions src/github.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'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');
Expand Down Expand Up @@ -36,15 +37,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 +116,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 +131,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
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 aeabe0f

Please sign in to comment.