Skip to content

Commit

Permalink
v4.5.1 Support running inside Docker (#115)
Browse files Browse the repository at this point in the history
* Refactor and improve how config and options are read

* Replace tslint with ESLint

* Cleanup types

* Re-organize files

* Move into `/services`

* Use accessToken instead of ssh keys

* Move postinstall to src files

* Improve spinners

* Add test for postinstall script

* Add Dockerfile and bash helper function

* Use `never` to require generic

* Rename prompts

* Remove wildcard imports (`* as something`)

* Add test for `getBranches`

* Add test for options

* Minor fixes

* Fix typo

* v4.4.2-docker-alpha-0.1

* Disallow empty strings in validateOptions

* 4.5.0-beta.1

* Ensure gitconfig is mounted

* Simplify package.json

* Update deps

* Rename to pre-push

* Add docs for running with docker

* Move docker help to readme

* Remove link to docker docs

* Update readme

* Rename `pullRequest` to `pullNumber`

* Rename `branch` to `baseBranch`

* 4.5.0-beta.2

* 4.5.0

* 4.5.1
  • Loading branch information
sorenlouv committed Apr 22, 2019
1 parent b9e2ec8 commit 3770d06
Show file tree
Hide file tree
Showing 76 changed files with 2,571 additions and 2,096 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
plugins: ['@typescript-eslint'],
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: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off'
}
};
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/node_modules
/coverage
/.coveralls.yml
/test/.DS_Store
/dist
/yarn-error.log
.DS_Store
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "node",
"request": "launch",
"name": "Start backport",
"program": "${workspaceRoot}/src/cli/index.ts",
"program": "${workspaceRoot}/src/index.ts",
"runtimeArgs": ["-r", "ts-node/register"],
"args": ["--branch=6.1", "--upstream=sqren/backport-demo"],
"console": "integratedTerminal"
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"editor.formatOnSave": true,
"eslint.autoFixOnSave": true,
"eslint.validate": [
"javascript",
{ "language": "typescript", "autoFix": true }
],
"files.exclude": {
"yarn.lock": true,
"coverage": true
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
**Run from source**

```
npx ts-node ./src/cli/index.ts --branch 6.1 --upstream sqren/backport-demo --all
npx ts-node ./src/index.ts --branch 6.1 --upstream sqren/backport-demo --all
```

**Run from build**

```
node dist/cli/index.js --branch 6.1 --upstream sqren/backport-demo --all
node dist/index.js --branch 6.1 --upstream sqren/backport-demo --all
```

**Compile typecscript**
Expand Down
57 changes: 44 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,76 @@ Source: [https://en.wikipedia.org/wiki/Backporting](https://en.wikipedia.org/wik

## Who is this tool for?

If your development workflow looks something like this:
This tools is for anybody who are working on a codebase where they have to maintain multiple versions. If you manually cherry-pick commits from master, this tool might save you a lot of time.

1. Write some code, merge those changes to master (eg. using a pull request)
2. Cherry-pick one or more commits from master onto one or more branches
3. Push those branches and a create new backport pull requests

Then `backport` might save you a lot of time and effort. The CLI will ask you which commit to backport, and to which branch and the cherry-pick the commit, and create a pull request towards the correct branch.
`backport` is a CLI that will ask you which commit to backport, and to which branch and then cherry-pick the commit, and create a pull request towards the correct branch.

## Requirements

- Node 8 or higher
- git

OR

- Docker

## Install
## Install with Node

```
npm install -g backport
```

After installation you must update the global config in `~/.backport/config` with your Github username and a Github access token. More info [here](https://github.com/sqren/backport/blob/master/docs/getting-started.md#new-user-create-user-config)
After installation you must update the [global config](https://github.com/sqren/backport/blob/master/docs/getting-started.md#new-user-create-user-config) in `~/.backport/config.json` with your Github username and a Github access token.

## Run via Docker

If you don't have Node.js or git installed locally, you can run `backport` via Docker.
The easiest way is to add the following snippet to your bash profile:

```sh
backport() {
BACKPORT_CONFIG_DIR=~/.backport
GIT_CONFIG_FILE=~/.gitconfig

docker run -it --rm -v $(pwd):/app:ro -v $BACKPORT_CONFIG_DIR:/root/.backport -v $GIT_CONFIG_FILE:/etc/gitconfig sqren/backport "$@"
}
```

Where:

- `BACKPORT_CONFIG_DIR`: This can be ANY empty folder on your local machine. Upon running the docker container for the first time, a [`config.json`](https://github.com/sqren/backport/blob/master/docs/getting-started.md#new-user-create-user-config) will be created automatically. This must be filled out with `username` and `accessToken` or these must be passed as CLI arguments: `backport --username <username> --accessToken <accessToken>`
- `GIT_CONFIG_FILE`: Must point to a local [`.gitconfig`](https://gist.github.com/sqren/618ab2f77ffb8b5388d675fe705ed6da) file that contains the user's name and email.

You can now use `backport` as if it was installed locally.

## Usage

Run the CLI in your project folder (eg. in the Kibana folder):
Run the CLI in your project folder (must contain a [`.backportrc.json`](https://github.com/sqren/backport/blob/master/docs/configuration.md#project-specific-configuration) file):

```
> backport
```

or run this from anywhere:

```
$ backport
> backport --upstream elastic/kibana
```

Follow the steps. You can use the `arrow keys` to choose options, `<space>` to select checkboxes and `<enter>` to proceed.
The above commands will start an interactive prompt. You can use the `arrow keys` to choose options, `<space>` to select checkboxes and `<enter>` to proceed.

### Options

| Option | Description | Accepts |
| ------------- | ----------------------------------------- | ------------------------ |
| --accessToken | Github access token | string |
| --all | Show all commits | boolean (default: false) |
| --branch | Branch to backport | array (default: []) |
| --branch | Branch to backport to | string |
| --labels | Pull request labels | string |
| --multiple | Backport multiple commits and/or branches | boolean |
| --upstream | Name of repository | string |
| --sha | Commit sha to backport | string |
| --upstream | Name of repository | string |
| --username | Github username | string |
| --help | Show help | |
| -v, --version | Show version number | |

Expand Down
4 changes: 0 additions & 4 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ fs.chmod = callbackify((...args) => {
return Promise.resolve('fs.chmod mock value');
});

fs.statSync = () => {
// console.log('Accessing directly statSync');
};

module.exports = fs;
11 changes: 11 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Build command: `docker build -t sqren/backport .`
FROM node:latest

RUN mkdir /app
WORKDIR /app

COPY ./entrypoint.sh /entrypoint.sh

RUN npm install backport -g
ENTRYPOINT ["/entrypoint.sh"]
CMD []
18 changes: 18 additions & 0 deletions docker/backport-alias.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Example usage:
# With .backport/config.json mounted:
# - `backport`
# Without .backport/config.json mounted:
# `backport --username sqren --accessToken myGithubAccessToken --upstream elastic/kibana`
backport() {
BACKPORT_FOLDER=~/.backport
GITCONFIG=~/.gitconfig

# -it: interactive shell
# --rm: remove container after exit
# -v: Mounts the following volumes:
# - current directory as read-only volume (to access .backportrc.json)
# - .backport folder (to access config.json and avoid re-cloning repos)
# - gitconfig (to apply correct username and email to cherry-picked commits)
# "$@": pass all bash arguments to docker (which into turn passes them to backport cli inside container)
docker run -it --rm -v $(pwd):/app:ro -v $BACKPORT_FOLDER:/root/.backport -v $GITCONFIG:/etc/gitconfig sqren/backport "$@"
}
11 changes: 11 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

EMAIL=$(git config --system user.email)
NAME=$(git config --system user.name)
if test -z "$EMAIL" || test -z "$NAME"
then
echo ".gitconfig must be mounted"
exit 1
else
backport "$@"
fi
18 changes: 9 additions & 9 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Configuration

* [User configuration](#userglobal-configuration)
* [Project configuration](#project-specific-configuration)
- [User configuration](#userglobal-configuration)
- [Project configuration](#project-specific-configuration)

### User/global configuration

Expand All @@ -22,13 +22,13 @@ A personal access token can be created here: https://github.com/settings/tokens/

Please select the necessary access scopes:

* Private repository
* repo:status
* repo_deployment
* public_repo
* repo:invite
* Public repository
* public_repo
- **Private repository**
- repo:status
- repo_deployment
- public_repo
- repo:invite
- **Public repository**
- public_repo

##### `username` (string) **required**

Expand Down
23 changes: 13 additions & 10 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Getting started

* [Setup for new users](#new-user-create-user-config)
* [Configure new project](#new-project-create-project-config)
- [Setup for new users](#new-user-create-user-config)
- [Configure new project](#new-project-create-project-config)

### New user: create user config

Expand All @@ -22,15 +22,18 @@ Edit `~/.backport/config.json`:

**Create access token**

You can create a new access token [here](https://github.com/settings/tokens/new). The minimum permissions depends on the type of repository:
You can create a new access token [here](https://github.com/settings/tokens/new) (password required). The minimum permissions depends on whether the repository you want to access is private or public

* Private repository
* repo:status
* repo_deployment
* public_repo
* repo:invite
* Public repository
* public_repo
- **Private repository**
- repo:status
- repo_deployment
- public_repo
- repo:invite
- **Public repository**

- public_repo

Read more about [Github Access Tokens](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)

**Use `backport`**

Expand Down
38 changes: 18 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
"branches",
"branching"
],
"version": "4.4.2",
"main": "./dist/cli/index.js",
"version": "4.5.1",
"main": "./dist/index.js",
"bin": {
"backport": "./dist/cli/index.js"
"backport": "./dist/index.js"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
"pre-commit": "lint-staged",
"pre-push": "yarn lint"
}
},
"license": "MIT",
"scripts": {
"build": "tsc",
"cover": "jest --coverage",
"format": "prettier \"./src/**/*.ts\" --write",
"lint": "npx tsc -p ./test/tsconfig.json && tslint \"./src/**/*.ts\"",
"postinstall": "node scripts/postinstall.js",
"prepublishOnly": "npx tsc",
"prepush": "yarn lint",
"lint": "tsc -p ./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)",
"test": "jest"
},
Expand All @@ -44,9 +44,7 @@
},
"files": [
"dist",
"yarn.lock",
"scripts/postinstall.js",
"templates/configTemplate.json"
"yarn.lock"
],
"author": "Søren Louv-Jansen",
"homepage": "https://github.com/sqren/backport/blob/master/README.md",
Expand Down Expand Up @@ -77,10 +75,8 @@
},
"dependencies": {
"axios": "^0.18.0",
"chalk": "^2.4.2",
"find-up": "^3.0.0",
"inquirer": "^6.2.2",
"joi": "^14.3.1",
"inquirer": "^6.3.1",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2",
"lodash.isempty": "^4.4.0",
Expand All @@ -96,7 +92,6 @@
"@types/find-up": "^2.1.1",
"@types/inquirer": "^6.0.0",
"@types/jest": "^24.0.11",
"@types/joi": "^14.3.2",
"@types/lodash.flatten": "^4.4.6",
"@types/lodash.get": "^4.4.6",
"@types/lodash.isempty": "^4.4.6",
Expand All @@ -107,17 +102,20 @@
"@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",
"axios-mock-adapter": "^1.16.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"husky": "^1.3.1",
"jest": "^24.7.1",
"lint-staged": "^8.1.5",
"lodash.last": "^3.0.0",
"nock": "^10.0.6",
"prettier": "^1.16.4",
"prettier": "^1.17.0",
"ts-jest": "^24.0.2",
"ts-node": "^8.0.3",
"tslint": "^5.15.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.4.3"
"ts-node": "^8.1.0",
"typescript": "^3.4.4"
}
}
19 changes: 0 additions & 19 deletions scripts/postinstall.js

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 3770d06

Please sign in to comment.