Skip to content

Commit

Permalink
Change label update and delete to script
Browse files Browse the repository at this point in the history
  • Loading branch information
codycooperross committed Jul 26, 2024
1 parent ba49dc8 commit c825744
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
48 changes: 48 additions & 0 deletions .github/scripts/manage_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests
import os
import re

GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
GH_API_URL = "https://api.github.com/"
DEST_REPO = os.environ.get("DEST_REPO")
GH_REPO = os.environ.get("GH_REPO")
NAME = os.environ.get("NAME")
NEW_NAME = os.environ.get("NEW_NAME")
EVENT_TYPE = os.environ.get("EVENT_TYPE")

def get_label_in_repo(label_name, repo):
response = requests.get(GH_API_URL + "repos/" + repo + "/labels/" + label_name, headers={"Authorization": "bearer " + GITHUB_TOKEN})
if response.status_code == 200:
return response.json()
else:
print(f"{label_name} not found in {repo}")
return {}

def update_label_in_dest_repo(label_name, local_label):
payload = {"new_name": local_label.get("name"), "color": local_label.get("color"), "description": local_label.get("description")}
response = requests.patch(GH_API_URL + "repos/" + DEST_REPO + "/labels/" + label_name, headers={"Authorization": "bearer " + GITHUB_TOKEN}, json=payload)
if response.status_code == 200:
print("Success")
else:
print("Failed")

def delete_label_in_dest_repo(label_name):
response = requests.delete(GH_API_URL + "repos/" + DEST_REPO + "/labels/" + label_name, headers={"Authorization": "bearer " + GITHUB_TOKEN})
if response.status_code == 204:
print("Success")
else:
print("Failed")

if EVENT_TYPE == "edited":
dest_label = get_label_in_repo(NAME, DEST_REPO)
local_label = get_label_in_repo(NEW_NAME, GH_REPO)

changed = dest_label.get("name") != local_label.get("name") or dest_label.get("color") != local_label.get("color") or dest_label.get("description") != local_label.get("description")

if dest_label and changed:
update_label_in_dest_repo(NAME, local_label)

if EVENT_TYPE == "deleted":
label_name = NAME
if label_exists_in_dest_repo(label_name):
delete_label_in_dest_repo(label_name)
16 changes: 0 additions & 16 deletions .github/workflows/delete_label.yml

This file was deleted.

30 changes: 14 additions & 16 deletions .github/workflows/update_label.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
name: Update labels
on:
label:
types: edited
types: [edited, deleted]
jobs:
update_labels:
update-front:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- run: |
response=$(gh label edit "$NAME" -n "$NEW_NAME" -c $NEW_COLOR -d "$NEW_DESCRIPTION" -R $DEST_REPO 2>&1)
exit_code=$?
- name: Checkout repository
uses: actions/checkout@v3

echo $response
echo $exit_code
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

if [[ "$response" == *"404"* ]] || [[ $exit_code != 1 ]]; then
echo "Success"
exit 0
else
echo "Failure"
exit 1
fi
- name: Install dependencies
run: |
python -m pip install requests
- name: Manage other labels
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
GH_REPO: ${{ github.repository }}
Expand All @@ -30,3 +27,4 @@ jobs:
NEW_COLOR: "${{ github.event.label.color }}"
NEW_DESCRIPTION: "${{ github.event.label.description }}"
DEST_REPO: ${{ vars.DEST_REPO }}
run: python .github/scripts/manage_labels.py

0 comments on commit c825744

Please sign in to comment.