| 1 |
// Copyright 2018 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
// PreReceiveHook represents a GitHub pre-receive hook for a repository. |
| 14 |
type PreReceiveHook struct { |
| 15 |
ID *int64 `json:"id,omitempty"` |
| 16 |
Name *string `json:"name,omitempty"` |
| 17 |
Enforcement *string `json:"enforcement,omitempty"` |
| 18 |
ConfigURL *string `json:"configuration_url,omitempty"` |
| 19 |
} |
| 20 |
|
| 21 |
func (p PreReceiveHook) String() string { |
| 22 |
return Stringify(p) |
| 23 |
} |
| 24 |
|
| 25 |
// ListPreReceiveHooks lists all pre-receive hooks for the specified repository. |
| 26 |
// |
| 27 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/repo-pre-receive-hooks#list-pre-receive-hooks-for-a-repository |
| 28 |
// |
| 29 |
//meta:operation GET /repos/{owner}/{repo}/pre-receive-hooks |
| 30 |
func (s *RepositoriesService) ListPreReceiveHooks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*PreReceiveHook, *Response, error) { |
| 31 |
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks", owner, repo) |
| 32 |
u, err := addOptions(u, opts) |
| 33 |
if err != nil { |
| 34 |
return nil, nil, err |
| 35 |
} |
| 36 |
|
| 37 |
req, err := s.client.NewRequest("GET", u, nil) |
| 38 |
if err != nil { |
| 39 |
return nil, nil, err |
| 40 |
} |
| 41 |
|
| 42 |
// TODO: remove custom Accept header when this API fully launches. |
| 43 |
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) |
| 44 |
|
| 45 |
var hooks []*PreReceiveHook |
| 46 |
resp, err := s.client.Do(ctx, req, &hooks) |
| 47 |
if err != nil { |
| 48 |
return nil, resp, err |
| 49 |
} |
| 50 |
|
| 51 |
return hooks, resp, nil |
| 52 |
} |
| 53 |
|
| 54 |
// GetPreReceiveHook returns a single specified pre-receive hook. |
| 55 |
// |
| 56 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/repo-pre-receive-hooks#get-a-pre-receive-hook-for-a-repository |
| 57 |
// |
| 58 |
//meta:operation GET /repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id} |
| 59 |
func (s *RepositoriesService) GetPreReceiveHook(ctx context.Context, owner, repo string, id int64) (*PreReceiveHook, *Response, error) { |
| 60 |
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id) |
| 61 |
req, err := s.client.NewRequest("GET", u, nil) |
| 62 |
if err != nil { |
| 63 |
return nil, nil, err |
| 64 |
} |
| 65 |
|
| 66 |
// TODO: remove custom Accept header when this API fully launches. |
| 67 |
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) |
| 68 |
|
| 69 |
h := new(PreReceiveHook) |
| 70 |
resp, err := s.client.Do(ctx, req, h) |
| 71 |
if err != nil { |
| 72 |
return nil, resp, err |
| 73 |
} |
| 74 |
|
| 75 |
return h, resp, nil |
| 76 |
} |
| 77 |
|
| 78 |
// UpdatePreReceiveHook updates a specified pre-receive hook. |
| 79 |
// |
| 80 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/repo-pre-receive-hooks#update-pre-receive-hook-enforcement-for-a-repository |
| 81 |
// |
| 82 |
//meta:operation PATCH /repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id} |
| 83 |
func (s *RepositoriesService) UpdatePreReceiveHook(ctx context.Context, owner, repo string, id int64, hook *PreReceiveHook) (*PreReceiveHook, *Response, error) { |
| 84 |
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id) |
| 85 |
req, err := s.client.NewRequest("PATCH", u, hook) |
| 86 |
if err != nil { |
| 87 |
return nil, nil, err |
| 88 |
} |
| 89 |
|
| 90 |
// TODO: remove custom Accept header when this API fully launches. |
| 91 |
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) |
| 92 |
|
| 93 |
h := new(PreReceiveHook) |
| 94 |
resp, err := s.client.Do(ctx, req, h) |
| 95 |
if err != nil { |
| 96 |
return nil, resp, err |
| 97 |
} |
| 98 |
|
| 99 |
return h, resp, nil |
| 100 |
} |
| 101 |
|
| 102 |
// DeletePreReceiveHook deletes a specified pre-receive hook. |
| 103 |
// |
| 104 |
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/repo-pre-receive-hooks#remove-pre-receive-hook-enforcement-for-a-repository |
| 105 |
// |
| 106 |
//meta:operation DELETE /repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id} |
| 107 |
func (s *RepositoriesService) DeletePreReceiveHook(ctx context.Context, owner, repo string, id int64) (*Response, error) { |
| 108 |
u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id) |
| 109 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 110 |
if err != nil { |
| 111 |
return nil, err |
| 112 |
} |
| 113 |
|
| 114 |
// TODO: remove custom Accept header when this API fully launches. |
| 115 |
req.Header.Set("Accept", mediaTypePreReceiveHooksPreview) |
| 116 |
|
| 117 |
return s.client.Do(ctx, req, nil) |
| 118 |
} |