Skip to content

Commit

Permalink
feat: support labels copier (#207)
Browse files Browse the repository at this point in the history
* feat: support labels copier

* docs

* rename

* Update mergify/labels-copier/README.md

* fix and add verbose

* docs

* fix

* improve

* fix

* the opposite

* add exclude

* refactor

* remove additional input for now

* docs

* simplify: gh pr edit allows passing multiple labels

* outputs

* unrequired
  • Loading branch information
v1v authored Jan 9, 2025
1 parent f788485 commit a2467bf
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/test-mergify-labels-copier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: test-mergify-labels-copier

on:
merge_group: ~
workflow_dispatch:
push:
paths:
- '.github/workflows/test-mergify-labels-copier.yml'
- 'mergify/labels-copier/**'

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get token
id: get_token
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a # v2.1.0
with:
app_id: ${{ secrets.OBS_AUTOMATION_APP_ID }}
private_key: ${{ secrets.OBS_AUTOMATION_APP_PEM }}
permissions: >-
{
"pull_requests": "read"
}
- uses: ./mergify/labels-copier
id: validation
with:
github-token: ${{ steps.get_token.outputs.token }}
repository: "elastic/apm-server"
pull-request: "15035"
excluded-labels-regex: "^backport-*"
env:
DRY_RUN: true

- name: assert labels are not empty
run: test -n "${{ steps.validation.outputs.labels }}"
49 changes: 49 additions & 0 deletions mergify/labels-copier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# <!--name-->github/labels-copier<!--/name-->

[![usages](https://img.shields.io/badge/usages-white?logo=githubactions&logoColor=blue)](https://github.com/search?q=elastic%2Foblt-actions%2F2Fmergify%labels-copier+%28path%3A.github%2Fworkflows+OR+path%3A**%2Faction.yml+OR+path%3A**%2Faction.yaml%29&type=code)
[![test-mergify-labels-copier](https://github.com/elastic/oblt-actions/actions/workflows/test-github-comment-reaction.yml/badge.svg?branch=main)](https://github.com/elastic/oblt-actions/actions/workflows/test-mergify-labels-copier.yml)

<!--description-->
copies pull request labels to backported PRs
<!--/description-->

## Inputs
<!--inputs-->
| Name | Description | Required | Default |
|-------------------------|-------------------------------------------------|----------|-------------------------------------------|
| `excluded-labels-regex` | labels to be excluded in regex format | `false` | ` ` |
| `github-token` | The GitHub token to use for API requests | `false` | `${{ github.token }}` |
| `repository` | The GitHub repository to use for API requests | `false` | `${{ github.repository }}` |
| `pull-request` | The GitHub pull-request to use for API requests | `false` | `${{ github.event.pull_request.number }}` |
<!--/inputs-->

## Outputs
<!--outputs-->
| Name | Description |
|----------|-------------------------|
| `labels` | The labels to be copied |
<!--/outputs-->

## Usage

<!--usage action="elastic/oblt-actions/**" version="env:VERSION"-->
```yaml
name: mergify backport labels copier

on:
pull_request_target:
types:
- opened
jobs:
react:
runs-on: ubuntu-latest
if: startsWith(github.head_ref, 'mergify/bp/')
permissions:
pull-requests: write
steps:
- uses: elastic/oblt-actions/mergify/labels-copier@v1
with:
excluded-labels-regex: "^backport-*"
# ...
```
<!--/usage-->
37 changes: 37 additions & 0 deletions mergify/labels-copier/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: 'github/labels-copier'

description: 'copies pull request labels to backported PRs'

inputs:
excluded-labels-regex:
description: 'labels to be excluded in regex format'
required: false
default: ''
github-token:
description: 'The GitHub token to use for API requests'
required: false
default: ${{ github.token }}
repository:
description: 'The GitHub repository to use for API requests'
required: false
default: ${{ github.repository }}
pull-request:
description: 'The GitHub pull-request to use for API requests'
required: false
default: ${{ github.event.pull_request.number }}
outputs:
labels:
description: 'The labels to be copied'
value: ${{ steps.copy-labels.outputs.labels }}
runs:
using: "composite"
steps:
- name: Copying labels
id: copy-labels
shell: bash
env:
REPOSITORY_URL: ${{ github.server_url }}/${{ inputs.repository }}
PR_URL: ${{ github.server_url }}/${{ inputs.repository }}/pull/${{ inputs.pull-request }}
GH_TOKEN: ${{ inputs.github-token}}
EXCLUDED_LABEL: ${{ inputs.excluded-labels-regex }}
run: ${{ github.action_path }}/copy.sh
34 changes: 34 additions & 0 deletions mergify/labels-copier/copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# Environment variables:
# PR_URL
# REPOSITORY_URL
# EXCLUDED_LABEL
# DRY_RUN
# RUNNER_DEBUG
#
# You can run this locally for testing purposes:
#
# $ REPOSITORY_URL=https://github.com/elastic/apm-server PR_URL=https://github.com/elastic/apm-server/pull/15035 EXCLUDED_LABEL=backport-* DRY_RUN=true sh mergify/labels-copier/copy.sh
#

set -eo pipefail

# Support debugging in GitHub actions when RUNNER_DEBUG is set.
if [ -n "$RUNNER_DEBUG" ] ; then
set -x
fi

# Get the PR Number from the body since Mergify uses the PR number as the body.
pr_number=$(gh pr view --json body -q ".body" "$PR_URL" | sed -n -e '/automatic backport of pull request/,/done/p' | cut -d"#" -f2 | cut -d" " -f1)

# Get the labels from the PR and filter out the excluded labels.
labels=$(gh pr view --json labels "${REPOSITORY_URL}/pull/$pr_number" | jq -r --arg regex "$EXCLUDED_LABEL" '.labels | map(select(.name | test($regex) | not)) | map(.name) | join(",")')
if [ -n "$CI" ] ; then
echo "labels=$labels" >> "$GITHUB_OUTPUT"
fi
echo ">> $labels will be added"
if [ "$DRY_RUN" == "true" ]; then
echo ">> DRY_RUN is set, skipping the label addition"
else
gh pr edit --add-label "$labels" "$PR_URL"
fi

0 comments on commit a2467bf

Please sign in to comment.