Skip to content

Commit

Permalink
Integrate into labeler instead
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
Drulikar committed Apr 14, 2024
1 parent 7bceb6d commit 9fa3438
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 141 deletions.
40 changes: 29 additions & 11 deletions .github/add_labels.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os, re
from github import Github
from github import Github, GithubException

# Format - Key: Array[Label, [StringsToIgnore]]
changelogToPrefix = {
Expand Down Expand Up @@ -39,9 +39,11 @@
}

githubLabel = "Github"
missingLogLabel = "Missing Changelog"

def get_labels(pr):
labels = {}
failed = False

files = pr.get_files()
for file in files:
Expand All @@ -56,9 +58,16 @@ def get_labels(pr):
if changelog_match is None:
changelog_match = re.search(r":cl:(.*)/:cl:", pr.body, re.S | re.M)
if changelog_match is None:
return labels
print("::warning ::No changelog detected.")
labels[missingLogLabel] = True
return labels, False

lines = changelog_match.group(1).split('\n')
for line in lines:
failed = len(lines) <= 2 # Make sure its not an empty changelog
if failed:
print("::error ::Empty changelog.")

for line in lines[1:-1]: # Skip first line with authors and last
line = line.strip()
if not line:
continue
Expand All @@ -68,34 +77,43 @@ def get_labels(pr):
key = contentSplit.pop(0).strip()
content = ":".join(contentSplit).strip()

if not key in changelogToPrefix:
if not key in changelogToPrefix: # Some key that we didn't expect
print(f"::error ::Invalid changelog entry: {line}")
failed = True
continue

if content in changelogToPrefix[key][1]:
if content in changelogToPrefix[key][1]: # They left the template entry in
print(f"::error ::Invalid changelog entry: {line}")
failed = True
continue

labels[changelogToPrefix[key][0]] = True

return list(labels)
return list(labels), failed

def main():
g = Github(os.environ["TOKEN"])
repo = g.get_repo(os.environ['REPO'])

pr = repo.get_pull(int(os.environ["PR_NUMBER"]))
if not pr:
print("Not a PR.")
print("::warning ::Not a PR.")
return

labels = get_labels(pr)
labels, failed = get_labels(pr)

if labels is None: # no labels to add
print("No labels to add.")
return
if not missingLogLabel in labels:
try:
pr.remove_from_labels(missingLogLabel)
except GithubException as e:
if e.status == 404:
pass # 404 if we try to remove a label that isn't set

for label in labels:
pr.add_to_labels(label)

if failed:
exit(1)

if __name__ == '__main__':
main()
78 changes: 0 additions & 78 deletions .github/verify_changelog.py

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Labeling
name: Labeling and Verification
on:
pull_request_target:
types: [opened]
types: [opened, edited]
jobs:
label:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -29,7 +29,7 @@ jobs:
python -m pip install --upgrade pip
python -m pip install pygithub
sudo apt-get install dos2unix
- name: Add Labels
- name: Add and verify labels
if: steps.value_holder.outputs.ACTIONS_ENABLED
run: |
python add_labels.py
Expand Down
49 changes: 0 additions & 49 deletions .github/workflows/verify_changelog.yml

This file was deleted.

0 comments on commit 9fa3438

Please sign in to comment.