diff --git a/github/client.go b/github/client.go index bd1272e90..6b90da1a6 100644 --- a/github/client.go +++ b/github/client.go @@ -1,16 +1,12 @@ package github import ( - "bytes" - "encoding/json" "fmt" - "io" "net/http" "net/url" "os" "os/exec" "strings" - "time" "github.com/github/gh-cli/version" ) @@ -35,226 +31,10 @@ type Client struct { cachedClient *simpleClient } -func (client *Client) PullRequest(project *Project, id string) (pr *PullRequest, err error) { - api, err := client.simpleApi() - if err != nil { - return - } - - res, err := api.Get(fmt.Sprintf("repos/%s/%s/pulls/%s", project.Owner, project.Name, id)) - if err = checkStatus(200, "getting pull request", res, err); err != nil { - return - } - - pr = &PullRequest{} - err = res.Unmarshal(pr) - - return -} - -func (client *Client) CreatePullRequest(project *Project, params map[string]interface{}) (pr *PullRequest, err error) { - api, err := client.simpleApi() - if err != nil { - return - } - - res, err := api.PostJSONPreview(fmt.Sprintf("repos/%s/%s/pulls", project.Owner, project.Name), params, draftsType) - if err = checkStatus(201, "creating pull request", res, err); err != nil { - if res != nil && res.StatusCode == 404 { - projectUrl := strings.SplitN(project.WebURL("", "", ""), "://", 2)[1] - err = fmt.Errorf("%s\nAre you sure that %s exists?", err, projectUrl) - } - return - } - - pr = &PullRequest{} - err = res.Unmarshal(pr) - - return -} - -func (client *Client) UpdatePullRequest(pr *PullRequest, params map[string]interface{}) (updatedPullRequest *PullRequest, err error) { - api, err := client.simpleApi() - if err != nil { - return - } - - res, err := api.PatchJSON(pr.ApiUrl, params) - if err = checkStatus(200, "updating pull request", res, err); err != nil { - return - } - - updatedPullRequest = &PullRequest{} - err = res.Unmarshal(updatedPullRequest) - return -} - -func (client *Client) RequestReview(project *Project, prNumber int, params map[string]interface{}) (err error) { - api, err := client.simpleApi() - if err != nil { - return - } - - res, err := api.PostJSON(fmt.Sprintf("repos/%s/%s/pulls/%d/requested_reviewers", project.Owner, project.Name, prNumber), params) - if err = checkStatus(201, "requesting reviewer", res, err); err != nil { - return - } - - res.Body.Close() - return -} - -func (client *Client) Repository(project *Project) (repo *Repository, err error) { - api, err := client.simpleApi() - if err != nil { - return - } - - res, err := api.Get(fmt.Sprintf("repos/%s/%s", project.Owner, project.Name)) - if err = checkStatus(200, "getting repository info", res, err); err != nil { - return - } - - repo = &Repository{} - err = res.Unmarshal(&repo) - return -} - -type Repository struct { - Name string `json:"name"` - FullName string `json:"full_name"` - Parent *Repository `json:"parent"` - Owner *User `json:"owner"` - Private bool `json:"private"` - HasWiki bool `json:"has_wiki"` - Permissions *RepositoryPermissions `json:"permissions"` - HtmlUrl string `json:"html_url"` - DefaultBranch string `json:"default_branch"` -} - -type RepositoryPermissions struct { - Admin bool `json:"admin"` - Push bool `json:"push"` - Pull bool `json:"pull"` -} - -type Issue struct { - Number int `json:"number"` - State string `json:"state"` - Title string `json:"title"` - Body string `json:"body"` - User *User `json:"user"` - - PullRequest *PullRequest `json:"pull_request"` - Head *PullRequestSpec `json:"head"` - Base *PullRequestSpec `json:"base"` - - MergeCommitSha string `json:"merge_commit_sha"` - MaintainerCanModify bool `json:"maintainer_can_modify"` - Draft bool `json:"draft"` - - Comments int `json:"comments"` - Labels []IssueLabel `json:"labels"` - Assignees []User `json:"assignees"` - Milestone *Milestone `json:"milestone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - MergedAt time.Time `json:"merged_at"` - - RequestedReviewers []User `json:"requested_reviewers"` - RequestedTeams []Team `json:"requested_teams"` - - ApiUrl string `json:"url"` - HtmlUrl string `json:"html_url"` - - ClosedBy *User `json:"closed_by"` -} - -type PullRequest Issue - -type PullRequestSpec struct { - Label string `json:"label"` - Ref string `json:"ref"` - Sha string `json:"sha"` - Repo *Repository `json:"repo"` -} - -func (pr *PullRequest) IsSameRepo() bool { - return pr.Head != nil && pr.Head.Repo != nil && - pr.Head.Repo.Name == pr.Base.Repo.Name && - pr.Head.Repo.Owner.Login == pr.Base.Repo.Owner.Login -} - -func (pr *PullRequest) HasRequestedReviewer(name string) bool { - for _, user := range pr.RequestedReviewers { - if strings.EqualFold(user.Login, name) { - return true - } - } - return false -} - -func (pr *PullRequest) HasRequestedTeam(name string) bool { - for _, team := range pr.RequestedTeams { - if strings.EqualFold(team.Slug, name) { - return true - } - } - return false -} - -type IssueLabel struct { - Name string `json:"name"` - Color string `json:"color"` -} - type User struct { Login string `json:"login"` } -type Team struct { - Name string `json:"name"` - Slug string `json:"slug"` -} - -type Milestone struct { - Number int `json:"number"` - Title string `json:"title"` -} - -func (client *Client) GenericAPIRequest(method, path string, data interface{}, headers map[string]string, ttl int) (*simpleResponse, error) { - api, err := client.simpleApi() - if err != nil { - return nil, err - } - api.CacheTTL = ttl - - var body io.Reader - switch d := data.(type) { - case map[string]interface{}: - if method == "GET" { - path = addQuery(path, d) - } else if len(d) > 0 { - json, err := json.Marshal(d) - if err != nil { - return nil, err - } - body = bytes.NewBuffer(json) - } - case io.Reader: - body = d - } - - return api.performRequest(method, path, body, func(req *http.Request) { - if body != nil { - req.Header.Set("Content-Type", "application/json; charset=utf-8") - } - for key, value := range headers { - req.Header.Set(key, value) - } - }) -} - func (client *Client) CurrentUser() (user *User, err error) { api, err := client.simpleApi() if err != nil { @@ -508,39 +288,3 @@ func authTokenNote(num int) (string, error) { return fmt.Sprintf("hub for %s@%s", n, h), nil } - -func perPage(limit, max int) int { - if limit > 0 { - limit = limit + (limit / 2) - if limit < max { - return limit - } - } - return max -} - -func addQuery(path string, params map[string]interface{}) string { - if len(params) == 0 { - return path - } - - query := url.Values{} - for key, value := range params { - switch v := value.(type) { - case string: - query.Add(key, v) - case nil: - query.Add(key, "") - case int: - query.Add(key, fmt.Sprintf("%d", v)) - case bool: - query.Add(key, fmt.Sprintf("%v", v)) - } - } - - sep := "?" - if strings.Contains(path, sep) { - sep = "&" - } - return path + sep + query.Encode() -}