Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion .github/workflows/base-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ on:
artifact-name:
type: string
default: acceptance-base-check
share-build:
description: "Opt in: reuse the head build that run-specs.yml (called with share-build: true in the SAME run, and ordered before this with needs:) uploaded, instead of rebuilding head. Falls back to building when the artifact is missing. Skipped on fork PRs. See docs/adopting.md."
type: boolean
default: false
share-build-artifact-name:
description: "The artifact-name that run-specs.yml was called with (its default is acceptance-results)."
type: string
default: acceptance-results
outputs:
verdict:
description: "pass, red-on-base-green-on-head, fail, passes-on-base, not-run or skipped. In mode report a failure still leaves the jobs green: read this."
value: ${{ jobs.verdict.outputs.verdict || (jobs.detect.outputs.has_specs == 'false' && 'skipped' || 'fail') }}
report-only:
description: "true when mode is report."
value: ${{ jobs.detect.outputs.report_only || 'false' }}
build-reused:
description: "true when the head build was taken from run-specs instead of being rebuilt."
value: ${{ jobs.head-run.outputs.build_reused || 'false' }}
secrets:
ACCEPTANCE_USER:
required: false
Expand All @@ -106,6 +124,7 @@ jobs:
has_specs: ${{ steps.diff.outputs.has_specs }}
issues: ${{ steps.diff.outputs.issues }}
base_sha: ${{ steps.diff.outputs.base_sha }}
report_only: ${{ steps.diff.outputs.report_only }}
steps:
- name: Only for pull_request
env:
Expand Down Expand Up @@ -158,6 +177,8 @@ jobs:
- name: Changed acceptance specs
id: diff
working-directory: ${{ inputs.working-directory }}
env:
MODE: ${{ inputs.mode }}
run: |
# --relative: paths are relative to the working directory, and only it is considered.
git diff --relative --name-status HEAD^1 HEAD -- 'acceptance/*/spec.json' > "$RUNNER_TEMP/changed-specs.txt"
Expand All @@ -166,6 +187,7 @@ jobs:
{
echo "issues=$ISSUES"
echo "base_sha=$(git rev-parse HEAD^1)"
if [ "$MODE" = "report" ]; then echo "report_only=true"; else echo "report_only=false"; fi
if [ -n "$ISSUES" ]; then echo "has_specs=true"; else echo "has_specs=false"; fi
} >> "$GITHUB_OUTPUT"

Expand All @@ -178,6 +200,8 @@ jobs:
timeout-minutes: ${{ inputs.timeout-minutes }}
env:
CHANGED_ISSUES: ${{ needs.detect.outputs.issues }}
outputs:
build_reused: ${{ steps.unpack.outputs.reused }}
steps:
- name: Only for pull_request
env:
Expand Down Expand Up @@ -250,8 +274,29 @@ jobs:
fi
{ echo "skip=$skip"; echo "base_url=$base_url"; } >> "$GITHUB_OUTPUT"

# Opt-in reuse (share-build): take the head build run-specs uploaded in this same run. A missing
# artifact or a bad archive falls back to the normal build below. Never on forks.
- name: Fetch the head build shared by run-specs
id: share
if: inputs.share-build && inputs.build-command != '' && steps.decide.outputs.skip != 'true' && github.event.pull_request.head.repo.full_name == github.repository
continue-on-error: true
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: ${{ inputs.share-build-artifact-name }}-build-${{ github.run_id }}
path: ${{ runner.temp }}/shared-build

- name: Unpack the shared head build
id: unpack
if: steps.share.outcome == 'success'
continue-on-error: true
working-directory: ${{ inputs.working-directory }}
run: |
bash "$RUNNER_TEMP/acceptance-scripts/share-build.sh" unpack "$RUNNER_TEMP/shared-build/acceptance-build-share.tar"
echo "reused=true" >> "$GITHUB_OUTPUT"
echo "::notice::Head build reused from run-specs (not rebuilt)."

- name: Build
if: inputs.build-command != '' && steps.decide.outputs.skip != 'true'
if: inputs.build-command != '' && steps.decide.outputs.skip != 'true' && steps.unpack.outputs.reused != 'true'
working-directory: ${{ inputs.working-directory }}
env:
BUILD_COMMAND: ${{ inputs.build-command }}
Expand Down Expand Up @@ -529,6 +574,8 @@ jobs:
if: always() && needs.detect.outputs.has_specs == 'true'
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 5
outputs:
verdict: ${{ steps.verdict.outputs.verdict }}
steps:
- name: Only for pull_request
env:
Expand Down Expand Up @@ -591,6 +638,7 @@ jobs:
name: ${{ inputs.artifact-name }}-base-${{ github.run_id }}
path: base-results
- name: Each changed spec is red on base and green on head
id: verdict
working-directory: ${{ inputs.working-directory }}
env:
PR_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }}
Expand All @@ -608,5 +656,9 @@ jobs:
--mode "$MODE" \
--out "$RUNNER_TEMP/spec-first-verdict.json" > "$RUNNER_TEMP/spec-first.md" || rc=$?
cat "$RUNNER_TEMP/spec-first.md"
# One clear verdict line first (job summary, annotation title, outputs), then the per-spec detail.
# It describes the result and never changes the exit code below.
node "$RUNNER_TEMP/acceptance-scripts/acceptance-verdict.mjs" --kind base-check \
--input "$RUNNER_TEMP/spec-first-verdict.json" --mode "$MODE"
grep -v '^::' "$RUNNER_TEMP/spec-first.md" >> "$GITHUB_STEP_SUMMARY" || true
exit "$rc"
165 changes: 165 additions & 0 deletions .github/workflows/comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
name: acceptance comment

# Reusable workflow (on: workflow_call). Posts ONE sticky pull request comment that summarises the
# acceptance jobs in a small table, found by a hidden marker and edited in place on every push (never a
# new comment per push). It exists because `mode: report` keeps every job green even when a spec is red,
# so the check marks hide the result; the comment shows it.
#
# Call it from a `pull_request` workflow ONLY, pinned to a full commit SHA, as its own job that needs the
# others and is the ONLY job granted `pull-requests: write`:
# comment:
# needs: [convention, specs, spec-first]
# if: always()
# permissions: { contents: read, pull-requests: write }
# uses: spiculedata/acceptance-spec/.github/workflows/comment.yml@<40-char-sha>
# with: { comment: true, convention-verdict: ..., run-specs-verdict: ..., base-check-verdict: ..., report-only: ... }
#
# Trust model: this is the only workflow here with a write scope, and it is the least powerful job that
# can hold it. It does NOT check out the caller's repository or run any PR code or PR text: it fetches
# only this repository's scripts at the pinned ref, reads only the verdict strings its caller passes in,
# and uses the token for two calls (list comments, create or update one). Fork PRs get a read-only token,
# so the job is skipped for them. The jobs that DO execute PR code (build, start, specs) never get a
# write scope.

on:
workflow_call:
inputs:
comment:
description: "Opt in. Nothing is posted unless this is true."
type: boolean
default: false
convention-verdict:
description: "needs.<convention job>.outputs.verdict. Empty: that row is left out."
type: string
default: ""
run-specs-verdict:
description: "needs.<run-specs job>.outputs.verdict. Empty: that row is left out."
type: string
default: ""
base-check-verdict:
description: "needs.<base-check job>.outputs.verdict. Empty: that row is left out."
type: string
default: ""
report-only:
description: "true when the jobs ran with mode: report (adds a note to the comment)."
type: string
default: "false"
heading:
description: "Heading of the comment."
type: string
default: "Acceptance results"
runs-on:
type: string
default: ubuntu-latest
node-version:
type: string
default: "22"

permissions:
contents: read

defaults:
run:
shell: bash

jobs:
comment:
name: acceptance comment
# Same-repository PRs only: a fork's token is read-only and the call would only fail.
if: inputs.comment && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 5
permissions:
contents: read
pull-requests: write
concurrency:
group: acceptance-comment-${{ github.event.pull_request.number }}
cancel-in-progress: false
steps:
- name: Only for pull_request
env:
EVENT_NAME: ${{ github.event_name }}
run: |
if [ "$EVENT_NAME" != "pull_request" ]; then
echo "::error::acceptance-spec workflows must be called from a pull_request workflow (got $EVENT_NAME). Use the plain pull_request trigger."
exit 1
fi

# job.workflow_repository / job.workflow_sha identify the REUSABLE workflow file that defines
# this job, i.e. this repository at the SHA the caller pinned in `uses:`. See README, "How the scripts are loaded".
- name: Guard the scripts ref
env:
WF_REPO: ${{ job.workflow_repository }}
WF_SHA: ${{ job.workflow_sha }}
run: |
if [ -z "$WF_REPO" ] || ! printf '%s' "$WF_SHA" | grep -Eq '^[0-9a-f]{40}$'; then
echo "::error::job.workflow_repository / job.workflow_sha are not populated; cannot load the scripts at the pinned ref."
exit 1
fi
echo "acceptance-spec scripts: $WF_REPO @ $WF_SHA"

# This is the only checkout in this workflow: this repository's scripts, never the caller's code.
- name: Fetch the acceptance-spec scripts (same pinned ref as this workflow)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ job.workflow_repository }}
ref: ${{ job.workflow_sha }}
path: .acceptance-spec-src
sparse-checkout: scripts
persist-credentials: false

- name: Move the scripts out of the workspace
env:
WF_SHA: ${{ job.workflow_sha }}
run: |
set -euo pipefail
[ "$(git -C .acceptance-spec-src rev-parse HEAD)" = "$WF_SHA" ]
mv .acceptance-spec-src/scripts "$RUNNER_TEMP/acceptance-scripts"
rm -rf .acceptance-spec-src

- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ${{ inputs.node-version }}

- name: Render the comment
id: render
env:
CONVENTION_VERDICT: ${{ inputs.convention-verdict }}
RUN_SPECS_VERDICT: ${{ inputs.run-specs-verdict }}
BASE_CHECK_VERDICT: ${{ inputs.base-check-verdict }}
REPORT_ONLY: ${{ inputs.report-only }}
HEADING: ${{ inputs.heading }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
rc=0
node "$RUNNER_TEMP/acceptance-scripts/acceptance-comment.mjs" --out "$RUNNER_TEMP/comment.md" || rc=$?
if [ "$rc" -eq 3 ]; then echo "post=false" >> "$GITHUB_OUTPUT"; exit 0; fi
if [ "$rc" -ne 0 ]; then exit "$rc"; fi
echo "post=true" >> "$GITHUB_OUTPUT"
cat "$RUNNER_TEMP/comment.md" >> "$GITHUB_STEP_SUMMARY"

# Find-and-update: the comment is found by its hidden marker AND by its author (the Actions bot), so
# a look-alike comment from anyone else is never edited. A failed call is a warning, not a red job:
# the comment is a convenience, the verdicts are also in each job summary.
- name: Create or update the sticky comment
if: steps.render.outputs.post == 'true'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -uo pipefail
MARKER="$(node "$RUNNER_TEMP/acceptance-scripts/acceptance-comment.mjs" --marker)"
export MARKER
id=$(gh api --paginate "repos/$REPO/issues/$PR_NUMBER/comments" \
--jq '.[] | select(.user.login == "github-actions[bot]" and (.body | contains(env.MARKER))) | .id' | sed -n 1p) || id=""
if [ -n "$id" ]; then
gh api --method PATCH "repos/$REPO/issues/comments/$id" -F "body=@$RUNNER_TEMP/comment.md" > /dev/null \
&& echo "Updated comment $id." \
|| echo "::warning::Could not update the acceptance comment (is pull-requests: write granted to this job?)."
else
gh api --method POST "repos/$REPO/issues/$PR_NUMBER/comments" -F "body=@$RUNNER_TEMP/comment.md" > /dev/null \
&& echo "Created the comment." \
|| echo "::warning::Could not create the acceptance comment (is pull-requests: write granted to this job?)."
fi
25 changes: 24 additions & 1 deletion .github/workflows/convention.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ on:
node-version:
type: string
default: "22"
outputs:
verdict:
description: "pass, fail, or skipped (nothing owed). In mode report a fail still leaves the job green: read this."
value: ${{ jobs.convention.outputs.verdict || 'fail' }}
report-only:
description: "true when mode is report."
value: ${{ jobs.convention.outputs.report-only || 'false' }}

permissions:
contents: read
Expand All @@ -45,6 +52,9 @@ jobs:
name: acceptance convention
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 5
outputs:
verdict: ${{ steps.verdict.outputs.verdict }}
report-only: ${{ steps.verdict.outputs.report-only }}
steps:
- name: Only for pull_request
env:
Expand Down Expand Up @@ -109,4 +119,17 @@ jobs:
PR_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }}
ACCEPTANCE_ENFORCE_FROM_ISSUE: ${{ inputs.enforce-from-issue }}
ACCEPTANCE_MODE: ${{ inputs.mode }}
run: node "$RUNNER_TEMP/acceptance-scripts/acceptance-check.mjs" --changed-files "$RUNNER_TEMP/changed-files.txt"
run: |
rc=0
node "$RUNNER_TEMP/acceptance-scripts/acceptance-check.mjs" --changed-files "$RUNNER_TEMP/changed-files.txt" \
--json "$RUNNER_TEMP/convention-result.json" || rc=$?
exit "$rc"

# One clear verdict line (job summary, annotation title, workflow output), even when the check above
# failed or, in mode report, only warned. It never changes the job's result.
- name: Verdict
id: verdict
if: always()
env:
MODE: ${{ inputs.mode }}
run: node "$RUNNER_TEMP/acceptance-scripts/acceptance-verdict.mjs" --kind convention --input "$RUNNER_TEMP/convention-result.json" --mode "$MODE"
Loading
Loading