Skip to content

Latest commit

 

History

History
119 lines (92 loc) · 6.44 KB

github.md

File metadata and controls

119 lines (92 loc) · 6.44 KB

github.md (scripts/forge/providers)

Revision: 1 Last modified: 2026-12-04T18:15:21Z

Overview

This is a library. It is sourced by scripts/forge/forge.sh, never executed. No shebang, mode rw-rw-r--.

GitHub adapter — GitHub REST (api.github.com) over curl. It implements the same provider contract as scripts/forge/providers/gitea.sh; read that guide for the contract itself. This guide records what is specific to GitHub.

Three GitHub behaviours are handled here and are easy to get wrong:

  1. A renamed repository answers 301 with a url on the same API origin (/repositories/<id>). prov_repo follows that one hop itself (because lib/http.sh never follows redirects) and continues with the canonical full_name, printing NOTE: repo_moved canonical=o/r. A redirect to any other origin is refused before a second request — that is the token pin.
  2. A draft release is invisible to GET /releases/tags/{tag}. On a 404 there, the first page of the release list (100 items) is searched for a draft of that tag, so a draft is reported (and release-ensure refuses it as present_mismatch) instead of a second release being created.
  3. A 422 on create means "already exists" only when the body says errors[].code == "already_exists". Any other 422 (bad target, bad tag) is a real error.

FG_TARGET_DERIVED=0: GitHub's target_commitish is the stored request field — documented as "unused if the Git tag already exists" — so a target read back is the value that was sent, never proof of where the tag points. forge.sh therefore verifies the tag's own commit and treats a matching target as NOTE: target_echo_only.

Prerequisites

As for every provider: sourced by forge.sh after lib/common.sh / lib/credentials.sh / lib/http.sh, with FG_OWNER, FG_REPO, FG_API_BASE, FG_API_ORIGIN and an initialised FG_TMP. Needs jq.

Usage examples

Through forge.sh (this file is not invoked directly):

bash scripts/forge/forge.sh detect --remote origin
bash scripts/forge/forge.sh probe --remote origin
bash scripts/forge/forge.sh tag-get --remote origin --tag v1.2.3

# a draft release is found by the list fallback and refused as a mismatch
bash scripts/forge/forge.sh release-get --remote origin --tag v1.2.3

Contract check in isolation:

bash -c '. scripts/forge/lib/common.sh; . scripts/forge/providers/github.sh; FG_OWNER=o FG_REPO=r; prov_init; \
  printf "%s|%s|%s|%s\n" "$FG_AUTH_FMT" "$FG_RPATH" "$FG_PRE_SUPPORTED" "$FG_TARGET_DERIVED"'

Exit codes

Library: functions return 0 when a state was determined and 1 only when no HTTP answer arrived (FG_NET set). Every HTTP status is a result encoded in the state variables. User-visible exit codes belong to forge.sh — see docs/scripts/forge.md.

Edge cases

  • A 301 to a foreign origin — refused with FG_NET=pin; no second request is made, so the token is never sent to the redirect target.
  • A 301 whose url does not match /repositories/<digits> on the pinned origin — also FG_NET=pin.
  • A 301 that resolves but whose full_name does not survive .-segment validation — FG_CODE is forced to 000, so the caller sees an unusable status rather than a half-trusted name.
  • A 404 on /releases/tags/{tag} — the list fallback runs. If the list itself is not 200, the answer is NOTE: draft_lookup_unavailable (release list answered status=<code>) and the state stays absent with FG_CODE=404. If the list holds 100 items with no draft of the tag, NOTE: draft_lookup_first_page_only — a draft beyond the first page is not seen, stated rather than assumed away.
  • An annotated tag — /git/ref/tags/{tag} reports object.type: tag, so a second GET /git/tags/{sha} peels it; the commit id is only used when the object type resolves to commit. Otherwise TAG: present sha=unknown.
  • A ref that does not actually point at the requested tag name — treated as absent, so a stale or differently-named ref cannot be mistaken for the tag.
  • A 422 without already_exists — a real error; FG_CREATE stays error and forge.sh reports unexpected_status_422.
  • Write paths (POST /releases, PATCH /releases/{id}) are LIVE-UNPROVEN.

Internal behaviour

  1. prov_init — Authorization: Bearer %s, headers Accept: application/vnd.github+json and X-GitHub-Api-Version: 2022-11-28, FG_RPATH="/repos/$FG_OWNER/$FG_REPO", FG_PRE_SUPPORTED=1, FG_TARGET_DERIVED=0.
  2. prov_repo GETs the repository; on 301 it validates the url against the pinned origin and the /repositories/<digits> shape, GETs it, and re-derives owner/repo from full_name after .-segment checks.
  3. prov_release_get GETs /releases/tags/{tag}; 200 fills the release fields, 404 calls _github_draft_lookup.
  4. _github_draft_lookup GETs /releases?per_page=100 and uses jq to count entries with a matching tag_name and draft == true; a hit is written to $FG_TMP/page.json and read back into the release fields with FG_REL_DRAFT=true.
  5. _github_payload builds {tag_name,target_commitish,name,body,draft:false,prerelease} with jq -n, reading the notes with --rawfile.
  6. prov_tag_get GETs /git/ref/tags/{tag}, requires .ref == refs/tags/<tag>, peels an annotated tag via /git/tags/{sha}, and sets FG_TAG_SHA only when the final object type is commit.
  7. prov_probe issues three read-only GETs: repo, /releases?per_page=1, /tags?per_page=1.

Related scripts

  • scripts/forge/providers/gitea.sh — the canonical provider-contract statement this file implements.
  • scripts/forge/forge.sh — the driver (prov_init, prov_tag_get, prov_release_*).
  • scripts/forge/lib/http.sh — the HTTP client; never follows redirects, hence the manual 301 hop here.
  • scripts/release/tag_and_release.sh — keeps its own gh legs by design decision; a separate path, not a duplicate.
  • config/forge/hosts.tsv — maps github.com to github with token variables GITHUB_TOKEN,GH_TOKEN.

Last verified

2026-12-04 — bash -n scripts/forge/providers/github.sh → exit 0. Contract check a reader can repeat:

bash -c '. scripts/forge/lib/common.sh; . scripts/forge/providers/github.sh; FG_OWNER=o FG_REPO=r; prov_init; \
  printf "%s|%s|%s|%s\n" "$FG_AUTH_FMT" "$FG_RPATH" "$FG_PRE_SUPPORTED" "$FG_TARGET_DERIVED"'

Expected: Authorization: Bearer %s|/repos/o/r|1|0.