diff --git a/.github/actions/notify-irm/action.yml b/.github/actions/notify-irm/action.yml index 658854cd..7364c93c 100644 --- a/.github/actions/notify-irm/action.yml +++ b/.github/actions/notify-irm/action.yml @@ -54,77 +54,4 @@ runs: SHA: ${{ github.sha }} ACTOR: ${{ github.actor }} SERVER_URL: ${{ github.server_url }} - run: |- - set -euo pipefail - if [ -z "${WEBHOOK_URL}" ]; then - echo "::warning::GRAFANA_IRM_WEBHOOK_URL is empty — skipping IRM notification." - exit 0 - fi - if [ "${JOB_FAILED}" != "true" ] && [ "${RESOLVE_ON_SUCCESS}" != "true" ]; then - echo "::notice::All jobs green and resolve-on-success=false — skipping IRM notification." - exit 0 - fi - run_url="${SERVER_URL}/${REPOSITORY}/actions/runs/${RUN_ID}" - - # Branch the payload by outcome. - # alert_uid is intentionally NOT keyed on SHA: a successful run on a - # later commit (different SHA) must resolve the open incident from the - # previous failed commit. Re-runs and follow-up commits on the same - # branch collapse into the same alert group. - if [ "${JOB_FAILED}" = "true" ]; then - state="alerting" - severity="critical" - title="CI failure on ${REF_NAME}: ${WORKFLOW_NAME}" - msg="Workflow '${WORKFLOW_NAME}' failed on ${REF_NAME} @ ${SHA} (actor: ${ACTOR})." - else - state="ok" - severity="info" - title="CI back to green on ${REF_NAME}: ${WORKFLOW_NAME}" - msg="Workflow '${WORKFLOW_NAME}' succeeded on ${REF_NAME} @ ${SHA} (actor: ${ACTOR}). Auto-resolving any open incident." - fi - echo "::notice::Posting state=${state} for ${WORKFLOW_NAME} on ${REF_NAME} @ ${SHA:0:7}" - - # Grafana IRM "Custom" webhook payload — fields map to standard alert - # grouping keys. Both `service` and `service_name` are sent because IRM - # uses `service_name` for SLOs/alerts/synthetic checks but `service` - # for incident & dashboard tags; emitting both keeps every consumer - # happy without an integration-side rename. - payload=$(jq -n \ - --arg uid "gha-${REPOSITORY}-${WORKFLOW_NAME}-${REF_NAME}" \ - --arg title "${title}" \ - --arg state "${state}" \ - --arg severity "${severity}" \ - --arg link "${run_url}" \ - --arg msg "${msg}" \ - --arg service "${SERVICE}" \ - --arg source "github-actions" \ - --arg repo "${REPOSITORY}" \ - --arg workflow "${WORKFLOW_NAME}" \ - --arg branch "${REF_NAME}" \ - --arg sha_short "${SHA:0:7}" \ - --arg actor "${ACTOR}" \ - '{ - alert_uid: $uid, - title: $title, - state: $state, - severity: $severity, - link_to_upstream_details: $link, - message: $msg, - labels: { - service: $service, - service_name: $service, - source: $source, - repo: $repo, - workflow: $workflow, - branch: $branch, - sha: $sha_short, - actor: $actor, - severity: $severity - } - }') - curl --silent --show-error --fail-with-body \ - --max-time 30 \ - --request POST \ - --header 'Content-Type: application/json' \ - --data "${payload}" \ - "${WEBHOOK_URL}" + run: bash "${GITHUB_ACTION_PATH}/notify.sh" diff --git a/.github/actions/notify-irm/notify.sh b/.github/actions/notify-irm/notify.sh new file mode 100755 index 00000000..ebcbf568 --- /dev/null +++ b/.github/actions/notify-irm/notify.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# +# POSTs the alert state to the Grafana IRM webhook (.github/actions/notify-irm, +# step "POST alert state to Grafana IRM webhook"): state=alerting when +# JOB_FAILED is true, state=ok otherwise unless RESOLVE_ON_SUCCESS is false. +# Skips when WEBHOOK_URL is empty. + +set -euo pipefail + +if [ -z "${WEBHOOK_URL}" ]; then + echo "::warning::GRAFANA_IRM_WEBHOOK_URL is empty — skipping IRM notification." + exit 0 +fi +if [ "${JOB_FAILED}" != "true" ] && [ "${RESOLVE_ON_SUCCESS}" != "true" ]; then + echo "::notice::All jobs green and resolve-on-success=false — skipping IRM notification." + exit 0 +fi +run_url="${SERVER_URL}/${REPOSITORY}/actions/runs/${RUN_ID}" + +# Branch the payload by outcome. +# alert_uid is intentionally NOT keyed on SHA: a successful run on a +# later commit (different SHA) must resolve the open incident from the +# previous failed commit. Re-runs and follow-up commits on the same +# branch collapse into the same alert group. +if [ "${JOB_FAILED}" = "true" ]; then + state="alerting" + severity="critical" + title="CI failure on ${REF_NAME}: ${WORKFLOW_NAME}" + msg="Workflow '${WORKFLOW_NAME}' failed on ${REF_NAME} @ ${SHA} (actor: ${ACTOR})." +else + state="ok" + severity="info" + title="CI back to green on ${REF_NAME}: ${WORKFLOW_NAME}" + msg="Workflow '${WORKFLOW_NAME}' succeeded on ${REF_NAME} @ ${SHA} (actor: ${ACTOR}). Auto-resolving any open incident." +fi +echo "::notice::Posting state=${state} for ${WORKFLOW_NAME} on ${REF_NAME} @ ${SHA:0:7}" + +# Grafana IRM "Custom" webhook payload — fields map to standard alert +# grouping keys. Both `service` and `service_name` are sent because IRM +# uses `service_name` for SLOs/alerts/synthetic checks but `service` +# for incident & dashboard tags; emitting both keeps every consumer +# happy without an integration-side rename. +payload=$(jq -n \ + --arg uid "gha-${REPOSITORY}-${WORKFLOW_NAME}-${REF_NAME}" \ + --arg title "${title}" \ + --arg state "${state}" \ + --arg severity "${severity}" \ + --arg link "${run_url}" \ + --arg msg "${msg}" \ + --arg service "${SERVICE}" \ + --arg source "github-actions" \ + --arg repo "${REPOSITORY}" \ + --arg workflow "${WORKFLOW_NAME}" \ + --arg branch "${REF_NAME}" \ + --arg sha_short "${SHA:0:7}" \ + --arg actor "${ACTOR}" \ + '{ + alert_uid: $uid, + title: $title, + state: $state, + severity: $severity, + link_to_upstream_details: $link, + message: $msg, + labels: { + service: $service, + service_name: $service, + source: $source, + repo: $repo, + workflow: $workflow, + branch: $branch, + sha: $sha_short, + actor: $actor, + severity: $severity + } + }') +curl --silent --show-error --fail-with-body \ + --max-time 30 \ + --request POST \ + --header 'Content-Type: application/json' \ + --data "${payload}" \ + "${WEBHOOK_URL}" diff --git a/.github/workflows/lint-caller.yml b/.github/workflows/lint-caller.yml index a4bfc9b0..a4e9e41a 100644 --- a/.github/workflows/lint-caller.yml +++ b/.github/workflows/lint-caller.yml @@ -28,9 +28,8 @@ jobs: # renovate: datasource=github-releases depName=golangci/golangci-lint # golangci-lint-version: "v2.12.2" lint-config-dir: config-sync/files - # Warnings only until this repository's own run: steps are moved into - # scripts (#537); the last PR of that stack makes it blocking. - lint-inline-scripts-blocking: false + # This repository has no inline scripts (#537), so it keeps it that way. + lint-inline-scripts-blocking: true lint-single-job: # Exercises `lint-job-mode: single` — the default for private callers — @@ -48,5 +47,6 @@ jobs: mise-version: "2026.10.19" lint-config-dir: config-sync/files lint-job-mode: single + lint-inline-scripts-blocking: true upload-sarif: false lint-zizmor: false diff --git a/.mise.toml b/.mise.toml index 583aea5d..409e423e 100644 --- a/.mise.toml +++ b/.mise.toml @@ -46,6 +46,8 @@ run = [ "bash ./tests/open-pr-push.sh", "bash ./tests/open-pr-api-commit.sh", "bash ./tests/open-pr-labels.sh", + "bash ./tests/release-publish-scripts.sh", + "bash ./tests/notify-irm-scripts.sh", "bash ./tests/config-sync-ai-toolkit-instructions.sh", "bash ./tests/config-sync-lint-excludes.sh", "bash ./tests/config-sync-renovate-stub.sh", diff --git a/actions/ai-commits-badge/action.yml b/actions/ai-commits-badge/action.yml index 3376e8ca..e611447a 100644 --- a/actions/ai-commits-badge/action.yml +++ b/actions/ai-commits-badge/action.yml @@ -52,7 +52,6 @@ runs: id: generate shell: bash env: - ACTION_PATH: ${{ github.action_path }} BADGE_REF: ${{ inputs.ref }} BADGE_SINCE: ${{ inputs.since }} BADGE_NAME: ${{ inputs.name }} @@ -60,23 +59,4 @@ runs: BADGE_OUT: ${{ inputs.out }} BADGE_THEMES: ${{ inputs.themes }} BADGE_REPO_DIR: ${{ inputs.repo-dir }} - run: |- - set -euo pipefail - args=( - "--repo-dir=${BADGE_REPO_DIR}" - "--ref=${BADGE_REF}" - "--since=${BADGE_SINCE}" - "--name=${BADGE_NAME}" - "--label=${BADGE_LABEL}" - "--out=${BADGE_OUT}" - "--github-output=${GITHUB_OUTPUT}" - ) - case "${BADGE_THEMES}" in - true) args+=(--themes) ;; - false) ;; - *) - echo "::error::themes must be true or false." >&2 - exit 1 - ;; - esac - python3 "${ACTION_PATH}/ai_commits_badge.py" "${args[@]}" + run: bash "${GITHUB_ACTION_PATH}/generate.sh" diff --git a/actions/ai-commits-badge/generate.sh b/actions/ai-commits-badge/generate.sh new file mode 100755 index 00000000..2c8c130b --- /dev/null +++ b/actions/ai-commits-badge/generate.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# +# Generates the AI commits badges (actions/ai-commits-badge, step "Generate AI +# commits badges") by running ai_commits_badge.py with the action's inputs. + +set -euo pipefail + +action_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +args=( + "--repo-dir=${BADGE_REPO_DIR}" + "--ref=${BADGE_REF}" + "--since=${BADGE_SINCE}" + "--name=${BADGE_NAME}" + "--label=${BADGE_LABEL}" + "--out=${BADGE_OUT}" + "--github-output=${GITHUB_OUTPUT}" +) +case "${BADGE_THEMES}" in +true) args+=(--themes) ;; +false) ;; +*) + echo "::error::themes must be true or false." >&2 + exit 1 + ;; +esac +python3 "${action_dir}/ai_commits_badge.py" "${args[@]}" diff --git a/actions/open-pr/action.yml b/actions/open-pr/action.yml index af0ab389..897a9117 100644 --- a/actions/open-pr/action.yml +++ b/actions/open-pr/action.yml @@ -177,85 +177,4 @@ runs: BODY: ${{ inputs.body }} LABELS: ${{ inputs.labels }} DRAFT: ${{ inputs.draft }} - run: |- - set -euo pipefail - - # Collect labels from newline-delimited input. - labels=() - while IFS= read -r line; do - trimmed="${line#"${line%%[![:space:]]*}"}" - trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" - [ -n "${trimmed}" ] && labels+=("${trimmed}") - done <<< "${LABELS}" - - # `gh pr create --label` fails outright on a label the repo doesn't - # have. Create missing labels first (the Pull requests permission is - # enough); drop one that can't be created with a warning rather than - # failing the whole PR. Check each label directly: a listing is paged - # and can miss labels. Re-check after a failed create, which also - # covers another run creating the label first. - label_exists() { - gh api "repos/{owner}/{repo}/labels/$(jq -rn --arg l "$1" '$l | @uri')" --silent >/dev/null 2>&1 - } - label_args=() - add_label_args=() - for label in ${labels[@]+"${labels[@]}"}; do - if ! label_exists "${label}" && - ! gh label create "${label}" >/dev/null 2>&1 && - ! label_exists "${label}"; then - echo "::warning::Label '${label}' does not exist and could not be created; continuing without it." - continue - fi - label_args+=(--label "${label}") - add_label_args+=(--add-label "${label}") - done - - draft_args=() - if [ "${DRAFT}" = "true" ]; then - draft_args+=(--draft) - fi - - # Try to create. If a PR already exists for `head:branch -> base`, - # `gh pr create` exits non-zero — fall back to looking it up. - if gh pr create \ - --base "${BASE}" \ - --head "${BRANCH}" \ - --title "${TITLE}" \ - --body "${BODY}" \ - "${label_args[@]}" \ - "${draft_args[@]}" \ - > /tmp/gh-pr-create.out 2>&1; then - url=$(tail -n1 /tmp/gh-pr-create.out) - number=$(gh pr view "${BRANCH}" --json number --jq '.number') - { - echo "created=true" - echo "pr-url=${url}" - echo "pr-number=${number}" - } >> "${GITHUB_OUTPUT}" - echo "Opened PR #${number}: ${url}" - else - # Surface the original error for debuggability, then try reuse. - cat /tmp/gh-pr-create.out - existing=$(gh pr list \ - --base "${BASE}" \ - --head "${BRANCH}" \ - --state open \ - --json number,url \ - --jq '.[0]') - if [ -z "${existing}" ] || [ "${existing}" = "null" ]; then - echo "::error::gh pr create failed and no existing open PR found for ${BRANCH} -> ${BASE}." >&2 - exit 1 - fi - number=$(printf '%s' "${existing}" | jq -r '.number') - url=$(printf '%s' "${existing}" | jq -r '.url') - if [ "${#add_label_args[@]}" -gt 0 ] && - ! gh pr edit "${number}" "${add_label_args[@]}" >/dev/null; then - echo "::warning::Could not add labels to existing PR #${number}." - fi - { - echo "created=false" - echo "pr-url=${url}" - echo "pr-number=${number}" - } >> "${GITHUB_OUTPUT}" - echo "Reused existing PR #${number}: ${url} (its branch holds the new commit)." - fi + run: bash "${GITHUB_ACTION_PATH}/open-pr.sh" diff --git a/actions/open-pr/open-pr.sh b/actions/open-pr/open-pr.sh new file mode 100755 index 00000000..7f3431f7 --- /dev/null +++ b/actions/open-pr/open-pr.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# +# Opens a pull request for BRANCH -> BASE, or reuses the open one +# (actions/open-pr, step "Open or reuse pull request"). Missing labels are +# created first; one that can't be created is skipped with a warning. Writes +# created, pr-url and pr-number to GITHUB_OUTPUT. + +set -euo pipefail + +# Collect labels from newline-delimited input. +labels=() +# shellcheck disable=SC2153 # LABELS comes from the step env. +while IFS= read -r line; do + trimmed="${line#"${line%%[![:space:]]*}"}" + trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" + [ -n "${trimmed}" ] && labels+=("${trimmed}") +done <<<"${LABELS}" + +# `gh pr create --label` fails outright on a label the repo doesn't +# have. Create missing labels first (the Pull requests permission is +# enough); drop one that can't be created with a warning rather than +# failing the whole PR. Check each label directly: a listing is paged +# and can miss labels. Re-check after a failed create, which also +# covers another run creating the label first. +label_exists() { + gh api "repos/{owner}/{repo}/labels/$(jq -rn --arg l "$1" '$l | @uri')" --silent >/dev/null 2>&1 +} +label_args=() +add_label_args=() +for label in ${labels[@]+"${labels[@]}"}; do + if ! label_exists "${label}" && + ! gh label create "${label}" >/dev/null 2>&1 && + ! label_exists "${label}"; then + echo "::warning::Label '${label}' does not exist and could not be created; continuing without it." + continue + fi + label_args+=(--label "${label}") + add_label_args+=(--add-label "${label}") +done + +draft_args=() +if [ "${DRAFT}" = "true" ]; then + draft_args+=(--draft) +fi + +# Try to create. If a PR already exists for `head:branch -> base`, +# `gh pr create` exits non-zero — fall back to looking it up. +if gh pr create \ + --base "${BASE}" \ + --head "${BRANCH}" \ + --title "${TITLE}" \ + --body "${BODY}" \ + "${label_args[@]}" \ + "${draft_args[@]}" \ + >/tmp/gh-pr-create.out 2>&1; then + url=$(tail -n1 /tmp/gh-pr-create.out) + number=$(gh pr view "${BRANCH}" --json number --jq '.number') + { + echo "created=true" + echo "pr-url=${url}" + echo "pr-number=${number}" + } >>"${GITHUB_OUTPUT}" + echo "Opened PR #${number}: ${url}" +else + # Surface the original error for debuggability, then try reuse. + cat /tmp/gh-pr-create.out + existing=$(gh pr list \ + --base "${BASE}" \ + --head "${BRANCH}" \ + --state open \ + --json number,url \ + --jq '.[0]') + if [ -z "${existing}" ] || [ "${existing}" = "null" ]; then + echo "::error::gh pr create failed and no existing open PR found for ${BRANCH} -> ${BASE}." >&2 + exit 1 + fi + number=$(printf '%s' "${existing}" | jq -r '.number') + url=$(printf '%s' "${existing}" | jq -r '.url') + if [ "${#add_label_args[@]}" -gt 0 ] && + ! gh pr edit "${number}" "${add_label_args[@]}" >/dev/null; then + echo "::warning::Could not add labels to existing PR #${number}." + fi + { + echo "created=false" + echo "pr-url=${url}" + echo "pr-number=${number}" + } >>"${GITHUB_OUTPUT}" + echo "Reused existing PR #${number}: ${url} (its branch holds the new commit)." +fi diff --git a/actions/release-publish/action.yml b/actions/release-publish/action.yml index 914d52e6..e0dc8e16 100644 --- a/actions/release-publish/action.yml +++ b/actions/release-publish/action.yml @@ -54,44 +54,7 @@ runs: TAG: ${{ inputs.tag }} REPO: ${{ github.repository }} EXTRA: ${{ inputs.extra-notes }} - run: | - set -euo pipefail - # Always: --latest (just this tag's slice) + --strip all (no header, - # no footer, no version heading — pure body suitable for a Release). - mise exec -- git-cliff --latest --strip all > /tmp/release-notes.md - - case "${EXTRA}" in - "") - ;; - log-sh) - { - printf '\n---\n\n' - printf '## log.sh distribution\n\n' - printf 'This release includes a vendorable copy of `log.sh`. See ' - printf '[docs/logging.md](https://github.com/%s/blob/%s/docs/logging.md#consuming-logsh-from-other-repositories) ' \ - "${REPO}" "${TAG}" - printf 'for the consumption snippet.\n\n' - printf '```sh\n' - printf 'curl -fsSL https://github.com/%s/releases/download/%s/log.sh -o scripts/lib/log.sh\n' \ - "${REPO}" "${TAG}" - printf 'curl -fsSL https://github.com/%s/releases/download/%s/log.sh.sha256 -o scripts/lib/log.sh.sha256\n' \ - "${REPO}" "${TAG}" - printf '( cd scripts/lib && sha256sum -c log.sh.sha256 )\n' - printf '```\n\n' - printf '### Verifying provenance\n\n' - printf 'Each binary asset is attested via [GitHub Artifact Attestations](https://docs.github.com/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds). ' - printf 'Verify with the GitHub CLI:\n\n' - printf '```sh\n' - printf 'gh attestation verify ./log.sh --repo %s\n' "${REPO}" - printf 'gh attestation verify ./log-sh-%s.tar.gz --repo %s\n' "${TAG}" "${REPO}" - printf '```\n' - } >> /tmp/release-notes.md - ;; - *) - printf 'Unknown extra-notes preset: %s\n' "${EXTRA}" >&2 - exit 1 - ;; - esac + run: bash "${GITHUB_ACTION_PATH}/release-notes.sh" - name: Create immutable GitHub Release shell: bash @@ -100,31 +63,4 @@ runs: TAG: ${{ inputs.tag }} REPO: ${{ github.repository }} ASSETS: ${{ inputs.assets }} - run: |- - set -euo pipefail - - # Convert newline-delimited assets into a bash array, stripping - # blank/whitespace-only lines (so trailing newlines in YAML don't - # turn into spurious "" arguments to gh). - asset_args=() - while IFS= read -r line; do - trimmed="${line#"${line%%[![:space:]]*}"}" - trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" - [ -n "${trimmed}" ] && asset_args+=("${trimmed}") - done <<< "${ASSETS}" - - # Single-shot upload: notes, title, and assets in one call so the - # release is compatible with the repo's Immutable-Releases setting - # (no later `gh release upload --clobber` / `gh release edit`). - if [ ${#asset_args[@]} -gt 0 ]; then - gh release create "${TAG}" \ - --repo "${REPO}" \ - --title "${TAG}" \ - --notes-file /tmp/release-notes.md \ - "${asset_args[@]}" - else - gh release create "${TAG}" \ - --repo "${REPO}" \ - --title "${TAG}" \ - --notes-file /tmp/release-notes.md - fi + run: bash "${GITHUB_ACTION_PATH}/create-release.sh" diff --git a/actions/release-publish/create-release.sh b/actions/release-publish/create-release.sh new file mode 100755 index 00000000..e2953add --- /dev/null +++ b/actions/release-publish/create-release.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# +# Creates the immutable GitHub Release for TAG in REPO (actions/release-publish, +# step "Create immutable GitHub Release") with the notes in /tmp/release-notes.md +# and the newline-delimited ASSETS. RELEASE_NOTES_FILE overrides the notes path +# for tests. + +set -euo pipefail + +notes_file="${RELEASE_NOTES_FILE:-/tmp/release-notes.md}" + +# Convert newline-delimited assets into a bash array, stripping +# blank/whitespace-only lines (so trailing newlines in YAML don't +# turn into spurious "" arguments to gh). +asset_args=() +while IFS= read -r line; do + trimmed="${line#"${line%%[![:space:]]*}"}" + trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" + [ -n "${trimmed}" ] && asset_args+=("${trimmed}") +done <<<"${ASSETS}" + +# Single-shot upload: notes, title, and assets in one call so the +# release is compatible with the repo's Immutable-Releases setting +# (no later `gh release upload --clobber` / `gh release edit`). +if [ ${#asset_args[@]} -gt 0 ]; then + gh release create "${TAG}" \ + --repo "${REPO}" \ + --title "${TAG}" \ + --notes-file "${notes_file}" \ + "${asset_args[@]}" +else + gh release create "${TAG}" \ + --repo "${REPO}" \ + --title "${TAG}" \ + --notes-file "${notes_file}" +fi diff --git a/actions/release-publish/release-notes.sh b/actions/release-publish/release-notes.sh new file mode 100755 index 00000000..70216400 --- /dev/null +++ b/actions/release-publish/release-notes.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# +# Writes the release notes for TAG to /tmp/release-notes.md (actions/release-publish, +# step "Generate release notes"): git-cliff's notes for the latest tag, plus the +# notes block of the EXTRA preset, if any. RELEASE_NOTES_FILE overrides the path +# for tests. + +set -euo pipefail + +notes_file="${RELEASE_NOTES_FILE:-/tmp/release-notes.md}" + +# Always: --latest (just this tag's slice) + --strip all (no header, +# no footer, no version heading — pure body suitable for a Release). +mise exec -- git-cliff --latest --strip all >"${notes_file}" + +case "${EXTRA}" in +"") ;; +log-sh) + # shellcheck disable=SC2016 # Backticks are Markdown, not command substitution. + { + printf '\n---\n\n' + printf '## log.sh distribution\n\n' + printf 'This release includes a vendorable copy of `log.sh`. See ' + printf '[docs/logging.md](https://github.com/%s/blob/%s/docs/logging.md#consuming-logsh-from-other-repositories) ' \ + "${REPO}" "${TAG}" + printf 'for the consumption snippet.\n\n' + printf '```sh\n' + printf 'curl -fsSL https://github.com/%s/releases/download/%s/log.sh -o scripts/lib/log.sh\n' \ + "${REPO}" "${TAG}" + printf 'curl -fsSL https://github.com/%s/releases/download/%s/log.sh.sha256 -o scripts/lib/log.sh.sha256\n' \ + "${REPO}" "${TAG}" + printf '( cd scripts/lib && sha256sum -c log.sh.sha256 )\n' + printf '```\n\n' + printf '### Verifying provenance\n\n' + printf 'Each binary asset is attested via [GitHub Artifact Attestations](https://docs.github.com/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds). ' + printf 'Verify with the GitHub CLI:\n\n' + printf '```sh\n' + printf 'gh attestation verify ./log.sh --repo %s\n' "${REPO}" + printf 'gh attestation verify ./log-sh-%s.tar.gz --repo %s\n' "${TAG}" "${REPO}" + printf '```\n' + } >>"${notes_file}" + ;; +*) + printf 'Unknown extra-notes preset: %s\n' "${EXTRA}" >&2 + exit 1 + ;; +esac diff --git a/docs/architecture.md b/docs/architecture.md index e4400d0b..74d1595a 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -201,7 +201,8 @@ count): Findings are annotated on the file and line of the `run:` key and listed in the job summary with the opt-outs. They are warnings by default; set `lint-inline-scripts-blocking: true` to report errors and fail the linter -(a file that can't be parsed then fails it too). A +(a file that can't be parsed then fails it too). This repository's own Lint +runs it blocking. A later major release makes blocking the default. The checker runs with the runner's system Python, which ships PyYAML (`ubuntu-24.04`). diff --git a/tests/ai-commits-badge.py b/tests/ai-commits-badge.py index e35926d4..2520ec21 100644 --- a/tests/ai-commits-badge.py +++ b/tests/ai-commits-badge.py @@ -80,16 +80,16 @@ def run_action(self, **overrides): "out": str(self.work / "out"), "themes": "false", "repo-dir": str(self.repo), } inputs.update(overrides) - env = dict(self.env, ACTION_PATH=str(ACTION), GITHUB_OUTPUT=str(self.work / "outputs")) + env = dict(self.env, GITHUB_OUTPUT=str(self.work / "outputs")) mappings = re.findall(r"^ ([A-Z_]+): \$\{\{ inputs\.([\w-]+) \}\}$", text, re.M) self.assertEqual({name for _, name in mappings}, set(inputs)) for variable, name in mappings: env[variable] = inputs[name] - step = re.split(r" run: \|[-+]?\n", text, maxsplit=1)[1] - script = "\n".join(line[8:] for line in step.splitlines()) + "\n" + self.assertIn(' run: bash "${GITHUB_ACTION_PATH}/generate.sh"\n', text) + generate = shlex.quote(str(ACTION / "generate.sh").replace("\\", "/")) # Windows's python3 app alias is not necessarily the interpreter running this test. interpreter = shlex.quote(sys.executable.replace("\\", "/")) - script = f"python3() {{ {interpreter} \"$@\"; }}\n" + script + script = f"python3() {{ {interpreter} \"$@\"; }}\nexport -f python3\nbash {generate}\n" return subprocess.run( [shutil.which("bash"), "--noprofile", "--norc", "-c", script], env=env, cwd=self.work, text=True, capture_output=True, encoding="utf-8", diff --git a/tests/lint-inline-scripts.py b/tests/lint-inline-scripts.py index 732ffb73..82540b98 100755 --- a/tests/lint-inline-scripts.py +++ b/tests/lint-inline-scripts.py @@ -8,13 +8,14 @@ Every case in tests/fixtures/lint-inline-scripts-cases.yml is written into a temporary repository and checked. The case passes when the annotations match `expect` one to one and in order, the opt-out count matches `opt-outs`, and the -exit code is 1 only for a blocking run with findings. A final run checks this -repository itself, which must parse cleanly in warning mode, and the files in -CLEAN, whose inline scripts have moved into scripts/, must pass a blocking run. +exit code is 1 only for a blocking run with findings. A final blocking run +checks this repository itself, which must have no inline scripts; its only +opt-outs are the App-key preflights that tests/app-key-confinement.py pins. """ from __future__ import annotations +import importlib.util import os import re import subprocess @@ -27,21 +28,19 @@ ROOT = Path(__file__).resolve().parent.parent CHECKER = ROOT / "scripts" / "lint-inline-scripts.py" CASES = ROOT / "tests" / "fixtures" / "lint-inline-scripts-cases.yml" -# Files of this repository without inline scripts; each layer of #537 adds the -# ones it cleans up, until the whole repository passes. -CLEAN = [ - ".github/workflows/apm-materialize.yml", - ".github/workflows/apm-sync.yml", - ".github/workflows/autofix.yml", - ".github/workflows/config-sync.yml", - ".github/workflows/label-sync.yml", - ".github/workflows/lint.yml", - ".github/workflows/pages.yml", - ".github/workflows/release-please.yml", - ".github/workflows/release.yml", - ".github/workflows/vendored-file-sync.yml", -] SUMMARY = re.compile(r"; (\d+) to move into a script, (\d+) opted out\.$", re.MULTILINE) +# A row of the opt-out table in the step summary: file, job (empty for a +# composite action) and step. +OPT_OUT = re.compile(r"^\| (\S+):\d+ \| (?:job '([^']+)'|[Cc]omposite action), step '([^']+)' \|", re.MULTILINE) + + +def key_preflights() -> set: + """(workflow, job id, step name) of the App-key preflights app-key-confinement.py pins.""" + sys.dont_write_bytecode = True # no __pycache__ in tests/ + spec = importlib.util.spec_from_file_location("app_key_confinement", ROOT / "tests" / "app-key-confinement.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return {(workflow, job, step) for workflow, job, step, _ in module.EMPTINESS_CHECKS} def run(args: list[str], cwd: Path, summary: Path) -> subprocess.CompletedProcess[str]: @@ -103,21 +102,28 @@ def main() -> int: for error in errors: print(f" {error}") with tempfile.TemporaryDirectory() as tmp: - result = run([str(ROOT)], ROOT, Path(tmp) / "summary.md") - if result.returncode != 0 or "Could not parse" in result.stdout or not SUMMARY.search(result.stdout): + summary = Path(tmp) / "summary.md" + result = run(["--blocking", str(ROOT)], ROOT, summary) + rows = OPT_OUT.findall(summary.read_text(encoding="utf-8")) if summary.is_file() else [] + counts = SUMMARY.search(result.stdout) + if result.returncode != 0 or not counts: failed += 1 - print(f"✗ this repository must parse cleanly in warning mode:\n{result.stdout}{result.stderr}") - with tempfile.TemporaryDirectory() as tmp: - result = run(["--blocking", *(str(ROOT / path) for path in CLEAN)], ROOT, Path(tmp) / "summary.md") - if result.returncode != 0: + print(f"✗ this repository must have no inline scripts:\n{result.stdout}{result.stderr}") + opt_outs = set(rows) + preflights = key_preflights() + if counts and int(counts.group(2)) != len(rows): + failed += 1 + print(f"✗ {counts.group(2)} opt-out(s) reported, but {len(rows)} parsed from the step summary") + if opt_outs != preflights: failed += 1 - print(f"✗ {', '.join(CLEAN)} must have no inline scripts:\n{result.stdout}{result.stderr}") + print(f"✗ only the App-key preflights may opt out: unexpected {sorted(opt_outs - preflights)}, " + f"missing {sorted(preflights - opt_outs)}") if failed: print(f"{failed} lint-inline-scripts case(s) failed.") return 1 print( - f"✓ lint-inline-scripts: {len(cases)} cases pass, this repository parses in warning mode, " - f"and {len(CLEAN)} file(s) have no inline scripts." + f"✓ lint-inline-scripts: {len(cases)} cases pass, and this repository has no inline scripts " + f"apart from {len(preflights)} App-key preflight(s)." ) return 0 diff --git a/tests/notify-irm-scripts.sh b/tests/notify-irm-scripts.sh new file mode 100755 index 00000000..48892930 --- /dev/null +++ b/tests/notify-irm-scripts.sh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash +# +# Tests .github/actions/notify-irm/notify.sh (#537): skips, the alerting and ok +# payloads, and a failed POST. A stand-in for curl records the request. + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +action_dir="${repo_root}/.github/actions/notify-irm" +tmp_dir="$(mktemp -d)" +trap 'rm -rf "${tmp_dir}"' EXIT + +fail() { + echo "error: $*" >&2 + exit 1 +} + +command -v jq >/dev/null 2>&1 || fail "jq is not installed" + +# shellcheck disable=SC2016 # Literal text in action.yml. +grep -Fxq ' run: bash "${GITHUB_ACTION_PATH}/notify.sh"' "${action_dir}/action.yml" || + fail "${action_dir}/action.yml no longer runs notify.sh; update this test" + +# Fake curl: the payload goes to ${case_dir}/payload, the URL to ${case_dir}/url. +mkdir -p "${tmp_dir}/bin" +cat >"${tmp_dir}/bin/curl" <<'EOF' +#!/usr/bin/env bash +while [ "$#" -gt 0 ]; do + case "$1" in + --data) printf '%s' "$2" >"${CASE_DIR}/payload" && shift ;; + --max-time | --request | --header) shift ;; + -*) ;; + *) echo "$1" >"${CASE_DIR}/url" ;; + esac + shift +done +[ -z "${CURL_FAILS:-}" ] || { echo "curl: (22) 500" >&2; exit 22; } +EOF +chmod +x "${tmp_dir}/bin/curl" + +# notify NAME=value...: runs notify.sh in a fresh case directory ($case_dir). +notify() { + case_dir="$(mktemp -d "${tmp_dir}/case.XXXXXX")" + env PATH="${tmp_dir}/bin:${PATH}" \ + CASE_DIR="${case_dir}" \ + WEBHOOK_URL="https://irm.example/hook" \ + JOB_FAILED="false" \ + SERVICE="ci" \ + RESOLVE_ON_SUCCESS="true" \ + WORKFLOW_NAME="Lint" \ + RUN_ID="42" \ + REPOSITORY="owner/repo" \ + REF_NAME="main" \ + SHA="0123456789abcdef" \ + ACTOR="octocat" \ + SERVER_URL="https://github.com" \ + "$@" \ + bash "${action_dir}/notify.sh" >"${case_dir}/log" 2>&1 +} + +# field FILTER: a jq field of the last payload. +field() { + jq -r "$1" "${case_dir}/payload" +} + +notify WEBHOOK_URL= JOB_FAILED=true || fail "an empty webhook URL must be skipped, not fail" +grep -Fq "::warning::GRAFANA_IRM_WEBHOOK_URL is empty" "${case_dir}/log" || fail "an empty webhook URL must warn" +[ -e "${case_dir}/payload" ] && fail "an empty webhook URL must not POST" + +notify RESOLVE_ON_SUCCESS=false || fail "a green run without resolve-on-success must be skipped, not fail" +grep -Fq "::notice::All jobs green and resolve-on-success=false" "${case_dir}/log" || fail "the skip must be explained" +[ -e "${case_dir}/payload" ] && fail "a green run without resolve-on-success must not POST" + +notify JOB_FAILED=true RESOLVE_ON_SUCCESS=false || fail "the alerting POST failed: $(cat "${case_dir}/log")" +[ "$(cat "${case_dir}/url")" = "https://irm.example/hook" ] || fail "the webhook URL was not called" +[ "$(field .alert_uid)" = "gha-owner/repo-Lint-main" ] || fail "alert_uid must be keyed on repo, workflow and branch only" +[ "$(field '[.state, .severity, .labels.severity] | join(" ")')" = "alerting critical critical" ] || + fail "a failed run must alert as critical" +[ "$(field .title)" = "CI failure on main: Lint" ] || fail "unexpected alerting title: $(field .title)" +[ "$(field .message)" = "Workflow 'Lint' failed on main @ 0123456789abcdef (actor: octocat)." ] || + fail "unexpected alerting message: $(field .message)" +[ "$(field .link_to_upstream_details)" = "https://github.com/owner/repo/actions/runs/42" ] || fail "the run link is wrong" +[ "$(field '.labels | [.service, .service_name, .source, .repo, .workflow, .branch, .sha, .actor] | join(" ")')" = \ + "ci ci github-actions owner/repo Lint main 0123456 octocat" ] || fail "unexpected labels: $(field .labels)" +grep -Fq "::notice::Posting state=alerting for Lint on main @ 0123456" "${case_dir}/log" || fail "the POST must be announced" + +notify SERVICE=web || fail "the ok POST failed: $(cat "${case_dir}/log")" +[ "$(field '[.state, .severity] | join(" ")')" = "ok info" ] || fail "a green run must post state=ok" +[ "$(field .alert_uid)" = "gha-owner/repo-Lint-main" ] || fail "state=ok must reuse the alerting alert_uid" +[ "$(field .title)" = "CI back to green on main: Lint" ] || fail "unexpected ok title: $(field .title)" +[ "$(field '[.labels.service, .labels.service_name] | join(" ")')" = "web web" ] || fail "the service input was not used" + +notify JOB_FAILED=true CURL_FAILS=1 && fail "a failed POST must fail the step" + +echo "✓ notify-irm: skips, alerting and ok payloads, and a failed POST." diff --git a/tests/open-pr-labels.sh b/tests/open-pr-labels.sh index c8f92477..21b4df74 100755 --- a/tests/open-pr-labels.sh +++ b/tests/open-pr-labels.sh @@ -2,7 +2,7 @@ # # Functional test for the label handling of actions/open-pr. # -# Runs the action's "Open or reuse pull request" script against a fake `gh` +# Runs the action's "Open or reuse pull request" script (open-pr.sh) against a fake `gh` # that, like the real one, rejects `gh pr create --label` for a label the repo # doesn't have. Before the fix, one missing label (e.g. `apm-sync` in a repo # that never synced it) made the whole step fail, and a reused PR never got @@ -21,17 +21,13 @@ fail() { exit 1 } -script="${tmp_dir}/open-pr.sh" -awk ' - $0 == " - name: Open or reuse pull request" { in_step = 1; next } - in_step && $0 ~ /^ run: [|]/ { in_run = 1; next } - in_run { - if ($0 ~ /^ - name:/ || $0 ~ /^ [a-z-]+:/) exit - sub(/^ /, "") - print - } -' "${action}" >"${script}" -[ -s "${script}" ] || fail "could not extract the PR script from ${action}" +script="${repo_root}/actions/open-pr/open-pr.sh" +# shellcheck disable=SC2016 # Literal text in action.yml. +grep -Fq 'run: bash "${GITHUB_ACTION_PATH}/open-pr.sh"' "${action}" || + fail "${action} no longer runs open-pr.sh; update this test" +for variable in GH_TOKEN BRANCH BASE TITLE BODY LABELS DRAFT; do + grep -Eq "^ ${variable}: " "${action}" || fail "${action} does not pass ${variable} to open-pr.sh" +done # Fake gh: repo labels live in $STATE/labels, every call is logged. mkdir -p "${tmp_dir}/bin" @@ -70,6 +66,7 @@ case "$1 $2" in shift done [ -f "${STATE}/pr-exists" ] && { echo "a pull request already exists" >&2; exit 1; } + [ -f "${STATE}/create-fails" ] && { echo "HTTP 502" >&2; exit 1; } echo "https://github.com/o/r/pull/7" ;; "pr view") echo 7 ;; @@ -99,7 +96,7 @@ run_case() { TITLE="chore: materialize" \ BODY="" \ LABELS="$2" \ - DRAFT="false" \ + DRAFT="${DRAFT_INPUT:-false}" \ bash "${script}" >"${STATE}/log" 2>&1 } @@ -132,4 +129,17 @@ run_case recheck $'bug' "" "" bug || fail "PR creation failed when a label looku grep -Fq "::warning::" "${STATE}/log" && fail "an existing label must not be dropped after a failed lookup" grep -F "pr create" "${STATE}/calls" | grep -Fq -- "--label bug" || fail "existing label was dropped after a failed lookup" -echo "✓ open-pr: missing, uncreatable, reused, absent and re-checked labels all handled." +# 6. draft: true opens a draft PR; the default does not. +grep -F "pr create" "${tmp_dir}/none/calls" | grep -Fq -- "--draft" && fail "a PR must not be a draft by default" +DRAFT_INPUT=true run_case draft "" || fail "draft PR creation failed" +grep -F "pr create" "${STATE}/calls" | grep -Fq -- "--draft" || fail "draft: true did not open a draft PR" + +# 7. A failed create with no open PR to reuse fails the step. +mkdir -p "${tmp_dir}/broken" && touch "${tmp_dir}/broken/create-fails" +run_case broken "" && fail "a failed create without an open PR must fail" +grep -Fq "::error::gh pr create failed and no existing open PR found for chore/apm-materialize -> main." "${STATE}/log" || + fail "a failed create must explain the failure: $(cat "${STATE}/log")" +grep -Fq "HTTP 502" "${STATE}/log" || fail "a failed create must surface the gh error" +[ -s "${STATE}/output" ] && fail "a failed create must not set outputs" + +echo "✓ open-pr: missing, uncreatable, reused, absent and re-checked labels, drafts and failed creates all handled." diff --git a/tests/release-publish-scripts.sh b/tests/release-publish-scripts.sh new file mode 100755 index 00000000..dc097a48 --- /dev/null +++ b/tests/release-publish-scripts.sh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash +# +# Tests the scripts of actions/release-publish (#537): release-notes.sh and +# create-release.sh. Stand-ins for `mise` (git-cliff) and `gh` record their +# calls; the action must run each script with the env it reads. + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +action_dir="${repo_root}/actions/release-publish" +action="${action_dir}/action.yml" +tmp_dir="$(mktemp -d)" +trap 'rm -rf "${tmp_dir}"' EXIT + +fail() { + echo "error: $*" >&2 + exit 1 +} + +for script in release-notes create-release; do + # shellcheck disable=SC2016 # Literal text in action.yml. + grep -Fxq " run: bash \"\${GITHUB_ACTION_PATH}/${script}.sh\"" "${action}" || + fail "${action} no longer runs ${script}.sh; update this test" +done +for variable in TAG REPO EXTRA ASSETS GH_TOKEN; do + grep -Eq "^ ${variable}: " "${action}" || fail "${action} does not pass ${variable}" +done + +mkdir -p "${tmp_dir}/bin" +cat >"${tmp_dir}/bin/mise" <<'EOF' +#!/usr/bin/env bash +echo "$*" >>"${CALLS}" +echo "### Features" +EOF +cat >"${tmp_dir}/bin/gh" <<'EOF' +#!/usr/bin/env bash +for arg in "$@"; do printf '[%s]' "${arg}"; done >>"${CALLS}" +echo >>"${CALLS}" +EOF +chmod +x "${tmp_dir}/bin/mise" "${tmp_dir}/bin/gh" + +# run SCRIPT NAME=value...: runs a script in a fresh case directory ($case_dir). +run() { + local script="$1" + shift + case_dir="$(mktemp -d "${tmp_dir}/case.XXXXXX")" + env PATH="${tmp_dir}/bin:${PATH}" \ + CALLS="${case_dir}/calls" \ + RELEASE_NOTES_FILE="${case_dir}/notes.md" \ + TAG="v1.2.0" \ + REPO="owner/repo" \ + EXTRA="" \ + ASSETS="" \ + "$@" \ + bash "${action_dir}/${script}.sh" >"${case_dir}/log" 2>&1 +} + +# --- release-notes.sh --------------------------------------------------------- + +run release-notes || fail "notes without a preset failed: $(cat "${case_dir}/log")" +grep -Fxq "exec -- git-cliff --latest --strip all" "${case_dir}/calls" || fail "git-cliff was not run for the latest tag" +[ "$(cat "${case_dir}/notes.md")" = "### Features" ] || fail "notes without a preset must be git-cliff's output only" + +run release-notes EXTRA=log-sh || fail "notes with the log-sh preset failed: $(cat "${case_dir}/log")" +head -n1 "${case_dir}/notes.md" | grep -Fxq "### Features" || fail "the preset must follow git-cliff's output" +# shellcheck disable=SC2016 # Backticks are Markdown. +for line in \ + '## log.sh distribution' \ + 'This release includes a vendorable copy of `log.sh`. See [docs/logging.md](https://github.com/owner/repo/blob/v1.2.0/docs/logging.md#consuming-logsh-from-other-repositories) for the consumption snippet.' \ + 'curl -fsSL https://github.com/owner/repo/releases/download/v1.2.0/log.sh -o scripts/lib/log.sh' \ + 'curl -fsSL https://github.com/owner/repo/releases/download/v1.2.0/log.sh.sha256 -o scripts/lib/log.sh.sha256' \ + '( cd scripts/lib && sha256sum -c log.sh.sha256 )' \ + 'gh attestation verify ./log.sh --repo owner/repo' \ + 'gh attestation verify ./log-sh-v1.2.0.tar.gz --repo owner/repo'; do + grep -Fxq -- "${line}" "${case_dir}/notes.md" || fail "log-sh notes lack: ${line}" +done +[ "$(grep -c '^```' "${case_dir}/notes.md")" -eq 4 ] || fail "log-sh notes must hold two closed code blocks" + +run release-notes EXTRA=unknown && fail "an unknown preset must fail" +grep -Fxq "Unknown extra-notes preset: unknown" "${case_dir}/log" || fail "an unknown preset must be named" + +# --- create-release.sh -------------------------------------------------------- + +common="[release][create][v1.2.0][--repo][owner/repo][--title][v1.2.0][--notes-file]" + +run create-release || fail "a notes-only release failed: $(cat "${case_dir}/log")" +[ "$(cat "${case_dir}/calls")" = "${common}[${case_dir}/notes.md]" ] || + fail "unexpected notes-only release call: $(cat "${case_dir}/calls")" + +run create-release ASSETS=$'dist/log.sh\n \n dist/log sh.sha256 \n\n' || + fail "a release with assets failed: $(cat "${case_dir}/log")" +[ "$(cat "${case_dir}/calls")" = "${common}[${case_dir}/notes.md][dist/log.sh][dist/log sh.sha256]" ] || + fail "assets must be trimmed, blank lines dropped and spaces kept: $(cat "${case_dir}/calls")" + +echo "✓ release-publish: release notes, presets and single-shot releases with and without assets."