ci: adjust approve (#48998)

* chore: simple code

* chore: more info

* chore: rename

* chore: rename

* chore: rename

* chore: adjust comment trigger

* chore: add auto comment update
This commit is contained in:
二货爱吃白萝卜 2024-05-21 10:47:07 +08:00 committed by GitHub
parent ecfdc05b21
commit 61aa6407b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 10 deletions

View File

@ -1,6 +1,6 @@
# Check the report passed or developer confirmed the diff # Check the report passed or developer confirmed the diff
name: 🤖 Visual Regression Diff Approver name: 🤖 Visual Regression Diff Approval
# on comment event # on comment event
on: on:
@ -11,9 +11,9 @@ permissions:
contents: read contents: read
jobs: jobs:
rebase: approval:
permissions: permissions:
pull-requests: read pull-requests: write
name: Check Virtual Regression Approval name: Check Virtual Regression Approval
if: github.event.issue.pull_request != '' if: github.event.issue.pull_request != ''
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -25,13 +25,27 @@ jobs:
id: check_approval id: check_approval
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: | script: |
const issue_number = context.payload.issue.number;
console.log('PR:', issue_number);
const prResponse = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: issue_number,
});
const prHeadSha = prResponse.data.head.sha;
console.log('Head SHA:', prHeadSha);
const { data: comments } = await github.rest.issues.listComments({ const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
issue_number: context.issue.number, issue_number: issue_number
}); });
console.log('Comments:', comments.length);
let hasDiffSuccess = false; let hasDiffSuccess = false;
let hasDiffFailed = false; let hasDiffFailed = false;
let hasMemberApprove = false; let hasMemberApprove = false;
@ -48,6 +62,23 @@ jobs:
} }
} }
console.log('hasDiffSuccess:', hasDiffSuccess);
console.log('hasDiffFailed:', hasDiffFailed);
console.log('hasMemberApprove:', hasMemberApprove);
const diffPassed = hasDiffSuccess || (hasDiffFailed && hasMemberApprove);
const mergedStatus = diffPassed ? 'success' : hasDiffFailed ? 'failure' : 'pending';
console.log('Status:', mergedStatus);
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: prHeadSha,
state: mergedStatus,
context: 'Visual Regression Diff Wait Approve',
description: diffPassed ? 'Visual diff is acceptable' : 'Visual diff is not pass',
});
if (hasDiffSuccess || (hasDiffFailed && hasMemberApprove)) { if (hasDiffSuccess || (hasDiffFailed && hasMemberApprove)) {
return 'success'; return 'success';
} else if (hasDiffFailed) { } else if (hasDiffFailed) {
@ -56,9 +87,6 @@ jobs:
return 'waiting'; return 'waiting';
} }
# Console need approve message and throw failure if the result is `failed` # Console check_approval status
- name: Fail if Visual Diff Failed - name: Console Status
if: steps.check_approval.outputs.result == 'failed' run: echo "Result -> ${{ steps.check_approval.outputs.result }}"
run: |
echo "Visual Diff Failed, please check the diff and approve it."
exit 1

View File

@ -140,3 +140,30 @@ jobs:
<!-- VISUAL_DIFF_REGRESSION_HOOK --> <!-- VISUAL_DIFF_REGRESSION_HOOK -->
body-include: '<!-- VISUAL_DIFF_REGRESSION_HOOK -->' body-include: '<!-- VISUAL_DIFF_REGRESSION_HOOK -->'
number: ${{ steps.pr.outputs.id }} number: ${{ steps.pr.outputs.id }}
# Update PR description with updated time: `snapshot:[TIMESTAMP]`
- name: Update timestamp
if: ${{ always() }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const timestamp = Date.now();
const prNumber = ${{ steps.pr.outputs.id }};
const { data } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
const { body: currentBody } = data;
let newBody = currentBody.replace(/<!-- timestamp:[^>]-->/, '').trim();
newBody = newBody + `\n\n<!-- timestamp: ${timestamp} -->`;
github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
body: newBody
});