From f5b992ff5fae356baf2abd38fe633dc0fbb10582 Mon Sep 17 00:00:00 2001 From: Super Zheng Date: Fri, 13 Jan 2023 16:03:40 +0800 Subject: [PATCH] feat(workflows): only uses non-merge commits as valid commits (#2862) Co-authored-by: Zoom Chan --- .github/workflows/gh_action_merge.yml | 41 ++++++++------ .github/workflows/reuse_classify_commits.yml | 59 ++++++++++++++++++++ 2 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/reuse_classify_commits.yml diff --git a/.github/workflows/gh_action_merge.yml b/.github/workflows/gh_action_merge.yml index f04239b5155..2aa0dd465cc 100644 --- a/.github/workflows/gh_action_merge.yml +++ b/.github/workflows/gh_action_merge.yml @@ -26,8 +26,15 @@ env: } jobs: + call_classify_commits: + if: github.repository == 'Tencent/Hippy' && (github.event.label.name == 'action(squash-merge)' || github.event.label.name == 'action(rebase-merge)') + uses: ./.github/workflows/reuse_classify_commits.yml + with: + pull_request_number: ${{ github.event.pull_request.number }} + squash_merge_action: - if: github.event.label.name == 'action(squash-merge)' && github.repository == 'Tencent/Hippy' + needs: call_classify_commits + if: github.event.label.name == 'action(squash-merge)' runs-on: ubuntu-latest steps: - name: Token @@ -43,19 +50,19 @@ jobs: github_token: ${{ steps.get-token.outputs.token }} labels: "action(squash-merge): failed" - name: Checkout - if: github.event.pull_request.commits > 1 + if: needs.call_classify_commits.outputs.normal_commits_count > 1 uses: actions/checkout@v3 with: fetch-depth: 0 - name: Setup - if: github.event.pull_request.commits > 1 + if: needs.call_classify_commits.outputs.normal_commits_count > 1 uses: actions/setup-node@v3 with: node-version: 16.x cache: 'npm' cache-dependency-path: package-lock.json - name: Install - if: github.event.pull_request.commits > 1 + if: needs.call_classify_commits.outputs.normal_commits_count > 1 run: | npm install commitlint-plugin-function-rules@1.6.0 @commitlint/cli@16.2.1 @commitlint/config-conventional@16.2.1 @commitlint/lint@16.2.1 @commitlint/prompt-cli@16.2.1 - name: Commitlint @@ -65,7 +72,7 @@ jobs: shell: bash {0} run: | ${{ env.OUTPUT_FUNCTION }} - if [ ${{ github.event.pull_request.commits }} -gt 1 ]; then + if [ ${{ needs.call_classify_commits.outputs.normal_commits_count }} -gt 1 ]; then result=`echo $PULL_REQUEST_TITLE | npx commitlint -c false 2>&1` output $? "$result" fi @@ -98,7 +105,7 @@ jobs: - @${{ github.event.sender.login }} + ${{ github.event.sender.type == 'User' && format('@{0}', github.event.sender.login) || '#help' }} with: github-token: ${{ steps.get-token.outputs.token }} script: | @@ -106,7 +113,7 @@ jobs: const { issues } = github.rest; const util = require('util'); const os = require('os'); - + let body; if (process.env.CHECK_RESULT) { body = util.format(process.env.FAILED_MESSAGE, 'pull request title does not meet the [Convention Commit](https://conventionalcommits.org/) guideline', process.env.CHECK_RESULT); @@ -138,8 +145,9 @@ jobs: } await Promise.all(p); - rebase_merge_action: - if: github.event.label.name == 'action(rebase-merge)' && github.repository == 'Tencent/Hippy' + rebase_merge_action_preparation: + needs: call_classify_commits + if: github.event.label.name == 'action(rebase-merge)' runs-on: ubuntu-latest steps: - name: Token @@ -149,13 +157,14 @@ jobs: private-key: ${{ secrets.ACTION_PRIVATE_KEY }} app-id: ${{ secrets.ACTION_APP_ID }} - name: Comment + if: needs.call_classify_commits.outputs.normal_commits_count > 1 env: COMMENT_MESSAGE: | :lock: `rebase-merge` action execution needs privilege escalation. Please wait for admin team member approval ... - @${{ github.event.sender.login }} + ${{ github.event.sender.type == 'User' && format('@{0}', github.event.sender.login) || '' }} WECHAT_WORK_MESSAGE: | [${{ github.event.sender.login }}](https://github.com/${{ github.event.sender.login }}) requested privilege escalation action on [#${{ github.event.pull_request.number }}](${{ github.event.pull_request.html_url }}) pull request. > ${{ github.event.pull_request.title }} @@ -219,10 +228,10 @@ jobs: await Promise.all(p); - rebase_merge_action_privileged: - needs: [ rebase_merge_action ] + rebase_merge_action: + needs: [ rebase_merge_action_preparation, call_classify_commits ] runs-on: ubuntu-latest - environment: github-actions-privileged + environment: ${{ needs.call_classify_commits.outputs.normal_commits_count > 1 && 'github-actions-privileged' || '' }} steps: - name: Token uses: navikt/github-app-token-generator@v1 @@ -263,7 +272,7 @@ jobs: - @${{ github.event.sender.login }} + ${{ github.event.sender.type == 'User' && format('@{0}', github.event.sender.login) || '#help' }} with: github-token: ${{ steps.get-token.outputs.token }} script: | @@ -293,8 +302,8 @@ jobs: } await Promise.all(p); - rebase_merge_action_reject: - needs: [ rebase_merge_action_privileged ] + rebase_merge_action_rejected: + needs: [ rebase_merge_action ] if: failure() runs-on: ubuntu-latest steps: diff --git a/.github/workflows/reuse_classify_commits.yml b/.github/workflows/reuse_classify_commits.yml new file mode 100644 index 00000000000..d5f17538106 --- /dev/null +++ b/.github/workflows/reuse_classify_commits.yml @@ -0,0 +1,59 @@ +name: '[reuse] classify commits' + +on: + workflow_call: + inputs: + pull_request_number: + description: 'Pull request number' + required: true + type: number + outputs: + merge_commits_sha: + description: 'Merge commits sha' + value: ${{ jobs.classify_commits.outputs.merge_commits_sha }} + merge_commits_count: + description: 'Merge commits count' + value: ${{ jobs.classify_commits.outputs.merge_commits_count }} + normal_commits_sha: + description: 'Normal commits sha' + value: ${{ jobs.classify_commits.outputs.normal_commits_sha }} + normal_commits_count: + description: 'Normal commits count' + value: ${{ jobs.classify_commits.outputs.normal_commits_count }} + +jobs: + classify_commits: + runs-on: ubuntu-latest + outputs: + merge_commits_sha: ${{ steps.action.outputs.merge_commits_sha }} + merge_commits_count: ${{ steps.action.outputs.merge_commits_count }} + normal_commits_sha: ${{ steps.action.outputs.normal_commits_sha }} + normal_commits_count: ${{ steps.action.outputs.normal_commits_count }} + steps: + - name: Action + id: action + uses: actions/github-script@v6.3.3 + with: + script: | + const { pulls } = github.rest; + const fs = require('fs'); + const os = require('os'); + + const commits = (await github.paginate(pulls.listCommits, { + per_page: 100, + pull_number: ${{ inputs.pull_request_number }}, + ...context.repo + })); + + if (commits.length >= 250) { // exceeded maximum of 250 commits per pull request + return; + } + + const merge_commits = []; + const normal_commits = []; + commits.forEach(commit => commit.parents.length > 1 ? merge_commits.push(commit.sha) : normal_commits.push(commit.sha)); + + fs.appendFileSync(process.env.GITHUB_OUTPUT, `merge_commits_sha=${JSON.stringify(merge_commits)}${os.EOL}`, { encoding: 'utf8' }); + fs.appendFileSync(process.env.GITHUB_OUTPUT, `merge_commits_count=${merge_commits.length}${os.EOL}`, { encoding: 'utf8' }); + fs.appendFileSync(process.env.GITHUB_OUTPUT, `normal_commits_sha=${JSON.stringify(normal_commits)}${os.EOL}`, { encoding: 'utf8' }); + fs.appendFileSync(process.env.GITHUB_OUTPUT, `normal_commits_count=${normal_commits.length}${os.EOL}`, { encoding: 'utf8' });