Skip to content

Commit 837331b

Browse files
MattyTheHackercodecov-ai[bot]
andauthored
Implement catching the authorisation error (#37)
* Implement catching the authorisation error * Improve check * Add Tests for PR#37 * Add tests --------- Co-authored-by: codecov-ai[bot] <156709835+codecov-ai[bot]@users.noreply.github.com>
1 parent 28724aa commit 837331b

2 files changed

Lines changed: 69 additions & 4 deletions

File tree

src/autoupdater.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,18 @@ export class AutoUpdater {
465465
break;
466466
} catch (e: unknown) {
467467
if (e instanceof Error) {
468-
/**
469-
* If this update was against a fork and we got a 403 then it's
470-
* probably because we don't have access to it.
471-
*/
468+
if (
469+
isRequestError(e) &&
470+
e.message.includes('Parameter token or opts.auth is required')
471+
) {
472+
ghCore.error(
473+
`Could not update pull request #${prNumber} due to an authorisation error. Error was: ${e.message}. Please confirm you are using the correct token and it has the correct authorisation scopes.`,
474+
);
475+
476+
setOutputFn(Output.Conflicted, false);
477+
478+
return false;
479+
}
472480
if (
473481
isRequestError(e) &&
474482
e.status === 403 &&

test/autoupdate.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '@octokit/webhooks-types/schema';
1818
import * as core from '@actions/core';
1919
import { Output } from '../src/Output';
20+
import * as isRequestErrorModule from '../src/helpers/isRequestError';
2021

2122
type PullRequestResponse =
2223
Endpoints['GET /repos/{owner}/{repo}/pulls/{pull_number}']['response'];
@@ -1090,6 +1091,28 @@ describe('test `update`', () => {
10901091
expect(setOutputMock).toHaveBeenCalledWith(Output.Conflicted, false);
10911092
});
10921093

1094+
test('merge: returns false and sets output if authorization error with token message', async () => {
1095+
const updater = new AutoUpdater(config, emptyEvent);
1096+
const error = new Error('Parameter token or opts.auth is required');
1097+
(error as any).status = 401;
1098+
Object.getPrototypeOf(updater.octokit.rest.repos).merge = jest
1099+
.fn()
1100+
.mockRejectedValue(error);
1101+
const setOutputMock = jest.fn();
1102+
const errorSpy = jest.spyOn(core, 'error').mockImplementation(() => {});
1103+
const result = await updater.merge(
1104+
owner,
1105+
1,
1106+
{ owner, repo, base, head } as any,
1107+
setOutputMock,
1108+
);
1109+
expect(result).toBe(false);
1110+
expect(setOutputMock).toHaveBeenCalledWith(Output.Conflicted, false);
1111+
expect(errorSpy).toHaveBeenCalledWith(
1112+
'Could not update pull request #1 due to an authorisation error. Error was: Parameter token or opts.auth is required. Please confirm you are using the correct token and it has the correct authorisation scopes.',
1113+
);
1114+
});
1115+
10931116
test('merge: retries if error and retries < retryCount', async () => {
10941117
const updater = new AutoUpdater(config, emptyEvent);
10951118
const error = new Error('retry me');
@@ -1390,3 +1413,37 @@ describe('merge() doMerge and merge conflict label/branches', () => {
13901413
infoSpy.mockRestore();
13911414
});
13921415
});
1416+
1417+
describe('AutoUpdater.merge authorisation error handling', () => {
1418+
test('handles missing token or opts.auth error', async () => {
1419+
const updater = new AutoUpdater(config, emptyEvent);
1420+
const mergeOpts = { owner, repo, base, head };
1421+
// Create an Error instance and add status property
1422+
const error: Error & { status?: number } = new Error(
1423+
'Parameter token or opts.auth is required',
1424+
);
1425+
error.status = 401;
1426+
// Mock octokit.rest.repos.merge to throw the specific error
1427+
Object.getPrototypeOf(updater.octokit.rest.repos).merge = jest
1428+
.fn()
1429+
.mockRejectedValue(error);
1430+
// Mock isRequestError to return true
1431+
jest.spyOn(isRequestErrorModule, 'isRequestError').mockReturnValue(true);
1432+
const errorSpy = jest.spyOn(core, 'error').mockImplementation(() => {});
1433+
const setOutputMock = jest.fn();
1434+
const result = await updater.merge(
1435+
owner,
1436+
123,
1437+
mergeOpts as any,
1438+
setOutputMock,
1439+
);
1440+
expect(errorSpy).toHaveBeenCalledWith(
1441+
expect.stringContaining(
1442+
'Could not update pull request #123 due to an authorisation error. Error was: Parameter token or opts.auth is required.',
1443+
),
1444+
);
1445+
expect(setOutputMock).toHaveBeenCalledWith(Output.Conflicted, false);
1446+
expect(result).toBe(false);
1447+
errorSpy.mockRestore();
1448+
});
1449+
});

0 commit comments

Comments
 (0)