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

Commit 92e76d6

Browse files
committed
add APIs to creare repo
1 parent 3b1d86c commit 92e76d6

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

gogs.go

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

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

2020
// Client represents a Gogs API client.
@@ -54,8 +54,11 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
5454
return nil, err
5555
}
5656

57-
if resp.StatusCode == 404 {
58-
return nil, errors.New("page not found")
57+
switch resp.StatusCode {
58+
case 403:
59+
return nil, errors.New("403 Forbidden")
60+
case 404:
61+
return nil, errors.New("404 Not Found")
5962
}
6063

6164
if resp.StatusCode != 200 && resp.StatusCode != 201 {

repo.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
package gogs
66

7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
11+
"net/http"
12+
)
13+
714
// Permission represents a API permission.
815
type Permission struct {
916
Admin bool `json:"admin"`
@@ -30,3 +37,34 @@ func (c *Client) ListMyRepos() ([]*Repository, error) {
3037
err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
3138
return repos, err
3239
}
40+
41+
type CreateRepoOption struct {
42+
Name string `json:"name" binding:"Required"`
43+
Description string `json:"description" binding:"MaxSize(255)"`
44+
Private bool `form:"private"`
45+
AutoInit bool `form:"auto_init"`
46+
Gitignore string `form:"gitignore"`
47+
License string `form:"license"`
48+
}
49+
50+
// CreateRepo creates a repository for authenticated user.
51+
func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
52+
body, err := json.Marshal(&opt)
53+
if err != nil {
54+
return nil, err
55+
}
56+
repo := new(Repository)
57+
return repo, c.getParsedResponse("POST", "/user/repos",
58+
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
59+
}
60+
61+
// CreateOrgRepo creates an organization repository for authenticated user.
62+
func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, error) {
63+
body, err := json.Marshal(&opt)
64+
if err != nil {
65+
return nil, err
66+
}
67+
repo := new(Repository)
68+
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org),
69+
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
70+
}

repo_hooks.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ type Hook struct {
2727

2828
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
2929
hooks := make([]*Hook, 0, 10)
30-
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
31-
return hooks, err
30+
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
3231
}
3332

3433
type CreateHookOption struct {
35-
Type string `json:"type"`
36-
Config map[string]string `json:"config"`
34+
Type string `json:"type" binding:"Required"`
35+
Config map[string]string `json:"config" binding:"Required"`
3736
Active bool `json:"active"`
3837
}
3938

@@ -43,14 +42,13 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook,
4342
return nil, err
4443
}
4544
h := new(Hook)
46-
err = c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
45+
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
4746
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
48-
return h, err
4947
}
5048

5149
type EditHookOption struct {
5250
Config map[string]string `json:"config"`
53-
Active bool `json:"active"`
51+
Active *bool `json:"active"`
5452
}
5553

5654
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {

0 commit comments

Comments
 (0)