Test #4633
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main, '[0-9]+-[0-9]+-*'] | |
workflow_run: | |
workflows: ['Test for Dependabot'] | |
types: | |
- completed | |
workflow_dispatch: | |
workflow_call: | |
inputs: | |
ref: | |
description: 'Checkout a different git ref instead of the SHA from the GitHub event.' | |
required: false | |
type: string | |
jobs: | |
test: | |
name: Test | |
if: |- | |
${{ | |
!startsWith(github.head_ref, 'workflows/tests/') && (( | |
github.actor == 'dependabot[bot]' && | |
github.event_name != 'push' && | |
github.event_name != 'pull_request' | |
) || ( | |
github.actor != 'dependabot[bot]' && | |
github.event_name != 'workflow_run' | |
)) | |
}} | |
runs-on: ubuntu-latest | |
permissions: write-all | |
steps: | |
- uses: actions/checkout@v4 | |
if: ${{ github.event_name != 'workflow_run' }} | |
with: | |
ref: ${{ inputs.ref }} | |
fetch-depth: 0 | |
- uses: actions/checkout@v4 | |
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: Git checkout base branch | |
id: git-checkout | |
env: | |
PULL_REQUEST_BRANCH_NAME: ${{ github.event.pull_request.head.ref }} | |
PULL_REQUEST_WORKFLOW_RUN_BRANCH_NAME: ${{ github.event.workflow_run.head_branch }} | |
run: | | |
BASE_BRANCH_NAME=`git branch --show-current` | |
if [ -z "$BASE_BRANCH_NAME" ] | |
then | |
BASE_BRANCH_NAME="$PULL_REQUEST_BRANCH_NAME" | |
fi | |
if [ -z "$BASE_BRANCH_NAME" ] | |
then | |
BASE_BRANCH_NAME="$PULL_REQUEST_WORKFLOW_RUN_BRANCH_NAME" | |
fi | |
if [ -z "$BASE_BRANCH_NAME" ] | |
then | |
echo "::error::Fail to identify Pull Request branch" | |
exit 1 | |
fi | |
echo "::notice::Base branch is $BASE_BRANCH_NAME" | |
git checkout $BASE_BRANCH_NAME | |
echo "base-branch-name=$BASE_BRANCH_NAME" >> $GITHUB_OUTPUT | |
echo "::notice::Base commit is `git rev-parse --verify HEAD`" | |
- name: Git checkout test base branch | |
env: | |
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
run: | | |
echo "::notice::Base branch is $TEST_BASE_BRANCH_NAME" | |
git checkout -b "$TEST_BASE_BRANCH_NAME" | |
- name: Make test base commit | |
env: | |
SHA: ${{ github.sha }} | |
RUN_NUMBER: ${{ github.run_number }} | |
RUN_ATTEMPT: ${{ github.run_attempt }} | |
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }} | |
run: | | |
git commit --allow-empty -m "Test base commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT" | |
echo "::notice::Test base commit is `git rev-parse --verify HEAD`" | |
- name: Git push test base branch | |
env: | |
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
run: | | |
git push -f --set-upstream origin "$TEST_BASE_BRANCH_NAME" | |
echo "::notice::Remote test branch is origin/$TEST_BASE_BRANCH_NAME" | |
- name: Open base PR | |
id: open-base-pr | |
env: | |
GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }} | |
SHA: ${{ github.sha }} | |
RUN_ID: ${{ github.run_id }} | |
RUN_NUMBER: ${{ github.run_number }} | |
RUN_ATTEMPT: ${{ github.run_attempt }} | |
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }} | |
run: | | |
echo "::notice::Run number is $RUN_NUMBER" | |
echo "::notice::Run attempt is $RUN_ATTEMPT" | |
echo "::notice::Base PR number is $BASE_PR_NUMBER" | |
gh pr create --base "main" --title "Test base PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. | |
Run ID: [$RUN_ID]($(gh run view $RUN_ID --json 'url' --jq '.url')). Number: $RUN_NUMBER. Attempt: $RUN_ATTEMPT." | |
TEST_BASE_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2` | |
echo "test-base-pr-number=$TEST_BASE_PR_NUMBER" >> $GITHUB_OUTPUT | |
echo "::notice::Test base PR number is $TEST_BASE_PR_NUMBER" | |
- name: Git checkout test head branch | |
env: | |
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
run: | | |
echo "::notice::Test head branch is $TEST_HEAD_BRANCH_NAME" | |
git checkout -b "$TEST_HEAD_BRANCH_NAME" | |
- name: Make test head commit | |
env: | |
SHA: ${{ github.sha }} | |
RUN_NUMBER: ${{ github.run_number }} | |
RUN_ATTEMPT: ${{ github.run_attempt }} | |
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }} | |
run: | | |
git commit --allow-empty -m "Test head commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT" | |
echo "::notice::Test head commit is `git rev-parse --verify HEAD`" | |
- name: Git push test head branch | |
env: | |
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
run: | | |
git push -f --set-upstream origin "$TEST_HEAD_BRANCH_NAME" | |
echo "::notice::Remote test branch is origin/$TEST_HEAD_BRANCH_NAME" | |
- name: Open head PR | |
id: open-head-pr | |
env: | |
GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }} | |
SHA: ${{ github.sha }} | |
RUN_ID: ${{ github.run_id }} | |
RUN_NUMBER: ${{ github.run_number }} | |
RUN_ATTEMPT: ${{ github.run_attempt }} | |
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }} | |
run: | | |
echo "::notice::Base PR number is $BASE_PR_NUMBER" | |
gh pr create --base "$TEST_BASE_BRANCH_NAME" --title "Test head PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. | |
Run ID: [$RUN_ID]($(gh run view $RUN_ID --json 'url' --jq '.url')). Number: $RUN_NUMBER. Attempt: $RUN_ATTEMPT. | |
#accept2ship" | |
TEST_HEAD_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2` | |
echo "test-head-pr-number=$TEST_HEAD_PR_NUMBER" >> $GITHUB_OUTPUT | |
echo "::notice::Test head PR number is $TEST_HEAD_PR_NUMBER" | |
- name: Approve PR | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh pr review --approve | |
- name: Check PR | |
id: check-pr | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
CHECKS_TEMP_FILE="$(mktemp)" | |
sleep 10 | |
PENDING=$(gh pr checks | cut -f2 | grep pending | wc -l) | |
while [ $PENDING -ne 0 ] | |
do | |
echo "::group::Pending checks: $PENDING" | |
gh pr checks --watch | |
sleep 10 | |
PENDING=$(gh pr checks | cut -f2 | grep pending | wc -l) | |
echo "::endgroup::" | |
done | |
gh pr checks >> $CHECKS_TEMP_FILE | |
echo "::notice::Temp file content: | |
$(cat $CHECKS_TEMP_FILE) | |
" | |
echo "checks-temp-file=$CHECKS_TEMP_FILE" >> $GITHUB_OUTPUT | |
- name: Report Checks | |
if: ${{ always() && steps.check-pr.outputs.checks-temp-file != '' }} | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }} | |
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }} | |
CHECKS_TEMP_FILE: ${{ steps.check-pr.outputs.checks-temp-file }} | |
run: | | |
gh pr comment "$BASE_PR_NUMBER" --body "**Checks from #$TEST_HEAD_PR_NUMBER** | |
| Check | Status | | |
| --- | --- | | |
$(cat $CHECKS_TEMP_FILE | grep -E 'https://' | tac | awk 'BEGIN { FS ="\t" } ; { if (! seen[$1]++) print }' | sed -E 's/(.*)\t(.*)\t(.*)\t(.*)\t(.*)/| [\1](\4) | \2 |/')" | |
rm -f $CHECKS_TEMP_FILE | |
- name: Verify PR | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'` | |
echo "test_pr_state=$TEST_PR_STATE" >> $GITHUB_OUTPUT | |
echo "::notice::Test PR state is $TEST_PR_STATE" | |
if [ "$TEST_PR_STATE" != 'MERGED' ] | |
then | |
exit 1 | |
fi | |
- name: Close head PR | |
if: ${{ failure() || cancelled() }} | |
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: ${{ steps.git-checkout.outputs.base-branch-name }} | |
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }} | |
run: | | |
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'` | |
if [ "$TEST_PR_STATE" = 'MERGED' ] | |
then | |
exit | |
fi | |
if [ -n "$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_HEAD_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR." | |
- name: Delete remote test head branch | |
if: ${{ always() }} | |
continue-on-error: true | |
env: | |
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
run: | | |
if test $(git ls-remote origin --heads "$TEST_HEAD_BRANCH_NAME" | wc -l ) != 0 | |
then | |
git push -d origin "$TEST_HEAD_BRANCH_NAME" | |
fi | |
- name: Close base 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: ${{ steps.git-checkout.outputs.base-branch-name }} | |
TEST_BASE_PR_NUMBER: ${{ steps.open-base-pr.outputs.test-base-pr-number }} | |
run: | | |
if [ -n "$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_BASE_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR." | |
- name: Delete remote test base branch | |
if: ${{ always() }} | |
continue-on-error: true | |
env: | |
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }} | |
run: | | |
if test $(git ls-remote origin --heads "$TEST_BASE_BRANCH_NAME" | wc -l ) != 0 | |
then | |
git push -d origin "$TEST_BASE_BRANCH_NAME" | |
fi |