gunk-dev/the-valley

github.go

source9ee95920e399main's published tip history
3.2 KiB
1package github
2
3import (
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.
11func 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.
16func 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.
21func 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.
26func 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.
31func 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.
36type PRReviewComment struct {
37 ID int64 `json:"id"`
38 Body string `json:"body"`
39 Path string `json:"path"`
40 User commentUser `json:"user"`
41}
42
43type commentUser struct {
44 Login string `json:"login"`
45}
46
47// FetchPRReviewComments fetches review comments for a PR.
48func 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.
53func 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.
58type 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.
64func 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.
69type graphQLRunner func(query string) ([]byte, error)
70
71func defaultGraphQLRunner(query string) ([]byte, error) {
72 return NewGHCLIClient("").runGraphQL(context.TODO(), query)
73}
74
75func fetchReviewThreadsWithRunner(query string, runner graphQLRunner) ([]ReviewThread, error) {
76 return fetchReviewThreadsImpl(query, runner)
77}
78
79func resolveReviewThreadWithRunner(threadID string, runner graphQLRunner) error {
80 return resolveReviewThreadImpl(threadID, runner)
81}
82
83// ResolveReviewThread resolves a review thread by its GraphQL node ID.
84func ResolveReviewThread(threadID string) error {
85 return NewGHCLIClient("").ResolveReviewThread(context.TODO(), threadID)
86}
87
88// FetchCollaborators returns the list of collaborator logins for a repo.
89func FetchCollaborators(owner, repo string) ([]string, error) {
90 return NewGHCLIClient("").FetchCollaborators(context.TODO(), owner, repo)
91}