From ab5ec70c5b1b7896322d0eadc09956ace6e25cce Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 07:43:55 +0000 Subject: [PATCH 1/7] [Vcpkg] clean PR clutters commit --- .github/workflows/clean-commit-messages.yml | 53 +++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .github/workflows/clean-commit-messages.yml diff --git a/.github/workflows/clean-commit-messages.yml b/.github/workflows/clean-commit-messages.yml new file mode 100644 index 00000000000000..233db5b2cc92b3 --- /dev/null +++ b/.github/workflows/clean-commit-messages.yml @@ -0,0 +1,53 @@ +name: Clean PR Commit Messages + +on: + pull_request: + types: [opened, synchronize] + +jobs: + clean-commit-message: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Extract PR commits + id: extract_pr_commits + uses: actions/github-script@v7 + with: + script: | + const commits = await github.pulls.listCommits({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number + }); + return { + commits: commits.data.map(commit => commit.sha).join('\n') + }; + + - name: Clean commit messages + run: | + // Extract the commit SHAs from the previous step output + commit_shas=$(echo "${{ steps.extract_pr_commits.outputs.commits }}" | tr '\n' ' ') + + // Extract and clean commit messages + cleaned_messages="" + for sha in $commit_shas; do + message=$(git log --format=%B -n 1 $sha) + cleaned_message=$(echo "$message" | sed 's///g' | sed '/^$/d') + cleaned_messages+="$cleaned_message"$'\n' + done + + # Reset the commits with cleaned messages + git reset --soft HEAD~$(echo $commit_shas | wc -w) + IFS=$'\n' + for cleaned_message in ${cleaned_messages}; do + git commit -m "${cleaned_message}" + done + + - name: Push cleaned commit messages + run: | + git push --force-with-lease + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 017bdb494e71d4a6a634915308ff1ee9fc6acc05 Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 07:50:40 +0000 Subject: [PATCH 2/7] update commit func --- .github/workflows/clean-commit-messages.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/clean-commit-messages.yml b/.github/workflows/clean-commit-messages.yml index 233db5b2cc92b3..01d065617bbd66 100644 --- a/.github/workflows/clean-commit-messages.yml +++ b/.github/workflows/clean-commit-messages.yml @@ -17,7 +17,7 @@ jobs: uses: actions/github-script@v7 with: script: | - const commits = await github.pulls.listCommits({ + const commits = await github.rest.pulls.listCommits({ owner: context.repo.owner, repo: context.repo.repo, pull_number: context.payload.pull_request.number @@ -28,10 +28,8 @@ jobs: - name: Clean commit messages run: | - // Extract the commit SHAs from the previous step output commit_shas=$(echo "${{ steps.extract_pr_commits.outputs.commits }}" | tr '\n' ' ') - // Extract and clean commit messages cleaned_messages="" for sha in $commit_shas; do message=$(git log --format=%B -n 1 $sha) @@ -39,7 +37,6 @@ jobs: cleaned_messages+="$cleaned_message"$'\n' done - # Reset the commits with cleaned messages git reset --soft HEAD~$(echo $commit_shas | wc -w) IFS=$'\n' for cleaned_message in ${cleaned_messages}; do From c1d735b5c3b40e2f036019f807bd121f9518bcae Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 07:55:28 +0000 Subject: [PATCH 3/7] check branch --- .github/workflows/clean-commit-messages.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/clean-commit-messages.yml b/.github/workflows/clean-commit-messages.yml index 01d065617bbd66..ce8802b3e2badc 100644 --- a/.github/workflows/clean-commit-messages.yml +++ b/.github/workflows/clean-commit-messages.yml @@ -11,6 +11,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.ref }} - name: Extract PR commits id: extract_pr_commits @@ -45,6 +47,6 @@ jobs: - name: Push cleaned commit messages run: | - git push --force-with-lease + git push origin HEAD:${{ github.event.pull_request.head.ref }} --force-with-lease env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From f3a6e9bad0139ed8e4fce030bd7f3cbe0c70a2c6 Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 07:59:33 +0000 Subject: [PATCH 4/7] add branch to env --- .github/workflows/clean-commit-messages.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/clean-commit-messages.yml b/.github/workflows/clean-commit-messages.yml index ce8802b3e2badc..54fd386e803840 100644 --- a/.github/workflows/clean-commit-messages.yml +++ b/.github/workflows/clean-commit-messages.yml @@ -46,7 +46,8 @@ jobs: done - name: Push cleaned commit messages - run: | - git push origin HEAD:${{ github.event.pull_request.head.ref }} --force-with-lease env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_BRANCH: ${{ github.event.pull_request.head.ref }} + run: | + git push origin HEAD:$PR_BRANCH --force-with-lease From 38fbb56e588eb001aec0ac3bcc3ba861a2594e9d Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 08:34:43 +0000 Subject: [PATCH 5/7] just check not push --- .github/workflows/clean-commit-messages.yml | 55 +++++++-------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/.github/workflows/clean-commit-messages.yml b/.github/workflows/clean-commit-messages.yml index 54fd386e803840..6854d5acdde865 100644 --- a/.github/workflows/clean-commit-messages.yml +++ b/.github/workflows/clean-commit-messages.yml @@ -1,53 +1,34 @@ -name: Clean PR Commit Messages +name: Check PR Comments Message on: pull_request: types: [opened, synchronize] jobs: - clean-commit-message: + Check: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 - with: - ref: ${{ github.event.pull_request.head.ref }} - - name: Extract PR commits - id: extract_pr_commits + - name: Check PR body for comments + id: check_pr_body uses: actions/github-script@v7 with: script: | - const commits = await github.rest.pulls.listCommits({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: context.payload.pull_request.number - }); - return { - commits: commits.data.map(commit => commit.sha).join('\n') - }; - - - name: Clean commit messages - run: | - commit_shas=$(echo "${{ steps.extract_pr_commits.outputs.commits }}" | tr '\n' ' ') - - cleaned_messages="" - for sha in $commit_shas; do - message=$(git log --format=%B -n 1 $sha) - cleaned_message=$(echo "$message" | sed 's///g' | sed '/^$/d') - cleaned_messages+="$cleaned_message"$'\n' - done - - git reset --soft HEAD~$(echo $commit_shas | wc -w) - IFS=$'\n' - for cleaned_message in ${cleaned_messages}; do - git commit -m "${cleaned_message}" - done + const prNumber = context.payload.pull_request.number; + const prBody = context.payload.pull_request.body; + + const commentRegex = //; + const hasComment = commentRegex.test(prBody); - - name: Push cleaned commit messages - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_BRANCH: ${{ github.event.pull_request.head.ref }} - run: | - git push origin HEAD:$PR_BRANCH --force-with-lease + if (hasComment) { + const comment = "This pull request comments contains `` comments."; + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + body: comment + }); + } From 383dcd33e2c5b25aa078a4551738e07e3fe620f1 Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 08:48:45 +0000 Subject: [PATCH 6/7] Modify the regular expression --- .../{clean-commit-messages.yml => check_pr_comments.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{clean-commit-messages.yml => check_pr_comments.yml} (91%) diff --git a/.github/workflows/clean-commit-messages.yml b/.github/workflows/check_pr_comments.yml similarity index 91% rename from .github/workflows/clean-commit-messages.yml rename to .github/workflows/check_pr_comments.yml index 6854d5acdde865..b8c8688d5a4190 100644 --- a/.github/workflows/clean-commit-messages.yml +++ b/.github/workflows/check_pr_comments.yml @@ -20,7 +20,7 @@ jobs: const prNumber = context.payload.pull_request.number; const prBody = context.payload.pull_request.body; - const commentRegex = //; + const commentRegex = //; const hasComment = commentRegex.test(prBody); if (hasComment) { From 27b8747feeb0df8f518fab930e2cf8ca6022b784 Mon Sep 17 00:00:00 2001 From: FrankXie Date: Tue, 30 Jul 2024 08:58:16 +0000 Subject: [PATCH 7/7] update --- .github/workflows/check_pr_comments.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check_pr_comments.yml b/.github/workflows/check_pr_comments.yml index b8c8688d5a4190..1c5007ff7185d8 100644 --- a/.github/workflows/check_pr_comments.yml +++ b/.github/workflows/check_pr_comments.yml @@ -13,7 +13,6 @@ jobs: uses: actions/checkout@v3 - name: Check PR body for comments - id: check_pr_body uses: actions/github-script@v7 with: script: | @@ -22,9 +21,8 @@ jobs: const commentRegex = //; const hasComment = commentRegex.test(prBody); - if (hasComment) { - const comment = "This pull request comments contains `` comments."; + const comment = "This pull request contains `` comments."; await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, @@ -32,3 +30,5 @@ jobs: body: comment }); } + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}