3.1.1 • Published 10 months ago

@lampajr/bper v3.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

:tada: BPER v3 is out!! You can backport Gitlab merge requests now :tada:


Git Backporter, also referenced as bper, is a NodeJS command line tool that provides capabilities to backport pull requests (on GitHub) and merge requests (on GitLab) in an automated way. This tool also comes with a predefined GitHub action in order to make CI/CD integration easier for all users.

Table of content

Who is this tool for?

bper is a tool that provides capabilities to backport pull requests (on GitHub) and merge requests (on GitLab) in an automated way.

What is backporting? - backporting is an action aiming to move a change (usually a commit) from a branch (usually the main one) to another one, which is generally referring to a still maintained release branch. Keeping it simple: it is about to move a specific change or a set of them from one branch to another.

Therefore this tools is for anybody who is working on projects where they have to maintain multiple active branches/versions at the same time. If you are actively cherry-picking many changes from your main branch to other ones, and you mainly do changes through pull requests or merge requests, maybe this tool may be right for you.

CLI tool

This tool is released on the public npm registry, therefore it can be easily installed using npm:

$ npm install -g @lampajr/bper

Then you just have to choose the pull request (or merge request on Gitlab) that you would like to backport and the target branch and the simply run the following command:

$ bper -tb <branch> -pr <pull-request-url> -a <git-token>

A real example could be the following one:

$ bper -tb develop -pr https://github.com/lampajr/backporting-example/pull/47 -a *****

This is the easiest invocation where you let the tool set / compute most of the backported pull request data. Obviously most of that data can be overridden with appropriate tool options, more details can be found in the inputs section.

Requirements

  • Node 16 or higher, more details on Node can be found here.
  • Git, see how to install if you need help.

Inputs

This tool comes with some inputs that allow users to override the default behavior, here the full list of available inputs:

NameCommandRequiredDescriptionDefault
Version-V, --version-Current version of the tool
Help-h, --help-Display the help message
Target Branch-tb, --target-branchNBranch where the changes must be backported to
Pull Request-pr, --pull-requestNOriginal pull request url, the one that must be backported, e.g., https://github.com/lampajr/backporting/pull/1
Configuration File-cf, --config-fileNConfiguration file, in JSON format, containing all options to be overridded, note that if provided all other CLI options will be ignored
Auth-a, --authNGITHUB_TOKEN, GITLAB_TOKEN or a repo scoped Personal Access Token""
Folder-f, --folderNLocal folder full name of the repository that will be checked out, e.g., /tmp/folder{cwd}/bp
Git User-gu, --git-userNLocal git user name"GitHub"
Git Email-ge, --git-emailNLocal git user email"noreply@github.com"
Title--titleNBackporting pull request title"{original-pr-title}"
Body--bodyNBackporting pull request body"{original-pr-body}"
Body Prefix--body-prefixNPrefix to the backporting pull request body"Backport: {original-pr-link}"
Reviewers--reviewersNBackporting pull request comma-separated reviewers list[]
Assignees--assignesNBackporting pull request comma-separated assignees list[]
No Reviewers Inheritance--no-inherit-reviewersNConsidered only if reviewers is empty, if true keep reviewers as empty list, otherwise inherit from original pull requestfalse
Backport Branch Name--bp-branch-nameNName of the backporting pull request branchbp-{target-branch}-{sha}
Dry Run-d, --dry-runNIf enabled the tool does not push nor create anything remotely, use this to skip PR creationfalse

NOTE: pull request and target branch are mandatory, they must be provided as CLI options or as part of the configuration file (if used).

Configuration file example

This is an example of a configuration file that can be used.

{
  "pullRequest": "https://gitlab.com/<namespace>/<repo>/-/merge_requests/1",
  "targetBranch": "old",
  "folder": "/tmp/my-folder",
  "title": "Override Title",
  "auth": "*****"
}

Keep in mind that its structue MUST match the Args interface, which is actually a camel-case version of the CLI options.

Supported git services

Right now bper supports the following git management services:

  • GITHUB: Introduced since the first release of this tool (version 1.0.0). The interaction with this system is performed using octokit client library.

  • GITLAB: This has been introduced since version 3.0.0, it works for both public and private GitLab servers. The interaction with this service is performed using plain axios requests. The gitlab api version that is used to make requests is v4, at the moment there is no possibility to override it.

NOTE: by default, all gitlab requests are performed setting rejectUnauthorized=false, planning to make this configurable too.

GitHub action

This action can be used in any GitHub workflow, below you can find a simple example of manually triggered workflow backporting a specific pull request (provided as input).

name: Pull Request Backporting using BPer

on: 
  workflow_dispatch:
    inputs:
      targetBranch:
        description: 'Target branch'
        required: true
        type: string
      pullRequest:
        description: 'Pull request'
        required: true 
        type: string
      dryRun:
        description: 'Dry run'
        required: false
        default: "true" 
        type: string

jobs:
  backporting:
    name: "Backporting"
    runs-on: ubuntu-latest
    steps:
      - name: Backporting
        uses: lampajr/backporting@main
        with:
          target-branch: ${{ inputs.targetBranch }}
          pull-request: ${{ inputs.pullRequest }}
          auth: ${{ secrets.GITHUB_TOKEN }}
          dry-run: ${{ inputs.dryRun }}

You can also use this action with other events - you'll just need to specify target-branch and pull-request params.

For example, this configuration creates a pull request against branch v1 once the current one is merged, provided that the label backport-v1 is applied:

name: Pull Request Backporting using BPer

on:
  pull_request_target:
    types:
      - closed
      - labeled

jobs:
  backporting:
    name: "Backporting"
    # Only react to merged PRs for security reasons.
    # See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target.
    if: >
      github.event.pull_request.merged
      && (
        github.event.action == 'closed'
          && contains(github.event.pull_request.labels.*.name, 'backport-v1')
        || (
          github.event.action == 'labeled'
          && contains(github.event.label.name, 'backport-v1')
        )
      )
    runs-on: ubuntu-latest
    steps:
      - name: Backporting
        uses: lampajr/backporting@main
        with:
          target-branch: v1
          pull-request: ${{ github.event.pull_request.url }}
          auth: ${{ secrets.GITHUB_TOKEN }}

For a complete description of all inputs see Inputs section.

Future works

BPer is still in development mode, this means that there are still many future works and extension. I'll try to summarize the most important ones:

  • Provide a way to backport single commit too (or a set of them), even if no original pull request is present.
  • Integrate this tool with other git management services (like Bitbucket) to make it as generic as possible.
  • Integrate it into other CI/CD services like gitlab CI.
  • Provide some reusable GitHub workflows.

Release

The release of this package is entirely based on release-it tool. I created some useful scripts that can make the release itself quite easy.

Automated release

The first step is to prepare the changes for the next release, this is done by running:

$ npm run release:prepare:all

NOTE: running locally this requires npm login, please consider using .github/workflows/prepare-release.yml if you don't have permission on the npm package.

This script performs the following steps: 1. Automatically computes the next version based on the last commits 2. Create a new branch release/v${computed_version} 3. Apply all changes, like version and changelog upgrade 4. Commit those changes: chore: release v${compute_version}

After that you should just push the new branch and open the pull request.

NOTE: if you don't want to run this preparation from you local environment, there is already a workflow that does all these steps, including the pull request. See Prepare release workflow.

Once the release preparion pull request got merged, you can run Release package workflow that automatically performs the release itself, including npm publishing, git tag and github release.

Manual release

In case we would like to perform a manual release, it would be enough to open a pull request changing the following items:

  • Package version inside the package.json
  • Provide exhaustive changelog information inside CHANGELOG.md
  • Commit like chore: release v<version>

Once the release preparion pull request got merged, run Release package workflow.

Contributing

This is an open source project, and you are more than welcome to contribute :heart:!

Every change must be submitted through a GitHub pull request (PR). Backporting uses continuous integration (CI). The CI runs checks against your branch after you submit the PR to ensure that your PR doesn’t introduce errors. If the CI identifies a potential problem, our friendly PR maintainers will help you resolve it.

Note: this project follows git-conventional-commits standards, thanks to the commit-msg hook you are not allowed to use commits that do not follow those standards.

  1. Fork it (https://github.com/lampajr/backporting).

  2. Create your feature branch: (git checkout -b feature).

  3. Commit your changes with a comment: (git commit -am 'Add some feature').

  4. Push to the branch to GitHub: (git push origin feature).

  5. Create a new pull request against main branch.

Note: you don't need to take care about typescript compilation and minifycation, there are automated git hooks taking care of that!

License

Backporting (BPer) open source project is licensed under the MIT license.

3.1.1

10 months ago

3.1.0

10 months ago

3.0.0

10 months ago

2.2.1

10 months ago

2.2.0

10 months ago

2.1.0

11 months ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.0-alpha

1 year ago

0.0.0

1 year ago

0.0.1

1 year ago