Skip to content

Commit 79fdc7c

Browse files
authored
Add --reset-author flag (#148)
* Add `—reset-author` flag * Add tests for dash-cased and camel-cased args * fix ts issue
1 parent eb8ef38 commit 79fdc7c

File tree

7 files changed

+67
-26
lines changed

7 files changed

+67
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ The above commands will start an interactive prompt. You can use the `arrow keys
9999
| --pr-description | Pull request description suffix | | string |
100100
| --pr-title | Pull request title pattern | | string |
101101
| --pr | Pull request to backport | | number |
102+
| --reset-author | Set yourself as commit author | | boolean |
102103
| --sha | Sha of commit to backport | | string |
103104
| --upstream | Name of organization and repository | | string |
104105
| --username | Github username | | string |

src/options/cliArgs.test.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { getOptionsFromCliArgs } from './cliArgs';
2+
import { OptionsFromConfigFiles } from './config/config';
23

34
describe('getOptionsFromCliArgs', () => {
4-
let res: ReturnType<typeof getOptionsFromCliArgs>;
5-
6-
beforeEach(async () => {
5+
it('should return correct options', () => {
76
const configOptions = {
87
accessToken: 'myAccessToken',
98
all: false,
@@ -32,10 +31,8 @@ describe('getOptionsFromCliArgs', () => {
3231
'sqren'
3332
];
3433

35-
res = getOptionsFromCliArgs(configOptions, argv);
36-
});
34+
const res = getOptionsFromCliArgs(configOptions, argv);
3735

38-
it('should return correct options', () => {
3936
expect(res).toEqual({
4037
accessToken: 'myAccessToken',
4138
all: true,
@@ -49,9 +46,37 @@ describe('getOptionsFromCliArgs', () => {
4946
multipleBranches: true,
5047
multipleCommits: false,
5148
prTitle: 'myPrTitle',
49+
resetAuthor: false,
5250
sha: undefined,
5351
upstream: 'sqren/backport-demo',
5452
username: 'sqren'
5553
});
5654
});
55+
56+
it('should accept both camel-case and dashed-case and convert them to camel cased', () => {
57+
const configOptions = {} as OptionsFromConfigFiles;
58+
const argv = [
59+
'--access-token',
60+
'my access token',
61+
'--apiHostname',
62+
'my api hostname'
63+
];
64+
65+
const res = getOptionsFromCliArgs(configOptions, argv);
66+
67+
expect(res.accessToken).toEqual('my access token');
68+
expect('access-token' in res).toEqual(false);
69+
expect(res.apiHostname).toEqual('my api hostname');
70+
expect('api-hostname' in res).toEqual(false);
71+
});
72+
73+
it('should accept aliases (--pr) but only return the full name (--pullNumber) in the result', () => {
74+
const configOptions = {} as OptionsFromConfigFiles;
75+
const argv = ['--pr', '1337'];
76+
77+
const res = getOptionsFromCliArgs(configOptions, argv);
78+
79+
expect(res.pullNumber).toEqual(1337);
80+
expect('pr' in res).toEqual(false);
81+
});
5782
});

src/options/cliArgs.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export function getOptionsFromCliArgs(
77
argv: string[]
88
) {
99
const cliArgs = yargs(argv)
10+
// TODO: remove `any` downcast as soon as this PR is merged: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/38108
11+
.parserConfiguration({
12+
'strip-dashed': true,
13+
'strip-aliased': true
14+
} as any)
1015
.usage('$0 [args]')
1116
.wrap(Math.max(100, Math.min(120, yargs.terminalWidth())))
1217
.option('accessToken', {
@@ -97,6 +102,11 @@ export function getOptionsFromCliArgs(
97102
type: 'number',
98103
alias: 'pr'
99104
})
105+
.option('resetAuthor', {
106+
default: false,
107+
description: 'Set yourself as commit author',
108+
type: 'boolean'
109+
})
100110
.option('sha', {
101111
description: 'Commit sha to backport',
102112
type: 'string',
@@ -116,27 +126,13 @@ export function getOptionsFromCliArgs(
116126
.version()
117127
.help().argv;
118128

129+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
130+
const { $0, _, ...rest } = cliArgs;
131+
119132
return {
120-
accessToken: cliArgs.accessToken,
121-
all: cliArgs.all,
122-
apiHostname: cliArgs.apiHostname,
123-
author: cliArgs.author,
124-
commitsCount: cliArgs.commitsCount,
133+
...rest,
125134
branchChoices: configOptions.branchChoices,
126-
branches: cliArgs.branches,
127-
editor: cliArgs.editor,
128-
fork: cliArgs.fork,
129-
gitHostname: cliArgs.gitHostname,
130-
labels: cliArgs.labels,
131-
multiple: cliArgs.multiple,
132135
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
133-
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple,
134-
path: cliArgs.path,
135-
prTitle: cliArgs.prTitle,
136-
prDescription: cliArgs.prDescription,
137-
pullNumber: cliArgs.pullNumber,
138-
sha: cliArgs.sha,
139-
upstream: cliArgs.upstream,
140-
username: cliArgs.username
136+
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple
141137
};
142138
}

src/options/options.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const validOptions: OptionsFromCliArgs = {
2020
prDescription: undefined,
2121
prTitle: 'myPrTitle',
2222
pullNumber: undefined,
23+
resetAuthor: false,
2324
sha: undefined,
2425
upstream: 'elastic/kibana',
2526
username: 'sqren'

src/services/git.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ export function cherrypick(options: BackportOptions, commitSha: string) {
103103
);
104104
}
105105

106+
export function setCommitAuthor(options: BackportOptions, username: string) {
107+
return exec(
108+
`git commit --amend --no-edit --author "${username} <${username}@users.noreply.github.com>"`,
109+
{
110+
cwd: getRepoPath(options)
111+
}
112+
);
113+
}
114+
106115
export async function isIndexDirty(options: BackportOptions) {
107116
try {
108117
await exec(`git diff-index --quiet HEAD --`, {

src/steps/doBackportVersions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
deleteFeatureBranch,
1111
isIndexDirty,
1212
pushFeatureBranch,
13-
getRemoteName
13+
getRemoteName,
14+
setCommitAuthor
1415
} from '../services/git';
1516
import { confirmPrompt } from '../services/prompts';
1617
import { createPullRequest } from '../services/github/createPullRequest';
@@ -66,6 +67,13 @@ export async function doBackportVersion(
6667
cherrypickAndConfirm(options, commit.sha)
6768
);
6869

70+
if (options.resetAuthor) {
71+
await withSpinner(
72+
{ text: `Changing author to "${options.username}"` },
73+
() => setCommitAuthor(options, options.username)
74+
);
75+
}
76+
6977
const headBranchName = getHeadBranchName(options, featureBranch);
7078

7179
await withSpinner({ text: `Pushing branch "${headBranchName}"` }, () =>

src/steps/steps.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('run through steps', () => {
4646
pullNumber: undefined,
4747
repoName: 'kibana',
4848
repoOwner: 'elastic',
49+
resetAuthor: false,
4950
sha: undefined,
5051
username: 'sqren'
5152
};

0 commit comments

Comments
 (0)