From 29fa1bf9fd49e27eeeb1f6526fd508de2c785571 Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Thu, 18 Apr 2019 19:37:20 +0700 Subject: [PATCH 1/7] convert body to query string in case of a GET request --- gitea/gitea.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++---- gitea/pull.go | 12 ++++------ 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/gitea/gitea.go b/gitea/gitea.go index fa35e09..bc4f1a0 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -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 @@ -52,11 +56,59 @@ 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) + if err != nil { + return nil, err + } + + unescaped, err := url.PathUnescape(path) if err != nil { return nil, err } + + // Set the encoded path data + u.RawPath = "/api/v1" + path + u.Path = "/api/v1" + unescaped + + 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" { + bodyBytes, err := json.Marshal(body) + 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) } @@ -70,7 +122,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 @@ -106,7 +158,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 diff --git a/gitea/pull.go b/gitea/pull.go index 6fcdd1d..8656a3b 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -63,18 +63,14 @@ type PRBranchInfo struct { // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { - Page int `json:"page"` - State string `json:"state"` + Page int `url:"page"` + State string `url:"state"` } // ListRepoPullRequests list PRs of one repository -func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } +func (c *Client) ListRepoPullRequests(owner, repo string, opt *ListPullRequestsOptions) ([]*PullRequest, error) { prs := make([]*PullRequest, 0, 10) - return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs) + return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, opt, &prs) } // GetPullRequest get information of one PR From e39daf01ed5db602fb85d2490587ffa82cd13537 Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Fri, 19 Apr 2019 12:26:04 +0700 Subject: [PATCH 2/7] pass CreatePullRequestOption as it is --- gitea/pull.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/gitea/pull.go b/gitea/pull.go index 8656a3b..7b0cef9 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -95,13 +95,9 @@ type CreatePullRequestOption struct { // CreatePullRequest create pull request with options 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) + jsonHeader, opt, pr) } // EditPullRequestOption options when modify pull request From 955dddd1312330dbfc7726ed5f2b3a2398841422 Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Fri, 19 Apr 2019 19:28:50 +0700 Subject: [PATCH 3/7] pass *Options as it is --- gitea/admin_org.go | 8 +------- gitea/admin_repo.go | 8 +------- gitea/admin_user.go | 12 ++---------- gitea/fork.go | 10 ++-------- gitea/hook.go | 12 ++---------- gitea/issue.go | 6 +----- gitea/issue_comment.go | 14 ++------------ gitea/issue_label.go | 26 ++++---------------------- gitea/issue_milestone.go | 14 ++------------ gitea/issue_tracked_time.go | 8 +------- gitea/pull.go | 8 +------- gitea/release.go | 18 ++++-------------- gitea/repo.go | 20 +++----------------- gitea/repo_key.go | 8 +------- gitea/status.go | 10 ++-------- gitea/user_app.go | 8 +------- gitea/user_email.go | 6 +----- gitea/user_gpgkey.go | 8 +------- gitea/user_key.go | 8 +------- 19 files changed, 33 insertions(+), 179 deletions(-) diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 4071b6f..94786e3 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -5,18 +5,12 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" ) // 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) } diff --git a/gitea/admin_repo.go b/gitea/admin_repo.go index cf565ff..19be903 100644 --- a/gitea/admin_repo.go +++ b/gitea/admin_repo.go @@ -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) } diff --git a/gitea/admin_user.go b/gitea/admin_user.go index a4df703..5c81dc5 100644 --- a/gitea/admin_user.go +++ b/gitea/admin_user.go @@ -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 @@ -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) } diff --git a/gitea/fork.go b/gitea/fork.go index 5722249..ee5e356 100644 --- a/gitea/fork.go +++ b/gitea/fork.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" ) @@ -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 } diff --git a/gitea/hook.go b/gitea/hook.go index d36e4d4..9c92a5c 100644 --- a/gitea/hook.go +++ b/gitea/hook.go @@ -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 diff --git a/gitea/issue.go b/gitea/issue.go index 0a1defb..4e506b9 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -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 diff --git a/gitea/issue_comment.go b/gitea/issue_comment.go index 2c8127c..41dd700 100644 --- a/gitea/issue_comment.go +++ b/gitea/issue_comment.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -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 @@ -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. diff --git a/gitea/issue_label.go b/gitea/issue_label.go index 47d1b82..41eb36e 100644 --- a/gitea/issue_label.go +++ b/gitea/issue_label.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" ) @@ -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 @@ -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 @@ -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 diff --git a/gitea/issue_milestone.go b/gitea/issue_milestone.go index 775a6a9..16c4a1e 100644 --- a/gitea/issue_milestone.go +++ b/gitea/issue_milestone.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -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 @@ -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 diff --git a/gitea/issue_tracked_time.go b/gitea/issue_tracked_time.go index 7f4b64c..7fb07a7 100644 --- a/gitea/issue_tracked_time.go +++ b/gitea/issue_tracked_time.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -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 diff --git a/gitea/pull.go b/gitea/pull.go index 7b0cef9..d76cfdc 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -115,13 +113,9 @@ type EditPullRequestOption struct { // EditPullRequest modify pull request with PR id and options 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) + jsonHeader, opt, pr) } // MergePullRequest merge a PR to repository by PR id diff --git a/gitea/release.go b/gitea/release.go index 396251d..aa05d34 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -62,14 +60,10 @@ type CreateReleaseOption struct { // CreateRelease create a release func (c *Client) CreateRelease(user, repo string, form CreateReleaseOption) (*Release, error) { - body, err := json.Marshal(form) - if err != nil { - return nil, err - } r := new(Release) - err = c.getParsedResponse("POST", + err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/releases", user, repo), - jsonHeader, bytes.NewReader(body), r) + jsonHeader, form, r) return r, err } @@ -85,14 +79,10 @@ type EditReleaseOption struct { // EditRelease edit a release func (c *Client) EditRelease(user, repo string, id int64, form EditReleaseOption) (*Release, error) { - body, err := json.Marshal(form) - if err != nil { - return nil, err - } r := new(Release) - err = c.getParsedResponse("PATCH", + err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d", user, repo, id), - jsonHeader, bytes.NewReader(body), r) + jsonHeader, form, r) return r, err } diff --git a/gitea/repo.go b/gitea/repo.go index 8b7c0b1..ffd0f98 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -90,22 +88,14 @@ type CreateRepoOption struct { // CreateRepo creates a repository for authenticated user. func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } repo := new(Repository) - return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo) + return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, opt, repo) } // CreateOrgRepo creates an organization repository for authenticated user. func (c *Client) CreateOrgRepo(org 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("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo) + return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, opt, repo) } // GetRepo returns information of a repository of given owner. @@ -141,12 +131,8 @@ type MigrateRepoOption struct { // To migrate a repository for a organization, the authenticated user must be a // owner of the specified organization. func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } repo := new(Repository) - return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo) + return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, opt, repo) } // MirrorSync adds a mirrored repository to the mirror sync queue. diff --git a/gitea/repo_key.go b/gitea/repo_key.go index a1ae458..420c15d 100644 --- a/gitea/repo_key.go +++ b/gitea/repo_key.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -57,12 +55,8 @@ type CreateKeyOption struct { // CreateDeployKey options when create one deploy key func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } key := new(DeployKey) - return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key) + return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, opt, key) } // DeleteDeployKey delete deploy key with key id diff --git a/gitea/status.go b/gitea/status.go index 3060ab1..04cdbaa 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -70,14 +68,10 @@ type ListStatusesOption struct { // CreateStatus creates a new Status for a given Commit // // POST /repos/:owner/:repo/statuses/:sha -func (c *Client) CreateStatus(owner, repo, sha string, opts CreateStatusOption) (*Status, error) { - body, err := json.Marshal(&opts) - if err != nil { - return nil, err - } +func (c *Client) CreateStatus(owner, repo, sha string, opt CreateStatusOption) (*Status, error) { status := &Status{} return status, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/statuses/%s", owner, repo, sha), - jsonHeader, bytes.NewReader(body), status) + jsonHeader, opt, status) } // ListStatuses returns all statuses for a given Commit diff --git a/gitea/user_app.go b/gitea/user_app.go index 023837f..f84e3f5 100644 --- a/gitea/user_app.go +++ b/gitea/user_app.go @@ -6,9 +6,7 @@ package gitea import ( - "bytes" "encoding/base64" - "encoding/json" "fmt" "net/http" ) @@ -47,16 +45,12 @@ type CreateAccessTokenOption struct { // CreateAccessToken create one access token with options func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } t := new(AccessToken) return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", user), http.Header{ "content-type": []string{"application/json"}, "Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, - bytes.NewReader(body), t) + opt, t) } // DeleteAccessToken delete token with key id diff --git a/gitea/user_email.go b/gitea/user_email.go index 721f521..95fcec6 100644 --- a/gitea/user_email.go +++ b/gitea/user_email.go @@ -31,12 +31,8 @@ type CreateEmailOption struct { // AddEmail add one email to current user with options func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } emails := make([]*Email, 0, 3) - return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), emails) + return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, opt, emails) } // DeleteEmailOption options when deleting email addresses diff --git a/gitea/user_gpgkey.go b/gitea/user_gpgkey.go index 0817d89..061a91b 100644 --- a/gitea/user_gpgkey.go +++ b/gitea/user_gpgkey.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -65,12 +63,8 @@ func (c *Client) GetGPGKey(keyID int64) (*GPGKey, error) { // CreateGPGKey create GPG key with options func (c *Client) CreateGPGKey(opt CreateGPGKeyOption) (*GPGKey, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } key := new(GPGKey) - return key, c.getParsedResponse("POST", "/user/gpg_keys", jsonHeader, bytes.NewReader(body), key) + return key, c.getParsedResponse("POST", "/user/gpg_keys", jsonHeader, opt, key) } // DeleteGPGKey delete GPG key with key id diff --git a/gitea/user_key.go b/gitea/user_key.go index cccaa65..e11e3c2 100644 --- a/gitea/user_key.go +++ b/gitea/user_key.go @@ -5,8 +5,6 @@ package gitea import ( - "bytes" - "encoding/json" "fmt" "time" ) @@ -45,12 +43,8 @@ func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) { // CreatePublicKey create public key with options func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } key := new(PublicKey) - return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key) + return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, opt, key) } // DeletePublicKey delete public key with key id From 0d4947e3e27be8958e27782e190ee7c8e6b973eb Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Fri, 17 May 2019 10:01:28 +0700 Subject: [PATCH 4/7] manage team's members/repositories --- gitea/org_team.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/gitea/org_team.go b/gitea/org_team.go index 9de0a8d..a1b8b17 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -5,6 +5,10 @@ package gitea +import ( + "fmt" +) + // Team represents a team in an organization type Team struct { ID int64 `json:"id"` @@ -38,3 +42,69 @@ type EditTeamOption struct { // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki Units []string `json:"units"` } + +// ListOrgTeams list all teams of an organization +func (c *Client) ListOrgTeams(orgname string) ([]*Team, error) { + teams := make([]*Team, 0, 0) + return teams, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams", orgname), nil, nil, &teams) +} + +// CreateTeam creates a new team +func (c *Client) CreateTeam(orgname string, opt CreateTeamOption) (*Team, error) { + team := new(Team) + return team, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/teams", orgname), jsonHeader, opt, team) +} + +// GetTeam gets a team by team ID +func (c *Client) GetTeam(teamID int64) (*Team, error) { + team := new(Team) + return team, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d", teamID), nil, nil, team) +} + +// DeleteTeam delete a team by team ID +func (c *Client) DeleteTeam(teamID int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d", teamID), nil, nil) + return err +} + +// EditTeam modify a team via options +func (c *Client) EditTeam(teamID int64, opt EditTeamOption) error { + _, err := c.getResponse("PATCH", fmt.Sprintf("/teams/%d", teamID), jsonHeader, opt) + return err +} + +// ListTeamMembers list all members of a team +func (c *Client) ListTeamMembers(teamID int64) ([]*User, error) { + users := make([]*User, 0, 0) + return users, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/members", teamID), nil, nil, &users) +} + +// AddTeamMember adds a member to a team +func (c *Client) AddTeamMember(teamID int64, username string) error { + _, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/members/%s", teamID, username), nil, nil) + return err +} + +// RemoveTeamMember removes a member from a team +func (c *Client) RemoveTeamMember(teamID int64, username string) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/members/%s", teamID, username), nil, nil) + return err +} + +// ListTeamRepos list all members of a team +func (c *Client) ListTeamRepos(teamID int64) ([]*Repository, error) { + repos := make([]*Repository, 0, 0) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/teams/%d/repos", teamID), nil, nil, &repos) +} + +// AddTeamRepo adds a repository to a team +func (c *Client) AddTeamRepo(teamID int64, org, repo string) error { + _, err := c.getResponse("PUT", fmt.Sprintf("/teams/%d/repos/%s/%s", teamID, org, repo), nil, nil) + return err +} + +// RemoveTeamRepo removes a repository from a team +func (c *Client) RemoveTeamRepo(teamID int64, org, repo string) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/teams/%d/repos/%s/%s", teamID, org, repo), nil, nil) + return err +} From c1bfa72fd95e47924672affe259d9e42f1a2ba9f Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Fri, 17 May 2019 14:33:16 +0700 Subject: [PATCH 5/7] pass path as it is --- gitea/gitea.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/gitea/gitea.go b/gitea/gitea.go index bc4f1a0..331774c 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -57,20 +57,11 @@ func (c *Client) SetSudo(sudo string) { } func (c *Client) doRequest(method, path string, header http.Header, body interface{}) (*http.Response, error) { - u, err := url.Parse(c.url) + u, err := url.Parse(c.url + "/api/v1" + path) if err != nil { return nil, err } - unescaped, err := url.PathUnescape(path) - if err != nil { - return nil, err - } - - // Set the encoded path data - u.RawPath = "/api/v1" + path - u.Path = "/api/v1" + unescaped - if method == "GET" { if body != nil { q, err := query.Values(body) From 268d4f439aa47c0f7b9a87691740264e2b48cace Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Fri, 17 May 2019 23:12:40 +0700 Subject: [PATCH 6/7] process body when method = PATCH --- gitea/gitea.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/gitea.go b/gitea/gitea.go index 331774c..8af9fb3 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -82,7 +82,7 @@ func (c *Client) doRequest(method, path string, header http.Header, body interfa Host: u.Host, } - if method == "POST" || method == "PUT" { + if method == "POST" || method == "PUT" || method == "PATCH" { bodyBytes, err := json.Marshal(body) if err != nil { return nil, err From 2ada8d6bec3b302ed8b2839c2a5901308e8134bd Mon Sep 17 00:00:00 2001 From: Quan Anh Tong Date: Fri, 24 May 2019 11:41:51 +0700 Subject: [PATCH 7/7] add AdminListOrgs --- gitea/admin_org.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gitea/admin_org.go b/gitea/admin_org.go index 94786e3..4aa0b4f 100644 --- a/gitea/admin_org.go +++ b/gitea/admin_org.go @@ -8,6 +8,12 @@ import ( "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) { org := new(Organization)