Skip to content

Commit

Permalink
Merge pull request #50 from crazy-max/github-throw-runtime-token
Browse files Browse the repository at this point in the history
github: throw if runtime token invalid
  • Loading branch information
crazy-max authored Feb 20, 2023
2 parents d09114e + c3aa7f2 commit d153cfa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
10 changes: 2 additions & 8 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,12 @@ describe('printActionsRuntimeTokenACs', () => {
process.env = originalEnv;
});
it('empty', async () => {
const warnSpy = jest.spyOn(core, 'warning');
process.env.ACTIONS_RUNTIME_TOKEN = '';
await GitHub.printActionsRuntimeTokenACs();
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy).toHaveBeenCalledWith(`ACTIONS_RUNTIME_TOKEN not set`);
await expect(GitHub.printActionsRuntimeTokenACs()).rejects.toThrowError(new Error('ACTIONS_RUNTIME_TOKEN not set'));
});
it('malformed', async () => {
const warnSpy = jest.spyOn(core, 'warning');
process.env.ACTIONS_RUNTIME_TOKEN = 'foo';
await GitHub.printActionsRuntimeTokenACs();
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy).toHaveBeenCalledWith(`Cannot parse Actions Runtime Token: Invalid token specified: Cannot read properties of undefined (reading 'replace')`);
await expect(GitHub.printActionsRuntimeTokenACs()).rejects.toThrowError(new Error("Cannot parse GitHub Actions Runtime Token: Invalid token specified: Cannot read properties of undefined (reading 'replace')"));
});
it('refs/heads/master', async () => {
const infoSpy = jest.spyOn(core, 'info');
Expand Down
8 changes: 3 additions & 5 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ export class GitHub {
try {
jwt = GitHub.actionsRuntimeToken;
} catch (e) {
core.warning(`Cannot parse Actions Runtime Token: ${e.message}`);
return;
throw new Error(`Cannot parse GitHub Actions Runtime Token: ${e.message}`);
}
if (!jwt) {
core.warning(`ACTIONS_RUNTIME_TOKEN not set`);
return;
throw new Error(`ACTIONS_RUNTIME_TOKEN not set`);
}
try {
<Array<GitHubActionsRuntimeTokenAC>>JSON.parse(`${jwt.ac}`).forEach(ac => {
Expand All @@ -85,7 +83,7 @@ export class GitHub {
core.info(`${ac.Scope}: ${permission}`);
});
} catch (e) {
core.warning(`Cannot parse Actions Runtime Token Access Controls: ${e.message}`);
throw new Error(`Cannot parse GitHub Actions Runtime Token ACs: ${e.message}`);
}
}
}

0 comments on commit d153cfa

Please sign in to comment.