Skip to content

Commit

Permalink
Add --reset-author flag (#148)
Browse files Browse the repository at this point in the history
* Add `—reset-author` flag

* Add tests for dash-cased and camel-cased args

* fix ts issue
  • Loading branch information
sorenlouv authored Sep 3, 2019
1 parent eb8ef38 commit 79fdc7c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ The above commands will start an interactive prompt. You can use the `arrow keys
| --pr-description | Pull request description suffix | | string |
| --pr-title | Pull request title pattern | | string |
| --pr | Pull request to backport | | number |
| --reset-author | Set yourself as commit author | | boolean |
| --sha | Sha of commit to backport | | string |
| --upstream | Name of organization and repository | | string |
| --username | Github username | | string |
Expand Down
37 changes: 31 additions & 6 deletions src/options/cliArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getOptionsFromCliArgs } from './cliArgs';
import { OptionsFromConfigFiles } from './config/config';

describe('getOptionsFromCliArgs', () => {
let res: ReturnType<typeof getOptionsFromCliArgs>;

beforeEach(async () => {
it('should return correct options', () => {
const configOptions = {
accessToken: 'myAccessToken',
all: false,
Expand Down Expand Up @@ -32,10 +31,8 @@ describe('getOptionsFromCliArgs', () => {
'sqren'
];

res = getOptionsFromCliArgs(configOptions, argv);
});
const res = getOptionsFromCliArgs(configOptions, argv);

it('should return correct options', () => {
expect(res).toEqual({
accessToken: 'myAccessToken',
all: true,
Expand All @@ -49,9 +46,37 @@ describe('getOptionsFromCliArgs', () => {
multipleBranches: true,
multipleCommits: false,
prTitle: 'myPrTitle',
resetAuthor: false,
sha: undefined,
upstream: 'sqren/backport-demo',
username: 'sqren'
});
});

it('should accept both camel-case and dashed-case and convert them to camel cased', () => {
const configOptions = {} as OptionsFromConfigFiles;
const argv = [
'--access-token',
'my access token',
'--apiHostname',
'my api hostname'
];

const res = getOptionsFromCliArgs(configOptions, argv);

expect(res.accessToken).toEqual('my access token');
expect('access-token' in res).toEqual(false);
expect(res.apiHostname).toEqual('my api hostname');
expect('api-hostname' in res).toEqual(false);
});

it('should accept aliases (--pr) but only return the full name (--pullNumber) in the result', () => {
const configOptions = {} as OptionsFromConfigFiles;
const argv = ['--pr', '1337'];

const res = getOptionsFromCliArgs(configOptions, argv);

expect(res.pullNumber).toEqual(1337);
expect('pr' in res).toEqual(false);
});
});
34 changes: 15 additions & 19 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function getOptionsFromCliArgs(
argv: string[]
) {
const cliArgs = yargs(argv)
// TODO: remove `any` downcast as soon as this PR is merged: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/38108
.parserConfiguration({
'strip-dashed': true,
'strip-aliased': true
} as any)
.usage('$0 [args]')
.wrap(Math.max(100, Math.min(120, yargs.terminalWidth())))
.option('accessToken', {
Expand Down Expand Up @@ -97,6 +102,11 @@ export function getOptionsFromCliArgs(
type: 'number',
alias: 'pr'
})
.option('resetAuthor', {
default: false,
description: 'Set yourself as commit author',
type: 'boolean'
})
.option('sha', {
description: 'Commit sha to backport',
type: 'string',
Expand All @@ -116,27 +126,13 @@ export function getOptionsFromCliArgs(
.version()
.help().argv;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { $0, _, ...rest } = cliArgs;

return {
accessToken: cliArgs.accessToken,
all: cliArgs.all,
apiHostname: cliArgs.apiHostname,
author: cliArgs.author,
commitsCount: cliArgs.commitsCount,
...rest,
branchChoices: configOptions.branchChoices,
branches: cliArgs.branches,
editor: cliArgs.editor,
fork: cliArgs.fork,
gitHostname: cliArgs.gitHostname,
labels: cliArgs.labels,
multiple: cliArgs.multiple,
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple,
path: cliArgs.path,
prTitle: cliArgs.prTitle,
prDescription: cliArgs.prDescription,
pullNumber: cliArgs.pullNumber,
sha: cliArgs.sha,
upstream: cliArgs.upstream,
username: cliArgs.username
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple
};
}
1 change: 1 addition & 0 deletions src/options/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const validOptions: OptionsFromCliArgs = {
prDescription: undefined,
prTitle: 'myPrTitle',
pullNumber: undefined,
resetAuthor: false,
sha: undefined,
upstream: 'elastic/kibana',
username: 'sqren'
Expand Down
9 changes: 9 additions & 0 deletions src/services/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ export function cherrypick(options: BackportOptions, commitSha: string) {
);
}

export function setCommitAuthor(options: BackportOptions, username: string) {
return exec(
`git commit --amend --no-edit --author "${username} <${username}@users.noreply.github.com>"`,
{
cwd: getRepoPath(options)
}
);
}

export async function isIndexDirty(options: BackportOptions) {
try {
await exec(`git diff-index --quiet HEAD --`, {
Expand Down
10 changes: 9 additions & 1 deletion src/steps/doBackportVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
deleteFeatureBranch,
isIndexDirty,
pushFeatureBranch,
getRemoteName
getRemoteName,
setCommitAuthor
} from '../services/git';
import { confirmPrompt } from '../services/prompts';
import { createPullRequest } from '../services/github/createPullRequest';
Expand Down Expand Up @@ -66,6 +67,13 @@ export async function doBackportVersion(
cherrypickAndConfirm(options, commit.sha)
);

if (options.resetAuthor) {
await withSpinner(
{ text: `Changing author to "${options.username}"` },
() => setCommitAuthor(options, options.username)
);
}

const headBranchName = getHeadBranchName(options, featureBranch);

await withSpinner({ text: `Pushing branch "${headBranchName}"` }, () =>
Expand Down
1 change: 1 addition & 0 deletions src/steps/steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('run through steps', () => {
pullNumber: undefined,
repoName: 'kibana',
repoOwner: 'elastic',
resetAuthor: false,
sha: undefined,
username: 'sqren'
};
Expand Down

0 comments on commit 79fdc7c

Please sign in to comment.