Skip to content

Commit

Permalink
📝 Chore(CI): Enable to format pr title
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Mar 31, 2024
1 parent 3095b17 commit 8f5a287
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/scripts/check_new_commit.py
Original file line number Diff line number Diff line change
@@ -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)
61 changes: 61 additions & 0 deletions .github/scripts/pr_format.py
Original file line number Diff line number Diff line change
@@ -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]} <title>")
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)
37 changes: 37 additions & 0 deletions .github/workflows/pr-auto-format.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 8f5a287

Please sign in to comment.