Giter Site home page Giter Site logo

ffittschen / pr-branch-labeler Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 4.0 976 KB

🏷 Automatically label your PRs based on their branches.

License: MIT License

TypeScript 71.82% JavaScript 28.18%
github-actions typescript jest pull-request label branch javascript-action

pr-branch-labeler's Introduction

PR Branch Labeler

A GitHub Action that offers an easy way to automatically add labels to opened PRs depending on their head and/or base branch. The branches can be specified directly or using patterns, such as feature/* or bugfix/*.

Usage

To use the PR Branch Labeler, you can add a YAML-based workflow file, e.g..github/workflows/pr-branch-labeler.yml, with the following content:

name: PR Branch Labeler

on: pull_request

jobs:
  label_prs:
    runs-on: ubuntu-latest
    steps:
    - name: Label PRs
      if: github.event.action == 'opened' # Only run the action when the PR was first opened
      uses: ffittschen/pr-branch-labeler@v1
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}

Warning: if the Pull Requests submitted on your repository come from a fork, you must use the event pull_request_target instead of pull_request. This will grant write permissions to secrets.GITHUB_TOKEN. If you use pull_request, the secrets.GITHUB_TOKEN will have read-only permissions which does not work. See GitHub Action event pull_request_target reference.

Configuration

Configure the PR Branch Labeler action by creating a .github/pr-branch-labeler.yml file, e.g. with the following:

# Apply label "feature" if head matches "feature/*"
feature:
  head: "feature/*"

# Apply label "bugfix" if head matches one of "bugfix/*" or "hotfix/*"
bugfix:
  head: ["bugfix/*", "hotfix/*"]

If only a head is specified, you can also use the shorthand notation. The following configuration is equivalent to the one above:

feature: "feature/*"
bugfix: ["bugfix/*", "hotfix/*"]

In case you want to assign labels if the PR has a specific base branch, you can specify it in the configuration as follows:

# Apply label "release" if base matches "release/*"
release:
  base: "release/*"

If you specify both, head and base, it will be seen as an AND condition:

# Apply label "🧩 Subtask" if head and base match "feature/*"
🧩 Subtask:
  head: "feature/*"
  base: "feature/*"

Note: If there are multiple rules matching one branch, all of the labels will be added to the PR. One example of this would be a configuration that contains the feature and subtask rules. If a new PR with head and base matching feature/* will be opened, the PR gets the labels feature AND 🧩 Subtask.

pr-branch-labeler's People

Contributors

dependabot[bot] avatar ffittschen avatar matks avatar thecleric avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pr-branch-labeler's Issues

Error: Bad credentials

I am running the labeler WF on an enterprise repo and it gives me the bad credentials error.

While running the same on a personal repo, it works smoothly. Not able to debug the issue.

Workflow :

name: PR Branch Labeler

on: pull_request

jobs:
  label_prs:
    runs-on: [<runner labels>]
    steps:
    - name: Label PRs
      if: github.event.action == 'opened' # Only run the action when the PR was first opened
      uses: ffittschen/pr-branch-labeler@v1
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}

Config :

# Apply label "feature" if head matches "feature/*"
major:
  head: feature/*

# Apply label "bugfix" if head matches "bugfix/*"
minor:
  head: "bugfix/*"

patch:
  head: "hotfix/*"

Cannot get it to add any labels

I don't seem to be able to get this action to add any labels.
Using the following workflow:

on:
  pull_request:
    types: [opened]
name: Add base branch label
jobs:
  base-branch:
    runs-on: ubuntu-latest
    steps:
      - name: Add base branch label
        uses: ffittschen/pr-branch-labeler@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

and the following pr-branch-labeler.yml

development:
  base: 'development'

stage:
  base: 'stage'

master:
  base: 'master'

The workflow is running, but there is no output (tested with both draft and normal PR) base branch was "development".

Unfortunately I am trying this in a private repo, otherwise I would share a link, but here are some screenshots:
image

image

image

Resource not accesible by integration

I have been using this action for a while but suddenly the workflow is crashing when opening a new PR. The error is the following:

Run ffittschen/pr-branch-labeler@v1
Error: Resource not accessible by integration
(node:1[4](https://github.com/****/runs/8230424748?check_suite_focus=true#step:2:5)97) UnhandledPromiseRejectionWarning: HttpError: Resource not accessible by integration
    at /home/runner/work/_actions/ffittschen/pr-branch-labeler/v1/node_modules/@octokit/request/dist-node/index.js:66:23
    at processTicksAndRejections (internal/process/task_queues.js:97:[5](https://github.com/****/runs/8230424748?check_suite_focus=true#step:2:6))
(node:149[7](https://github.com/****/runs/8230424748?check_suite_focus=true#step:2:8)) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1497) [DEP001[8](https://github.com/****/runs/8230424748?check_suite_focus=true#step:2:9)] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It seems to disappear if I just sent a new commit to the opened PR. Any idea?

Create a new release

Hi,

Is it possible to create a new release for this action?
This PR has been merged since v1 and is quite useful.

I tried using master but that fails with this error:
Error: Cannot find module '@actions/core'

I would also prefer to just use a tag instead of master.

Cannot use event pull_request on OSS repositories, GitHub token is read-only

Hello, this GitHub Action works well if you are using the same repository for all contributors.

But if you use it with a OSS repository that require contributors to use their fork, it does not work because the GitHub API token has read-only permissions only, so it cannot add labels to a PR. See reference https://github.community/t/github-actions-are-severely-limited-on-prs/18179

The solution suggested by GitHub is to rely on GA event pull_request_target

In order to solve this, we’ve added a new pull_request_target event, which behaves in an almost identical way to the pull_request event with the same set of filters and payload. However, instead of running against the workflow and code from the merge commit, the event runs against the workflow and code from the base of the pull request. This means the workflow is running from a trusted source and is given access to a read/write token as well as secrets enabling the maintainer to safely comment on or label a pull request. This event can be used in combination with the private repository settings as well.

Does not add tag for matching `base` branch.

I'm trying to make have a release label for any PRs that are to be merged in a release branch (e.g. release/1.0.1). However, this doesn't appear to be working, as the release label is never added. Other labels are added however, such as the chore label, which is done because the head is names chore/some-branch-name.

This is the configuration. After running the action, only the chore label is added to the PR.

Only chore is added.

HEAD: chore/pr-labeler
BASE: release/3.15

Configuration file (.github/pr-branch-labeler.yml);

# Apply label "release" is base matches "release/*"
release:
  base: ["release/**"]

# Apply label "feature" if head matches "feature/*"
feature:
  head: ["feature/*"]

# Apply label "bugfix" if head matches one of "bugfix/*" or "hotfix/*"
bugfix:
  head: ["bugfix/*", "hotfix/*"]

# Apply label "chore" if head matches one of "chore/*" or "maintenance/*"
chore:
  head: ["chore/*", "maintenance/*"]

The version in the README.md (base: "release/*") has also been tried.

Workflow action;

name: PR Preflight

on:
  pull_request:

jobs:
  label_prs:
    runs-on: macOS-11
    steps:
    - name: Label PRs
      uses: ffittschen/pr-branch-labeler@v1
      with:
        repo-token: ${{ github.token }}

Support for dynamic labels based on branches

My team has a need for dynamic labels based on the branch names in the PR.

Specifically, if we are merging into a branch marked release/XXX (such as release/1.0) then we apply the tag XXX (so release/1.0 would get the tag 1.0).

To facilitate this I have added a feature to my fork of this library that is completely backwards compatible, but allows specifying regular expressions with groups, and labels based off those groups.

For example in my use case above, you could create a config like so:

$1:
  baseRegExp: 'release[/](.*)'

This would then attempt to match the base branch of a PR with the above regular expression. If it matches it would apply the first matched group ($1) as the label. So release/1.0 would return 1.0 as the label as needed above.

Here's the release version with the change in it (including unit tests).

https://github.com/TheCleric/pr-branch-labeler/tree/v3.1

If you feel like this would be of benefit to merge into your library, please let me know and I'll create a PR. I also recognize that this may be a little too specific or complex for the goal of this library. Either way is fine with me.

Config not parsed

Hey there,

I've been looking around for a simple action to label my pull-request depending on their base branch.
I'm having an issue setting up this action, here's a few settings I tried:

Screen Shot 2021-03-24 at 1 37 45 pm

Here's the log from the actions

Screen Shot 2021-03-24 at 1 33 45 pm

As you can see, it's only adding "feature" label (which I assumed was an undocumented default config?)

Screen Shot 2021-03-24 at 1 38 03 pm

I've tried to dig in the code a bit, but what I can't explain is why this line doesn't even appear in the action log: https://github.com/ffittschen/pr-branch-labeler/blob/master/src/main.ts#L20

Am I missing something ?

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.