Skip to main content

Module github

Module github 

Source
Expand description

Issue ↔ PR ↔ branch link storage + GitHub API fetch (via gh CLI).

Storage lives in git branch config: branch.<name>.gwm-issue and branch.<name>.gwm-pr. Issue numbers are auto-detected from the <type>/#<N>-<slug> branch convention when no explicit override is set.

Fetch shells out to gh and parses its JSON output. The parsing functions (parse_issue_json, parse_pr_json) are exposed publicly so tests can cover the JSON contract without depending on a real gh binary.

Structs§

BranchLink
Resolved link for one branch: which issue (if any), which PR (if any), and where each number came from.
CreatedIssue
CreatedPr
IssueCreateRequest
IssueStatus
PrCreateRequest
PrHead
The slice of PR metadata gwm review needs to materialise a worktree: the head ref name (slug source), the author login (path component), and the base ref (diff base). Distinct from PrStatus so the TUI’s status/CI path stays untouched.
PrStatus

Enums§

CiState
Overall CI outcome derived from a PR’s statusCheckRollup (issue #299). A single ordered signal so the sidebar can render pass/fail/running at a glance instead of a bare N/M count. Priority is failing > running > passing: the most actionable state always wins, so a red check is never hidden behind an in-flight one.
IssueState
LinkSource
Where the issue or PR number came from.
PrState

Functions§

apply_detected_pr
Stamp an auto-detected PR number onto link when no PR is already linked. Pure helper (issue #181): the caller supplies the detection result — typically find_pr_for_branch(slug, branch).ok().flatten() — and this decides whether to apply it.
clear_persisted_detected_pr
Drop a persisted auto-detection (issue #283). A no-op when no detected PR was stored. Used when a detection no longer holds (the branch’s PR went away) so a stale number doesn’t linger in the config.
create_issue
create_milestone
Create one milestone upstream via gh api -X POST. Returns Ok(()) — the caller already has the spec; we don’t bother parsing the response back into a RemoteMilestone.
create_pr
Shell out to gh pr create with a body file already rendered by crate::pr_templates::render_pr_body. Parses the URL printed by gh on success to extract the PR number.
delete_label
Delete one label on the remote via gh label delete --yes. Used by gwm labels push --prune for labels declared on the remote but not in .gwm.toml.
delete_milestone
Delete one milestone on the remote via gh api -X DELETE. Used by gwm milestones push --prune for milestones declared on the remote but not in .gwm.toml.
fetch_issue
Run gh issue view <n> --repo <slug> --json … and parse the result.
fetch_issue_with
fetch_issue with an explicitly resolved gh program path. Used by the TUI’s off-thread fetch (issue #217): the program is resolved on the main thread via gh_program and handed to the worker thread, so the thread never touches GWM_GH / the process environment concurrently with env-mutating callers.
fetch_pr
Run gh pr view <n> --repo <slug> --json … and parse the result.
fetch_pr_head
Run gh pr view <n> --repo <slug> --json … and parse the head metadata gwm review needs (author / head ref / base ref). Works for PRs in any state — open, draft, closed, or merged.
fetch_pr_with
fetch_pr with an explicitly resolved gh program path — PR-side counterpart to fetch_issue_with, used by the TUI off-thread fetch (issue #217).
fetch_remote_labels
Run gh label list --repo <slug> --json … and parse the result. Returns an empty vec when the remote has no labels (which is distinct from “gh not installed” — that surfaces as CommandFailed).
fetch_remote_milestones
Run gh api repos/<slug>/milestones?state=all and parse the result. Returns an empty vec when the remote has no milestones.
find_pr_argv
Argv for gh pr list --repo <slug> --head <branch> --state all --json number --limit 1. Extracted so the test suite can pin the gh contract without shelling out; find_pr_for_branch is the caller that actually invokes it. --state all is the load-bearing bit: a closed or merged PR for the branch is still detected (its PrState is resolved later via fetch_pr).
find_pr_for_branch
Find the most recent PR opened from branch (head ref) on the given repo, regardless of state. Returns Ok(Some(N)) if at least one PR exists (open, draft, closed, or merged — gh pr list --state all), Ok(None) otherwise. Callers that need state-aware filtering should pair this with fetch_pr to inspect PrState afterwards.
gh_command_line
Build the human-readable command line stored on the Command Logs transcript (issue #226) for a gh invocation: the program’s file name (so a GWM_GH=/usr/bin/gh override still reads as gh issue view … rather than leaking the full path) followed by the resolved args. Kept pure and pub so its argv format is unit-testable without spawning gh (which CI runners do not have).
gh_program
Resolve the gh program to invoke: $GWM_GH when set (test / override hook), else gh on PATH. Read once on the calling thread so off-thread fetches can capture it without re-reading the environment.
issue_url
Build the canonical GitHub URL for an issue, given the repo slug.
label_create_argv
Argv for gh label create <name> --color <hex> [--description <desc>] --force --repo <slug>. The --force flag is the key contract bit: GitHub’s CLI uses it to mean “create OR update”, which is exactly what gwm labels push needs (no separate “edit” call). When description is None we omit the flag entirely rather than pass "" — gh would otherwise wipe an existing description that the user didn’t intend to touch.
label_delete_argv
Argv for gh label delete <name> --repo <slug> --yes. The --yes flag bypasses the interactive confirm prompt; without it gh blocks on a TTY read and gwm labels push --prune hangs.
label_list_argv
Argv for gh label list --repo <slug> --json name,color,description --limit 1000. Extracted so the test suite can pin the contract; callers should prefer fetch_remote_labels which actually shells out.
link_issue
link_pr
milestone_create_argv
Argv for gh api -X POST repos/<slug>/milestones -f title=… [-f description=…] [-f due_on=…] -f state=…. Each optional field is omitted entirely when absent — gh would otherwise wipe the existing remote value.
milestone_delete_argv
Argv for gh api -X DELETE repos/<slug>/milestones/<number>. gh api -X DELETE is non-interactive by construction (no TTY confirm), so there’s no --yes equivalent to add.
milestone_list_argv
Argv for gh api --paginate repos/<slug>/milestones?state=all&per_page=100.
milestone_update_argv
Argv for gh api -X PATCH repos/<slug>/milestones/<number> -f …. Same omission rules as milestone_create_argv: absent optionals are skipped so the remote value isn’t wiped.
parse_issue_json
parse_labels_json
Parse the JSON returned by gh label list --json name,color,description. Exposed publicly so unit tests can cover the contract without shelling out. Two normalisations happen here so callers get a uniformly-shaped RemoteLabel:
parse_milestones_json
Parse the JSON returned by gh api repos/:owner/:repo/milestones?state=all. Exposed publicly so unit tests can cover the contract without shelling out. The state field is mapped to the strict MilestoneState enum — an unknown value is a hard error rather than a silent third state on the diff side.
parse_pr_head_json
Parse the JSON from gh pr view <n> --json number,author,headRefName,baseRefName. Kept pure + pub so its shape is unit-testable without spawning gh.
parse_pr_json
parse_pr_list_number
Parse the JSON array printed by gh pr list --json number --limit 1, returning the first PR number if any. Exposed for unit tests so the parse contract is covered without a gh shell-out.
persist_detected_pr
Persist an auto-detected PR number to its own branch-config key (gwm-pr-detected, issue #283), distinct from the explicit gwm-pr. This lets the no-fetch table read path surface the detected PR on every row without a per-row gh shell-out, while keeping the detected/explicit distinction the pane badge needs. An explicit gwm link --pr still wins in read_link. Re-detection overwrites the stored value and clears a cached title only when the detected number actually changed.
persist_detected_pr_state
persist_detected_pr_title
persist_issue_state
persist_issue_title
persist_pr_state
persist_pr_title
pr_url
Build the canonical GitHub URL for a PR, given the repo slug.
push_label
Push one label upstream via gh label create --force. Returns Ok(()) on success; the caller is responsible for tracking which label was created vs. updated (the diff already knows).
read_link
Read the link for branch. Explicit overrides win over branch-name auto-detect.
read_link_with_pr_detection
Resolve the link for branch and, unless a PR is explicitly linked, auto-detect the branch’s PR from GitHub via gh (issue #181). The detected PR is marked LinkSource::Detected.
repo_slug
Extract the owner/repo slug from the origin remote URL. Supports the two GitHub URL flavours: git@github.com:owner/repo(.git)? and https://github.com/owner/repo(.git)?.
unlink_issue
unlink_pr
update_milestone
Update one milestone upstream via gh api -X PATCH. number is the GitHub-issued identifier carried through MilestoneUpdate.