Skip to content

Commit

Permalink
Ensure Backporting labels are adding when triggering merge
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Jan 10, 2025
1 parent 7c09c8f commit 8ee5958
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/actions/ensure_backporting/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: ensure backport labels
description: |
A Github action to ensure that a Pull request contains backport labels.
inputs:
token:
description: The Github token to use to perform operations.
required: true

runs:
using: composite
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install required python libraries
run: pip install -U requests
shell: bash

- name: Run
run: python ${{ github.action_path }}/add_labels.py --repository ${{ github.repository }} --issue-id ${{ github.event.number }}
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.token }}
68 changes: 68 additions & 0 deletions .github/actions/ensure_backporting/add_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3

import argparse
import os
import re
import sys

import requests


class RequestError(Exception):
pass


def main():
parser = argparse.ArgumentParser(
description="Ensure an issue contains the backport labels."
)
parser.add_argument(
"--issue-id", type=int, required=True, help="The pull request number."
)
parser.add_argument(
"--repository",
type=str,
required=True,
help="The Github project name, e.g: 'ansible-collections/amazon.aws'.",
)
args = parser.parse_args(sys.argv[1:])
# Get list of labels attached to the pull requests
headers = {
"Accept": "application/vnd.github+json",
"Authorization": "Bearer {0}".format(os.environ.get("GITHUB_TOKEN")),
"X-GitHub-Api-Version": "2022-11-28",
}

# Get PR Labels
response = requests.get(
f"https://api.github.com/repos/{args.repository}/issues/{args.issue_id}/labels",
headers=headers,
)
if not response.ok:
raise RequestError(response.reason)
issue_backport_labels = [i['name'] for i in response.json() if re.match("^backport-[0-9]*$", i["name"])]

# Get Repository Labels
response = requests.get(
f"https://api.github.com/repos/{args.repository}/labels",
headers=headers,
)
if not response.ok:
raise RequestError(response.reason)
repository_backport_labels = [i['name'] for i in response.json() if re.match("^backport-[0-9]*$", i["name"])]

labels_to_add = list(set(repository_backport_labels) - set(issue_backport_labels))
print(f"issue_backport_labels = {issue_backport_labels} - repository_backport_labels = {repository_backport_labels} - labels_to_add = {labels_to_add}")
if labels_to_add:
data = {"labels": labels_to_add}
response = requests.post(
f"https://api.github.com/repos/{args.repository}/issues/{args.issue_id}/labels",
headers=headers,
json=data,
)
if not response.ok:
raise RequestError(response.reason)


if __name__ == "__main__":
main()
15 changes: 15 additions & 0 deletions .github/workflows/ensure_backporting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Ensure Backporting
on:
workflow_call:
secrets:
GH_TOKEN:
required: false

jobs:
labelling:
runs-on: ubuntu-latest
if: ${{ contains(github.event.pull_request.labels.*.name, 'mergeit') && !contains(github.event.pull_request.labels.*.name, 'do_not_backport') }}
steps:
- uses: abikouo/github_actions/.github/actions/ensure_backporting@ensure_backporting_1
with:
token: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }}

0 comments on commit 8ee5958

Please sign in to comment.