|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gitea |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | +) |
| 12 | + |
| 13 | +// GitHook represents a Git repository hook |
| 14 | +type GitHook struct { |
| 15 | + Name string `json:"name"` |
| 16 | + IsActive bool `json:"is_active"` |
| 17 | + Content string `json:"content,omitempty"` |
| 18 | +} |
| 19 | + |
| 20 | +// GitHookList represents a list of Git hooks |
| 21 | +type GitHookList []*GitHook |
| 22 | + |
| 23 | +// ListRepoGitHooks list all the Git hooks of one repository |
| 24 | +func (c *Client) ListRepoGitHooks(user, repo string) (GitHookList, error) { |
| 25 | + hooks := make([]*GitHook, 0, 10) |
| 26 | + return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git", user, repo), nil, nil, &hooks) |
| 27 | +} |
| 28 | + |
| 29 | +// GetRepoGitHook get a Git hook of a repository |
| 30 | +func (c *Client) GetRepoGitHook(user, repo, id string) (*GitHook, error) { |
| 31 | + h := new(GitHook) |
| 32 | + return h, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil, h) |
| 33 | +} |
| 34 | + |
| 35 | +// EditGitHookOption options when modifying one Git hook |
| 36 | +type EditGitHookOption struct { |
| 37 | + Content string `json:"content"` |
| 38 | +} |
| 39 | + |
| 40 | +// EditRepoGitHook modify one Git hook of a repository |
| 41 | +func (c *Client) EditRepoGitHook(user, repo, id string, opt EditGitHookOption) error { |
| 42 | + body, err := json.Marshal(&opt) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + _, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), jsonHeader, bytes.NewReader(body)) |
| 47 | + return err |
| 48 | +} |
| 49 | + |
| 50 | +// DeleteRepoGitHook delete one Git hook from a repository |
| 51 | +func (c *Client) DeleteRepoGitHook(user, repo, id string) error { |
| 52 | + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/git/%s", user, repo, id), nil, nil) |
| 53 | + return err |
| 54 | +} |
0 commit comments