Skip to content

Commit

Permalink
feat(workflows): only uses non-merge commits as valid commits (Tencen…
Browse files Browse the repository at this point in the history
…t#2862)

Co-authored-by: Zoom Chan <[email protected]>
  • Loading branch information
2 people authored and pba-cra committed Feb 1, 2023
1 parent 500d632 commit 814b03a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
41 changes: 25 additions & 16 deletions .github/workflows/gh_action_merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [email protected] @commitlint/[email protected] @commitlint/[email protected] @commitlint/[email protected] @commitlint/[email protected]
- name: Commitlint
Expand All @@ -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
Expand Down Expand Up @@ -98,15 +105,15 @@ jobs:
</details>
@${{ 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: |
const { pull_request, sender } = context.payload;
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);
Expand Down Expand Up @@ -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
Expand All @@ -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 }}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -263,7 +272,7 @@ jobs:
</details>
@${{ 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: |
Expand Down Expand Up @@ -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:
Expand Down
59 changes: 59 additions & 0 deletions .github/workflows/reuse_classify_commits.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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' });

0 comments on commit 814b03a

Please sign in to comment.