Skip to content

marhali/gitlab-mr-labeler

Repository files navigation

gitlab-mr-labeler

Github Release Github Actions Workflow Status Codecov Docker Stars Docker Pulls Static Badge Donate

Solves the problem of missing automation of labeling merge requests in GitLab. Based on merge request pipelines, labels are automatically assigned using various configuration options.

Features

  • Dedicated JSON configuration file to configure all your labels
  • Supports regular expressions (RegExp with JS Flavor) to match the suitable labels
  • Label assignment method can be either APPEND or OVERRIDE
  • Only uses the official GitLab API
  • Works efficiently and saves resources (fast & lightweight)
  • Supports any GitLab instance (On-Premise & Cloud)

Builtin variants

The following configuration variants are currently supported.

Git Diff Paths

Assigns labels based on the paths of the changed files. Compares the git diff between the source and target branch of the specific merge request.

Git Log Messages

Assigns labels based on the commit messages of the made changes. Compares the git log between the source and target branch of the specific merge request.

Usage

Getting started

In order to use gitlab-mr-labeler you must follow these steps:

  1. Create a Project Access Token in GitLab
  2. Add a dedicated JSON configuration file
  3. Configure a CI job that runs on every merge request

You need ~ 7 minutes to get your initial setup up and running 🚀.

Create a Project Access Token in GitLab

This tool needs a PAT in order to be able to assign labels on merge requests. Just follow the steps as described in the GitLab documentation. See Create a project access token.

Make sure to set the following variables:

  • Select a role: Developer
  • Select scopes: api

Add your newly created token as an CI/CD variable. See Define a CI/CD variable in the UI.

Add a dedicated JSON configuration file

In order to configure which labels should be applied you need to provide a json configuration file. You can place the configuration file anywhere in your project repository. This file will be referenced when the gitlab-mr-labeler CLI is called.

You must define the following properties. If you want to disable any label matcher just set the equivalent property to an empty object ({}).

  • assignMethod: Can be either APPEND or OVERRIDE
  • gitLogMessages: Object of label keys and an array value of matchable regex strings
  • gitDiffPaths: Object of label keys and an array value of matchable regex strings

Regular expressions must be defined using the JavaScript flavor. The format must therefore correspond to the following pattern: /PATTERN/OPTIONAL_FLAGS (regular expression literal). For most use cases you should always supply the m flag to enable multi-line matching.

// .gitlab/gitlab-mr-labeler.config.json

{
  "assignMethod": "APPEND",
  "gitLogMessages": {
    "feature": ["/^feat/mi"]
  },
  "gitDiffPaths": {
    "dependencies": ["/package\.json$/m"]
  }
}

Check out the predefined examples to find a good starting point to label your merge requests 🔥.

Configure a CI job that runs on every merge request

To automatically determine and assign the appropriate labels you need to define a merge request pipeline job that executes the gitlab-mr-labeler CLI with your created project access token and the relative path to your json configuration file.

# .gitlab-ci.yml

stages:
  - prepare

gitlab_mr_labeler:
  stage: prepare
  image: docker.io/marhali/gitlab-mr-labeler:latest
  script: gitlab-mr-labeler $MY_PROCESS_ACCESS_TOKEN .gitlab/gitlab-mr-labeler.config.json
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Marcel Haßlinger - @marhali_de - Portfolio Website

Project Link: https://github.com/marhali/gitlab-mr-labeler

Donation

If this tool helps you to reduce development time, you can give me a cup of coffee :)

About

Automation of label assignment for GitLab merge requests.

Topics

Resources

License

Stars

Watchers

Forks