2024-02-23 00:26:49 +00:00
|
|
|
// Copyright 2024 The Forgjo Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
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.
|
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
package forgejo
|
2014-11-14 22:07:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-10-13 02:34:01 +01:00
|
|
|
"time"
|
2014-11-15 04:35:52 +00:00
|
|
|
)
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// Hook a hook is a web hook when one repository changed
|
|
|
|
type Hook struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
URL string `json:"-"`
|
|
|
|
Config map[string]string `json:"config"`
|
|
|
|
Events []string `json:"events"`
|
|
|
|
Active bool `json:"active"`
|
|
|
|
Updated time.Time `json:"updated_at"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
}
|
2017-08-21 09:23:17 +01:00
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
// HookType represent all webhook types forgejo currently offer
|
2021-07-01 16:10:21 +01:00
|
|
|
type HookType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HookTypeDingtalk webhook that dingtalk understand
|
|
|
|
HookTypeDingtalk HookType = "dingtalk"
|
|
|
|
// HookTypeDiscord webhook that discord understand
|
|
|
|
HookTypeDiscord HookType = "discord"
|
2024-02-23 00:26:49 +00:00
|
|
|
// HookTypeForgejo webhook that forgejo understand
|
|
|
|
HookTypeForgejo HookType = "forgejo"
|
2021-07-01 16:10:21 +01:00
|
|
|
// HookTypeGitea webhook that gitea understand
|
|
|
|
HookTypeGitea HookType = "gitea"
|
|
|
|
// HookTypeGogs webhook that gogs understand
|
|
|
|
HookTypeGogs HookType = "gogs"
|
|
|
|
// HookTypeMsteams webhook that msteams understand
|
|
|
|
HookTypeMsteams HookType = "msteams"
|
|
|
|
// HookTypeSlack webhook that slack understand
|
|
|
|
HookTypeSlack HookType = "slack"
|
|
|
|
// HookTypeTelegram webhook that telegram understand
|
|
|
|
HookTypeTelegram HookType = "telegram"
|
|
|
|
// HookTypeFeishu webhook that feishu understand
|
|
|
|
HookTypeFeishu HookType = "feishu"
|
|
|
|
)
|
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// ListHooksOptions options for listing hooks
|
|
|
|
type ListHooksOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// ListOrgHooks list all the hooks of one organization
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListOrgHooks(org string, opt ListHooksOptions) ([]*Hook, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
hooks := make([]*Hook, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks?%s", org, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
|
|
|
return hooks, resp, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:04:00 +00:00
|
|
|
// ListMyHooks list all the hooks of the authenticated user
|
|
|
|
func (c *Client) ListMyHooks(opt ListHooksOptions) ([]*Hook, *Response, error) {
|
|
|
|
opt.setDefaults()
|
|
|
|
hooks := make([]*Hook, 0, opt.PageSize)
|
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/hooks?%s", opt.getURLQuery().Encode()), nil, nil, &hooks)
|
|
|
|
return hooks, resp, err
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// ListRepoHooks list all the hooks of one repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListRepoHooks(user, repo string, opt ListHooksOptions) ([]*Hook, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
hooks := make([]*Hook, 0, opt.PageSize)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
|
|
|
return hooks, resp, err
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// GetOrgHook get a hook of an organization
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetOrgHook(org string, id int64) (*Hook, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-12-24 02:50:46 +00:00
|
|
|
h := new(Hook)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil, h)
|
|
|
|
return h, resp, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:04:00 +00:00
|
|
|
// GetMyHook get a hook of the authenticated user
|
|
|
|
func (c *Client) GetMyHook(id int64) (*Hook, *Response, error) {
|
|
|
|
h := new(Hook)
|
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/hooks/%d", id), nil, nil, h)
|
|
|
|
return h, resp, err
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// GetRepoHook get a hook of a repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetRepoHook(user, repo string, id int64) (*Hook, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-12-24 02:50:46 +00:00
|
|
|
h := new(Hook)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil, h)
|
|
|
|
return h, resp, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreateHookOption options when create a hook
|
|
|
|
type CreateHookOption struct {
|
2023-09-26 01:01:48 +01:00
|
|
|
Type HookType `json:"type"`
|
|
|
|
Config map[string]string `json:"config"`
|
|
|
|
Events []string `json:"events"`
|
|
|
|
BranchFilter string `json:"branch_filter"`
|
|
|
|
Active bool `json:"active"`
|
|
|
|
AuthorizationHeader string `json:"authorization_header"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 11:47:55 +01:00
|
|
|
// Validate the CreateHookOption struct
|
|
|
|
func (opt CreateHookOption) Validate() error {
|
|
|
|
if len(opt.Type) == 0 {
|
|
|
|
return fmt.Errorf("hook type needed")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// CreateOrgHook create one hook for an organization, with options
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) CreateOrgHook(org string, opt CreateHookOption) (*Hook, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-06-09 11:47:55 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2020-06-09 11:47:55 +01:00
|
|
|
}
|
2016-12-24 02:50:46 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
h := new(Hook)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/orgs/%s/hooks", org), jsonHeader, bytes.NewReader(body), h)
|
|
|
|
return h, resp, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:04:00 +00:00
|
|
|
// CreateMyHook create one hook for the authenticated user, with options
|
|
|
|
func (c *Client) CreateMyHook(opt CreateHookOption) (*Hook, *Response, error) {
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
h := new(Hook)
|
|
|
|
resp, err := c.getParsedResponse("POST", "/user/hooks", jsonHeader, bytes.NewReader(body), h)
|
|
|
|
return h, resp, err
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// CreateRepoHook create one hook for a repository, with options
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-11-14 22:07:41 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
2014-11-18 16:06:47 +00:00
|
|
|
h := new(Hook)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
|
|
|
|
return h, resp, err
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// EditHookOption options when modify one hook
|
|
|
|
type EditHookOption struct {
|
2023-09-26 01:01:48 +01:00
|
|
|
Config map[string]string `json:"config"`
|
|
|
|
Events []string `json:"events"`
|
|
|
|
BranchFilter string `json:"branch_filter"`
|
|
|
|
Active *bool `json:"active"`
|
|
|
|
AuthorizationHeader string `json:"authorization_header"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// EditOrgHook modify one hook of an organization, with hook id and options
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) EditOrgHook(org string, id int64, opt EditHookOption) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-24 02:50:46 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:04:00 +00:00
|
|
|
// EditMyHook modify one hook of the authenticated user, with hook id and options
|
|
|
|
func (c *Client) EditMyHook(id int64, opt EditHookOption) (*Response, error) {
|
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/user/hooks/%d", id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// EditRepoHook modify one hook of a repository, with hook id and options
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-14 22:07:41 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2016-07-17 01:46:54 +01:00
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// DeleteOrgHook delete one hook from an organization, with hook id
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) DeleteOrgHook(org string, id int64) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/hooks/%d", org, id), nil, nil)
|
|
|
|
return resp, err
|
2016-12-24 02:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:04:00 +00:00
|
|
|
// DeleteMyHook delete one hook from the authenticated user, with hook id
|
|
|
|
func (c *Client) DeleteMyHook(id int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/hooks/%d", id), nil, nil)
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2016-12-24 02:50:46 +00:00
|
|
|
// DeleteRepoHook delete one hook from a repository, with hook id
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) DeleteRepoHook(user, repo string, id int64) (*Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
|
|
|
|
return resp, err
|
2014-11-14 22:07:41 +00:00
|
|
|
}
|