diff --git a/.github/add_labels.py b/.github/add_labels.py index 0b0a821e6c85..764f90df0c50 100644 --- a/.github/add_labels.py +++ b/.github/add_labels.py @@ -1,5 +1,5 @@ import os, re -from github import Github +from github import Github, GithubException # Format - Key: Array[Label, [StringsToIgnore]] changelogToPrefix = { @@ -39,9 +39,11 @@ } githubLabel = "Github" +missingLogLabel = "Missing Changelog" def get_labels(pr): labels = {} + failed = False files = pr.get_files() for file in files: @@ -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 @@ -68,15 +77,19 @@ 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"]) @@ -84,18 +97,23 @@ def main(): 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() diff --git a/.github/verify_changelog.py b/.github/verify_changelog.py deleted file mode 100644 index 93f399a97c6a..000000000000 --- a/.github/verify_changelog.py +++ /dev/null @@ -1,78 +0,0 @@ -import os, re -from enum import Enum -from github import Github - -# 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"]], - 'soundadd': ["Sound", ["added a new sound thingy"]], - 'sounddel': ["Sound", ["removed an old sound thingy"]], - 'imageadd': ["Sprites", ["added some icons and images"]], - 'imagedel': ["Sprites", ["deleted some icons and images"]], - 'mapadd': ["Mapping", ["added a new map or section to a map"]], - 'maptweak': ["Mapping", ["tweaked a map"]], - 'ui' : ["UI", ["changed something relating to user interfaces"]] -} - -class Result(Enum): - INVALID = -1 - VALID = 0 - MISSING = 1 - -def validate_changelog(pr): - 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 Result.MISSING - - lines = changelog_match.group(2).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: # some key that we didn't expect - print("Invalid changelog entry: " + line) - return Result.INVALID - - if content in changelogToPrefix[key][1]: # They left the template entry in - print("Invalid changelog entry: " + line) - return Result.INVALID - - return Result.VALID - -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.") - return Result.INVALID - - result = validate_changelog(pr) - - if result == Result.MISSING: - print("No changelog detected.") - - return result - - -if __name__ == '__main__': - main() diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 8a688f207f11..4448e0861a2a 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 @@ -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 diff --git a/.github/workflows/verify_changelog.yml b/.github/workflows/verify_changelog.yml deleted file mode 100644 index f53acf7dbf26..000000000000 --- a/.github/workflows/verify_changelog.yml +++ /dev/null @@ -1,49 +0,0 @@ -name: Changelog Verification -on: - pull_request_target: - types: [edited] -jobs: - verify: - runs-on: ubuntu-latest - steps: - - name: "Check for ACTION_ENABLER secret and pass true to output if it exists to be checked by later steps" - id: value_holder - env: - ENABLER_SECRET: ${{ secrets.ACTION_ENABLER }} - run: | - unset SECRET_EXISTS - if [ -n "$ENABLER_SECRET" ]; then SECRET_EXISTS=true ; fi - echo "::set-output name=ACTIONS_ENABLED::$SECRET_EXISTS" - - name: Get The Script - if: steps.value_holder.outputs.ACTIONS_ENABLED - run: | - wget "https://raw.githubusercontent.com/${{ github.repository }}/master/.github/verify_changelog.py" - - name: Set up Python - if: steps.value_holder.outputs.ACTIONS_ENABLED - uses: actions/setup-python@v3 - with: - python-version: 3.8 - - name: Install dependencies - if: steps.value_holder.outputs.ACTIONS_ENABLED - run: | - python -m pip install --upgrade pip - python -m pip install pygithub - sudo apt-get install dos2unix - - name: Verify Changelog - id: verify_holder - if: steps.value_holder.outputs.ACTIONS_ENABLED - run: | - output=$(python verify_changelog.py) - echo "::set-output name=VERIFY_RESULT::$output" - env: - REPO: ${{ github.repository }} - TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.number }} - - name: Note Missing Changelog - if: steps.value_holder.outputs.ACTIONS_ENABLED && steps.verify_holder.outputs.VERIFY_RESULT == 1 - run: | - echo "::notice ::No changelog detected" - - name: Note Invalid Changelog - if: steps.value_holder.outputs.ACTIONS_ENABLED && steps.verify_holder.outputs.VERIFY_RESULT == -1 - run: | - echo "::error ::Changelog is invalid"