Skip to content

GitHub Action to automatically label PRs with merge conflicts

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
COPYING
Notifications You must be signed in to change notification settings

prince-chrismc/label-merge-conflicts-action

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Label Pull Requests with Merge Conflicts Automatically

MIT codecov

Purpose

This action intuitively checks open pull request(s) for merge conflicts and marks them with a label and optionally leaves a comment to alter the author.

comic

Work by Geek & Poke: Being A Coder Made Easy (CC BY 3.0) just shorter.

Add it to Your Project

Create a Label

You'll need to decide on a label and manually create it if it does not already exist.

Setup a Workflow

name: Auto Label Conflicts
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  auto-label:
    runs-on: ubuntu-latest
    steps:
      - uses: prince-chrismc/label-merge-conflicts-action@v3
        with:
          conflict_label_name: "has conflict"
          github_token: ${{ github.token }}

          # --- Optional Inputs ---
          # To make sure the merge commit exactly matches the branch
          detect_merge_changes: false # or true to handle as conflicts
          # By default a comment will be left, adding `conflict_comment: ''` will disable comments
          # The optional `${author}` will be replaced with the username of the pull request
          conflict_comment: |
            :wave: Hi, @${author},
            I detected conflicts against the base branch :speak_no_evil:
            You'll want to sync :arrows_counterclockwise: your branch with upstream!

Limitations

  1. GitHub does not reliably compute the mergeable status which is used by this action to detect merge conflicts. If the base branch (e.g. main) changes then the mergeable status is unknown until someone (like this action) requests it. GitHub then tries to compute the status with an async job.. This is usually quick and simple, but there are no guarantees and GitHub might have issues. You can tweak max_retries and wait_ms to increase the timeout before giving up on the Pull Request.
  2. GitHub does not run actions on pull requests which have conflicts - which will prevent this action from working. When there is a conflict it prevents the merge commit from being calculated. See this thread for more details. This is required for the mergeable as per the API documentation.

FAQ - What are Merge Changes?

When merging a pull request, no matter the strategy, there may inadvertently be changes (i.e git blobs which over lap and need to be recombined) which can have negative side effects. For example...

I was working on an app with a friend and [...] I ran git pull. There were no merge conflicts, but git added duplicate functions to a file after merge. I spent an hour trying to figure our what the problem was before realizing that git had made a mistake while merging. ref

FAQ - How do I fix "Resource not accessible by integration"?

ℹ️ This is an evolving topic. Feel free to open an issue if there's any problems.

If a user without write access opens a 'Pull Request' from their fork then GitHub will not grant adequate permissions to set the labels (detailed here). Hence the not accessible error. Try the following steps:

  • using the (potentially dangerous) event pull_request_target

  • setting the workflow permissions by adding the minimum permission required to add labels or comments

    permissions:
      issues: write
      pull-requests: write

It boils down to the GitHub Action's permissions current behavior. The default permissions for any event type is read only. The default can be adjusted for the repository or set for each workflow explicitly. However, as per the documentation...

Pull requests from public forks are still considered a special case and will receive a read token regardless of these settings.

See actions/labeler#12, actions/first-interaction#10, and Actions are severely limited for more information and some history on the issue.