2024-05-20 19:55:59 +08:00
|
|
|
# Check the report passed or developer confirmed the diff
|
|
|
|
|
2024-05-21 10:47:07 +08:00
|
|
|
name: 🤖 Visual Regression Diff Approval
|
2024-05-20 19:55:59 +08:00
|
|
|
|
|
|
|
# on comment event
|
|
|
|
on:
|
|
|
|
issue_comment:
|
|
|
|
types: [created, edited, deleted]
|
|
|
|
|
|
|
|
permissions:
|
|
|
|
contents: read
|
|
|
|
|
|
|
|
jobs:
|
2024-05-21 10:47:07 +08:00
|
|
|
approval:
|
2024-05-20 19:55:59 +08:00
|
|
|
permissions:
|
2024-05-21 10:47:07 +08:00
|
|
|
pull-requests: write
|
2024-05-21 11:29:52 +08:00
|
|
|
statuses: write # Allow to modfiy commit status
|
2024-05-20 19:55:59 +08:00
|
|
|
name: Check Virtual Regression Approval
|
|
|
|
if: github.event.issue.pull_request != ''
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
# Check all the comments if contains `VISUAL_DIFF_SUCCESS` or `VISUAL_DIFF_FAILED`
|
|
|
|
# Check if member of repo comment `Pass Visual Diff`
|
|
|
|
# Return type: `success` or `failed` or `waiting`
|
|
|
|
- name: Statistic Visual Diff Approval
|
|
|
|
id: check_approval
|
|
|
|
uses: actions/github-script@v7
|
|
|
|
with:
|
2024-05-21 10:47:07 +08:00
|
|
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
2024-05-20 19:55:59 +08:00
|
|
|
script: |
|
2024-05-21 10:47:07 +08:00
|
|
|
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);
|
|
|
|
|
2024-05-20 19:55:59 +08:00
|
|
|
const { data: comments } = await github.rest.issues.listComments({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
2024-05-21 10:47:07 +08:00
|
|
|
issue_number: issue_number
|
2024-05-20 19:55:59 +08:00
|
|
|
});
|
|
|
|
|
2024-05-21 10:47:07 +08:00
|
|
|
console.log('Comments:', comments.length);
|
|
|
|
|
2024-05-20 19:55:59 +08:00
|
|
|
let hasDiffSuccess = false;
|
|
|
|
let hasDiffFailed = false;
|
|
|
|
let hasMemberApprove = false;
|
|
|
|
|
|
|
|
for (const comment of comments) {
|
|
|
|
if (comment.body.includes('VISUAL_DIFF_SUCCESS')) {
|
|
|
|
hasDiffSuccess = true;
|
|
|
|
}
|
|
|
|
if (comment.body.includes('VISUAL_DIFF_FAILED')) {
|
|
|
|
hasDiffFailed = true;
|
|
|
|
}
|
2024-11-14 15:47:22 +08:00
|
|
|
|
|
|
|
// https://regex101.com/r/kLjudz/1
|
|
|
|
const RE = /(?<=\>\s\[!IMPORTANT\].*?- \[ \])/s;
|
|
|
|
if (
|
|
|
|
comment.body.includes('- [x] Visual diff is acceptable') &&
|
|
|
|
comment.body.match(RE) == null /** 检查 IMPORTANT 是否存在未勾选的 */
|
|
|
|
) {
|
2024-05-20 19:55:59 +08:00
|
|
|
hasMemberApprove = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 10:47:07 +08:00
|
|
|
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);
|
|
|
|
|
2024-11-21 10:14:55 +08:00
|
|
|
const { data: currentStatuses } = await github.rest.repos.listCommitStatusesForRef({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
ref: prHeadSha,
|
2024-05-21 10:47:07 +08:00
|
|
|
});
|
|
|
|
|
2024-11-21 10:14:55 +08:00
|
|
|
const currentStatus = currentStatuses.find(status => status.context === 'Visual Regression Diff Wait Approve');
|
|
|
|
if (currentStatus && currentStatus.state === mergedStatus) {
|
|
|
|
console.log('Status has not changed, no need to update:', currentStatus.state);
|
|
|
|
} else {
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-20 19:55:59 +08:00
|
|
|
if (hasDiffSuccess || (hasDiffFailed && hasMemberApprove)) {
|
|
|
|
return 'success';
|
|
|
|
} else if (hasDiffFailed) {
|
|
|
|
return 'failed';
|
|
|
|
} else {
|
|
|
|
return 'waiting';
|
|
|
|
}
|
|
|
|
|
2024-05-21 10:47:07 +08:00
|
|
|
# Console check_approval status
|
|
|
|
- name: Console Status
|
|
|
|
run: echo "Result -> ${{ steps.check_approval.outputs.result }}"
|