From 5aab4fe1f6d598fe72894ebddda455f8b8bc0e39 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 14 Nov 2014 17:07:41 -0500 Subject: [PATCH] init commit --- README.md | 10 +++++-- gogs.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ repo.go | 32 +++++++++++++++++++++ repo_hooks.go | 57 +++++++++++++++++++++++++++++++++++++ user.go | 12 ++++++++ 5 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 gogs.go create mode 100644 repo.go create mode 100644 repo_hooks.go create mode 100644 user.go diff --git a/README.md b/README.md index 44127e2..ae52096 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ -go-gogs-client -============== +Gogs API client in Go +===================== -Gogs API client in Go. +This package is still in experiment. + +## License + +This project is under the MIT License. See the [LICENSE](https://github.com/gogits/gogs/blob/master/LICENSE) file for the full license text. \ No newline at end of file diff --git a/gogs.go b/gogs.go new file mode 100644 index 0000000..1edb6fb --- /dev/null +++ b/gogs.go @@ -0,0 +1,78 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +import ( + "encoding/json" + "errors" + "io" + "io/ioutil" + "net/http" + "strings" +) + +func Version() string { + return "0.0.1" +} + +// Client represents a Gogs API client. +type Client struct { + url string + accessToken string + client *http.Client +} + +// NewClient initializes and returns a API client. +func NewClient(url, token string) *Client { + return &Client{ + url: strings.TrimSuffix(url, "/"), + accessToken: token, + client: &http.Client{}, + } +} + +func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) { + req, err := http.NewRequest(method, c.url+"/api/v1"+path, body) + if err != nil { + return nil, err + } + req.Header.Set("Authorization", "token "+c.accessToken) + for k, v := range header { + req.Header[k] = v + } + + resp, err := c.client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if resp.StatusCode == 404 { + return nil, errors.New("page not found") + } + + if resp.StatusCode != 200 && resp.StatusCode != 201 { + errMap := make(map[string]interface{}) + if err = json.Unmarshal(data, &errMap); err != nil { + return nil, err + } + return nil, errors.New(errMap["message"].(string)) + } + + return data, nil +} + +func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) error { + data, err := c.getResponse(method, path, header, body) + if err != nil { + return err + } + return json.Unmarshal(data, obj) +} diff --git a/repo.go b/repo.go new file mode 100644 index 0000000..a697402 --- /dev/null +++ b/repo.go @@ -0,0 +1,32 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +// Permission represents a API permission. +type Permission struct { + Admin bool `json:"admin"` + Push bool `json:"push"` + Pull bool `json:"pull"` +} + +// Repository represents a API repository. +type Repository struct { + Id int64 `json:"id"` + Owner User `json:"owner"` + FullName string `json:"full_name"` + Private bool `json:"private"` + Fork bool `json:"fork"` + HtmlUrl string `json:"html_url"` + CloneUrl string `json:"clone_url"` + SshUrl string `json:"ssh_url"` + Permissions Permission `json:"permissions"` +} + +// ListMyRepos lists all repositories for the authenticated user that has access to. +func (c *Client) ListMyRepos() ([]*Repository, error) { + repos := make([]*Repository, 0, 10) + err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos) + return repos, err +} diff --git a/repo_hooks.go b/repo_hooks.go new file mode 100644 index 0000000..3a2d2c6 --- /dev/null +++ b/repo_hooks.go @@ -0,0 +1,57 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +type Hook struct { + Id int64 `json:"id"` + Type string `json:"type"` + Events []string `json:"events"` + Active bool `json:"active"` + Config map[string]string `json:"config"` +} + +func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) { + hooks := make([]*Hook, 0, 10) + err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks) + return hooks, err +} + +type CreateHookOption struct { + Type string `json:"type"` + Config map[string]string `json:"config"` + Active bool `json:"active"` +} + +func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + return err +} + +type EditHookOption struct { + Config map[string]string `json:"config"` + Active bool `json:"active"` +} + +func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + return err +} diff --git a/user.go b/user.go new file mode 100644 index 0000000..92981bc --- /dev/null +++ b/user.go @@ -0,0 +1,12 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +// User represents a API user. +type User struct { + Id int64 `json:"id"` + UserName string `json:"username"` + AvatarUrl string `json:"avatar_url"` +}