add APIs to creare repo

This commit is contained in:
Unknwon 2014-12-12 20:29:51 -05:00
parent 3b1d86c3a8
commit 92e76d616a
3 changed files with 49 additions and 10 deletions

View File

@ -14,7 +14,7 @@ import (
)
func Version() string {
return "0.0.1"
return "0.0.2"
}
// Client represents a Gogs API client.
@ -54,8 +54,11 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
return nil, err
}
if resp.StatusCode == 404 {
return nil, errors.New("page not found")
switch resp.StatusCode {
case 403:
return nil, errors.New("403 Forbidden")
case 404:
return nil, errors.New("404 Not Found")
}
if resp.StatusCode != 200 && resp.StatusCode != 201 {

38
repo.go
View File

@ -4,6 +4,13 @@
package gogs
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// Permission represents a API permission.
type Permission struct {
Admin bool `json:"admin"`
@ -30,3 +37,34 @@ func (c *Client) ListMyRepos() ([]*Repository, error) {
err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
return repos, err
}
type CreateRepoOption struct {
Name string `json:"name" binding:"Required"`
Description string `json:"description" binding:"MaxSize(255)"`
Private bool `form:"private"`
AutoInit bool `form:"auto_init"`
Gitignore string `form:"gitignore"`
License string `form:"license"`
}
// 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",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), 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),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
}

View File

@ -27,13 +27,12 @@ type Hook struct {
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
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
}
type CreateHookOption struct {
Type string `json:"type"`
Config map[string]string `json:"config"`
Type string `json:"type" binding:"Required"`
Config map[string]string `json:"config" binding:"Required"`
Active bool `json:"active"`
}
@ -43,14 +42,13 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook,
return nil, err
}
h := new(Hook)
err = c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
return h, err
}
type EditHookOption struct {
Config map[string]string `json:"config"`
Active bool `json:"active"`
Active *bool `json:"active"`
}
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {