1import { Command } from '@expo/commander';
2
3import logger from '../Logger';
4import { reviewPullRequestAsync } from '../code-review';
5
6type ActionOptions = {
7  pr: string;
8};
9
10async function action(options: ActionOptions) {
11  if (isNaN(Number(options.pr))) {
12    throw new Error('Flag `--pr` must be provided with a number value.');
13  }
14  if (!process.env.GITHUB_TOKEN) {
15    throw new Error('Environment variable `GITHUB_TOKEN` is required for this command.');
16  }
17  try {
18    await reviewPullRequestAsync(+options.pr);
19  } catch (error) {
20    logger.error(error);
21    throw error;
22  }
23}
24
25export default (program: Command) => {
26  program
27    .command('code-review')
28    .alias('review')
29    .description('Reviews the pull request.')
30    .option('-p, --pr <string>', 'ID of the pull request to review.')
31    .asyncAction(action);
32};
33