Skip to content

Test

Test #2384

Workflow file for this run

name: Test
on:
push:
branches: [main]
pull_request:
branches: [main, '[0-9]+-[0-9]+-*']
workflow_run:
workflows: ['Dependabot']
branches-ignore: ['workflows/tests/*']
types:
- completed
workflow_dispatch:
workflow_call:
jobs:
test:
name: Test (${{ matrix.config.name }})
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
strategy:
fail-fast: false
matrix:
config:
- name: Flat Config
id: flat
- name: Legacy Config
id: legacy
env:
SHA: ${{ github.sha }}
RUN_ID: ${{ github.run_id }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}/id-{1}/run-{2}/attempt-{3}/sha-{4}', matrix.config.id, 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 }}
CONFIG_TYPE: ${{ matrix.config.name }}
steps:
- uses: actions/checkout@v4
if: ${{ github.event_name != 'workflow_run' }}
with:
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: Randomly select test files
id: select-files
run: |
SELECTED_FILES=`((ls tests/javascript/fixes/*.js | grep -vE '\-correct(\-\d+)?\.js$' | shuf -n 1) && (ls tests/javascript/suggestions/*.js | grep -vE '\-correct(\-\d+)?\.js$' | shuf -n 1)) | paste -sd ',' -`
echo "::notice::Selected files are $SELECTED_FILES"
echo "selected-files=$SELECTED_FILES" >> $GITHUB_OUTPUT
- name: Make commit
env:
SELECTED_FILES: ${{ steps.select-files.outputs.selected-files }}
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"
mkdir -p src/__tests__
echo "$SELECTED_FILES" | tr ',' '\n' | xargs -I % cp % src/__tests__
git add src/__tests__
BASE_NAME=$([[ -z $BASE_PR_NUMBER ]] && echo "$BASE_BRANCH_NAME" || echo "#$BASE_PR_NUMBER")
COMMIT_MESSAGE="Test commit for $BASE_NAME ($SHA) by run $RUN_ID ($RUN_NUMBER-$RUN_ATTEMPT)"
git commit -am "$COMMIT_MESSAGE"
echo "::notice::Test commit is `git rev-parse --verify HEAD`"
- name: Git push
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 }}
run: |
echo "::notice::Base PR number is $BASE_PR_NUMBER"
echo "::notice::Base branch is $BASE_BRANCH_NAME"
BASE_NAME=$([[ -z $BASE_PR_NUMBER ]] && echo "$BASE_BRANCH_NAME" || echo "#$BASE_PR_NUMBER")
gh pr create --base "$BASE_BRANCH_NAME" --title "$CONFIG_TYPE Test PR for $BASE_NAME ($SHA)" --body "Generated from $BASE_NAME ($SHA).
Run ID: [$RUN_ID]($(gh run view $RUN_ID --json 'url' --jq '.url')). Number: $RUN_NUMBER. Attempt: $RUN_ATTEMPT."
TEST_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2`
echo "test-pr-number=$TEST_PR_NUMBER" >> $GITHUB_OUTPUT
echo "::notice::Test PR number is $TEST_PR_NUMBER"
- name: Check PR
id: check-pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }}
run: |
CHECKS_TEMP_FILE="$(mktemp)"
echo "checks-temp-file=$CHECKS_TEMP_FILE" >> $GITHUB_OUTPUT;
sleep 10
CHECKS=$(gh pr checks "$TEST_PR_NUMBER" | tee -a $CHECKS_TEMP_FILE)
PENDING=$(echo "$CHECKS" | cut -f2 | grep pending | wc -l)
while [[ $PENDING -gt 0 && -n "$PENDING" ]]
do
echo "::group::Pending checks: $PENDING"
gh pr checks "$TEST_PR_NUMBER" --watch --fail-fast | tee -a $CHECKS_TEMP_FILE
sleep 10
CHECKS=$(gh pr checks "$TEST_PR_NUMBER" | tee -a $CHECKS_TEMP_FILE)
PENDING=$(echo "$CHECKS" | cut -f2 | grep pending | wc -l)
echo "::endgroup::"
done
if [[ -s $CHECKS_TEMP_FILE ]]
then
echo "::group::Temp file content"
cat $CHECKS_TEMP_FILE
echo "::endgroup::"
else
echo "checks-temp-file=" >> $GITHUB_OUTPUT;
fi
gh pr checks "$TEST_PR_NUMBER" # Let check outcome determine the step exit code
- name: Report Checks
if: ${{ always() && steps.check-pr.outputs.checks-temp-file != '' && (github.event_name == 'pull_request' || github.event_name == 'workflow_run')}}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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 $CONFIG_TYPE Test PR #$TEST_PR_NUMBER**
| Check | Status | Time |
| --- | --- | --: |
$(cat $CHECKS_TEMP_FILE |
grep -E 'https://' |
tac |
awk 'BEGIN { FS ="\t" } ; { if (! seen[$1]++) print }' |
sed -E 's/(.*)\t(pass)\t(.*)\t(.*)\t(.*)/| [**\1**](\4) | ✅ | \3 |/;
s/(.*)\t(fail)\t(.*)\t(.*)\t(.*)/| [*\1*](\4) | ⛔ | \3 |/;
s/(.*)\t(skipping)\t(.*)\t(.*)\t(.*)/| [~\1~](\4) | ⏭️ | |/;
s/(.*)\t(pending)\t(.*)\t(.*)\t(.*)/| [\1](\4) | 🔄 | |/;
s/(.*)\t(.*)\t(.*)\t(.*)\t(.*)/| [\1](\4) | \2 | \3 |/')"
rm -f $CHECKS_TEMP_FILE
- name: Close PR
if: ${{ always() }}
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }}
run: |
git checkout "$BASE_BRANCH_NAME"
echo "::notice::Base branch is `git branch --show-current`"
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
run: |
git push -d origin "$TEST_BRANCH_NAME"