| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | ) |
| 6 | |
| 7 | // Package-level functions are kept for backward compatibility. |
| 8 | // They delegate to a default GHCLIClient. |
| 9 | |
| 10 | // GetRepoOwnerAndName returns owner/repo by querying gh. |
| 11 | func GetRepoOwnerAndName() (string, string, error) { |
| 12 | return NewGHCLIClient("").GetRepoOwnerAndName(context.TODO()) |
| 13 | } |
| 14 | |
| 15 | // APIGet runs gh api with the given path and returns parsed JSON. |
| 16 | func APIGet(path string) ([]byte, error) { |
| 17 | return NewGHCLIClient("").APIGet(context.TODO(), path) |
| 18 | } |
| 19 | |
| 20 | // APIPost runs gh api with POST method and field arguments. |
| 21 | func APIPost(path string, fields map[string]string) error { |
| 22 | return NewGHCLIClient("").APIPost(context.TODO(), path, fields) |
| 23 | } |
| 24 | |
| 25 | // APIPostJSON runs gh api with POST method and a raw JSON body via --input. |
| 26 | func APIPostJSON(path string, body interface{}) ([]byte, error) { |
| 27 | return NewGHCLIClient("").APIPostJSON(context.TODO(), path, body) |
| 28 | } |
| 29 | |
| 30 | // GetRepoOwnerAndNameFromDir returns owner/repo for the git repo at the given directory. |
| 31 | func GetRepoOwnerAndNameFromDir(dir string) (string, string, error) { |
| 32 | return NewGHCLIClient("").GetRepoOwnerAndNameFromDir(context.TODO(), dir) |
| 33 | } |
| 34 | |
| 35 | // PRReviewComment represents a single PR review comment from the GitHub API. |
| 36 | type PRReviewComment struct { |
| 37 | ID int64 `json:"id"` |
| 38 | Body string `json:"body"` |
| 39 | Path string `json:"path"` |
| 40 | User commentUser `json:"user"` |
| 41 | } |
| 42 | |
| 43 | type commentUser struct { |
| 44 | Login string `json:"login"` |
| 45 | } |
| 46 | |
| 47 | // FetchPRReviewComments fetches review comments for a PR. |
| 48 | func FetchPRReviewComments(owner, repo, prNumber string) ([]PRReviewComment, error) { |
| 49 | return NewGHCLIClient("").FetchPRReviewComments(context.TODO(), owner, repo, prNumber) |
| 50 | } |
| 51 | |
| 52 | // ReplyToReviewComment posts a reply to a specific PR review comment. |
| 53 | func ReplyToReviewComment(owner, repo, prNumber string, commentID int64, body string) error { |
| 54 | return NewGHCLIClient("").ReplyToReviewComment(context.TODO(), owner, repo, prNumber, commentID, body) |
| 55 | } |
| 56 | |
| 57 | // ReviewThread represents a GitHub PR review thread with its GraphQL node ID. |
| 58 | type ReviewThread struct { |
| 59 | ID string `json:"id"` |
| 60 | IsResolved bool `json:"isResolved"` |
| 61 | } |
| 62 | |
| 63 | // FetchReviewThreads fetches review threads for a PR using the GraphQL API. |
| 64 | func FetchReviewThreads(owner, repo string, prNumber int) ([]ReviewThread, error) { |
| 65 | return NewGHCLIClient("").FetchReviewThreads(context.TODO(), owner, repo, prNumber) |
| 66 | } |
| 67 | |
| 68 | // graphQLRunner abstracts the gh api graphql call for testing. |
| 69 | type graphQLRunner func(query string) ([]byte, error) |
| 70 | |
| 71 | func defaultGraphQLRunner(query string) ([]byte, error) { |
| 72 | return NewGHCLIClient("").runGraphQL(context.TODO(), query) |
| 73 | } |
| 74 | |
| 75 | func fetchReviewThreadsWithRunner(query string, runner graphQLRunner) ([]ReviewThread, error) { |
| 76 | return fetchReviewThreadsImpl(query, runner) |
| 77 | } |
| 78 | |
| 79 | func resolveReviewThreadWithRunner(threadID string, runner graphQLRunner) error { |
| 80 | return resolveReviewThreadImpl(threadID, runner) |
| 81 | } |
| 82 | |
| 83 | // ResolveReviewThread resolves a review thread by its GraphQL node ID. |
| 84 | func ResolveReviewThread(threadID string) error { |
| 85 | return NewGHCLIClient("").ResolveReviewThread(context.TODO(), threadID) |
| 86 | } |
| 87 | |
| 88 | // FetchCollaborators returns the list of collaborator logins for a repo. |
| 89 | func FetchCollaborators(owner, repo string) ([]string, error) { |
| 90 | return NewGHCLIClient("").FetchCollaborators(context.TODO(), owner, repo) |
| 91 | } |