fetchGitHubPR fetches PR data for the current branch using the gh CLI.
()
| 116 | |
| 117 | // fetchGitHubPR fetches PR data for the current branch using the gh CLI. |
| 118 | func fetchGitHubPR() (*GitHubPR, error) { |
| 119 | if _, err := exec.LookPath("gh"); err != nil { |
| 120 | return nil, fmt.Errorf("GitHub CLI (gh) not found. Install it from https://cli.github.com/") |
| 121 | } |
| 122 | |
| 123 | out, err := exec.Command("gh", "pr", "view", "--json", ghPRJSONFields).Output() |
| 124 | if err != nil { |
| 125 | return nil, fmt.Errorf("no pull request found for the current branch. Create one with: gh pr create") |
| 126 | } |
| 127 | |
| 128 | var raw struct { |
| 129 | Number int `json:"number"` |
| 130 | URL string `json:"url"` |
| 131 | Title string `json:"title"` |
| 132 | State string `json:"state"` |
| 133 | Branch string `json:"headRefName"` |
| 134 | UpdatedAt string `json:"updatedAt"` |
| 135 | Merge *ghMergeCommit `json:"mergeCommit"` |
| 136 | MergedAt string `json:"mergedAt"` |
| 137 | } |
| 138 | if err := json.Unmarshal(out, &raw); err != nil { |
| 139 | return nil, fmt.Errorf("failed to parse gh output: %w", err) |
| 140 | } |
| 141 | |
| 142 | // Extract owner/repo from the PR URL (e.g. https://github.com/PerpetualSoftware/pad/pull/5) |
| 143 | repo := "" |
| 144 | if parts := strings.Split(raw.URL, "/"); len(parts) >= 5 { |
| 145 | repo = parts[3] + "/" + parts[4] |
| 146 | } |
| 147 | |
| 148 | return &GitHubPR{ |
| 149 | Number: raw.Number, |
| 150 | URL: raw.URL, |
| 151 | Title: raw.Title, |
| 152 | State: raw.State, |
| 153 | Branch: raw.Branch, |
| 154 | Repo: repo, |
| 155 | UpdatedAt: raw.UpdatedAt, |
| 156 | MergeSHA: raw.Merge.sha(), |
| 157 | MergedAt: ghMergedAt(raw.MergedAt), |
| 158 | }, nil |
| 159 | } |
| 160 | |
| 161 | func githubLinkCmd() *cobra.Command { |
| 162 | var supplied GitHubPR |
no test coverage detected