add APIs to creare repo
This commit is contained in:
parent
3b1d86c3a8
commit
92e76d616a
3 changed files with 49 additions and 10 deletions
9
gogs.go
9
gogs.go
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Version() string {
|
func Version() string {
|
||||||
return "0.0.1"
|
return "0.0.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client represents a Gogs API client.
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == 404 {
|
switch resp.StatusCode {
|
||||||
return nil, errors.New("page not found")
|
case 403:
|
||||||
|
return nil, errors.New("403 Forbidden")
|
||||||
|
case 404:
|
||||||
|
return nil, errors.New("404 Not Found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 && resp.StatusCode != 201 {
|
if resp.StatusCode != 200 && resp.StatusCode != 201 {
|
||||||
|
|
38
repo.go
38
repo.go
|
@ -4,6 +4,13 @@
|
||||||
|
|
||||||
package gogs
|
package gogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
// Permission represents a API permission.
|
// Permission represents a API permission.
|
||||||
type Permission struct {
|
type Permission struct {
|
||||||
Admin bool `json:"admin"`
|
Admin bool `json:"admin"`
|
||||||
|
@ -30,3 +37,34 @@ func (c *Client) ListMyRepos() ([]*Repository, error) {
|
||||||
err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
|
err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
|
||||||
return repos, err
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -27,13 +27,12 @@ type Hook struct {
|
||||||
|
|
||||||
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
|
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
|
||||||
hooks := make([]*Hook, 0, 10)
|
hooks := make([]*Hook, 0, 10)
|
||||||
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
|
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
|
||||||
return hooks, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateHookOption struct {
|
type CreateHookOption struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type" binding:"Required"`
|
||||||
Config map[string]string `json:"config"`
|
Config map[string]string `json:"config" binding:"Required"`
|
||||||
Active bool `json:"active"`
|
Active bool `json:"active"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,14 +42,13 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
h := new(Hook)
|
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)
|
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
|
||||||
return h, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EditHookOption struct {
|
type EditHookOption struct {
|
||||||
Config map[string]string `json:"config"`
|
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 {
|
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
|
||||||
|
|
Loading…
Reference in a new issue