Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

ListPullRequestsOptions does not work? #162

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions gitea/admin_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
package gitea

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

// AdminListOrgs list all organizations
func (c *Client) AdminListOrgs() ([]*Organization, error) {
orgs := make([]*Organization, 0, 5)
return orgs, c.getParsedResponse("GET", "/admin/orgs", nil, nil, &orgs)
}

// AdminCreateOrg create an organization
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
org := new(Organization)
return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user),
jsonHeader, bytes.NewReader(body), org)
jsonHeader, opt, org)
}
8 changes: 1 addition & 7 deletions gitea/admin_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
package gitea

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

// AdminCreateRepo create a repo
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user),
jsonHeader, bytes.NewReader(body), repo)
jsonHeader, opt, repo)
}
12 changes: 2 additions & 10 deletions gitea/admin_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ type CreateUserOption struct {

// AdminCreateUser create a user
func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
user := new(User)
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, opt, user)
}

// EditUserOption edit user options
Expand Down Expand Up @@ -76,10 +72,6 @@ func (c *Client) AdminDeleteUser(user string) error {

// AdminCreateUserPublicKey create one user with options
func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
key := new(PublicKey)
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, opt, key)
}
10 changes: 2 additions & 8 deletions gitea/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package gitea

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

Expand All @@ -27,13 +25,9 @@ type CreateForkOption struct {

// CreateFork create a fork of a repository
func (c *Client) CreateFork(user, repo string, form CreateForkOption) (*Repository, error) {
body, err := json.Marshal(form)
if err != nil {
return nil, err
}
fork := new(Repository)
err = c.getParsedResponse("POST",
err := c.getParsedResponse("POST",
fmt.Sprintf("/repos/%s/%s/forks", user, repo),
jsonHeader, bytes.NewReader(body), &fork)
jsonHeader, form, &fork)
return fork, err
}
51 changes: 47 additions & 4 deletions gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
package gitea

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/google/go-querystring/query"
)

// Version return the library version
Expand Down Expand Up @@ -52,11 +56,50 @@ func (c *Client) SetSudo(sudo string) {
c.sudo = sudo
}

func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
func (c *Client) doRequest(method, path string, header http.Header, body interface{}) (*http.Response, error) {
u, err := url.Parse(c.url + "/api/v1" + path)
if err != nil {
return nil, err
}

if method == "GET" {
if body != nil {
q, err := query.Values(body)
if err != nil {
return nil, err
}
u.RawQuery = q.Encode()
}
}

req := &http.Request{
Method: method,
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Host: u.Host,
}

if method == "POST" || method == "PUT" || method == "PATCH" {
bodyBytes, err := json.Marshal(body)
Copy link
Member

@lunny lunny Apr 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body has been read to end on above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain?

if err != nil {
return nil, err
}
bodyReader := bytes.NewReader(bodyBytes)

u.RawQuery = ""
req.Body = ioutil.NopCloser(bodyReader)
req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bodyReader), nil
}
req.ContentLength = int64(bodyReader.Len())
req.Header.Set("Content-Type", "application/json")
}

req.Header.Set("Accept", "application/json")

if len(c.accessToken) != 0 {
req.Header.Set("Authorization", "token "+c.accessToken)
}
Expand All @@ -70,7 +113,7 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
return c.client.Do(req)
}

func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
func (c *Client) getResponse(method, path string, header http.Header, body interface{}) ([]byte, error) {
resp, err := c.doRequest(method, path, header, body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -106,7 +149,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
return data, nil
}

func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) error {
func (c *Client) getParsedResponse(method, path string, header http.Header, body interface{}, obj interface{}) error {
data, err := c.getResponse(method, path, header, body)
if err != nil {
return err
Expand Down
12 changes: 2 additions & 10 deletions gitea/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,14 @@ type CreateHookOption struct {

// CreateOrgHook create one hook for an organization, with options
func (c *Client) CreateOrgHook(org string, opt CreateHookOption) (*Hook, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
h := new(Hook)
return h, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, bytes.NewReader(body), h)
return h, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, opt, h)
}

// CreateRepoHook create one hook for a repository, with options
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
h := new(Hook)
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, opt, h)
}

// EditHookOption options when modify one hook
Expand Down
6 changes: 1 addition & 5 deletions gitea/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,9 @@ type EditIssueOption struct {

// EditIssue modify an existing issue for a given repository
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
issue := new(Issue)
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
jsonHeader, bytes.NewReader(body), issue)
jsonHeader, opt, issue)
}

// StartIssueStopWatch starts a stopwatch for an existing issue for a given
Expand Down
14 changes: 2 additions & 12 deletions gitea/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package gitea

import (
"bytes"
"encoding/json"
"fmt"
"time"
)
Expand Down Expand Up @@ -45,12 +43,8 @@ type CreateIssueCommentOption struct {

// CreateIssueComment create comment on an issue.
func (c *Client) CreateIssueComment(owner, repo string, index int64, opt CreateIssueCommentOption) (*Comment, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
comment := new(Comment)
return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), jsonHeader, bytes.NewReader(body), comment)
return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), jsonHeader, opt, comment)
}

// EditIssueCommentOption options for editing a comment
Expand All @@ -61,12 +55,8 @@ type EditIssueCommentOption struct {

// EditIssueComment edits an issue comment.
func (c *Client) EditIssueComment(owner, repo string, index, commentID int64, opt EditIssueCommentOption) (*Comment, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
comment := new(Comment)
return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments/%d", owner, repo, index, commentID), jsonHeader, bytes.NewReader(body), comment)
return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments/%d", owner, repo, index, commentID), jsonHeader, opt, comment)
}

// DeleteIssueComment deletes an issue comment.
Expand Down
26 changes: 4 additions & 22 deletions gitea/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package gitea

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

Expand Down Expand Up @@ -44,13 +42,9 @@ type CreateLabelOption struct {

// CreateLabel create one label of repository
func (c *Client) CreateLabel(owner, repo string, opt CreateLabelOption) (*Label, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
label := new(Label)
return label, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
jsonHeader, bytes.NewReader(body), label)
jsonHeader, opt, label)
}

// EditLabelOption options for editing a label
Expand All @@ -61,12 +55,8 @@ type EditLabelOption struct {

// EditLabel modify one label with options
func (c *Client) EditLabel(owner, repo string, id int64, opt EditLabelOption) (*Label, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
label := new(Label)
return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label)
return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, opt, label)
}

// DeleteLabel delete one label of repository by id
Expand All @@ -90,22 +80,14 @@ func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, erro

// AddIssueLabels add one or more labels to one issue
func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
var labels []*Label
return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, opt, &labels)
}

// ReplaceIssueLabels replace old labels of issue with new labels
func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
var labels []*Label
return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, opt, &labels)
}

// DeleteIssueLabel delete one label of one issue by issue id and label id
Expand Down
14 changes: 2 additions & 12 deletions gitea/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package gitea

import (
"bytes"
"encoding/json"
"fmt"
"time"
)
Expand Down Expand Up @@ -47,12 +45,8 @@ type CreateMilestoneOption struct {

// CreateMilestone create one milestone with options
func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
milestone := new(Milestone)
return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), jsonHeader, bytes.NewReader(body), milestone)
return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), jsonHeader, opt, milestone)
}

// EditMilestoneOption options for editing a milestone
Expand All @@ -65,12 +59,8 @@ type EditMilestoneOption struct {

// EditMilestone modify milestone with options
func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
milestone := new(Milestone)
return milestone, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), milestone)
return milestone, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, opt, milestone)
}

// DeleteMilestone delete one milestone by milestone id
Expand Down
8 changes: 1 addition & 7 deletions gitea/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package gitea

import (
"bytes"
"encoding/json"
"fmt"
"time"
)
Expand Down Expand Up @@ -52,13 +50,9 @@ type AddTimeOption struct {

// AddTime adds time to issue with the given index
func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*TrackedTime, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
t := new(TrackedTime)
return t, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/times", owner, repo, index),
jsonHeader, bytes.NewReader(body), t)
jsonHeader, opt, t)
}

// ListTrackedTimes get tracked times of one issue via issue id
Expand Down
Loading