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

Commit 5aab4fe

Browse files
committed
init commit
1 parent 2b1076c commit 5aab4fe

File tree

5 files changed

+186
-3
lines changed

5 files changed

+186
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
go-gogs-client
2-
==============
1+
Gogs API client in Go
2+
=====================
33

4-
Gogs API client in Go.
4+
This package is still in experiment.
5+
6+
## License
7+
8+
This project is under the MIT License. See the [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) file for the full license text.

gogs.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2014 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 gogs
6+
7+
import (
8+
"encoding/json"
9+
"errors"
10+
"io"
11+
"io/ioutil"
12+
"net/http"
13+
"strings"
14+
)
15+
16+
func Version() string {
17+
return "0.0.1"
18+
}
19+
20+
// Client represents a Gogs API client.
21+
type Client struct {
22+
url string
23+
accessToken string
24+
client *http.Client
25+
}
26+
27+
// NewClient initializes and returns a API client.
28+
func NewClient(url, token string) *Client {
29+
return &Client{
30+
url: strings.TrimSuffix(url, "/"),
31+
accessToken: token,
32+
client: &http.Client{},
33+
}
34+
}
35+
36+
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
37+
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
38+
if err != nil {
39+
return nil, err
40+
}
41+
req.Header.Set("Authorization", "token "+c.accessToken)
42+
for k, v := range header {
43+
req.Header[k] = v
44+
}
45+
46+
resp, err := c.client.Do(req)
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer resp.Body.Close()
51+
52+
data, err := ioutil.ReadAll(resp.Body)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
if resp.StatusCode == 404 {
58+
return nil, errors.New("page not found")
59+
}
60+
61+
if resp.StatusCode != 200 && resp.StatusCode != 201 {
62+
errMap := make(map[string]interface{})
63+
if err = json.Unmarshal(data, &errMap); err != nil {
64+
return nil, err
65+
}
66+
return nil, errors.New(errMap["message"].(string))
67+
}
68+
69+
return data, nil
70+
}
71+
72+
func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) error {
73+
data, err := c.getResponse(method, path, header, body)
74+
if err != nil {
75+
return err
76+
}
77+
return json.Unmarshal(data, obj)
78+
}

repo.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2014 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 gogs
6+
7+
// Permission represents a API permission.
8+
type Permission struct {
9+
Admin bool `json:"admin"`
10+
Push bool `json:"push"`
11+
Pull bool `json:"pull"`
12+
}
13+
14+
// Repository represents a API repository.
15+
type Repository struct {
16+
Id int64 `json:"id"`
17+
Owner User `json:"owner"`
18+
FullName string `json:"full_name"`
19+
Private bool `json:"private"`
20+
Fork bool `json:"fork"`
21+
HtmlUrl string `json:"html_url"`
22+
CloneUrl string `json:"clone_url"`
23+
SshUrl string `json:"ssh_url"`
24+
Permissions Permission `json:"permissions"`
25+
}
26+
27+
// ListMyRepos lists all repositories for the authenticated user that has access to.
28+
func (c *Client) ListMyRepos() ([]*Repository, error) {
29+
repos := make([]*Repository, 0, 10)
30+
err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
31+
return repos, err
32+
}

repo_hooks.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2014 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 gogs
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
"net/http"
12+
)
13+
14+
type Hook struct {
15+
Id int64 `json:"id"`
16+
Type string `json:"type"`
17+
Events []string `json:"events"`
18+
Active bool `json:"active"`
19+
Config map[string]string `json:"config"`
20+
}
21+
22+
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
23+
hooks := make([]*Hook, 0, 10)
24+
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
25+
return hooks, err
26+
}
27+
28+
type CreateHookOption struct {
29+
Type string `json:"type"`
30+
Config map[string]string `json:"config"`
31+
Active bool `json:"active"`
32+
}
33+
34+
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) error {
35+
body, err := json.Marshal(&opt)
36+
if err != nil {
37+
return err
38+
}
39+
_, err = c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
40+
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
41+
return err
42+
}
43+
44+
type EditHookOption struct {
45+
Config map[string]string `json:"config"`
46+
Active bool `json:"active"`
47+
}
48+
49+
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
50+
body, err := json.Marshal(&opt)
51+
if err != nil {
52+
return err
53+
}
54+
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id),
55+
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
56+
return err
57+
}

user.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2014 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 gogs
6+
7+
// User represents a API user.
8+
type User struct {
9+
Id int64 `json:"id"`
10+
UserName string `json:"username"`
11+
AvatarUrl string `json:"avatar_url"`
12+
}

0 commit comments

Comments
 (0)