Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pull Request] Add PR autorename Action. #271

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/pr_autorename.py
Original file line number Diff line number Diff line change
@@ -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("【", "[").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]: <title>'
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]))
40 changes: 40 additions & 0 deletions .github/workflows/pr-autorename.yml
Original file line number Diff line number Diff line change
@@ -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"'" }'
Loading