Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
API support for pull requests
  • Loading branch information
lstahlman committed Oct 12, 2016
commit 95c5f7a280c6f2b16588c8d9388d41a860065e0d
10 changes: 10 additions & 0 deletions gogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ func (c *Client) getParsedResponse(method, path string, header http.Header, body
}
return json.Unmarshal(data, obj)
}

func (c *Client) getStatusCode(method, path string, header http.Header, body io.Reader) (int, error) {
resp, err := c.doRequest(method, path, header, body)
if err != nil {
return -1, err
}
defer resp.Body.Close()

return resp.StatusCode, nil
}
97 changes: 89 additions & 8 deletions pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package gogs

import (
"bytes"
"encoding/json"
"fmt"
"time"
)

// PullRequest represents a pull reqesut API object.
// PullRequest represents a pull request API object.
type PullRequest struct {
// Copied from issue.go
ID int64 `json:"id"`
Index int64 `json:"number"`
Poster *User `json:"user"`
Expand All @@ -22,16 +24,95 @@ type PullRequest struct {
State StateType `json:"state"`
Comments int `json:"comments"`

HeadBranch string `json:"head_branch"`
HeadRepo *Repository `json:"head_repo"`
BaseBranch string `json:"base_branch"`
BaseRepo *Repository `json:"base_repo"`

HTMLURL string `json:"html_url"`

Mergeable *bool `json:"mergeable"`
Mergeable bool `json:"mergeable"`
HasMerged bool `json:"merged"`
Merged *time.Time `json:"merged_at"`
MergedCommitID *string `json:"merge_commit_sha"`
MergedBy *User `json:"merged_by"`

Base *PRBranchInfo `json:"base"`
Head *PRBranchInfo `json:"head"`
MergeBase string `json:"merge_base"`
}

type PRBranchInfo struct {
Name string `json:"name"`
RepoID int64 `json:"repo_id"`
Repository *Repository `json:"repo"`
}

type ListPullRequestsOptions struct {
Page int `json:"page"`
State string `json:"state"`
}

func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
prs := make([]*PullRequest, 0, 10)
return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs)
}

func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) {
pr := new(PullRequest)
return pr, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
}

type CreatePullRequestOption struct {
Head string `json:"head" binding:"Required"`
Base string `json:"base" binding:"Required"`
Title string `json:"title" binding:"Required"`
Body string `json:"body"`
Assignee string `json:"assignee"`
Milestone int64 `json:"milestone"`
Labels []int64 `json:"labels"`
}

func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
pr := new(PullRequest)
return pr, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
jsonHeader, bytes.NewReader(body), pr)
}

type EditPullRequestOption struct {
Title string `json:"title"`
Body string `json:"body"`
Assignee string `json:"assignee"`
Milestone int64 `json:"milestone"`
Labels []int64 `json:"labels"`
State *string `json:"state"`
}

func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
pr := new(PullRequest)
return pr, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
jsonHeader, bytes.NewReader(body), pr)
}

func (c *Client) MergePullRequest(owner, repo string, index int64) error {
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
return err
}

func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) {
statusCode, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)

if err != nil {
return false, err
}

return statusCode == 204, nil

}