Skip to content

Commit

Permalink
Set up proper test Workflow with PR creation (#5)
Browse files Browse the repository at this point in the history
* Set up proper test Workflow with PR creation

* Fixed typo

* Added condition to test

* Added condition to other Workflows

* Added condition to ESLint Workflow

* Removed path filter in test PR Workflow

* Approving the test PR

* Only writing last checks to temp file

* Hooked up check_run[completed] and pull_request_review[submitted, edited]

* added logging for non-pull-request events

* Set up debug job

* Hooked up check_suite[completed]

* Always reporting checks

* Set merge-method input to squash based on repo settings

* Fixed typo in MERGED
  • Loading branch information
CatChen authored Aug 13, 2022
1 parent 1ded185 commit 08e2c76
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ on:

jobs:
build:
name: Build
if: ${{ !startsWith(github.head_ref, 'workflows/tests/') }}
runs-on: ubuntu-latest

steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ on:
jobs:
analyze:
name: Analyze
if: ${{ !startsWith(github.head_ref, 'workflows/tests/') }}
runs-on: ubuntu-latest
permissions:
actions: read
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:

jobs:
eslint:
name: ESLint
if: ${{ !startsWith(github.head_ref, 'workflows/tests/') }}
runs-on: ubuntu-latest

steps:
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test Pull Request

on:
pull_request:
types: [labeled, opened, edited, reopened, synchronize]
check_run:
types: [completed]
check_suite:
types: [completed]
pull_request_review:
types: [submitted, edited]
workflow_dispatch:

jobs:
test_in_pull_request:
name: Test in Pull Request
if: ${{ startsWith(github.head_ref, 'workflows/tests/') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: ./
with:
merge-method: squash

debug_information:
name: Debug Information
runs-on: ubuntu-latest

steps:
- name: Print contexts
env:
GITHUB: ${{ toJSON(github) }}
JOB: ${{ toJSON(job) }}
INPUTS: ${{ toJSON(inputs) }}
run: |
echo "::debug::github = $GITHUB"
echo "::debug::job = $JOB"
echo "::debug::inputs = $INPUTS"
134 changes: 130 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,144 @@
name: Test

on:
push:
branches: [main]
pull_request:
branches: [main, "[0-9]+-[0-9]+-*"]
workflow_dispatch:

jobs:
test:
name: Test
if: ${{ !startsWith(github.head_ref, 'workflows/tests/') }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Test
uses: ./
- 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/{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"
git commit --allow-empty -m "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/{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 "Test PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT.
#accept2ship"
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: 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
gh pr checks --watch
gh pr checks >> $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: ${{ always() && 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: Verify PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }}
run: |
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'`
echo "::set-output name=test_pr_state::$TEST_PR_STATE"
echo "::notice::Test PR state is $TEST_PR_STATE"
if [ $TEST_PR_STATE != 'MERGED' ]
then
exit 1
fi
- 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/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
git push -d origin "$TEST_BRANCH_NAME"
1 change: 1 addition & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SLEEP_INTERVAL = 10 * 1000; // 10 seconds

async function run(): Promise<void> {
if (context.eventName !== "pull_request") {
info(JSON.stringify(context));
setFailed("This action is for pull_request event only.");
}
const octokit = getOctokit();
Expand Down

0 comments on commit 08e2c76

Please sign in to comment.