Skip to content

Commit

Permalink
Backport via pull number
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Sep 19, 2018
1 parent 2ce1092 commit 9e572a4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 64 deletions.
48 changes: 35 additions & 13 deletions src/cli/cliService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {
listBranches,
confirmConflictResolved
} from '../lib/prompts';
import {
createPullRequest,
addLabels,
getCommit,
getCommits
} from '../lib/github';
import * as github from '../lib/github';
import { HandledError } from '../lib/errors';
import { getRepoPath } from '../lib/env';
import * as logger from '../lib/logger';
Expand Down Expand Up @@ -98,9 +93,13 @@ export async function doBackportVersion(

return withSpinner({ text: 'Creating pull request' }, async () => {
const payload = getPullRequestPayload(branch, commits, username);
const pullRequest = await createPullRequest(owner, repoName, payload);
const pullRequest = await github.createPullRequest(
owner,
repoName,
payload
);
if (labels.length > 0) {
await addLabels(owner, repoName, pullRequest.number, labels);
await github.addLabels(owner, repoName, pullRequest.number, labels);
}
return pullRequest;
});
Expand Down Expand Up @@ -139,7 +138,30 @@ export async function getCommitBySha(
) {
const spinner = ora().start();
try {
const commit = await getCommit(owner, repoName, sha);
const commit = await github.getCommitBySha(owner, repoName, sha);
spinner.stopAndPersist({
symbol: chalk.green('?'),
text: `${chalk.bold('Select commit')} ${chalk.cyan(commit.message)}`
});
return commit;
} catch (e) {
spinner.stop();
throw e;
}
}

export async function getCommitByPullNumber(
owner: string,
repoName: string,
pullNumber: number
) {
const spinner = ora().start();
try {
const commit = await github.getCommitByPullNumber(
owner,
repoName,
pullNumber
);
spinner.stopAndPersist({
symbol: chalk.green('?'),
text: `${chalk.bold('Select commit')} ${chalk.cyan(commit.message)}`
Expand All @@ -159,7 +181,7 @@ export async function getCommitsByPrompt(
) {
const spinner = ora('Loading commits...').start();
try {
const commits = await getCommits(owner, repoName, author);
const commits = await github.getCommitsByAuthor(owner, repoName, author);
if (isEmpty(commits)) {
spinner.stopAndPersist({
symbol: chalk.green('?'),
Expand Down Expand Up @@ -209,12 +231,12 @@ function getShortSha(commit: Commit) {
}

export function getReferenceLong(commit: Commit) {
return commit.pullRequest ? `#${commit.pullRequest}` : getShortSha(commit);
return commit.pullNumber ? `#${commit.pullNumber}` : getShortSha(commit);
}

function getReferenceShort(commit: Commit) {
return commit.pullRequest
? `pr-${commit.pullRequest}`
return commit.pullNumber
? `pr-${commit.pullNumber}`
: `commit-${getShortSha(commit)}`;
}

Expand Down
5 changes: 5 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function getOptions(config: CombinedConfig, cliArgs: yargs.Arguments) {
...config,
branches: flattenBranches(cliArgs.branch),
sha: cliArgs.sha,
pullNumber: cliArgs.pull,
all: cliArgs.all,
multiple: cliArgs.multiple,
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
Expand Down Expand Up @@ -80,6 +81,10 @@ async function initYargs() {
description: 'Commit sha to backport',
type: 'string'
})
.option('pull', {
description: 'Pull request to backport',
type: 'number'
})
.option('show-config', {
description: 'Show config settings',
type: 'boolean'
Expand Down
48 changes: 30 additions & 18 deletions src/cli/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,18 @@ import {
getBranchesByPrompt,
doBackportVersions,
handleErrors,
maybeSetupRepo
maybeSetupRepo,
getCommitByPullNumber
} from './cliService';
import { BackportOptions, BranchChoice } from '../types/types';
import { BackportOptions } from '../types/types';

export async function initSteps(options: BackportOptions) {
const [owner, repoName] = options.upstream.split('/');
setAccessToken(options.accessToken);

try {
const author = options.all ? null : options.username;
const commits = options.sha
? [await getCommitBySha(owner, repoName, options.sha)]
: await getCommitsByPrompt(
owner,
repoName,
author,
options.multipleCommits
);

const branches = !isEmpty(options.branches)
? (options.branches as string[])
: await getBranchesByPrompt(
options.branchChoices as BranchChoice[],
options.multipleBranches
);
const commits = await getCommits(owner, repoName, options);
const branches = await getBranches(options);

await maybeSetupRepo(owner, repoName, options.username);
await doBackportVersions(
Expand All @@ -46,3 +33,28 @@ export async function initSteps(options: BackportOptions) {
handleErrors(e);
}
}

async function getCommits(
owner: string,
repoName: string,
options: BackportOptions
) {
if (options.sha) {
return [await getCommitBySha(owner, repoName, options.sha)];
}

if (options.pullNumber) {
return [await getCommitByPullNumber(owner, repoName, options.pullNumber)];
}

const author = options.all ? null : options.username;
return getCommitsByPrompt(owner, repoName, author, options.multipleCommits);
}

function getBranches(options: BackportOptions) {
if (!isEmpty(options.branches)) {
return options.branches!;
}

return getBranchesByPrompt(options.branchChoices!, options.multipleBranches);
}
58 changes: 44 additions & 14 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
GithubIssue,
GithubPullRequestPayload,
GithubApiError,
GithubSearch
GithubSearch,
GithubPullRequest
} from '../types/types';

import axios, { AxiosResponse } from 'axios';
Expand All @@ -20,7 +21,7 @@ function getCommitMessage(message: string) {
return message.split('\n')[0].trim();
}

export async function getCommits(
export async function getCommitsByAuthor(
owner: string,
repoName: string,
author: string | null
Expand All @@ -42,22 +43,24 @@ export async function getCommits(
)}`
);

const promises = res.data.map(async commit => {
const sha = commit.sha;
return {
message: getCommitMessage(commit.commit.message),
sha,
pullRequest: await getPullRequestBySha(owner, repoName, sha)
};
});
const promises = res.data.map(
async (commit): Promise<Commit> => {
const sha = commit.sha;
return {
message: getCommitMessage(commit.commit.message),
sha,
pullNumber: await getPullNumberBySha(owner, repoName, sha)
};
}
);

return Promise.all(promises);
} catch (e) {
throw getError(e);
}
}

export async function getCommit(
export async function getCommitBySha(
owner: string,
repoName: string,
sha: string
Expand All @@ -78,12 +81,39 @@ export async function getCommit(

const commitRes = res.data.items[0];
const fullSha = commitRes.sha;
const pullRequest = await getPullRequestBySha(owner, repoName, fullSha);
const pullNumber = await getPullNumberBySha(owner, repoName, fullSha);

return {
message: getCommitMessage(commitRes.commit.message),
sha: fullSha,
pullRequest
pullNumber
};
} catch (e) {
throw getError(e);
}
}

export async function getCommitByPullNumber(
owner: string,
repoName: string,
pullNumber: number
): Promise<Commit> {
try {
const res: AxiosResponse<GithubPullRequest> = await axios(
`https://api.github.com/repos/${owner}/${repoName}/pulls/${pullNumber}?access_token=${accessToken}`
);

if (!res.data.merged) {
throw new HandledError(
`Pull request #${pullNumber} has not been merged to master`
);
}

const sha = res.data.merge_commit_sha;
return {
sha,
message: res.data.title,
pullNumber
};
} catch (e) {
throw getError(e);
Expand Down Expand Up @@ -125,7 +155,7 @@ export async function addLabels(
}
}

async function getPullRequestBySha(
async function getPullNumberBySha(
owner: string,
repoName: string,
commitSha: string
Expand Down
9 changes: 8 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface GithubSearch<T> {
items: T[];
}

export interface GithubPullRequest {
title: string;
merge_commit_sha: string;
merged: boolean;
}

export interface GithubPullRequestPayload {
title: string;
head: string;
Expand Down Expand Up @@ -88,6 +94,7 @@ export interface BackportOptions {
username: string;
branchChoices?: BranchChoice[];
branches?: string[];
pullNumber?: number;
sha?: string;
}

Expand All @@ -107,7 +114,7 @@ export interface PullRequest {
export interface Commit {
sha: string;
message: string;
pullRequest?: number;
pullNumber?: number;
}

/*
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/steps.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Array [
"short": "Update readme: Make ui-framework url a link",
"value": Object {
"message": "Update readme: Make ui-framework url a link",
"pullRequest": "PR for commitSha",
"pullNumber": "PR for commitSha",
"sha": "commitSha",
},
},
Expand All @@ -80,7 +80,7 @@ Array [
"short": "Update react 15.6.1 (#13672)",
"value": Object {
"message": "Update react 15.6.1 (#13672)",
"pullRequest": "PR for commitSha2",
"pullNumber": "PR for commitSha2",
"sha": "commitSha2",
},
},
Expand All @@ -89,7 +89,7 @@ Array [
"short": "Mock out static files when running in Jest (#13315)",
"value": Object {
"message": "Mock out static files when running in Jest (#13315)",
"pullRequest": "PR for commitSha3",
"pullNumber": "PR for commitSha3",
"sha": "commitSha3",
},
},
Expand Down
10 changes: 5 additions & 5 deletions test/cliService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ describe('doBackportVersion', () => {
{
sha: 'mySha',
message: 'myCommitMessage',
pullRequest: 1000
pullNumber: 1000
},
{
sha: 'mySha2',
message: 'myOtherCommitMessage',
pullRequest: 2000
pullNumber: 2000
}
];

Expand Down Expand Up @@ -128,7 +128,7 @@ describe('getCommitBySha', () => {
expect(commits).toEqual({
message: '[Chrome] Bootstrap Angular into document.body (#15158)',
sha: 'myCommitSha',
pullRequest: undefined
pullNumber: undefined
});
});

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('getCommitBySha', () => {

expect(await getCommitBySha('elastic', 'kibana', 'myCommitSha')).toEqual({
message: '[Chrome] Bootstrap Angular into document.body (#15158)',
pullRequest: 1338,
pullNumber: 1338,
sha: 'myCommitSha'
});
});
Expand All @@ -178,7 +178,7 @@ describe('getReferenceLong', () => {
it('should return a pr', () => {
expect(
getReferenceLong({
pullRequest: 1337,
pullNumber: 1337,
sha: 'mySha1234567',
message: 'myMessage'
})
Expand Down
Loading

0 comments on commit 9e572a4

Please sign in to comment.