-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move pr labeler to separate repository
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
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |