Skip to content
Permalink
Browse files
Add remote repository support (Fixes #9) (#10)
* Add logging of owner/repo and SHA

* Add support for `repo` and `sha` inputs (Fixes #9)

* Add missing comment.

* Revert to defaults being assigned on declaration

* Add missing `required: false`

To keep consistent, even if it's the default:

https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/metadata-syntax-for-github-actions#inputsinput_idrequired

* Add check for invalid `repo` format
  • Loading branch information
Saviq committed Sep 28, 2020
1 parent 88ce8c5 commit 9f02872da71b6f558c6a6f190f925dde5e4d8798
Showing 7 changed files with 116 additions and 6 deletions.
@@ -31,6 +31,14 @@ See the [examples workflow](.github/workflows/examples.yml) for more details and

## Inputs

### `repo`

_Optional_ The target repository (`owner/repo`) on which to manage the check run. Defaults to the current repository.

### `sha`

_Optional_ The SHA of the target commit. Defaults to the current commit.

### `token`

**Required** Your `GITHUB_TOKEN`
@@ -7,6 +7,7 @@ test('test runs (creation)', () => {
const options: cp.ExecSyncOptions = {
env: {
GITHUB_REPOSITORY: 'LB/ABC',
GITHUB_SHA: 'SHA',
INPUT_TOKEN: 'ABC',
INPUT_NAME: 'ABC',
INPUT_STATUS: 'completed',
@@ -19,7 +20,7 @@ test('test runs (creation)', () => {
const error = e as Error & {stdout: Buffer};
const output = error.stdout.toString();
console.log(output);
expect(output).toMatch(/::debug::Creating a new Run/);
expect(output).toMatch(/::debug::Creating a new Run on LB\/ABC@SHA/);
expect(output).toMatch(/::debug::HttpError: Bad credentials/);
}
});
@@ -29,6 +30,7 @@ test('test runs (update)', () => {
const options: cp.ExecSyncOptions = {
env: {
GITHUB_REPOSITORY: 'LB/ABC',
GITHUB_SHA: 'SHA',
INPUT_TOKEN: 'ABC',
INPUT_CHECK_ID: '123',
INPUT_STATUS: 'completed',
@@ -41,9 +43,83 @@ test('test runs (update)', () => {
const error = e as Error & {stdout: Buffer};
const output = error.stdout.toString();
console.log(output);
expect(output).toMatch(/::debug::Updating a Run/);
expect(output).toMatch(/::debug::Updating a Run on LB\/ABC@SHA \(123\)/);
expect(output).toMatch(/::debug::HttpError: Bad credentials/);
}
});

test('test runs (creation on remote repository)', () => {
const entry = path.join(__dirname, '..', 'lib', 'main.js');
const options: cp.ExecSyncOptions = {
env: {
GITHUB_REPOSITORY: 'LB/ABC',
GITHUB_SHA: 'SHA',
INPUT_TOKEN: 'ABC',
INPUT_NAME: 'ABC',
INPUT_STATUS: 'completed',
INPUT_CONCLUSION: 'success',
INPUT_REPO: 'remote/repo',
INPUT_SHA: 'DEF',
},
};
try {
console.log(cp.execSync(`node ${entry}`, options).toString());
} catch (e) {
const error = e as Error & {stdout: Buffer};
const output = error.stdout.toString();
console.log(output);
expect(output).toMatch(/::debug::Creating a new Run on remote\/repo@DEF/);
expect(output).toMatch(/::debug::HttpError: Bad credentials/);
}
});

test('test runs (update on remote repository)', () => {
const entry = path.join(__dirname, '..', 'lib', 'main.js');
const options: cp.ExecSyncOptions = {
env: {
GITHUB_REPOSITORY: 'LB/ABC',
GITHUB_SHA: 'SHA',
INPUT_TOKEN: 'ABC',
INPUT_CHECK_ID: '123',
INPUT_STATUS: 'completed',
INPUT_CONCLUSION: 'success',
INPUT_REPO: 'remote/repo',
INPUT_SHA: 'DEF',
},
};
try {
console.log(cp.execSync(`node ${entry}`, options).toString());
} catch (e) {
const error = e as Error & {stdout: Buffer};
const output = error.stdout.toString();
console.log(output);
expect(output).toMatch(/::debug::Updating a Run on remote\/repo@DEF \(123\)/);
expect(output).toMatch(/::debug::HttpError: Bad credentials/);
}
});

test('test rejects invalid repo', () => {
const entry = path.join(__dirname, '..', 'lib', 'main.js');
const options: cp.ExecSyncOptions = {
env: {
GITHUB_REPOSITORY: 'LB/ABC',
GITHUB_SHA: 'SHA',
INPUT_TOKEN: 'ABC',
INPUT_CHECK_ID: '123',
INPUT_STATUS: 'completed',
INPUT_CONCLUSION: 'success',
INPUT_REPO: 'invalid',
INPUT_SHA: 'DEF',
},
};
try {
console.log(cp.execSync(`node ${entry}`, options).toString());
} catch (e) {
const error = e as Error & {stdout: Buffer};
const output = error.stdout.toString();
console.log(output);
expect(output).toMatch(/::debug::Error: repo needs to be in the {owner}\/{repository} format/);
}
});

// TODO: add more
@@ -5,6 +5,12 @@ branding:
icon: 'check-circle'
color: 'green'
inputs:
repo:
description: 'the target `owner/repo` to manage the check run on (defaults to the current repository'
required: false
sha:
description: 'the target commit''s SHA (defaults to the current commit)'
required: false
token:
description: 'your GITHUB_TOKEN'
required: true

Large diffs are not rendered by default.

@@ -17,6 +17,8 @@ const parseJSON = <T>(getInput: GetInput, property: string): T | undefined => {
};

export const parseInputs = (getInput: GetInput): Inputs.Args => {
const repo = getInput('repo');
const sha = getInput('sha');
const token = getInput('token', {required: true});

const name = getInput('name');
@@ -28,6 +30,10 @@ export const parseInputs = (getInput: GetInput): Inputs.Args => {
const actionURL = getInput('action_url');
const detailsURL = getInput('details_url');

if (repo && repo.split('/').length != 2) {
throw new Error('repo needs to be in the {owner}/{repository} format');
}

if (name && checkIDStr) {
throw new Error(`can only provide 'name' or 'check_id'`);
}
@@ -73,6 +79,8 @@ export const parseInputs = (getInput: GetInput): Inputs.Args => {
}

return {
repo,
sha,
name,
token,
status,
@@ -20,15 +20,25 @@ async function run(): Promise<void> {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
};
const sha = github.context.sha;
let sha = github.context.sha;

if (inputs.repo) {
const repo = inputs.repo.split('/');
ownership.owner = repo[0];
ownership.repo = repo[1];
}

if (inputs.sha) {
sha = inputs.sha;
}

if (isCreation(inputs)) {
core.debug(`Creating a new Run`);
core.debug(`Creating a new Run on ${ownership.owner}/${ownership.repo}@${sha}`);
const id = await createRun(octokit, inputs.name, sha, ownership, inputs);
core.setOutput('check_id', id);
} else {
const id = inputs.checkID;
core.debug(`Updating a Run (${id})`);
core.debug(`Updating a Run on ${ownership.owner}/${ownership.repo}@${sha} (${id})`);
await updateRun(octokit, id, ownership, inputs);
}
core.debug(`Done`);
@@ -1,6 +1,8 @@
import {RestEndpointMethodTypes} from '@octokit/rest';

interface ArgsBase {
repo?: string;
sha?: string;
token: string;
conclusion?: Conclusion;
status: Status;

0 comments on commit 9f02872

Please sign in to comment.