Skip to content

Latest commit

 

History

History
93 lines (73 loc) · 5.28 KB

provider.md

File metadata and controls

93 lines (73 loc) · 5.28 KB

provider.sh — guide

Revision: 1 Last modified: 2026-12-04T00:00:00Z

Library: scripts/pr/lib/provider.sh (sourced by scripts/pr/lib/preflight.sh and lib/steps.sh; never executed on its own). Engine guide: docs/scripts/pr_deliver.md. Manual: docs/manuals/pr_delivery_engine_manual.md.

Overview

Decide WHICH forge answers for the configured remote, WHICH credential may be used for it, and route every provider read and write to one adapter. The adapter surface is deliberately small and provider-neutral, so the state machine in lib/steps.sh never contains a provider name.

This file owns four things: resolving the provider from the remote URL, probing the credential, calling a REST provider with the token OUT of argv and out of the child environment, and dispatching each verb to prv_<provider>_<verb> (implemented in provider_github.sh and provider_rest.sh).

Prerequisites

  • config/forge/hosts.tsv — the existing host → provider → api_base → token_vars map, reused, never copied.
  • git (the remote URL is the only place a provider is derived from), gh for GitHub, curl + jq for the REST providers.
  • Credentials supplied by name in the environment. The value is read at call time and never printed.

Usage examples

. "$PR_SELF_DIR/lib/provider.sh"

pr_provider_resolve || exit 3        # sets PR_PROVIDER PR_API_BASE PR_TOKEN_VARS PR_REMOTE_URL PR_REPO_SLUG
pr_cred_probe   || exit 3            # sets PR_CRED_NAME PR_CRED_KIND (env|keyring); never a value
prv_identity                         # the authenticated account name
prv_repo_probe                       # archived=… default_branch=… allow_merge_commit=… push=… admin=…
prv_pr_list_head task/CON-641-x      # number= state= head= base= draft= url=

Which credential answered, without ever printing it:

$ bash scripts/pr/pr_deliver.sh preflight --task CON-641 | sed -n '/^CHECK P5/p'
CHECK P5 credential PASS identity=… via=env:GITHUB_TOKEN_PR_WRITE

Edge cases

  • pr_remote unset is tolerated only when the repository has exactly ONE remote; two or more is provider_ambiguous:pr_remote_unset_and_<n>_remotes.
  • A remote with several push URLs is refused (provider_ambiguous:pr_remote_<r>_has_<n>_push_urls). A fan-out remote would push a task branch to every mirror, widening retirement and publishing the branch where nobody expects it. This is why the declaration names a single-host remote.
  • A configured pr_provider that disagrees with the hosts map is a refusal, not a preference: provider_ambiguous:config_says_<a>_host_says_<b>.
  • An unknown host is never guessed at (unknown_forge:<host>), and a provider with no adapter is provider_unimplemented:<provider>.
  • Every credential candidate is tried, then the GitHub keyring last. A candidate that is unset is recorded as (unset), a candidate the provider refuses as (refused); the first line of the provider's own error text is kept and redacted, so a refusal can quote the platform instead of inventing a reason.
  • A missing jq is a refusal, never a silent empty answer (pr_tool_absent:jq at preflight; the REST adapters print a diagnostic and return 1).
  • The token never appears in argv or in the child environment: pr_rest_call writes a 0600 curl config to $TMPDIR, passes it with --config, and removes it immediately after the call.

Internal behaviour

  • pr_host_of_url / pr_slug_of_url derive the host and owner/repo from either an https:// or a git@host:path URL; pr_hosts_row looks the host up in the TSV.
  • pr_cred_probe probes one cheap authenticated GET (repos/<slug> for GitHub, /projects/<urlencoded> for GitLab, /repos/<slug> for Gitea) and records the winner in PR_CRED_NAME / PR_CRED_KIND. On total failure it sets PR_CRED_TRIED and PR_CRED_LAST_ERR for the refusal text.
  • pr_cred_describe is the honest one-line record: keyring-oauth, env:<NAME> or none.
  • pr_rest_call METHOD PATH [BODY_FILE] prints the body and returns 0 on 2xx, otherwise prints HTTP <code> and returns 1; PR_LAST_HTTP carries the code. A missing token answers HTTP 401 without a request.
  • pr_adapter VERB … resolves prv_<provider>_<verb>; a missing implementation prints REASON: provider_unimplemented:<provider>:<verb> and returns 3, so a partially implemented provider fails loudly instead of silently doing nothing.
  • Every call clears the caller's GIT_* repository-locating variables (pr_git in lib/common.sh), so an inherited environment can never redirect a delivery at a different repository.

Related scripts

  • provider_github.md — the gh adapter (the only lifecycle measured on this host).
  • provider_rest.md — the GitLab and Gitea REST adapters, and what is UNCONFIRMED there.
  • preflight.md — consumes the resolve/probe results as checks P3–P5.
  • common.md — pr_field, pr_redact, pr_git.
  • forge_tool.md — the sibling tool that reads the same config/forge/hosts.tsv.

Last verified

2026-12-04 — read against scripts/pr/lib/provider.sh (345 lines). The config/forge/hosts.tsv reuse and the single-push-URL refusal are the code's, not a policy wish.