Skip to content

Commit

Permalink
move pr labeler to separate repository
Browse files Browse the repository at this point in the history
The YAML and Python script originated from the OMPI repository (diverged here:
open-mpi/ompi@ef866a4)

An OMPI maintainer wanted this and the git commit checker in two of their other
repos. Instead of copying the code to their repo(s), it is far simpler to move
the actions to separate repos and publish them on the GitHub marketplace.

The functionality remains the same, but the YAML had to be tweaked to work as a
reusable (marketplace) action. These changes are:

* add / change the action name, description, branding, and inputs
* remove the trigger
* remove permissions
* specify composite action (runs:)
* remove os specification
* remove code checkout (it wasn't necessary for the labeler in the first place)
* specify shell for each command run
* use github.action_path instead of the env variable GITHUB_WORKSPACE

Signed-off-by: Joe Downs <joe@dwns.dev>
  • Loading branch information
Joe-Downs committed Sep 20, 2022
1 parent 98c7d1e commit 186f0fb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Pull Request Labeler
A minor action which assigns labels to pull requests based off of the base
branch. For instance, if a pull request is being made into `main`, then it will
assign the label `Target: main` to the pull request. If the label corresponding
to the base branch does not exist, then the label will neither be created nor
applied.


*Uses [SemVer 2.0.0](https://semver.org/spec/v2.0.0.html)*
28 changes: 28 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Pull Request Labeler
description: Labels pull requests based on the base branch
branding:
icon: tag
color: blue
inputs:
token:
description: The GITHUB_TOKEN secret
required: true

runs:
using: composite
steps:
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Get the PyGithub module
run: pip install PyGithub
shell: bash

- name: Label the PR (if needed)
run: ${{ github.action_path }}/github-labeler.py
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.token }}
PR_NUM: ${{ github.event.number }}
59 changes: 59 additions & 0 deletions github-labeler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3

"""
The script applies a label in the form "Target: {branchName}. If necessary, it
removes labels in the same form, but NOT for the target branch. For instance, if
someone edited the target branch from v4.0.x to v5.0.x
"""

import os
import re
import sys

from github import Github

# ==============================================================================

GITHUB_BASE_REF = os.environ.get('GITHUB_BASE_REF')
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')
GITHUB_REPOSITORY = os.environ.get('GITHUB_REPOSITORY')
PR_NUM = os.environ.get('PR_NUM')

# Sanity check
if (GITHUB_BASE_REF is None or
GITHUB_TOKEN is None or
GITHUB_REPOSITORY is None or
PR_NUM is None):
print("Error: this script is designed to run as a Github Action")
exit(1)

# ==============================================================================

# Given a pullRequest object, the function checks what labels are currently on
# the pull request, removes any matching the form "Target: {branch}" (if
# {branch} is not the current target branch), and adds the correct label.
def ensureLabels(pullRequest):
needsLabel = True
targetPrefix = "Target: "
targetLabel = f"{targetPrefix}{GITHUB_BASE_REF}"
for label in pullRequest.get_labels():
if label.name.startswith(targetPrefix):
if label.name == targetLabel:
needsLabel = False
else:
print(f"Removing label '{label.name}")
pullRequest.remove_from_labels(label)
if needsLabel:
print(f"Adding label '{targetLabel}")
pullRequest.add_to_labels(targetLabel)
return None

# ==============================================================================

g = Github(GITHUB_TOKEN)
repo = g.get_repo(GITHUB_REPOSITORY)
prNum = int(PR_NUM)
pr = repo.get_pull(prNum)
ensureLabels(pr)

0 comments on commit 186f0fb

Please sign in to comment.