143a76f73STomasz Sapetaimport { Command } from '@expo/commander';
243a76f73STomasz Sapeta
3*451b29e0STomasz Sapetaimport logger from '../Logger';
443a76f73STomasz Sapetaimport { reviewPullRequestAsync } from '../code-review';
543a76f73STomasz Sapeta
643a76f73STomasz Sapetatype ActionOptions = {
743a76f73STomasz Sapeta  pr: string;
843a76f73STomasz Sapeta};
943a76f73STomasz Sapeta
1043a76f73STomasz Sapetaasync function action(options: ActionOptions) {
1143a76f73STomasz Sapeta  if (isNaN(Number(options.pr))) {
1243a76f73STomasz Sapeta    throw new Error('Flag `--pr` must be provided with a number value.');
1343a76f73STomasz Sapeta  }
1443a76f73STomasz Sapeta  if (!process.env.GITHUB_TOKEN) {
1543a76f73STomasz Sapeta    throw new Error('Environment variable `GITHUB_TOKEN` is required for this command.');
1643a76f73STomasz Sapeta  }
17*451b29e0STomasz Sapeta  try {
1843a76f73STomasz Sapeta    await reviewPullRequestAsync(+options.pr);
19*451b29e0STomasz Sapeta  } catch (error) {
20*451b29e0STomasz Sapeta    logger.error(error);
21*451b29e0STomasz Sapeta    throw error;
22*451b29e0STomasz Sapeta  }
2343a76f73STomasz Sapeta}
2443a76f73STomasz Sapeta
2543a76f73STomasz Sapetaexport default (program: Command) => {
2643a76f73STomasz Sapeta  program
2743a76f73STomasz Sapeta    .command('code-review')
2843a76f73STomasz Sapeta    .alias('review')
2943a76f73STomasz Sapeta    .description('Reviews the pull request.')
3043a76f73STomasz Sapeta    .option('-p, --pr <string>', 'ID of the pull request to review.')
3143a76f73STomasz Sapeta    .asyncAction(action);
3243a76f73STomasz Sapeta};
33