Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add —pr options to backport via pull request number #145

Merged
merged 5 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'plugin:prettier/recommended' // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
rules: {
'@typescript-eslint/no-object-literal-type-assertion': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/camelcase': 'off',
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ The above commands will start an interactive prompt. You can use the `arrow keys
| --path | Only list commits touching files under a specific path | | string |
| --pr-description | Pull request description suffix | | string |
| --pr-title | Pull request title pattern | | string |
| --pr | Pull request to backport | | number |
| --sha | Sha of commit to backport | | string |
| --upstream | Name of organization and repository | | string |
| --username | Github username | | string |
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
setupFiles: ['./test/unit/automatic-mocks.ts'],
setupFiles: ['./src/test/automatic-mocks.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
testRegex: 'test/(unit|integration)/.*test.ts$',
testRegex: '(test|src)/.*test.ts$',
moduleFileExtensions: ['ts', 'js', 'json'],
globals: {
'ts-jest': {
Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"scripts": {
"cover": "jest --coverage",
"format": "prettier \"./{src,test}/**/*.ts\" --write",
"lint": "tsc --project ./test/tsconfig.json && eslint ./src/**/*.ts ./test/**/*.ts",
"lint": "tsc --project ./src/test/tsconfig.json && eslint ./src/**/*.ts",
"postinstall": "test -f ./dist/scripts/runPostinstall.js && node ./dist/scripts/runPostinstall.js || echo 'Dist folder missing'",
"prepublishOnly": "tsc",
"publish-dry-run": "tar -tf $(npm pack)",
Expand Down Expand Up @@ -65,41 +65,41 @@
},
"dependencies": {
"axios": "^0.19.0",
"del": "^5.0.0",
"del": "^5.1.0",
"find-up": "^4.1.0",
"inquirer": "^6.5.0",
"inquirer": "^7.0.0",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2",
"lodash.isempty": "^4.4.0",
"lodash.isstring": "^4.0.1",
"make-dir": "^3.0.0",
"ora": "^3.4.0",
"strip-json-comments": "^3.0.1",
"yargs": "^13.3.0"
"yargs": "^14.0.0"
},
"devDependencies": {
"@types/core-js": "^2.5.2",
"@types/find-up": "^4.0.0",
"@types/inquirer": "^6.0.3",
"@types/jest": "^24.0.15",
"@types/lodash": "^4.14.136",
"@types/inquirer": "^6.5.0",
"@types/jest": "^24.0.18",
"@types/lodash": "^4.14.137",
"@types/lodash.flatten": "^4.4.6",
"@types/lodash.get": "^4.4.6",
"@types/lodash.isempty": "^4.4.6",
"@types/lodash.isstring": "^4.0.6",
"@types/nock": "^10.0.3",
"@types/node": "^10.14.4",
"@types/node": "^10.14.16",
"@types/strip-json-comments": "^3.0.0",
"@types/yargs": "^13.0.0",
"@typescript-eslint/eslint-plugin": "^1.12.0",
"@typescript-eslint/parser": "^1.12.0",
"eslint": "^6.0.1",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"@types/yargs": "^13.0.2",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"eslint": "^6.2.2",
"eslint-config-prettier": "^6.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.0",
"husky": "^3.0.0",
"jest": "^24.8.0",
"lint-staged": "^9.2.0",
"husky": "^3.0.4",
"jest": "^24.9.0",
"lint-staged": "^9.2.4",
"lodash": "^4.17.15",
"nock": "^10.0.6",
"prettier": "^1.18.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOptionsFromCliArgs } from '../../../src/options/cliArgs';
import { getOptionsFromCliArgs } from './cliArgs';

describe('getOptionsFromCliArgs', () => {
let res: ReturnType<typeof getOptionsFromCliArgs>;
Expand Down
6 changes: 6 additions & 0 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export function getOptionsFromCliArgs(
description: 'Description to be added to pull request',
type: 'string'
})
.option('pullNumber', {
description: 'Pull request to backport',
type: 'number',
alias: 'pr'
})
.option('sha', {
description: 'Commit sha to backport',
type: 'string',
Expand Down Expand Up @@ -129,6 +134,7 @@ export function getOptionsFromCliArgs(
path: cliArgs.path,
prTitle: cliArgs.prTitle,
prDescription: cliArgs.prDescription,
pullNumber: cliArgs.pullNumber,
sha: cliArgs.sha,
upstream: cliArgs.upstream,
username: cliArgs.username
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PromiseReturnType } from '../../../../src/types/commons';
import { getOptionsFromConfigFiles } from '../../../../src/options/config/config';
import { PromiseReturnType } from '../../types/commons';
import { getOptionsFromConfigFiles } from './config';

describe('getOptionsFromConfigFiles', () => {
let res: PromiseReturnType<typeof getOptionsFromConfigFiles>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as fs from '../../../../src/services/fs-promisified';
import { PromiseReturnType } from '../../../../src/types/commons';
import {
getGlobalConfig,
maybeCreateGlobalConfig
} from '../../../../src/options/config/globalConfig';
import * as fs from '../../services/fs-promisified';
import { PromiseReturnType } from '../../types/commons';
import { getGlobalConfig, maybeCreateGlobalConfig } from './globalConfig';
import makeDir from 'make-dir';

describe('config', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import findUp from 'find-up';
import * as fs from '../../../../src/services/fs-promisified';
import { PromiseReturnType } from '../../../../src/types/commons';
import { getProjectConfig } from '../../../../src/options/config/projectConfig';
import * as fs from '../../services/fs-promisified';
import { PromiseReturnType } from '../../types/commons';
import { getProjectConfig } from './projectConfig';

describe('getProjectConfig', () => {
afterEach(() => jest.restoreAllMocks());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OptionsFromCliArgs } from '../../../src/options/cliArgs';
import { validateRequiredOptions } from '../../../src/options/options';
import { OptionsFromCliArgs } from './cliArgs';
import { validateRequiredOptions } from './options';

const validOptions: OptionsFromCliArgs = {
accessToken: 'myAccessToken',
Expand All @@ -17,8 +17,9 @@ const validOptions: OptionsFromCliArgs = {
multipleBranches: true,
multipleCommits: false,
path: undefined,
prTitle: 'myPrTitle',
prDescription: undefined,
prTitle: 'myPrTitle',
pullNumber: undefined,
sha: undefined,
upstream: 'elastic/kibana',
username: 'sqren'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as globalConfig from '../../../src/options/config/globalConfig';
import { postinstall } from '../../../src/scripts/postinstall';
import * as globalConfig from '../options/config/globalConfig';
import { postinstall } from './postinstall';

describe('postinstall', () => {
it("should create global config if it doesn't exist", async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/services/env.test.ts → src/services/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BackportOptions } from '../../../src/options/options';
import { BackportOptions } from '../options/options';
import {
getGlobalConfigPath,
getRepoOwnerPath,
getRepoPath,
getReposPath
} from '../../../src/services/env';
} from '../services/env';

describe('env', () => {
test('getGlobalConfigPath', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/services/git.test.ts → src/services/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BackportOptions } from '../../../src/options/options';
import { addRemote } from '../../../src/services/git';
import * as childProcess from '../../../src/services/child-process-promisified';
import { BackportOptions } from '../options/options';
import { addRemote } from '../services/git';
import * as childProcess from '../services/child-process-promisified';

describe('addRemote', () => {
it('add correct origin remote', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
exports[`fetchCommitBySha should return single commit with pull request 1`] = `
Array [
Array [
"https://api.github.com/search/commits?q=hash:myCommitSha%20repo:elastic/kibana&per_page=1&access_token=myAccessToken",
"https://api.github.com/search/commits?q=hash:sha123456789%20repo:elastic/kibana&per_page=1&access_token=myAccessToken",
Object {
"headers": Object {
"Accept": "application/vnd.github.cloak-preview",
},
},
],
Array [
"https://api.github.com/search/issues?q=repo:elastic/kibana+myCommitSha+base:master&access_token=myAccessToken",
],
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Array [
associatedPullRequests(first: 1) {
edges {
node {
repository {
owner {
login
}
name
}
number
timelineItems(
last: 20
Expand Down
51 changes: 51 additions & 0 deletions src/services/github/commitFormatters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
getFirstCommitMessageLine,
getFormattedCommitMessage
} from './commitFormatters';

describe('getFirstCommitMessageLine', () => {
it('should only return the first line of the message', () => {
expect(
getFirstCommitMessageLine(
'My commit message (#1234)\n\n Additional commit message body'
)
).toEqual('My commit message (#1234)');
});

it('should return the commit message as-is', () => {
expect(getFirstCommitMessageLine('My commit message')).toEqual(
'My commit message'
);
});
});

describe('getFormattedCommitMessage', () => {
it('should return the first message line verbatim', () => {
expect(
getFormattedCommitMessage({
message: 'This is my commit message (#1234)\n\nthis is a second line',
pullNumber: 1234,
sha: 'sha123456789'
})
).toBe('This is my commit message (#1234)');
});

it('should add pullNumber as suffix', () => {
expect(
getFormattedCommitMessage({
message: 'This is my commit message\n\nthis is a second line',
pullNumber: 1234,
sha: 'sha123456789'
})
).toBe('This is my commit message (#1234)');
});

it('should add commit sha as suffix', () => {
expect(
getFormattedCommitMessage({
message: 'This is my commit message\n\nthis is a second line',
sha: 'sha123456789'
})
).toBe('This is my commit message (sha12345)');
});
});
40 changes: 25 additions & 15 deletions src/services/github/commitFormatters.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import { CommitChoice, CommitSelected } from './Commit';

export function getHumanReadableReference(commit: CommitSelected) {
return commit.pullNumber ? `#${commit.pullNumber}` : getShortSha(commit.sha);
}

export function getShortSha(sha: string) {
return sha.slice(0, 8);
}

export function getFirstCommitMessageLine(message: string) {
return message.split('\n')[0].replace(/\s\(#\d+\)/g, '');
return message.split('\n')[0];
}

export function withFormattedCommitMessage<
T extends CommitSelected | CommitChoice
>(commit: T): T {
const firstMessageLine = getFirstCommitMessageLine(commit.message);
return {
...commit,
message: `${firstMessageLine} (${getHumanReadableReference(commit)})`
};
export function getFormattedCommitMessage({
message,
pullNumber,
sha
}: {
message: string;
pullNumber?: number;
sha: string;
}) {
const firstMessageLine = getFirstCommitMessageLine(message);
if (pullNumber) {
const messageHasPullNumber = firstMessageLine.includes(`#${pullNumber}`);

// message already contain pull number
if (messageHasPullNumber) {
return firstMessageLine;
}

// message doesn't contain pull number. Add it
return `${firstMessageLine} (#${pullNumber})`;
}

// pull number not available. Add commit
return `${firstMessageLine} (${getShortSha(sha)})`;
}
Loading