зеркало из https://github.com/electron/trop.git
refactor: add try catch to reviewer (#279)
* refactor: add try catch block with logging error message * refactor: error message * refactor: error message with log * test: add test for request reviewers error * test: format test * test: add new utils test file * test: add new utils test file * test: remove mock constant * test: update test name --------- Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
This commit is contained in:
Родитель
d4766f5008
Коммит
3b3824a4ce
|
@ -0,0 +1,77 @@
|
|||
import * as logUtils from '../src/utils/log-util';
|
||||
import { LogLevel } from '../src/enums';
|
||||
import { tagBackportReviewers } from '../src/utils';
|
||||
|
||||
const backportPROpenedEvent = require('./fixtures/backport_pull_request.opened.json');
|
||||
|
||||
jest.mock('../src/constants', () => ({
|
||||
...jest.requireActual('../src/constants'),
|
||||
DEFAULT_BACKPORT_REVIEW_TEAM: 'electron/wg-releases',
|
||||
}));
|
||||
|
||||
describe('utils', () => {
|
||||
describe('tagBackportReviewers()', () => {
|
||||
const octokit = {
|
||||
pulls: {
|
||||
requestReviewers: jest.fn(),
|
||||
},
|
||||
repos: {
|
||||
getCollaboratorPermissionLevel: jest.fn().mockReturnValue(
|
||||
Promise.resolve({
|
||||
data: {
|
||||
permission: 'admin',
|
||||
},
|
||||
}),
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
const context = {
|
||||
octokit,
|
||||
repo: jest.fn((obj) => obj),
|
||||
...backportPROpenedEvent,
|
||||
};
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
it('correctly tags team reviewers when user is undefined', async () => {
|
||||
await tagBackportReviewers({ context, targetPrNumber: 1234 });
|
||||
expect(octokit.pulls.requestReviewers).toHaveBeenCalled();
|
||||
expect(octokit.pulls.requestReviewers).toHaveBeenCalledWith({
|
||||
pull_number: 1234,
|
||||
team_reviewers: ['wg-releases'],
|
||||
reviewers: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly tags team reviewers and reviewers when user is defined', async () => {
|
||||
const user = 'abc';
|
||||
await tagBackportReviewers({ context, targetPrNumber: 1234, user });
|
||||
expect(octokit.pulls.requestReviewers).toHaveBeenCalled();
|
||||
expect(octokit.pulls.requestReviewers).toHaveBeenCalledWith({
|
||||
pull_number: 1234,
|
||||
team_reviewers: ['wg-releases'],
|
||||
reviewers: [user],
|
||||
});
|
||||
});
|
||||
|
||||
it('logs an error if requestReviewers throws an error', async () => {
|
||||
const error = new Error('Request failed');
|
||||
context.octokit.pulls.requestReviewers = jest
|
||||
.fn()
|
||||
.mockRejectedValue(error);
|
||||
|
||||
const logSpy = jest.spyOn(logUtils, 'log');
|
||||
await tagBackportReviewers({ context, targetPrNumber: 1234 });
|
||||
|
||||
expect(octokit.pulls.requestReviewers).toHaveBeenCalled();
|
||||
|
||||
expect(logSpy).toHaveBeenCalledWith(
|
||||
'tagBackportReviewers',
|
||||
LogLevel.ERROR,
|
||||
`Failed to request reviewers for PR #1234`,
|
||||
error,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
23
src/utils.ts
23
src/utils.ts
|
@ -425,13 +425,22 @@ export const tagBackportReviewers = async ({
|
|||
}
|
||||
|
||||
if (Math.max(reviewers.length, teamReviewers.length) > 0) {
|
||||
await context.octokit.pulls.requestReviewers(
|
||||
context.repo({
|
||||
pull_number: targetPrNumber,
|
||||
reviewers,
|
||||
team_reviewers: teamReviewers,
|
||||
}),
|
||||
);
|
||||
try {
|
||||
await context.octokit.pulls.requestReviewers(
|
||||
context.repo({
|
||||
pull_number: targetPrNumber,
|
||||
reviewers,
|
||||
team_reviewers: teamReviewers,
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
log(
|
||||
'tagBackportReviewers',
|
||||
LogLevel.ERROR,
|
||||
`Failed to request reviewers for PR #${targetPrNumber}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче