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

Commit d628d07

Browse files
ethantkoeniglunny
authored andcommitted
Webhook functions for organizations (#26)
1 parent 24b59f8 commit d628d07

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

gitea/repo_hook.go renamed to gitea/hook.go

+48-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,30 @@ type Hook struct {
3030
Created time.Time `json:"created_at"`
3131
}
3232

33+
// ListOrgHooks list all the hooks of one organization
34+
func (c *Client) ListOrgHooks(org string) ([]*Hook, error) {
35+
hooks := make([]*Hook, 0, 10)
36+
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks", org), nil, nil, &hooks)
37+
}
38+
3339
// ListRepoHooks list all the hooks of one repository
3440
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
3541
hooks := make([]*Hook, 0, 10)
3642
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
3743
}
3844

45+
// GetOrgHook get a hook of an organization
46+
func (c *Client) GetOrgHook(org string, id int64) (*Hook, error) {
47+
h := new(Hook)
48+
return h, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil, h)
49+
}
50+
51+
// GetRepoHook get a hook of a repository
52+
func (c *Client) GetRepoHook(user, repo string, id int64) (*Hook, error) {
53+
h := new(Hook)
54+
return h, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil, h)
55+
}
56+
3957
// CreateHookOption options when create a hook
4058
type CreateHookOption struct {
4159
Type string `json:"type" binding:"Required"`
@@ -44,7 +62,17 @@ type CreateHookOption struct {
4462
Active bool `json:"active"`
4563
}
4664

47-
// CreateRepoHook create one hook with options
65+
// CreateOrgHook create one hook for an organization, with options
66+
func (c *Client) CreateOrgHook(org string, opt CreateHookOption) (*Hook, error) {
67+
body, err := json.Marshal(&opt)
68+
if err != nil {
69+
return nil, err
70+
}
71+
h := new(Hook)
72+
return h, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, bytes.NewReader(body), h)
73+
}
74+
75+
// CreateRepoHook create one hook for a repository, with options
4876
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
4977
body, err := json.Marshal(&opt)
5078
if err != nil {
@@ -61,7 +89,17 @@ type EditHookOption struct {
6189
Active *bool `json:"active"`
6290
}
6391

64-
// EditRepoHook modify one hook with hook id and options
92+
// EditOrgHook modify one hook of an organization, with hook id and options
93+
func (c *Client) EditOrgHook(org string, id int64, opt EditHookOption) error {
94+
body, err := json.Marshal(&opt)
95+
if err != nil {
96+
return err
97+
}
98+
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), jsonHeader, bytes.NewReader(body))
99+
return err
100+
}
101+
102+
// EditRepoHook modify one hook of a repository, with hook id and options
65103
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
66104
body, err := json.Marshal(&opt)
67105
if err != nil {
@@ -71,7 +109,14 @@ func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) e
71109
return err
72110
}
73111

74-
// DeleteRepoHook delete one hook with hook id
112+
// DeleteOrgHook delete one hook from an organization, with hook id
113+
func (c *Client) DeleteOrgHook(org string, id int64) error {
114+
_, err := c.getResponse("DELETE", fmt.Sprintf("/org/%s/hooks/%d", org, id), nil, nil)
115+
return err
116+
}
117+
118+
119+
// DeleteRepoHook delete one hook from a repository, with hook id
75120
func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
76121
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
77122
return err

0 commit comments

Comments
 (0)