2019-04-25 19:06:53 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GitHook represents a Git repository hook
|
|
|
|
type GitHook struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
IsActive bool `json:"is_active"`
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-27 15:48:09 +02:00
|
|
|
// ListRepoGitHooksOptions options for listing repository's githooks
|
|
|
|
type ListRepoGitHooksOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:06:53 +02:00
|
|
|
// ListRepoGitHooks list all the Git hooks of one repository
|
2020-09-16 04:01:41 +02:00
|
|
|
func (c *Client) ListRepoGitHooks(user, repo string, opt ListRepoGitHooksOptions) ([]*GitHook, *Response, error) {
|
2020-05-27 15:48:09 +02:00
|
|
|
opt.setDefaults()
|
|
|
|
hooks := make([]*GitHook, 0, opt.PageSize)
|
2020-09-16 04:01:41 +02:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &hooks)
|
|
|
|
return hooks, resp, err
|
2019-04-25 19:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoGitHook get a Git hook of a repository
|
2020-09-16 04:01:41 +02:00
|
|
|
func (c *Client) GetRepoGitHook(user, repo, id string) (*GitHook, *Response, error) {
|
2019-04-25 19:06:53 +02:00
|
|
|
h := new(GitHook)
|
2020-09-16 04:01:41 +02:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil, h)
|
|
|
|
return h, resp, err
|
2019-04-25 19:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditGitHookOption options when modifying one Git hook
|
|
|
|
type EditGitHookOption struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditRepoGitHook modify one Git hook of a repository
|
2020-09-16 04:01:41 +02:00
|
|
|
func (c *Client) EditRepoGitHook(user, repo, id string, opt EditGitHookOption) (*Response, error) {
|
2019-04-25 19:06:53 +02:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-16 04:01:41 +02:00
|
|
|
return nil, err
|
2019-04-25 19:06:53 +02:00
|
|
|
}
|
2020-09-16 04:01:41 +02:00
|
|
|
_, resp, err := c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2019-04-25 19:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRepoGitHook delete one Git hook from a repository
|
2020-09-16 04:01:41 +02:00
|
|
|
func (c *Client) DeleteRepoGitHook(user, repo, id string) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 19:06:53 +02:00
|
|
|
}
|