@@ -521,3 +521,175 @@ def test_app_tokens_reach_only_the_writing_steps(self):
521521 sorted (re .findall (r"- id: (\w+_token)\n" , text )),
522522 )
523523 self .assertNotIn ("merge --auto" , text )
524+
525+ def test_tag_job_runs_in_the_release_environment (self ):
526+ self .assert_in_job ("tag" , """\
527+ tag:
528+ runs-on: ubuntu-latest
529+ timeout-minutes: 10
530+ environment: release-trigger
531+ permissions:
532+ contents: read # the checkout and its tags
533+ pull-requests: read # the pull request the pushed commit merged
534+ steps:
535+ """ + self .CHECKOUT )
536+
537+ def test_reads_merged_by_from_the_pull_request_itself (self ):
538+ self .assert_in_job ("tag" , """\
539+ - name: Read the merged pull request
540+ env:
541+ GH_TOKEN: ${{ github.token }}
542+ GH_REPO: ${{ github.repository }}
543+ run: |
544+ numbers="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/commits/$GITHUB_SHA/pulls" \\
545+ --jq '[.[] | select(.merge_commit_sha == env.GITHUB_SHA and .merged_at) | "\\ (.number)"] | join(" ")')"
546+ if [[ -z $numbers ]]; then
547+ echo null > "$RUNNER_TEMP/pr.json"
548+ elif [[ $numbers =~ ^[0-9]+$ ]]; then
549+ timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls/$numbers" > "$RUNNER_TEMP/pr.json"
550+ else
551+ echo "::error::More than one merged pull request has $GITHUB_SHA as its merge commit."
552+ exit 1
553+ fi
554+ """ )
555+
556+ def test_decides_with_the_script_and_fails_red (self ):
557+ self .assert_in_job ("tag" , """\
558+ - name: Decide
559+ id: decide
560+ env:
561+ APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot]
562+ run: |
563+ tags="$RUNNER_TEMP/tags.txt"
564+ git show-ref --tags --dereference > "$tags"
565+ result="$(python3 -I scripts/release_pr.py tag-decision --tags "$tags" --pr "$RUNNER_TEMP/pr.json" \\
566+ --head "$GITHUB_SHA" --app-login "$APP_LOGIN")"
567+ decision="$(sed -n 's/^decision=//p' <<< "$result")"
568+ version="$(sed -n 's/^version=//p' <<< "$result")"
569+ case $decision in
570+ tag) echo "Tagging v$version at $GITHUB_SHA." ;;
571+ noop) echo "v$version already points at $GITHUB_SHA." ;;
572+ skip) echo "$GITHUB_SHA is not drptbl's merge of the App's release pull request, so nothing is tagged." ;;
573+ fail)
574+ echo "::error::v$version cannot be tagged at $GITHUB_SHA." \\
575+ "The version is not x.y.z+tibiash.N, or the tag points at another commit."
576+ exit 1
577+ ;;
578+ *)
579+ echo "::error::release_pr.py tag-decision printed an unknown decision."
580+ exit 1
581+ ;;
582+ esac
583+ echo "decision=$decision" >> "$GITHUB_OUTPUT"
584+ echo "version=$version" >> "$GITHUB_OUTPUT"
585+ """ )
586+
587+ def test_creates_the_tag_with_a_contents_token (self ):
588+ self .assert_in_job ("tag" , """\
589+ - id: tag_token
590+ if: ${{ steps.decide.outputs.decision == 'tag' }}
591+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
592+ with:
593+ client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
594+ private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
595+ owner: tibia-sh
596+ repositories: tibiawiki-sql
597+ permission-contents: write
598+ - name: Create the tag
599+ if: ${{ steps.decide.outputs.decision == 'tag' }}
600+ env:
601+ GH_TOKEN: ${{ steps.tag_token.outputs.token }}
602+ GH_REPO: ${{ github.repository }}
603+ VERSION: ${{ steps.decide.outputs.version }}
604+ run: |
605+ if [[ ! $VERSION =~ ^[0-9]+\\ .[0-9]+\\ .[0-9]+\\ +tibiash\\ .[0-9]+$ || ! $GITHUB_SHA =~ ^[0-9a-f]{40}$ ]]; then
606+ echo "::error::The version is not x.y.z+tibiash.N, or the pushed commit is not a full SHA."
607+ exit 1
608+ fi
609+ timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/git/refs" --silent \\
610+ -f ref="refs/tags/v$VERSION" -f sha="$GITHUB_SHA"
611+ echo "Tagged v$VERSION at $GITHUB_SHA."
612+ """ )
613+
614+
615+ class TestReleaseWorkflow (WorkflowTestCase ):
616+ WORKFLOW = "release.yml"
617+
618+ def test_publish_step_reports_published_after_the_upload (self ):
619+ self .assert_in_job ("release" , """\
620+ outputs:
621+ published: ${{ steps.publish.outputs.published }} # 'true' once the release and its assets are uploaded
622+ """ )
623+ self .assert_in_job ("release" , """\
624+ - name: Create the release
625+ id: publish
626+ if: github.event_name == 'push'
627+ env:
628+ GH_TOKEN: ${{ github.token }}
629+ GH_REPO: ${{ github.repository }}
630+ WHEEL: ${{ steps.build.outputs.wheel }}
631+ SDIST: ${{ steps.build.outputs.sdist }}
632+ run: |
633+ gh release create "$GITHUB_REF_NAME" "$WHEEL" "$SDIST" dist/SHA256SUMS \\
634+ --verify-tag --notes-file "$RUNNER_TEMP/notes.md"
635+ echo "published=true" >> "$GITHUB_OUTPUT"
636+ """ )
637+ release = job (self .WORKFLOW , "release" )
638+ self .assertLess (release .index ("uses: actions/attest-build-provenance@" ), release .index ("id: publish" ))
639+ self .assertEqual (1 , self .text ().count ("published=true" ))
640+
641+ def test_downstream_runs_only_after_a_tag_push_published (self ):
642+ # A dry run is a workflow_dispatch on main, which publishes nothing, so it has no downstream job.
643+ self .assert_in_job ("downstream" , """\
644+ downstream:
645+ needs: [check, release]
646+ if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') &&
647+ needs.release.outputs.published == 'true'
648+ runs-on: ubuntu-latest
649+ timeout-minutes: 8
650+ environment: release-trigger
651+ permissions: {}
652+ env:
653+ VERSION: ${{ needs.check.outputs.version }}
654+ steps:
655+ """ )
656+
657+ def test_downstream_dispatches_with_a_contents_token (self ):
658+ self .assert_in_job ("downstream" , """\
659+ - id: token
660+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
661+ with:
662+ client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
663+ private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
664+ owner: tibia-sh
665+ repositories: tibiawiki-mcp
666+ permission-contents: write
667+ """ )
668+ self .assert_in_job ("downstream" , """\
669+ - name: Tell tibiawiki-mcp about the release
670+ env:
671+ GH_TOKEN: ${{ steps.token.outputs.token }}
672+ run: |
673+ if [[ ! $VERSION =~ ^[0-9]+\\ .[0-9]+\\ .[0-9]+\\ +tibiash\\ .[0-9]+$ || $GITHUB_REF_NAME != "v$VERSION" ]]; then
674+ echo "::error::The released version is not x.y.z+tibiash.N, or it is not the tag's."
675+ exit 1
676+ fi
677+ # shellcheck disable=SC2016 # $version is jq's, not the shell's
678+ payload='{event_type: "generator-release", client_payload: {version: $version}}'
679+ body="$(jq -n --arg version "$VERSION" "$payload")"
680+ for attempt in 1 2 3; do
681+ if [ "$attempt" -gt 1 ]; then
682+ sleep 30
683+ fi
684+ if timeout --kill-after=10 120 gh api repos/tibia-sh/tibiawiki-mcp/dispatches --input - <<< "$body"; then
685+ echo "Told tibia-sh/tibiawiki-mcp about $VERSION in attempt $attempt."
686+ exit 0
687+ fi
688+ done
689+ echo "::error::Could not tell tibia-sh/tibiawiki-mcp about $VERSION in 3 attempts, 30 seconds apart." \\
690+ "Run generator.yml there by hand with the version, as README.md describes."
691+ exit 1
692+ """ )
693+ downstream = job (self .WORKFLOW , "downstream" )
694+ self .assertNotIn ("checkout" , downstream )
695+ self .assertEqual (1 , downstream .count ("uses:" ))
0 commit comments