ListPullRequestsOptions does not work? #162

Closed
lunny wants to merge 7 commits from quantonganh/master into main
21 changed files with 161 additions and 196 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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,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)
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 +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
@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -5,8 +5,6 @@
package gitea
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
@ -63,18 +61,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
@ -99,13 +93,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
@ -123,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

View File

@ -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
}

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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