diff --git a/.github/workflows/test-pr.yml b/.github/workflows/test-pr.yml index 0d3bff5..3ca877d 100644 --- a/.github/workflows/test-pr.yml +++ b/.github/workflows/test-pr.yml @@ -8,13 +8,12 @@ on: jobs: test_dirty_push_in_pull_request: name: Test Dirty Push in Pull Request - if: ${{ startsWith(github.head_ref, 'workflows/tests/') }} + if: ${{ startsWith(github.head_ref, 'workflows/tests/push/') }} outputs: touched-filename: ${{ steps.make-dirty.outputs.touched-filename }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - with: - name: Make dirty id: make-dirty @@ -41,7 +40,7 @@ jobs: verify_dirty_push_in_pull_request: name: Verify Dirty Push in Pull Request - if: ${{ startsWith(github.head_ref, 'workflows/tests/') }} + if: ${{ startsWith(github.head_ref, 'workflows/tests/push/') }} needs: test_dirty_push_in_pull_request runs-on: ubuntu-latest steps: @@ -58,3 +57,55 @@ jobs: then exit 1 fi + + test_dirty_request_changes_in_pull_request: + name: Test Dirty Request Changes in Pull Request + if: ${{ startsWith(github.head_ref, 'workflows/tests/request-changes/') }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + + - name: Make dirty + id: make-dirty + run: | + touch `git rev-parse --verify HEAD` + + - uses: ./ + id: test-action + with: + fail-if-not-clean: false + push-if-not-clean: false + request-changes-if-not-clean: true + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Verify outputs + env: + TEST_ACTION_OUTPUT: ${{ steps.test-action.outputs.status }} + run: | + if [[ $TEST_ACTION_OUTPUT != "dirty" ]] + then + exit 1; + fi + + verify_dirty_request_changes_in_pull_request: + name: Verify Dirty Push in Pull Request + if: ${{ startsWith(github.head_ref, 'workflows/tests/request-changes/') }} + needs: test_dirty_request_changes_in_pull_request + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + + - name: Verify outputs + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + REVIEW_DECISION=$(gh pr status --json reviewDecision -q .currentBranch.reviewDecision) + echo "::notice::Review decision is $REVIEW_DECISION" + if [[ $REVIEW_DECISION != "CHANGES_REQUESTED" ]] + then + exit 1; + fi diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5eef06b..9a50552 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -102,7 +102,7 @@ jobs: RUN_NUMBER: ${{ github.run_number }} RUN_ATTEMPT: ${{ github.run_attempt }} BASE_BRANCH_NAME: ${{ github.head_ref }} - TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} + TEST_BRANCH_NAME: ${{ format('workflows/tests/push/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} BASE_PR_NUMBER: ${{ github.event.pull_request.number }} run: | echo "::notice::Run number is $RUN_NUMBER" @@ -118,7 +118,7 @@ jobs: - name: Git push env: - TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} + TEST_BRANCH_NAME: ${{ format('workflows/tests/push/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} run: | git push -f --set-upstream origin "$TEST_BRANCH_NAME" echo "::notice::Remote test branch is origin/$TEST_BRANCH_NAME" @@ -134,7 +134,7 @@ jobs: BASE_PR_NUMBER: ${{ github.event.pull_request.number }} run: | echo "::notice::Base PR number is $BASE_PR_NUMBER" - gh pr create --base "$BASE_BRANCH_NAME" --title "Test PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT." + gh pr create --base "$BASE_BRANCH_NAME" --title "Push Test PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT." TEST_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2` echo "::set-output name=test-pr-number::$TEST_PR_NUMBER" echo "::notice::Test PR number is $TEST_PR_NUMBER" @@ -198,6 +198,140 @@ jobs: SHA: ${{ github.sha }} RUN_NUMBER: ${{ github.run_number }} RUN_ATTEMPT: ${{ github.run_attempt }} - TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} + TEST_BRANCH_NAME: ${{ format('workflows/tests/push/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} + run: | + git push -d origin "$TEST_BRANCH_NAME" + + test_dirty_request_changes: + name: Test Dirty Request Changes + if: |- + ${{ + !startsWith(github.head_ref, 'workflows/tests/') && ( + (github.event_name == 'pull_request' && github.actor != 'dependabot[bot]') || + (github.event_name == 'workflow_run' && github.actor == 'dependabot[bot]') || + github.event_name == 'workflow_dispatch' + ) + }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + if: ${{ github.event_name != 'workflow_run' }} + with: + fetch-depth: 0 + + - uses: actions/checkout@v3 + if: ${{ github.event_name == 'workflow_run' }} + with: + ref: ${{ github.event.workflow_run.head_branch }} + fetch-depth: 0 + + - name: Config Git + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" + + - name: Make commit + env: + SHA: ${{ github.sha }} + RUN_NUMBER: ${{ github.run_number }} + RUN_ATTEMPT: ${{ github.run_attempt }} + BASE_BRANCH_NAME: ${{ github.head_ref }} + TEST_BRANCH_NAME: ${{ format('workflows/tests/request-changes/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} + BASE_PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + echo "::notice::Run number is $RUN_NUMBER" + echo "::notice::Run attempt is $RUN_ATTEMPT" + echo "::notice::Base branch is $BASE_BRANCH_NAME" + echo "::notice::Base commit is `git rev-parse --verify HEAD`" + echo "::notice::Test branch is $TEST_BRANCH_NAME" + git checkout -b "$TEST_BRANCH_NAME" + touch $SHA + git add -A + git commit -am "Test commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT" + echo "::notice::Test commit is `git rev-parse --verify HEAD`" + + - name: Git push + env: + TEST_BRANCH_NAME: ${{ format('workflows/tests/request-changes/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} + run: | + git push -f --set-upstream origin "$TEST_BRANCH_NAME" + echo "::notice::Remote test branch is origin/$TEST_BRANCH_NAME" + + - name: Open PR + id: open-pr + env: + GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }} + SHA: ${{ github.sha }} + RUN_NUMBER: ${{ github.run_number }} + RUN_ATTEMPT: ${{ github.run_attempt }} + BASE_BRANCH_NAME: ${{ github.head_ref }} + BASE_PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + echo "::notice::Base PR number is $BASE_PR_NUMBER" + gh pr create --base "$BASE_BRANCH_NAME" --title "Request Change Test PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT." + TEST_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2` + echo "::set-output name=test-pr-number::$TEST_PR_NUMBER" + echo "::notice::Test PR number is $TEST_PR_NUMBER" + + - name: Check PR + id: check-pr + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + CHECKS_TEMP_FILE="$(mktemp)" + sleep 10 + gh pr checks --watch >> $CHECKS_TEMP_FILE + sleep 10 + gh pr checks --watch >> $CHECKS_TEMP_FILE + echo "::notice::Temp file content: + + $(cat $CHECKS_TEMP_FILE) + " + echo "::set-output name=checks-temp-file::$CHECKS_TEMP_FILE" + + - name: Report Checks + if: ${{ steps.check-pr.outputs.checks-temp-file != '' }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BASE_PR_NUMBER: ${{ github.event.pull_request.number }} + TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }} + CHECKS_TEMP_FILE: ${{ steps.check-pr.outputs.checks-temp-file }} + run: | + gh pr comment "$BASE_PR_NUMBER" --body "**Checks from #$TEST_PR_NUMBER** + + | Check | Status | URL | + | --- | --- | --- | + $(cat $CHECKS_TEMP_FILE | grep -E 'https://github.com/[^/]*/[^/]*/runs/' | sed -E 's/(.*)\t(.*)\t(.*)\t(.*)/| \1 | \2 | \4 |/')" + rm -f $CHECKS_TEMP_FILE + + - name: Close PR + if: ${{ always() }} + continue-on-error: true + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SHA: ${{ github.sha }} + RUN_NUMBER: ${{ github.run_number }} + RUN_ATTEMPT: ${{ github.run_attempt }} + BASE_BRANCH_NAME: ${{ github.head_ref }} + TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }} + run: | + if [ $BASE_BRANCH_NAME != '' ] + then + git checkout "$BASE_BRANCH_NAME" + echo "::notice::Base branch is `git branch --show-current`" + else + git checkout "$SHA" + echo "::notice::Base commit is `git rev-parse --verify HEAD`" + fi + gh pr close $TEST_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR." + + - name: Delete remote branch + if: ${{ always() }} + continue-on-error: true + env: + SHA: ${{ github.sha }} + RUN_NUMBER: ${{ github.run_number }} + RUN_ATTEMPT: ${{ github.run_attempt }} + TEST_BRANCH_NAME: ${{ format('workflows/tests/request-changes/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }} run: | git push -d origin "$TEST_BRANCH_NAME"