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:
Alice Zhao 2024-04-10 11:15:05 -07:00 коммит произвёл GitHub
Родитель d4766f5008
Коммит 3b3824a4ce
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 93 добавлений и 7 удалений

77
spec/utils.spec.ts Normal file
Просмотреть файл

@ -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,
);
});
});
});

Просмотреть файл

@ -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,
);
}
}
};