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

Commit af4eb9d

Browse files
authored
Merge pull request #1 from lunny/master
Sync from upstream gogits/go-gogs-client and merge
2 parents c8b7600 + b489697 commit af4eb9d

25 files changed

+1170
-145
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
.idea

admin_org.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
)
12+
13+
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) {
14+
body, err := json.Marshal(&opt)
15+
if err != nil {
16+
return nil, err
17+
}
18+
org := new(Organization)
19+
return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user),
20+
jsonHeader, bytes.NewReader(body), org)
21+
}

admin_repo.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
)
12+
13+
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) {
14+
body, err := json.Marshal(&opt)
15+
if err != nil {
16+
return nil, err
17+
}
18+
repo := new(Repository)
19+
return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user),
20+
jsonHeader, bytes.NewReader(body), repo)
21+
}

admin_user.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
)
12+
13+
type CreateUserOption struct {
14+
SourceID int64 `json:"source_id"`
15+
LoginName string `json:"login_name"`
16+
Username string `json:"username" binding:"Required;AlphaDashDot;MaxSize(35)"`
17+
FullName string `json:"full_name" binding:"MaxSize(100)"`
18+
Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
19+
Password string `json:"password" binding:"MaxSize(255)"`
20+
SendNotify bool `json:"send_notify"`
21+
}
22+
23+
func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
24+
body, err := json.Marshal(&opt)
25+
if err != nil {
26+
return nil, err
27+
}
28+
user := new(User)
29+
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
30+
}
31+
32+
type EditUserOption struct {
33+
SourceID int64 `json:"source_id"`
34+
LoginName string `json:"login_name"`
35+
FullName string `json:"full_name" binding:"MaxSize(100)"`
36+
Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
37+
Password string `json:"password" binding:"MaxSize(255)"`
38+
Website string `json:"website" binding:"MaxSize(50)"`
39+
Location string `json:"location" binding:"MaxSize(50)"`
40+
Active *bool `json:"active"`
41+
Admin *bool `json:"admin"`
42+
AllowGitHook *bool `json:"allow_git_hook"`
43+
AllowImportLocal *bool `json:"allow_import_local"`
44+
MaxRepoCreation *int `json:"max_repo_creation"`
45+
}
46+
47+
func (c *Client) AdminEditUser(user string, opt EditUserOption) error {
48+
body, err := json.Marshal(&opt)
49+
if err != nil {
50+
return err
51+
}
52+
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
53+
return err
54+
}
55+
56+
func (c *Client) AdminDeleteUser(user string) error {
57+
_, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil)
58+
return err
59+
}
60+
61+
func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) {
62+
body, err := json.Marshal(&opt)
63+
if err != nil {
64+
return nil, err
65+
}
66+
key := new(PublicKey)
67+
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
68+
}

gitea.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func Version() string {
17-
return "0.0.2"
17+
return "0.12.3"
1818
}
1919

2020
// Client represents a Gogs API client.
@@ -33,7 +33,12 @@ func NewClient(url, token string) *Client {
3333
}
3434
}
3535

36-
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
36+
// SetHTTPClient replaces default http.Client with user given one.
37+
func (c *Client) SetHTTPClient(client *http.Client) {
38+
c.client = client
39+
}
40+
41+
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) {
3742
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
3843
if err != nil {
3944
return nil, err
@@ -43,7 +48,11 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
4348
req.Header[k] = v
4449
}
4550

46-
resp, err := c.client.Do(req)
51+
return c.client.Do(req)
52+
}
53+
54+
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
55+
resp, err := c.doRequest(method, path, header, body)
4756
if err != nil {
4857
return nil, err
4958
}
@@ -61,7 +70,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
6170
return nil, errors.New("404 Not Found")
6271
}
6372

64-
if resp.StatusCode != 200 && resp.StatusCode != 201 {
73+
if resp.StatusCode/100 != 2 {
6574
errMap := make(map[string]interface{})
6675
if err = json.Unmarshal(data, &errMap); err != nil {
6776
return nil, err

issue.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2016 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
"time"
12+
)
13+
14+
type StateType string
15+
16+
const (
17+
STATE_OPEN StateType = "open"
18+
STATE_CLOSED StateType = "closed"
19+
)
20+
21+
type PullRequestMeta struct {
22+
HasMerged bool `json:"merged"`
23+
Merged *time.Time `json:"merged_at"`
24+
}
25+
26+
type Issue struct {
27+
ID int64 `json:"id"`
28+
Index int64 `json:"number"`
29+
Poster *User `json:"user"`
30+
Title string `json:"title"`
31+
Body string `json:"body"`
32+
Labels []*Label `json:"labels"`
33+
Milestone *Milestone `json:"milestone"`
34+
Assignee *User `json:"assignee"`
35+
State StateType `json:"state"`
36+
Comments int `json:"comments"`
37+
Created time.Time `json:"created_at"`
38+
Updated time.Time `json:"updated_at"`
39+
40+
PullRequest *PullRequestMeta `json:"pull_request"`
41+
}
42+
43+
type ListIssueOption struct {
44+
Page int
45+
}
46+
47+
func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) {
48+
issues := make([]*Issue, 0, 10)
49+
return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues)
50+
}
51+
52+
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) {
53+
issue := new(Issue)
54+
return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
55+
}
56+
57+
type CreateIssueOption struct {
58+
Title string `json:"title" binding:"Required"`
59+
Body string `json:"body"`
60+
Assignee string `json:"assignee"`
61+
Milestone int64 `json:"milestone"`
62+
Labels []int64 `json:"labels"`
63+
Closed bool `json:"closed"`
64+
}
65+
66+
func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) {
67+
body, err := json.Marshal(&opt)
68+
if err != nil {
69+
return nil, err
70+
}
71+
issue := new(Issue)
72+
return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
73+
jsonHeader, bytes.NewReader(body), issue)
74+
}
75+
76+
type EditIssueOption struct {
77+
Title string `json:"title"`
78+
Body *string `json:"body"`
79+
Assignee *string `json:"assignee"`
80+
Milestone *int64 `json:"milestone"`
81+
State *string `json:"state"`
82+
}
83+
84+
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) {
85+
body, err := json.Marshal(&opt)
86+
if err != nil {
87+
return nil, err
88+
}
89+
issue := new(Issue)
90+
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
91+
jsonHeader, bytes.NewReader(body), issue)
92+
}

issue_comment.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2016 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
"time"
12+
)
13+
14+
// Comment represents a comment in commit and issue page.
15+
type Comment struct {
16+
ID int64 `json:"id"`
17+
Poster *User `json:"user"`
18+
Body string `json:"body"`
19+
Created time.Time `json:"created_at"`
20+
Updated time.Time `json:"updated_at"`
21+
}
22+
23+
// ListIssueComments list comments on an issue.
24+
func (c *Client) ListIssueComments(owner, repo string, index int64) ([]*Comment, error) {
25+
comments := make([]*Comment, 0, 10)
26+
return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), nil, nil, &comments)
27+
}
28+
29+
// CreateIssueCommentOption is option when creating an issue comment.
30+
type CreateIssueCommentOption struct {
31+
Body string `json:"body" binding:"Required"`
32+
}
33+
34+
// CreateIssueComment create comment on an issue.
35+
func (c *Client) CreateIssueComment(owner, repo string, index int64, opt CreateIssueCommentOption) (*Comment, error) {
36+
body, err := json.Marshal(&opt)
37+
if err != nil {
38+
return nil, err
39+
}
40+
comment := new(Comment)
41+
return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments", owner, repo, index), jsonHeader, bytes.NewReader(body), comment)
42+
}
43+
44+
// EditIssueCommentOption is option when editing an issue comment.
45+
type EditIssueCommentOption struct {
46+
Body string `json:"body" binding:"Required"`
47+
}
48+
49+
// EditIssueComment edits an issue comment.
50+
func (c *Client) EditIssueComment(owner, repo string, index, commentID int64, opt EditIssueCommentOption) (*Comment, error) {
51+
body, err := json.Marshal(&opt)
52+
if err != nil {
53+
return nil, err
54+
}
55+
comment := new(Comment)
56+
return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments/%d", owner, repo, index, commentID), jsonHeader, bytes.NewReader(body), comment)
57+
}

0 commit comments

Comments
 (0)