From 4c61da113efefcdb36e75f7077b809ac46d6c48f Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Wed, 12 Jun 2024 16:50:10 +0800 Subject: [PATCH] fix: Skip package if the last commit is after the comment --- .github/workflows/package_for_test.yml | 55 +++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/.github/workflows/package_for_test.yml b/.github/workflows/package_for_test.yml index a475701b0c..6455a3303d 100644 --- a/.github/workflows/package_for_test.yml +++ b/.github/workflows/package_for_test.yml @@ -6,8 +6,61 @@ on: push: jobs: + check_issue_comment: + if: ${{ (github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/package') && contains(fromJSON('["OWNER", "COLLABORATOR"]'), github.event.comment.author_association)) }} + + runs-on: ubuntu-20.04 + + steps: + - name: Check without commit after comment + uses: actions/github-script@v7 + env: + ISSUE_NUMBER: ${{github.event.issue.number}} + COMMENT_ID: ${{ github.event.comment.id }} + with: + script: | + const { ISSUE_NUMBER, COMMENT_ID } = process.env + let page = 1 + let hasFoundComment = false + while(true) { + const { data: timelines } = await github.rest.issues.listEventsForTimeline({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: ISSUE_NUMBER, + page, + per_page: 100, + }) + if (timelines.find(v => { + hasFoundComment = hasFoundComment || (v.event === 'commented' && `${v.id}` === `${COMMENT_ID}`) + return hasFoundComment && v.event === 'committed' + })) { + throw new Error('The last commit comes after the comment, please comment and package after last commit') + } + if (timelines.length === 0) { + return + } + page += 1 + } + + comment_when_check_issue_comment_failed: + needs: [check_issue_comment] + if: ${{ always() && needs.check_issue_comment.result == 'failure' }} + name: Append failed comment to the comment + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - name: Comment by pull request comment event when package failed + if: ${{ github.event_name == 'issue_comment' }} + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ github.event.comment.id }} + body: Packageing failed cause by commiting after comment in [${{ github.run_id }}](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}). @${{ github.event.comment.user.login }} + edit-mode: append + packaging: - if: ${{ (github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/package') && contains(fromJSON('["OWNER", "COLLABORATOR"]'), github.event.comment.author_association)) || github.event_name == 'push' }} + needs: [check_issue_comment] + if: ${{ (always() && needs.check_issue_comment.result == 'skipped' && github.event_name == 'push') || success() }} strategy: matrix: