From 8f5604b2fe4916f06fa9aa8505031d3dc01d4301 Mon Sep 17 00:00:00 2001 From: Drathek <76988376+Drulikar@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:59:45 -0700 Subject: [PATCH] Verify Changelog Workflow (#6142) # About the pull request Malformed changelogs have been getting more frequent. This PR does the following: - Updates the labeler workflow to activate on edits and use github warning labeling for output - Adds changelog verification checking to the labeler workflow: If a changelog is missing it will pass but be labeled with "Missing Changelog", otherwise it will fail if its malformed. - Fixes the depreciation warning "Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files." Testing performed in a separate repo. --- .github/add_labels.py | 158 +++++++++++++++++++--------------- .github/workflows/labeler.yml | 8 +- 2 files changed, 92 insertions(+), 74 deletions(-) diff --git a/.github/add_labels.py b/.github/add_labels.py index 3e903c362d82..764f90df0c50 100644 --- a/.github/add_labels.py +++ b/.github/add_labels.py @@ -1,19 +1,19 @@ import os, re -from github import Github +from github import Github, GithubException # Format - Key: Array[Label, [StringsToIgnore]] changelogToPrefix = { - 'fix': ["Fix", ["fixed a few things"]], - 'qol': ["Quality of Life", ["made something easier to use"]], - 'add': ["Feature", ["Added new mechanics or gameplay changes", "Added more things"]], - 'del': ["Removal", ["Removed old things"]], - 'spellcheck': ["Grammar and Formatting", ["fixed a few typos"]], - 'balance': ["Balance", ["rebalanced something"]], - 'code': ["Code Improvement", ["changed some code"]], - 'refactor': ["Refactor", ["refactored some code"]], - 'config': ["Config", ["changed some config setting"]], - 'admin': ["Admin", ["messed with admin stuff"]], - 'server': ["Server", ["something server ops should know"]], + 'fix': ["Fix", ["fixed a few things"]], + 'qol': ["Quality of Life", ["made something easier to use"]], + 'add': ["Feature", ["Added new mechanics or gameplay changes", "Added more things"]], + 'del': ["Removal", ["Removed old things"]], + 'spellcheck': ["Grammar and Formatting", ["fixed a few typos"]], + 'balance': ["Balance", ["rebalanced something"]], + 'code': ["Code Improvement", ["changed some code"]], + 'refactor': ["Refactor", ["refactored some code"]], + 'config': ["Config", ["changed some config setting"]], + 'admin': ["Admin", ["messed with admin stuff"]], + 'server': ["Server", ["something server ops should know"]], 'soundadd': ["Sound", ["added a new sound thingy"]], 'sounddel': ["Sound", ["removed an old sound thingy"]], 'imageadd': ["Sprites", ["added some icons and images"]], @@ -24,78 +24,96 @@ } fileToPrefix = { - 'wav': 'Sound', - 'ogg': 'Sound', + 'wav': 'Sound', + 'ogg': 'Sound', 'mp3': 'Sound', ## Can't believe they forgot about the best sound format - 'dmm': 'Mapping', + 'dmm': 'Mapping', - 'js': 'UI', - 'tsx': 'UI', - 'ts': 'UI', - 'jsx': 'UI', - 'scss': 'UI', + 'js': 'UI', + 'tsx': 'UI', + 'ts': 'UI', + 'jsx': 'UI', + 'scss': 'UI', - 'dmi': "Sprites", + 'dmi': "Sprites", } githubLabel = "Github" +missingLogLabel = "Missing Changelog" def get_labels(pr): - labels = {} - - files = pr.get_files() - for file in files: - prefix = file.filename.split(".")[-1] - if file.filename.startswith(".github"): - labels[githubLabel] = True - if not prefix in fileToPrefix: - continue - labels[fileToPrefix[prefix]] = True - - changelog_match = re.search(r"🆑(.*)/🆑", pr.body, re.S | re.M) - 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 - lines = changelog_match.group(1).split('\n') - for line in lines: - line = line.strip() - if not line: - continue - - contentSplit = line.split(":") - - key = contentSplit.pop(0).strip() - content = ":".join(contentSplit).strip() - - if not key in changelogToPrefix: - continue - - if content in changelogToPrefix[key][1]: - continue - - labels[changelogToPrefix[key][0]] = True - - return list(labels) + labels = {} + failed = False + + files = pr.get_files() + for file in files: + prefix = file.filename.split(".")[-1] + if file.filename.startswith(".github"): + labels[githubLabel] = True + if not prefix in fileToPrefix: + continue + labels[fileToPrefix[prefix]] = True + + changelog_match = re.search(r"🆑(.*)/🆑", pr.body, re.S | re.M) + if changelog_match is None: + changelog_match = re.search(r":cl:(.*)/:cl:", pr.body, re.S | re.M) + if changelog_match is None: + print("::warning ::No changelog detected.") + labels[missingLogLabel] = True + return labels, False + + lines = changelog_match.group(1).split('\n') + 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 + + contentSplit = line.split(":") + + key = contentSplit.pop(0).strip() + content = ":".join(contentSplit).strip() + + 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]: # They left the template entry in + print(f"::error ::Invalid changelog entry: {line}") + failed = True + continue + + labels[changelogToPrefix[key][0]] = True + + return list(labels), failed def main(): - g = Github(os.environ["TOKEN"]) - repo = g.get_repo(os.environ['REPO']) + 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.") - return + pr = repo.get_pull(int(os.environ["PR_NUMBER"])) + if not 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) + for label in labels: + pr.add_to_labels(label) + if failed: + exit(1) if __name__ == '__main__': - main() + main() diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 8a688f207f11..cdf7ce11784e 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -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 @@ -13,7 +13,7 @@ jobs: run: | unset SECRET_EXISTS if [ -n "$ENABLER_SECRET" ]; then SECRET_EXISTS=true ; fi - echo "::set-output name=ACTIONS_ENABLED::$SECRET_EXISTS" + echo "ACTIONS_ENABLED=$SECRET_EXISTS" >> $GITHUB_OUTPUT - name: Get The Script if: steps.value_holder.outputs.ACTIONS_ENABLED run: | @@ -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