Skip to content

Commit

Permalink
Show existing backports on commit list
Browse files Browse the repository at this point in the history
- Show existing backports on commit list
 - Add option to choose number of commits to be listed (Closes #76)
 - Add options to filter commits by username
 - Show link to authorize sso on access token in error message
 - do not track baseBranch when creating feature branch
 - cleanup by removing local feature branch after push
 - delete “origin” remote to avoid confusion
 - Migrate some APIs to graphql (closes #63)
  • Loading branch information
sorenlouv committed Jul 6, 2019
1 parent 0245a6b commit 7b54834
Show file tree
Hide file tree
Showing 60 changed files with 2,299 additions and 1,462 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module.exports = {
plugins: ['@typescript-eslint'],
plugins: ['@typescript-eslint', 'import'],
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'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: {
'import/order': 2,
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off'
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/no-explicit-any': 'off'
}
};
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ yarn tsc
node dist/index.js --branch 6.1 --upstream sqren/backport-demo --all
```

**Run `backport` CLI globally**

```
yarn global remove backport
npm -g uninstall backport
yarn unlink backport
yarn link
sudo chmod +x dist/index.js
yarn tsc --watch
```

You can now use `backport` command anywhere, and it'll point to the development version.

### Debug

**Run tests**
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ The above commands will start an interactive prompt. You can use the `arrow keys
| --accessToken | Github access token | | string |
| --all | Show commits from other than me | false | boolean |
| --apiHostname | Hostname for the Github API | api.github.com | string |
| --author | Filter commits by author | _Current user_ | string |
| --branch | Branch to backport to | | string |
| --commitsCount | Number of commits to choose from | 10 | number |
| --gitHostname | Hostname for Git remotes | github.com | string |
| --labels | Pull request labels | | string |
| --multiple | Select multiple branches and/or commits | false | boolean |
Expand Down
33 changes: 17 additions & 16 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/**/*.ts\" --write",
"lint": "tsc -p ./test/tsconfig.json && eslint ./src/**/*.ts",
"lint": "tsc -p ./test/tsconfig.json && eslint ./src/**/*.ts ./test/**/*.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 @@ -98,35 +98,36 @@
"yargs": "^13.2.2"
},
"devDependencies": {
"@types/core-js": "^2.5.0",
"@types/core-js": "^2.5.2",
"@types/find-up": "^2.1.1",
"@types/inquirer": "^6.0.0",
"@types/jest": "^24.0.11",
"@types/inquirer": "^6.0.3",
"@types/jest": "^24.0.15",
"@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/lodash.last": "^3.0.6",
"@types/mkdirp": "^0.5.2",
"@types/nock": "^10.0.0",
"@types/nock": "^10.0.3",
"@types/node": "^10.14.4",
"@types/rimraf": "^2.0.2",
"@types/strip-json-comments": "^0.0.30",
"@types/yargs": "^13.0.0",
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
"@typescript-eslint/eslint-plugin": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"axios-mock-adapter": "^1.16.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"husky": "^2.0.0",
"jest": "^24.7.1",
"lint-staged": "^8.1.5",
"eslint": "^6.0.1",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-prettier": "^3.1.0",
"husky": "^2.7.0",
"jest": "^24.8.0",
"lint-staged": "^8.2.1",
"lodash.last": "^3.0.0",
"nock": "^10.0.6",
"prettier": "^1.17.0",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
"ts-node": "^8.3.0",
"typescript": "^3.5.2"
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { initSteps } from './steps/steps';
import { getOptions } from './options/options';
import { initSteps } from './steps/steps';

async function init() {
try {
Expand Down
12 changes: 12 additions & 0 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ export function getOptionsFromCliArgs(
description: 'Hostname for the Github API',
type: 'string'
})
.option('author', {
default: configOptions.author,
description: 'Show commits by specific author',
type: 'string'
})
.option('branches', {
default: [] as string[],
description: 'Branch(es) to backport to',
type: 'array',
alias: 'branch',
string: true // ensure `6.0` is not coerced to `6`
})
.option('commitsCount', {
default: configOptions.commitsCount,
description: 'Number of commits to choose from',
type: 'number'
})
.option('gitHostname', {
default: configOptions.gitHostname,
description: 'Hostname for Github',
Expand Down Expand Up @@ -89,6 +99,8 @@ export function getOptionsFromCliArgs(
accessToken: cliArgs.accessToken,
all: cliArgs.all,
apiHostname: cliArgs.apiHostname,
author: cliArgs.author,
commitsCount: cliArgs.commitsCount,
branchChoices: configOptions.branchChoices,
branches: cliArgs.branches,
gitHostname: cliArgs.gitHostname,
Expand Down
6 changes: 3 additions & 3 deletions src/options/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import isString from 'lodash.isstring';
import { Config } from '../../types/Config';
import { PromiseReturnType } from '../../types/commons';
import { getGlobalConfig } from './globalConfig';
import { getProjectConfig } from './projectConfig';
import { PromiseReturnType } from '../../types/commons';
import { Config } from '../../types/Config';
import isString from 'lodash.isstring';

export type OptionsFromConfigFiles = PromiseReturnType<
typeof getOptionsFromConfigFiles
Expand Down
4 changes: 2 additions & 2 deletions src/options/config/globalConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readConfigFile } from './readConfigFile';
import { mkdirp, chmod, writeFile } from '../../services/rpc';
import { chmod, mkdirp, writeFile } from '../../services/rpc';
import { getGlobalConfigPath, getReposPath } from '../../services/env';
import { readConfigFile } from './readConfigFile';

export async function getGlobalConfig() {
await maybeCreateGlobalConfigAndFolder();
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/readConfigFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stripJsonComments from 'strip-json-comments';
import { Config } from '../../types/Config';
import { HandledError } from '../../services/HandledError';
import { readFile } from '../../services/rpc';
import { Config } from '../../types/Config';

export async function readConfigFile(filepath: string) {
const fileContents = await readFile(filepath, 'utf8');
Expand Down
4 changes: 2 additions & 2 deletions src/options/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isEmpty from 'lodash.isempty';
import { HandledError } from '../services/HandledError';
import { OptionsFromCliArgs, getOptionsFromCliArgs } from './cliArgs';
import { getOptionsFromConfigFiles } from './config/config';
import { PromiseReturnType } from '../types/commons';
import { getGlobalConfigPath } from '../services/env';
import { OptionsFromCliArgs, getOptionsFromCliArgs } from './cliArgs';
import { getOptionsFromConfigFiles } from './config/config';

export type BackportOptions = Readonly<PromiseReturnType<typeof getOptions>>;
export async function getOptions(argv: typeof process.argv) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import os from 'os';
import path from 'path';
import { BackportOptions } from '../options/options';

export function getGlobalConfigPath() {
Expand Down
44 changes: 24 additions & 20 deletions src/services/git.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import childProcess from 'child_process';
import rimraf from 'rimraf';
import { exec, stat } from './rpc';
import { HandledError } from './HandledError';
import { getRepoPath, getRepoOwnerPath } from './env';
import { BackportOptions } from '../options/options';
import { HandledError } from './HandledError';
import { exec, stat } from './rpc';
import { getRepoOwnerPath, getRepoPath } from './env';

async function folderExists(path: string): Promise<boolean> {
try {
Expand Down Expand Up @@ -87,9 +87,7 @@ export async function deleteRemote(
export async function addRemote(options: BackportOptions, remoteName: string) {
try {
await exec(
`git remote add ${remoteName} https://${options.accessToken}@${
options.gitHostname
}/${remoteName}/${options.repoName}.git`,
`git remote add ${remoteName} https://${options.accessToken}@${options.gitHostname}/${remoteName}/${options.repoName}.git`,
{
cwd: getRepoPath(options)
}
Expand All @@ -101,9 +99,12 @@ export async function addRemote(options: BackportOptions, remoteName: string) {
}

export function cherrypick(options: BackportOptions, commitSha: string) {
return exec(`git cherry-pick ${commitSha}`, {
cwd: getRepoPath(options)
});
return exec(
`git fetch ${options.repoOwner} master:master --force && git cherry-pick ${commitSha}`,
{
cwd: getRepoPath(options)
}
);
}

export async function isIndexDirty(options: BackportOptions) {
Expand All @@ -117,14 +118,14 @@ export async function isIndexDirty(options: BackportOptions) {
}
}

export async function createAndCheckoutBranch(
export async function createFeatureBranch(
options: BackportOptions,
baseBranch: string,
featureBranch: string
) {
try {
return await exec(
`git fetch origin ${baseBranch} && git branch ${featureBranch} origin/${baseBranch} --force && git checkout ${featureBranch} `,
`git reset --hard && git clean -d --force && git fetch ${options.repoOwner} ${baseBranch} && git checkout -B ${featureBranch} ${options.repoOwner}/${baseBranch} --no-track`,
{
cwd: getRepoPath(options)
}
Expand All @@ -142,18 +143,21 @@ export async function createAndCheckoutBranch(
}
}

export function push(options: BackportOptions, featureBranch: string) {
return exec(
`git push ${options.username} ${featureBranch}:${featureBranch} --force`,
{
cwd: getRepoPath(options)
}
);
export function deleteFeatureBranch(
options: BackportOptions,
featureBranch: string
) {
return exec(`git checkout master && git branch -D ${featureBranch}`, {
cwd: getRepoPath(options)
});
}

export async function resetAndPullMaster(options: BackportOptions) {
export function pushFeatureBranch(
options: BackportOptions,
featureBranch: string
) {
return exec(
`git reset --hard && git clean -d --force && git checkout master && git pull origin master`,
`git push ${options.username} ${featureBranch}:${featureBranch} --force`,
{
cwd: getRepoPath(options)
}
Expand Down
Loading

0 comments on commit 7b54834

Please sign in to comment.