-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Pull in the GraphQL API functionality #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c5ffa59
d6eaad1
a24db0f
52c009e
28e4975
41e0089
cf60199
c956bf8
3420220
0e260ec
7007a46
04861a1
3fecba6
55cbf37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "os" | ||
| "os/user" | ||
| "regexp" | ||
|
|
||
| "github.com/github/gh-cli/version" | ||
| ) | ||
|
|
||
| type graphQLResponse struct { | ||
| Data interface{} | ||
| Errors []struct { | ||
| Message string | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| graphQL usage | ||
|
|
||
| type repoResponse struct { | ||
| Repository struct { | ||
| CreatedAt string | ||
| } | ||
| } | ||
|
|
||
| query := `query { | ||
| repository(owner: "golang", name: "go") { | ||
| createdAt | ||
| } | ||
| }` | ||
|
|
||
| variables := map[string]string{} | ||
|
|
||
| var resp repoResponse | ||
| err := graphql(query, map[string]string{}, &resp) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| fmt.Printf("%+v\n", resp) | ||
| */ | ||
| func graphQL(query string, variables map[string]string, data interface{}) error { | ||
| url := "https://api.github.com/graphql" | ||
| reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| token, err := getToken() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| req.Header.Set("Authorization", "token "+token) | ||
| req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
| req.Header.Set("User-Agent", "GitHub CLI "+version.Version) | ||
|
|
||
| debugRequest(req, string(reqBody)) | ||
|
|
||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| debugResponse(resp, string(body)) | ||
| return handleResponse(resp, body, data) | ||
| } | ||
|
|
||
| func handleResponse(resp *http.Response, body []byte, data interface{}) error { | ||
| success := resp.StatusCode >= 200 && resp.StatusCode < 300 | ||
|
|
||
| if !success { | ||
| return handleHTTPError(resp, body) | ||
| } | ||
|
|
||
| gr := &graphQLResponse{Data: data} | ||
| err := json.Unmarshal(body, &gr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(gr.Errors) > 0 { | ||
| errorMessages := gr.Errors[0].Message | ||
| for _, e := range gr.Errors[1:] { | ||
| errorMessages += ", " + e.Message | ||
| } | ||
| return fmt.Errorf("graphql error: '%s'", errorMessages) | ||
| } | ||
| return nil | ||
|
|
||
| } | ||
|
|
||
| func handleHTTPError(resp *http.Response, body []byte) error { | ||
| var message string | ||
| var parsedBody struct { | ||
| Message string `json:"message"` | ||
| } | ||
| err := json.Unmarshal(body, &parsedBody) | ||
| if err != nil { | ||
| message = string(body) | ||
| } else { | ||
| message = parsedBody.Message | ||
| } | ||
|
|
||
| return fmt.Errorf("http error, '%s' failed (%d): '%s'", resp.Request.URL, resp.StatusCode, message) | ||
| } | ||
|
|
||
| func debugRequest(req *http.Request, body string) { | ||
| if _, ok := os.LookupEnv("DEBUG"); !ok { | ||
| return | ||
| } | ||
|
|
||
| fmt.Printf("DEBUG: GraphQL request to %s:\n %s\n\n", req.URL, body) | ||
| } | ||
|
|
||
| func debugResponse(resp *http.Response, body string) { | ||
| if _, ok := os.LookupEnv("DEBUG"); !ok { | ||
| return | ||
| } | ||
|
|
||
| fmt.Printf("DEBUG: GraphQL response:\n%+v\n\n%s\n\n", resp, body) | ||
| } | ||
|
|
||
| // TODO: Everything below this line will be removed when Nate's context work is complete | ||
| func getToken() (string, error) { | ||
| usr, err := user.Current() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| content, err := ioutil.ReadFile(usr.HomeDir + "/.config/hub") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| r := regexp.MustCompile(`oauth_token: (\w+)`) | ||
| token := r.FindStringSubmatch(string(content)) | ||
| return token[1], nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/github/gh-cli/git" | ||
| "github.com/github/gh-cli/github" | ||
| ) | ||
|
|
||
| type PullRequestsPayload struct { | ||
| ViewerCreated []PullRequest | ||
| ReviewRequested []PullRequest | ||
| CurrentPR *PullRequest | ||
| } | ||
|
|
||
| type PullRequest struct { | ||
| Number int | ||
| Title string | ||
| URL string | ||
| HeadRefName string | ||
| } | ||
|
|
||
| func PullRequests() (PullRequestsPayload, error) { | ||
| type edges struct { | ||
| Edges []struct { | ||
| Node PullRequest | ||
| } | ||
| PageInfo struct { | ||
| HasNextPage bool | ||
| EndCursor string | ||
| } | ||
| } | ||
|
|
||
| type response struct { | ||
| Repository struct { | ||
| PullRequests edges | ||
| } | ||
| ViewerCreated edges | ||
| ReviewRequested edges | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I was using the json tag syntax like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by default go tries to auto-guess field names for serializing. if it works, it works. |
||
| } | ||
|
|
||
| query := ` | ||
| fragment pr on PullRequest { | ||
| number | ||
| title | ||
| url | ||
| headRefName | ||
| } | ||
|
|
||
| query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { | ||
| repository(owner: $owner, name: $repo) { | ||
| pullRequests(headRefName: $headRefName, first: 1) { | ||
| edges { | ||
| node { | ||
| ...pr | ||
| } | ||
| } | ||
| } | ||
| } | ||
| viewerCreated: search(query: $viewerQuery, type: ISSUE, first: $per_page) { | ||
| edges { | ||
| node { | ||
| ...pr | ||
| } | ||
| } | ||
| pageInfo { | ||
| hasNextPage | ||
| } | ||
| } | ||
| reviewRequested: search(query: $reviewerQuery, type: ISSUE, first: $per_page) { | ||
| edges { | ||
| node { | ||
| ...pr | ||
| } | ||
| } | ||
| pageInfo { | ||
| hasNextPage | ||
| } | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| project := project() | ||
| owner := project.Owner | ||
| repo := project.Name | ||
| currentBranch := currentBranch() | ||
|
|
||
| viewerQuery := fmt.Sprintf("repo:%s/%s state:open is:pr author:%s", owner, repo, currentUsername()) | ||
| reviewerQuery := fmt.Sprintf("repo:%s/%s state:open review-requested:%s", owner, repo, currentUsername()) | ||
|
|
||
| variables := map[string]string{ | ||
| "viewerQuery": viewerQuery, | ||
| "reviewerQuery": reviewerQuery, | ||
| "owner": owner, | ||
| "repo": repo, | ||
| "headRefName": currentBranch, | ||
| } | ||
|
|
||
| var resp response | ||
| err := graphQL(query, variables, &resp) | ||
| if err != nil { | ||
| return PullRequestsPayload{}, err | ||
| } | ||
|
|
||
| var viewerCreated []PullRequest | ||
| for _, edge := range resp.ViewerCreated.Edges { | ||
| viewerCreated = append(viewerCreated, edge.Node) | ||
| } | ||
|
|
||
| var reviewRequested []PullRequest | ||
| for _, edge := range resp.ReviewRequested.Edges { | ||
| reviewRequested = append(reviewRequested, edge.Node) | ||
| } | ||
|
|
||
| var currentPR *PullRequest | ||
| for _, edge := range resp.Repository.PullRequests.Edges { | ||
| currentPR = &edge.Node | ||
| } | ||
|
|
||
| payload := PullRequestsPayload{ | ||
| viewerCreated, | ||
| reviewRequested, | ||
| currentPR, | ||
| } | ||
|
|
||
| return payload, nil | ||
| } | ||
|
|
||
| // TODO: Everything below this line will be removed when Nate's context work is complete | ||
| func project() github.Project { | ||
| remotes, error := github.Remotes() | ||
| if error != nil { | ||
| panic(error) | ||
| } | ||
|
|
||
| for _, remote := range remotes { | ||
| if project, error := remote.Project(); error == nil { | ||
| return *project | ||
| } | ||
| } | ||
|
|
||
| panic("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?") | ||
| } | ||
|
|
||
| func currentBranch() string { | ||
| currentBranch, err := git.Head() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| return strings.Replace(currentBranch, "refs/heads/", "", 1) | ||
| } | ||
|
|
||
| func currentUsername() string { | ||
| host, err := github.CurrentConfig().DefaultHost() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return host.User | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about this pattern. But since the struct is only used in the
PullRequestsfunction I figured it keeps everything more contained.