2014-11-14 22:07:41 +00:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2017-02-13 11:50:09 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2014-11-14 22:07:41 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-11-07 01:57:49 +00:00
|
|
|
package gitea
|
2014-11-14 22:07:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2019-05-11 16:38:52 +01:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2014-11-15 04:35:52 +00:00
|
|
|
)
|
|
|
|
|
2019-05-11 16:38:52 +01:00
|
|
|
// Hook is equal to structs.Hook
|
|
|
|
type Hook = structs.Hook
|
2014-11-14 22:07:41 +00:00
|
|
|
|
2019-05-11 16:38:52 +01:00
|
|
|
// HookList is equal to structs.HookList
|
|
|
|
type HookList = structs.HookList
|
2017-08-21 09:23:17 +01:00
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// ListOrgHooks list all the hooks of one organization
|
2017-08-21 09:23:17 +01:00
|
|
|
func (c *Client) ListOrgHooks(org string) (HookList, error) {
|
2016-12-24 02:50:46 +00:00
|
|
|
hooks := make([]*Hook, 0, 10)
|
|
|
|
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks", org), nil, nil, &hooks)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// ListRepoHooks list all the hooks of one repository
|
2017-08-21 09:23:17 +01:00
|
|
|
func (c *Client) ListRepoHooks(user, repo string) (HookList, error) {
|
2014-11-14 22:07:41 +00:00
|
|
|
hooks := make([]*Hook, 0, 10)
|
2014-12-13 01:29:51 +00:00
|
|
|
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// GetOrgHook get a hook of an organization
|
|
|
|
func (c *Client) GetOrgHook(org string, id int64) (*Hook, error) {
|
|
|
|
h := new(Hook)
|
|
|
|
return h, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoHook get a hook of a repository
|
|
|
|
func (c *Client) GetRepoHook(user, repo string, id int64) (*Hook, error) {
|
|
|
|
h := new(Hook)
|
|
|
|
return h, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrgHook create one hook for an organization, with options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) CreateOrgHook(org string, opt structs.CreateHookOption) (*Hook, error) {
|
2016-12-24 02:50:46 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h := new(Hook)
|
|
|
|
return h, c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, bytes.NewReader(body), h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRepoHook create one hook for a repository, with options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) CreateRepoHook(user, repo string, opt structs.CreateHookOption) (*Hook, error) {
|
2014-11-14 22:07:41 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2014-11-18 16:06:47 +00:00
|
|
|
return nil, err
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
2014-11-18 16:06:47 +00:00
|
|
|
h := new(Hook)
|
2016-07-17 01:46:54 +01:00
|
|
|
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// EditOrgHook modify one hook of an organization, with hook id and options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) EditOrgHook(org string, id int64, opt structs.EditHookOption) error {
|
2016-12-24 02:50:46 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditRepoHook modify one hook of a repository, with hook id and options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) EditRepoHook(user, repo string, id int64, opt structs.EditHookOption) error {
|
2014-11-14 22:07:41 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-17 01:46:54 +01:00
|
|
|
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// DeleteOrgHook delete one hook from an organization, with hook id
|
|
|
|
func (c *Client) DeleteOrgHook(org string, id int64) error {
|
2019-06-14 04:05:32 +01:00
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil)
|
2016-12-24 02:50:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRepoHook delete one hook from a repository, with hook id
|
2016-07-17 01:46:54 +01:00
|
|
|
func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
|
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
|
2014-11-14 22:07:41 +00:00
|
|
|
return err
|
|
|
|
}
|