From a74ce4a19f845916d1490e0cde3dce279766cdd9 Mon Sep 17 00:00:00 2001 From: ChenPi11 Date: Sun, 1 Oct 2023 22:11:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=92=BE=20Feat:=20Add=20PR=20autorenam?= =?UTF-8?q?e=20Action.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新文件: .github/pr_autorename.py 新文件: .github/workflows/pr-autorename.yml --- .github/pr_autorename.py | 51 +++++++++++++++++++++++++++++ .github/workflows/pr-autorename.yml | 40 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100755 .github/pr_autorename.py create mode 100644 .github/workflows/pr-autorename.yml diff --git a/.github/pr_autorename.py b/.github/pr_autorename.py new file mode 100755 index 0000000..1ae811a --- /dev/null +++ b/.github/pr_autorename.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +"""Update the name of PR. +""" +import sys +def check(title:str): + """Check and update the name of PR. + + Args: + title (str): _description_ + + Returns: + _type_: _description_ + """ + + title = title.strip().replace("【", "[") + if(not title.startswith("[Pull Request]")): + temp = title.replace("(", "").replace("{", "").replace("[", "") \ + .replace(")", "").replace("}", "").replace("]", "") \ + .replace(" ", "").replace("_", "").replace("-", "") \ + .lower().replace("ull", "").replace("equest", "") + if(temp.startswith("pr")): + temp = title.lower().replace("(", "[").replace("{", "[").replace(")", "]").replace("}", "]").replace("-", "_").replace(" ", "_") + content = "" + # 1. '[pull_request]: ' + if(temp.startswith("[pull_request]:")): + content = title[len("[pull_request]:"):].strip() + # 2. '[pull request] <title>' + elif(temp.startswith("[pull_request]")): + content = title[len("[pull_request]"):].strip() + + # 3. '[pr]: <title>' + elif(temp.startswith("[pr]:")): + content = title[len("[pr]:"):].strip() + # 4. '[pr] <title>' + elif(temp.startswith("[pr]")): + content = title[len("[pr]"):].strip() + + # 5. 'pull request: <title>' + elif(temp.startswith("pull_request:")): + content = title[len("pull_request:"):].strip() + # 5. 'pr: <title>' + elif(temp.startswith("pr:")): + content = title[len("pr:"):].strip() + else: + content = title + title = "[Pull Request] " + content + else: + title = "[Pull Request] " + title + return title + +print(check(sys.argv[1])) diff --git a/.github/workflows/pr-autorename.yml b/.github/workflows/pr-autorename.yml new file mode 100644 index 0000000..682387f --- /dev/null +++ b/.github/workflows/pr-autorename.yml @@ -0,0 +1,40 @@ +name: Check Pull Request Title + +on: + pull_request: + types: + - opened + +jobs: + check_title: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Comment on Pull Request + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + 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 it..." + curl -H "Authorization: Bearer ${GITHUB_TOKEN}" \ + -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/pr_autorename.py" "$current_title") + token=${{ secrets.GITHUB_TOKEN }} + curl -X PATCH \ + -H "Authorization: Bearer $token" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/${{ github.repository }}/pulls/$pull_request_number" \ + -d '{ "title": "'"$updated_title"'" }' From 53549b91063917dfb28af493e87102f653d6feb6 Mon Sep 17 00:00:00 2001 From: ChenPi11 <102936596+ChenPi11@users.noreply.github.com> Date: Sun, 1 Oct 2023 22:36:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A7=20Fix:=20Fix=20a=20bug=20in=20?= =?UTF-8?q?`.github/pr=5Fautorename.py`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改: .github/pr_autorename.py --- .github/pr_autorename.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/pr_autorename.py b/.github/pr_autorename.py index 1ae811a..aa57c5e 100755 --- a/.github/pr_autorename.py +++ b/.github/pr_autorename.py @@ -12,7 +12,7 @@ def check(title:str): _type_: _description_ """ - title = title.strip().replace("【", "[") + title = title.strip().replace("【", "[").replace("】", "]") if(not title.startswith("[Pull Request]")): temp = title.replace("(", "").replace("{", "").replace("[", "") \ .replace(")", "").replace("}", "").replace("]", "") \