orgs_hooks.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015 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. // ListHooks lists all Hooks for the specified organization.
  11. //
  12. // GitHub API docs: https://developer.github.com/v3/orgs/hooks/#list-hooks
  13. func (s *OrganizationsService) ListHooks(ctx context.Context, org string, opt *ListOptions) ([]*Hook, *Response, error) {
  14. u := fmt.Sprintf("orgs/%v/hooks", org)
  15. u, err := addOptions(u, opt)
  16. if err != nil {
  17. return nil, nil, err
  18. }
  19. req, err := s.client.NewRequest("GET", u, nil)
  20. if err != nil {
  21. return nil, nil, err
  22. }
  23. var hooks []*Hook
  24. resp, err := s.client.Do(ctx, req, &hooks)
  25. if err != nil {
  26. return nil, resp, err
  27. }
  28. return hooks, resp, nil
  29. }
  30. // GetHook returns a single specified Hook.
  31. //
  32. // GitHub API docs: https://developer.github.com/v3/orgs/hooks/#get-single-hook
  33. func (s *OrganizationsService) GetHook(ctx context.Context, org string, id int64) (*Hook, *Response, error) {
  34. u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
  35. req, err := s.client.NewRequest("GET", u, nil)
  36. if err != nil {
  37. return nil, nil, err
  38. }
  39. hook := new(Hook)
  40. resp, err := s.client.Do(ctx, req, hook)
  41. return hook, resp, err
  42. }
  43. // CreateHook creates a Hook for the specified org.
  44. // Name and Config are required fields.
  45. //
  46. // GitHub API docs: https://developer.github.com/v3/orgs/hooks/#create-a-hook
  47. func (s *OrganizationsService) CreateHook(ctx context.Context, org string, hook *Hook) (*Hook, *Response, error) {
  48. u := fmt.Sprintf("orgs/%v/hooks", org)
  49. req, err := s.client.NewRequest("POST", u, hook)
  50. if err != nil {
  51. return nil, nil, err
  52. }
  53. h := new(Hook)
  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. // EditHook updates a specified Hook.
  61. //
  62. // GitHub API docs: https://developer.github.com/v3/orgs/hooks/#edit-a-hook
  63. func (s *OrganizationsService) EditHook(ctx context.Context, org string, id int64, hook *Hook) (*Hook, *Response, error) {
  64. u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
  65. req, err := s.client.NewRequest("PATCH", u, hook)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. h := new(Hook)
  70. resp, err := s.client.Do(ctx, req, h)
  71. return h, resp, err
  72. }
  73. // PingHook triggers a 'ping' event to be sent to the Hook.
  74. //
  75. // GitHub API docs: https://developer.github.com/v3/orgs/hooks/#ping-a-hook
  76. func (s *OrganizationsService) PingHook(ctx context.Context, org string, id int64) (*Response, error) {
  77. u := fmt.Sprintf("orgs/%v/hooks/%d/pings", org, id)
  78. req, err := s.client.NewRequest("POST", u, nil)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return s.client.Do(ctx, req, nil)
  83. }
  84. // DeleteHook deletes a specified Hook.
  85. //
  86. // GitHub API docs: https://developer.github.com/v3/orgs/hooks/#delete-a-hook
  87. func (s *OrganizationsService) DeleteHook(ctx context.Context, org string, id int64) (*Response, error) {
  88. u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
  89. req, err := s.client.NewRequest("DELETE", u, nil)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return s.client.Do(ctx, req, nil)
  94. }