You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repos_prereceive_hooks.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // PreReceiveHook represents a GitHub pre-receive hook for a repository.
  11. type PreReceiveHook struct {
  12. ID *int64 `json:"id,omitempty"`
  13. Name *string `json:"name,omitempty"`
  14. Enforcement *string `json:"enforcement,omitempty"`
  15. ConfigURL *string `json:"configuration_url,omitempty"`
  16. }
  17. func (p PreReceiveHook) String() string {
  18. return Stringify(p)
  19. }
  20. // ListPreReceiveHooks lists all pre-receive hooks for the specified repository.
  21. //
  22. // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#list-pre-receive-hooks
  23. func (s *RepositoriesService) ListPreReceiveHooks(ctx context.Context, owner, repo string, opt *ListOptions) ([]*PreReceiveHook, *Response, error) {
  24. u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks", owner, repo)
  25. u, err := addOptions(u, opt)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. req, err := s.client.NewRequest("GET", u, nil)
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. // TODO: remove custom Accept header when this API fully launches.
  34. req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
  35. var hooks []*PreReceiveHook
  36. resp, err := s.client.Do(ctx, req, &hooks)
  37. if err != nil {
  38. return nil, resp, err
  39. }
  40. return hooks, resp, nil
  41. }
  42. // GetPreReceiveHook returns a single specified pre-receive hook.
  43. //
  44. // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#get-a-single-pre-receive-hook
  45. func (s *RepositoriesService) GetPreReceiveHook(ctx context.Context, owner, repo string, id int64) (*PreReceiveHook, *Response, error) {
  46. u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
  47. req, err := s.client.NewRequest("GET", u, nil)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. // TODO: remove custom Accept header when this API fully launches.
  52. req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
  53. h := new(PreReceiveHook)
  54. resp, err := s.client.Do(ctx, req, h)
  55. if err != nil {
  56. return nil, resp, err
  57. }
  58. return h, resp, nil
  59. }
  60. // UpdatePreReceiveHook updates a specified pre-receive hook.
  61. //
  62. // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#update-pre-receive-hook-enforcement
  63. func (s *RepositoriesService) UpdatePreReceiveHook(ctx context.Context, owner, repo string, id int64, hook *PreReceiveHook) (*PreReceiveHook, *Response, error) {
  64. u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
  65. req, err := s.client.NewRequest("PATCH", u, hook)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. // TODO: remove custom Accept header when this API fully launches.
  70. req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
  71. h := new(PreReceiveHook)
  72. resp, err := s.client.Do(ctx, req, h)
  73. if err != nil {
  74. return nil, resp, err
  75. }
  76. return h, resp, nil
  77. }
  78. // DeletePreReceiveHook deletes a specified pre-receive hook.
  79. //
  80. // GitHub API docs: https://developer.github.com/enterprise/2.13/v3/repos/pre_receive_hooks/#remove-enforcement-overrides-for-a-pre-receive-hook
  81. func (s *RepositoriesService) DeletePreReceiveHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  82. u := fmt.Sprintf("repos/%v/%v/pre-receive-hooks/%d", owner, repo, id)
  83. req, err := s.client.NewRequest("DELETE", u, nil)
  84. if err != nil {
  85. return nil, err
  86. }
  87. // TODO: remove custom Accept header when this API fully launches.
  88. req.Header.Set("Accept", mediaTypePreReceiveHooksPreview)
  89. return s.client.Do(ctx, req, nil)
  90. }