# `MobDev.Release.Publish`
[🔗](https://github.com/genericjam/mob_dev/blob/master/lib/mob_dev/release/publish.ex#L1)

Replaces `scripts/release/publish.sh` — uploads built tarballs to a
GitHub release tagged `otp-<hash>`. The release is created if it
doesn't exist. Published assets are immutable: if an asset with the
same name is already on the release, nothing is uploaded (a rebuilt
runtime is published under a new hash). Each uploaded asset's SHA-256
is returned as the pin line for `MobDev.Download.Pins`.

## Why this module carries the most error categories

Of the five `MobDev.Release.*` orchestrators this is the one most
likely to fail through no fault of ours — GitHub goes down, `gh`
auth expires, the local network drops. The shell version of this
step blobs all of those together as `exit 1`; the user can't tell
whether it's their problem or ours. This module classifies every
`gh` failure into one of:

  * `:auth_required` — gh CLI isn't authenticated, or token lacks
    `repo` scope. Hint suggests
    `gh auth login --scopes "repo,write:packages"`.
  * `:infra_unreachable` — GitHub returned 5xx or the network's
    down. Detail carries the first line of `gh`'s stderr so it's
    obvious what to check on status.github.com.
  * `:precondition_failed` — no tarballs in `out_dir` matching
    `hash`, or an asset of the same name is already on the release.
    Hint points the user at `mix mob.release.tarball all`, or at a
    new hash.
  * `:cmd_failed` — fallback for unexpected `gh` failures (e.g.
    malformed tag, write permission denied on a non-our repo).

This is exactly the "is GitHub down or am I broken?" distinction the
build-system migration plan called for. Test coverage exercises each
category against a representative `gh` stderr snippet.

## Public API

    MobDev.Release.Publish.publish()                    # defaults
    MobDev.Release.Publish.publish(repo: "fork/mob")    # publish to fork
    MobDev.Release.Publish.publish(assets: ["otp-android"])  # subset

Returns `{:ok, %{tag, repo, assets, pins}}` on success or a tagged error.

## What it does end-to-end

  1. Resolve `repo`, `hash`, `out_dir` (env or opts).
  2. Discover which of the five canonical tarball basenames are
     present in `out_dir` for this hash. At least one must exist.
  3. SHA-256 every tarball to upload.
  4. `gh release view otp-<hash>` — exists?
     - If not (`release not found`), `gh release create`.
     - If 401/403, `:auth_required`.
     - If 5xx/network, `:infra_unreachable`.
  5. List the release's existing assets. If any overlaps with what we're
     about to upload, stop with `:precondition_failed`: every released
     mob_dev pins the bytes behind that URL, so replacing them breaks it.
  6. Single `gh release upload otp-<hash> <paths…>`.
  7. Verify by re-listing assets — returned in `info.assets`; the
     uploaded assets' URL/SHA-256 pairs are in `info.pins`.

# `info`

```elixir
@type info() :: %{
  tag: String.t(),
  repo: String.t(),
  assets: [String.t()],
  pins: [{String.t(), String.t()}]
}
```

Successful publish result.

# `asset_url`

```elixir
@spec asset_url(String.t(), String.t(), String.t()) :: String.t()
```

Public download URL of `asset` on release `tag` (the key in `MobDev.Download.Pins`).

# `candidate_basenames`

```elixir
@spec candidate_basenames() :: [String.t()]
```

Canonical tarball basenames the publisher knows how to upload.

# `classify`

```elixir
@spec classify(String.t()) :: :not_found | :auth | :infra | :other
```

Classify a `gh` stderr/stdout line into one of `:not_found`, `:auth`,
`:infra`, or `:other`. Public for testing — the regexes are the
contract.

# `default_repo`

```elixir
@spec default_repo() :: String.t()
```

Default GitHub repo (mirrors publish.sh's `${REPO:=GenericJam/mob}`).

# `discover_assets`

```elixir
@spec discover_assets(module(), Path.t(), String.t()) :: [String.t()]
```

Asset basenames that exist in `out_dir` for the given `hash`. Returns
an ordered list (matching `candidate_basenames/0` order).

# `publish`

```elixir
@spec publish(keyword()) :: {:ok, info()} | MobDev.Release.Errors.t()
```

Run the publish pipeline. See module doc for options.

Recognized opts:

  * `:repo` — `owner/name` (default: `GenericJam/mob`)
  * `:hash` — release hash. Default: detected from `OTP_SRC` git or `$HASH`.
  * `:otp_src` — OTP checkout for hash detection. Default per `Helpers`.
  * `:out_dir` — directory containing the built tarballs. Default per `Helpers`.
  * `:assets` — list of tarball basenames to upload. Each may be
    either the bare base (`"otp-android"`) — in which case
    `-<hash>.tar.gz` is appended — or the full filename. Default:
    auto-discover any of the five canonical names that exist.

# `tag_for`

```elixir
@spec tag_for(String.t()) :: String.t()
```

Build the release tag for a hash (`otp-<hash>`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
