| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | : "${ARTIFACTS_DIR:?}" "${GH_TOKEN:?}" "${APP_ID:?}" "${GITHUB_REPOSITORY:?}" "${RUN_URL:?}" "${TRUSTED_DIR:?}" |
| 5 | |
| 6 | git config user.name 'codewhale-agent[bot]' |
| 7 | git config user.email "${APP_ID}+codewhale-agent[bot]@users.noreply.github.com" |
| 8 | git fetch --quiet origin main |
| 9 | base=$(git rev-parse origin/main) |
| 10 | |
| 11 | published=0 |
| 12 | for lane in derived copy-freshness link-health docs-drift; do |
| 13 | dir="${ARTIFACTS_DIR}/web-maintainer-${lane}" |
| 14 | patch="${dir}/change.patch" |
| 15 | if [ ! -s "$patch" ]; then |
| 16 | echo "${lane}: no patch to publish" |
| 17 | continue |
| 18 | fi |
| 19 | rules="${TRUSTED_DIR}/allowlist.txt" |
| 20 | [ "$lane" = derived ] && rules="${TRUSTED_DIR}/derived.txt" |
| 21 | |
| 22 | branch="web-maintainer/${lane}" |
| 23 | git checkout --quiet --detach "$base" |
| 24 | git reset --quiet --hard "$base" |
| 25 | git clean --quiet -fdx -e node_modules |
| 26 | |
| 27 | if [ "$(wc -c < "$patch")" -gt 262144 ]; then |
| 28 | echo "::error::${lane}: patch exceeds 256 KiB; not published" |
| 29 | continue |
| 30 | fi |
| 31 | if ! git apply --index --whitespace=nowarn "$patch"; then |
| 32 | echo "::error::${lane}: patch does not apply to current main; not published" |
| 33 | continue |
| 34 | fi |
| 35 | if [ -n "$(git diff --cached --summary)" ]; then |
| 36 | echo "::error::${lane}: patch creates, deletes, renames or changes modes; not published" |
| 37 | git reset --quiet --hard "$base" |
| 38 | continue |
| 39 | fi |
| 40 | if ! git diff --cached --name-status | node "${TRUSTED_DIR}/check-allowlist.mjs" "$rules" "$lane"; then |
| 41 | echo "::error::${lane}: patch is outside the allowlist; not published" |
| 42 | git reset --quiet --hard "$base" |
| 43 | continue |
| 44 | fi |
| 45 | |
| 46 | title="chore(web): ${lane} maintenance" |
| 47 | git commit --quiet -m "$title" \ |
| 48 | -m "Proposed by .github/workflows/web-maintainer.yml (lane: ${lane}). Reviewed by a person before merge." |
| 49 | git push --force-with-lease="refs/heads/${branch}" \ |
| 50 | "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" "HEAD:refs/heads/${branch}" |
| 51 | |
| 52 | { |
| 53 | echo "Proposed by the web maintainer, lane \`${lane}\`. Nothing merges automatically; review it like any other change." |
| 54 | echo |
| 55 | if [ "$lane" = derived ]; then |
| 56 | echo "Deterministic: regenerated files only. No model wrote these changes." |
| 57 | else |
| 58 | echo "A model (DeepSeek through \`codewhale exec\`) edited existing website copy inside this lane's path allowlist. The job checked every changed path against \`web/maintainer/allowlist.txt\`, then ran \`npm run check\` and \`vitest\` in \`web/\` before this pull request was opened." |
| 59 | fi |
| 60 | echo |
| 61 | echo "Files changed ($(git diff --name-only "$base" HEAD | wc -l | tr -d ' ')):" |
| 62 | git diff --name-only "$base" HEAD | sed 's/^/- `/; s/$/`/' |
| 63 | echo |
| 64 | echo '```text' |
| 65 | git diff --stat "$base" HEAD |
| 66 | echo '```' |
| 67 | if [ "$lane" != derived ]; then |
| 68 | echo |
| 69 | echo "Model-written copy. Read the full diff, not only the diffstat, before approving." |
| 70 | fi |
| 71 | echo |
| 72 | echo "Run: ${RUN_URL}" |
| 73 | echo |
| 74 | echo "To stop these proposals, unset the repository variable \`WEB_MAINTAINER_ENABLED\`. See \`web/maintainer/README.md\`." |
| 75 | } > "${RUNNER_TEMP:-/tmp}/pr-body-${lane}.md" |
| 76 | |
| 77 | existing=$(gh pr list --head "$branch" --state open --json number --jq '.[0].number // empty') |
| 78 | if [ -n "$existing" ]; then |
| 79 | gh pr edit "$existing" --title "$title" --body-file "${RUNNER_TEMP:-/tmp}/pr-body-${lane}.md" |
| 80 | echo "${lane}: updated #${existing}" | tee -a "${GITHUB_STEP_SUMMARY:-/dev/null}" |
| 81 | else |
| 82 | url=$(gh pr create --base main --head "$branch" --title "$title" --body-file "${RUNNER_TEMP:-/tmp}/pr-body-${lane}.md") |
| 83 | echo "${lane}: opened ${url}" | tee -a "${GITHUB_STEP_SUMMARY:-/dev/null}" |
| 84 | fi |
| 85 | published=$((published + 1)) |
| 86 | done |
| 87 | |
| 88 | echo "published ${published} pull request(s)" | tee -a "${GITHUB_STEP_SUMMARY:-/dev/null}" |
| 89 |