-
Resolve the PR number — skip if user supplied one:
gh pr view --json number,body -q '.number'
If no PR exists on the branch, stop and tell the user.
-
Extract issue number + repo from the input URL, or accept a bare #1234 for current repo.
-
Fetch the Linear ticket from the issue's linear-linkback comment:
gh issue view <n> --repo <owner>/<repo> --json comments \
-q '.comments[] | select(.author.login=="linear-code") | .body' \
| grep -Eioe '[a-z]+-[0-9]+' | head -1
If no match, fall back to asking the user for the Linear key, or omit it.
-
Read the existing PR body so you can append rather than overwrite:
gh pr view <pr-number> --json body -q '.body'
-
Construct the new body. If the body is empty, use just the #### Issues block. Otherwise, append it after a blank line. Don't duplicate — if #### Issues is already present, replace that section instead of adding a second one.
Format:
#### Issues
* Resolves: #<n>
* Resolves: <linear-key>
-
Update the PR with a heredoc to preserve newlines:
gh pr edit <pr-number> --body "$(cat <<'EOF'
<new body>
EOF
)"
-
Confirm by echoing the resulting PR URL:
gh pr view <pr-number> --json url -q '.url'