From 8f5a28704ae17eb817f8871d5d87789caee70e91 Mon Sep 17 00:00:00 2001 From: Dynesshely Date: Sun, 31 Mar 2024 15:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Chore(CI):=20Enable=20to=20forma?= =?UTF-8?q?t=20pr=20title?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/scripts/check_new_commit.py | 33 +++++++++++++++ .github/scripts/pr_format.py | 61 ++++++++++++++++++++++++++++ .github/workflows/pr-auto-format.yml | 37 +++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 .github/scripts/check_new_commit.py create mode 100644 .github/scripts/pr_format.py create mode 100644 .github/workflows/pr-auto-format.yml diff --git a/.github/scripts/check_new_commit.py b/.github/scripts/check_new_commit.py new file mode 100644 index 0000000..cd57ed6 --- /dev/null +++ b/.github/scripts/check_new_commit.py @@ -0,0 +1,33 @@ +import sys +import os +from datetime import datetime, timedelta, timezone + +current_utc_time = datetime.utcnow() +current_utc_time = current_utc_time.replace(tzinfo=timezone.utc) + +print("Args list: ", sys.argv) + +time_str = sys.argv[1] +days_deference = int(sys.argv[2]) + +datetime_obj = datetime.fromisoformat(time_str) +datetime_obj = datetime_obj.replace(tzinfo=timezone.utc) + +time_difference = current_utc_time - datetime_obj + +print("Time delta: ", time_difference) + +has_new_commit = time_difference > timedelta(days=days_deference) +env_to_add = "HAS_NEW_COMMIT=" + +if time_difference > timedelta(days=days_deference): + print("No new commit found. Check in env var: [env.HAS_NEW_COMMIT].") + env_to_add = env_to_add + 'false' +else: + print("New commit found. Check in env var: [env.HAS_NEW_COMMIT].") + env_to_add = env_to_add + 'true' + +command = "echo \"" + env_to_add + "\" >> $env:GITHUB_ENV" + +with open('set_env.ps1', 'w', encoding='utf-8') as file: + file.write(command) diff --git a/.github/scripts/pr_format.py b/.github/scripts/pr_format.py new file mode 100644 index 0000000..f74ed24 --- /dev/null +++ b/.github/scripts/pr_format.py @@ -0,0 +1,61 @@ +""" +Format the pull request title to the standard format. +""" + +import sys +import re + +# Chinese replacement. +replacemap = { + # Brackets. + "(": "(", + ")": ")", + "『": "[", + "』": "]", + "「": "[", + "」": "]", + "【": "[", + "】": "]", + "〔": "{", + "〕": "}", + # Quotation marks. + "‘": "'", + "’": "'", + "“": '"', + "”": '"', + ",": ", ", + "。": ". ", + ";": "; ", + ":": ": ", + "?": "? ", + "!": "! ", + "、": ", ", + "…": "...", + "—": "-", + "·": ".", + "~": "~", +} + + +HEAD = "[Pull Request]" +FORMAT_REGEX = r"^(\[?[pP][rR]\]?|\[?[pP][uU][lL][lL][- _]?[rR][eE][qQ][uU][eE][sS][tT]\]?|\[?[pP][uU][lL][lL]\]?)([^\n]*)$" # pylint: disable=line-too-long + +if __name__ == "__main__": + if len(sys.argv) < 2: + print(f"Usage: python {sys.argv[0]} ") + sys.exit(1) + title = sys.argv[1] + + # Step 1: Convert Chinese to English. + for key, value in replacemap.items(): + title = title.replace(key, value) + + # Step 2: Check and update the name of PR. + if not title.startswith(HEAD): + result = re.match(FORMAT_REGEX, title, re.M | re.I) + if result: + title = f"{HEAD} {result.group(2).strip()}" + else: + title = f"{HEAD} {title.strip()}" + + print(title) diff --git a/.github/workflows/pr-auto-format.yml b/.github/workflows/pr-auto-format.yml new file mode 100644 index 0000000..c13790c --- /dev/null +++ b/.github/workflows/pr-auto-format.yml @@ -0,0 +1,37 @@ +name: Format Pull Request Title + +on: + pull_request: + types: + - opened + +jobs: + check_title: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Comment on Pull Request + run: | + pull_request_number=${{ github.event.pull_request.number }} + current_title="${{ github.event.pull_request.title }}" + message_body="The origin title of this PR is: '$current_title'. GitHub Action is checking and format it ..." + + curl -H "Authorization: Bearer ${{ secrets.GITHUBTOKEN }}" \ + -H "Content-Type: application/json" \ + -X POST \ + -d '{"body":"'"$message_body"'"}' \ + "https://api.github.com/repos/${{ github.repository }}/issues/$pull_request_number/comments" + + - name: Update Pull Request Title + run: | + pull_request_number=${{ github.event.pull_request.number }} + current_title="${{ github.event.pull_request.title }}" + updated_title=$(python3 "./.github/scripts/pr_format.py" "$current_title") + + curl -X PATCH \ + -H "Authorization: Bearer ${{ secrets.GITHUBTOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + -d '{ "title": "'"$updated_title"'" }' \ + "https://api.github.com/repos/${{ github.repository }}/pulls/$pull_request_number"