Skip to content

Commit

Permalink
Add --reviewer option (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Dec 26, 2021
1 parent d751b02 commit d3c9fea
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ Please note that dashes between the words are optional, for instance you can typ
| --access-token | | Github access token | | `string` |
| --all | -a | Show commits from other than yourself | false | `boolean` |
| --author | | Filter commits by Github username | _Current user_ | `string` |
| --assignee | --assign | Assign users to target pull request | | `string` |
| --auto-assign | | Assign current user to target pull request | false | `boolean` |
| --assignee | --assign | Assign users to the target PR | | `string` |
| --auto-assign | | Assign current user to the target PR | false | `boolean` |
| --branch | -b | Target branch to backport to | | `string` |
| --ci | | Disable interactive prompts | false | `boolean` |
| --dry-run | | Perform backport without pushing to Github | false | `string` |
Expand All @@ -81,6 +81,7 @@ Please note that dashes between the words are optional, for instance you can typ
| --pr-filter | | Find PRs using [Github's search syntax][2] | | `string` |
| --pr-title | --title | Title of pull request | | `string` |
| --reset-author | | Set yourself as commit author | | `boolean` |
| --reviewer | | Add reviewer to the target PR | | `boolean` |
| --sha | | Sha of commit to backport | | `string` |
| --source-branch | | Specify a non-default branch to backport from | | `string` |
| --source-pr-label | | Labels added to the source PR | | `string` |
Expand Down
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ Change the author of the backported commit to the current user

CLI: `--reset-author`

#### `reviewers`

Add reviewers to the target pull request

CLI: `--reviewer`

#### `sha`

Backport a commit by specifying its commit sha
Expand Down
1 change: 1 addition & 0 deletions src/options/ConfigOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type ConfigOptions = Partial<{
prTitle: string;
pullNumber: number;
resetAuthor: boolean;
reviewers: string[];
sha: string;
sourceBranch: string;
sourcePRLabels: string[];
Expand Down
1 change: 1 addition & 0 deletions src/options/cliArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('getOptionsFromCliArgs', () => {
upstream: 'elastic/kibana',
username: 'sqren',
mainline: 1,
reviewers: [],
assignees: [],
commitPaths: [],
});
Expand Down
8 changes: 8 additions & 0 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ export function getOptionsFromCliArgs(
type: 'boolean',
})

.option('reviewer', {
description: 'Add reviewer to the target PR',
type: 'array',
string: true,
})

.option('sha', {
conflicts: ['pullNumber', 'prFilter'],
description: 'Commit sha to backport',
Expand Down Expand Up @@ -304,6 +310,7 @@ export function getOptionsFromCliArgs(
// array types (should be renamed to plural form)
assignee,
path,
reviewer,
sourcePRLabel,
targetBranch,
targetBranchChoice,
Expand All @@ -322,6 +329,7 @@ export function getOptionsFromCliArgs(
// rename array types to plural
assignees: assignee ?? [],
commitPaths: path ?? [],
reviewers: reviewer ?? [],
sourcePRLabels: sourcePRLabel,
targetBranchChoices: targetBranchChoice,
targetBranches: targetBranch,
Expand Down
1 change: 1 addition & 0 deletions src/options/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('getOptions', () => {
repoName: 'kibana',
repoOwner: 'elastic',
resetAuthor: false,
reviewers: [],
sourceBranch: 'default-branch-from-github',
sourcePRLabels: [],
targetBranchChoices: [
Expand Down
1 change: 1 addition & 0 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parseRequiredOptions } from './parseRequiredOptions';
export type ValidConfigOptions = Readonly<
Awaited<ReturnType<typeof getOptions>>
>;

export async function getOptions(
argv: string[],
optionsFromModule?: ConfigOptions
Expand Down
3 changes: 2 additions & 1 deletion src/runWithOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('runWithOptions', () => {
autoMerge: false,
autoMergeMethod: 'merge',
branchLabelMapping: undefined,
ci: false,
cherrypickRef: true,
ci: false,
commitPaths: [],
details: false,
editor: 'code',
Expand All @@ -78,6 +78,7 @@ describe('runWithOptions', () => {
repoName: 'kibana',
repoOwner: 'elastic',
resetAuthor: false,
reviewers: [],
sha: undefined,
sourceBranch: 'my-source-branch-from-options',
sourcePRLabels: [],
Expand Down
35 changes: 35 additions & 0 deletions src/services/github/v3/addReviewersToPullRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Octokit } from '@octokit/rest';
import ora from 'ora';
import { ValidConfigOptions } from '../../../options/options';
import { logger } from '../../logger';

export async function addReviewersToPullRequest(
{ githubApiBaseUrlV3, repoName, repoOwner, accessToken }: ValidConfigOptions,
pullNumber: number,
reviewers: string[]
) {
const text = `Adding reviewers: ${reviewers}`;
logger.info(text);
const spinner = ora(text).start();

try {
const octokit = new Octokit({
auth: accessToken,
baseUrl: githubApiBaseUrlV3,
log: logger,
});

await octokit.pulls.requestReviewers({
owner: repoOwner,
repo: repoName,
pull_number: pullNumber,
reviewers,
});

spinner.succeed();
} catch (e) {
const message = e.response?.data?.message;
spinner.fail(`Adding reviewers. ${message ? message : ''}`);
logger.info(`Could not add reviewers to PR ${pullNumber}`, e.stack);
}
}
15 changes: 9 additions & 6 deletions src/ui/cherrypickAndCreatePullRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
beforeEach(async () => {
const options = {
assignees: [] as string[],
githubApiBaseUrlV3: 'https://api.github.com',
fork: true,
targetPRLabels: ['backport'],
githubApiBaseUrlV3: 'https://api.github.com',
prDescription: 'myPrSuffix',
prTitle: '[{targetBranch}] {commitMessages}',
repoName: 'kibana',
repoOwner: 'elastic',
username: 'sqren',
reviewers: [] as string[],
sourceBranch: 'myDefaultSourceBranch',
sourcePRLabels: [] as string[],
targetPRLabels: ['backport'],
username: 'sqren',
} as ValidConfigOptions;

const commits: Commit[] = [
Expand Down Expand Up @@ -146,14 +147,15 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
beforeEach(async () => {
const options = {
assignees: [] as string[],
githubApiBaseUrlV3: 'https://api.github.com',
fork: true,
targetPRLabels: ['backport'],
githubApiBaseUrlV3: 'https://api.github.com',
prTitle: '[{targetBranch}] {commitMessages}',
repoName: 'kibana',
repoOwner: 'elastic',
username: 'sqren',
reviewers: [] as string[],
sourcePRLabels: [] as string[],
targetPRLabels: ['backport'],
username: 'sqren',
} as ValidConfigOptions;

const commits = [
Expand Down Expand Up @@ -207,6 +209,7 @@ describe('cherrypickAndCreateTargetPullRequest', () => {
prTitle: '[{targetBranch}] {commitMessages}',
repoName: 'kibana',
repoOwner: 'elastic',
reviewers: [] as string[],
sourceBranch: 'myDefaultSourceBranch',
sourcePRLabels: [] as string[],
targetPRLabels: ['backport'],
Expand Down
10 changes: 10 additions & 0 deletions src/ui/cherrypickAndCreateTargetPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { getFirstLine, getShortSha } from '../services/github/commitFormatters';
import { addAssigneesToPullRequest } from '../services/github/v3/addAssigneesToPullRequest';
import { addLabelsToPullRequest } from '../services/github/v3/addLabelsToPullRequest';
import { addReviewersToPullRequest } from '../services/github/v3/addReviewersToPullRequest';
import {
createPullRequest,
getTitle,
Expand Down Expand Up @@ -74,6 +75,15 @@ export async function cherrypickAndCreateTargetPullRequest({
);
}

// add reviewers to target pull request
if (options.reviewers.length > 0) {
await addReviewersToPullRequest(
options,
targetPullRequest.number,
options.reviewers
);
}

// add labels to target pull request
if (options.targetPRLabels.length > 0) {
await addLabelsToPullRequest(
Expand Down

0 comments on commit d3c9fea

Please sign in to comment.