diff --git a/.github/workflows/context.yaml b/.github/workflows/context.yaml new file mode 100644 index 0000000..c711adb --- /dev/null +++ b/.github/workflows/context.yaml @@ -0,0 +1,87 @@ +--- + +# this file is *not* meant to cover or endorse the use of GitHub Actions, but +# rather to help make automated releases for this project + +name: Context testing + +on: [push, pull_request] # yamllint disable-line rule:truthy + +jobs: + dump_contexts_to_log: + runs-on: ubuntu-latest + steps: + - name: Dump GitHub context + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + # "event.pull_request.commits" is interesting + run: | + echo "$GITHUB_CONTEXT" + echo "$GITHUB_CONTEXT" | jq -c '.event.pull_request.commits' + - name: Count test runs for this PR + if: ${{ !cancelled() }} + id: count + uses: actions/github-script@v9 + with: + script: | + const pr = context.payload.pull_request; + + if (!pr) { + core.setFailed("This workflow must be triggered by a pull request."); + return; + } + + const pr_number = pr.number; + const workflow_name = context.workflow; + const branch_name = pr.head.ref; + const event_match = "pull_request"; + console.log(pr_number); + console.log(workflow_name); + console.log(branch_name); + + if (!pr_number) { + console.log("No pull request number found in context."); + core.setOutput("count", 0); + return; + } + + const runs = await github.rest.actions.listWorkflowRunsForRepo({ + owner: context.repo.owner, + repo: context.repo.repo, + event: event_match, + branch: branch_name, + per_page: 100 + }); + // console.log(runs); + + const filtered = runs.data.workflow_runs.filter(run => + run.name === workflow_name && + run.event === event_match && + run.pull_requests?.some(p => p.number === pr_number) + ); + // console.log(filtered); + console.log(filtered.length); + + core.setOutput("count", filtered.length); + - name: Dump job context + run: echo "Test run count = ${{ steps.count.outputs.count }}" + - name: Dump job context + env: + JOB_CONTEXT: ${{ toJson(job) }} + run: echo "$JOB_CONTEXT" + - name: Dump steps context + env: + STEPS_CONTEXT: ${{ toJson(steps) }} + run: echo "$STEPS_CONTEXT" + - name: Dump runner context + env: + RUNNER_CONTEXT: ${{ toJson(runner) }} + run: echo "$RUNNER_CONTEXT" + - name: Dump strategy context + env: + STRATEGY_CONTEXT: ${{ toJson(strategy) }} + run: echo "$STRATEGY_CONTEXT" + - name: Dump matrix context + env: + MATRIX_CONTEXT: ${{ toJson(matrix) }} + run: echo "$MATRIX_CONTEXT"