fetchGitHubPR fetches PR data for the current branch using the gh CLI.
()
| 79 | |
| 80 | // fetchGitHubPR fetches PR data for the current branch using the gh CLI. |
| 81 | func fetchGitHubPR() (*GitHubPR, error) { |
| 82 | if _, err := exec.LookPath("gh"); err != nil { |
| 83 | return nil, fmt.Errorf("GitHub CLI (gh) not found. Install it from https://cli.github.com/") |
| 84 | } |
| 85 | |
| 86 | out, err := exec.Command("gh", "pr", "view", "--json", "number,url,title,state,headRefName,updatedAt").Output() |
| 87 | if err != nil { |
| 88 | return nil, fmt.Errorf("no pull request found for the current branch. Create one with: gh pr create") |
| 89 | } |
| 90 | |
| 91 | var raw struct { |
| 92 | Number int `json:"number"` |
| 93 | URL string `json:"url"` |
| 94 | Title string `json:"title"` |
| 95 | State string `json:"state"` |
| 96 | Branch string `json:"headRefName"` |
| 97 | UpdatedAt string `json:"updatedAt"` |
| 98 | } |
| 99 | if err := json.Unmarshal(out, &raw); err != nil { |
| 100 | return nil, fmt.Errorf("failed to parse gh output: %w", err) |
| 101 | } |
| 102 | |
| 103 | // Extract owner/repo from the PR URL (e.g. https://github.com/PerpetualSoftware/pad/pull/5) |
| 104 | repo := "" |
| 105 | if parts := strings.Split(raw.URL, "/"); len(parts) >= 5 { |
| 106 | repo = parts[3] + "/" + parts[4] |
| 107 | } |
| 108 | |
| 109 | return &GitHubPR{ |
| 110 | Number: raw.Number, |
| 111 | URL: raw.URL, |
| 112 | Title: raw.Title, |
| 113 | State: raw.State, |
| 114 | Branch: raw.Branch, |
| 115 | Repo: repo, |
| 116 | UpdatedAt: raw.UpdatedAt, |
| 117 | }, nil |
| 118 | } |
| 119 | |
| 120 | func githubLinkCmd() *cobra.Command { |
| 121 | return &cobra.Command{ |