repos_hooks.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2013 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. "time"
  10. )
  11. // WebHookPayload represents the data that is received from GitHub when a push
  12. // event hook is triggered. The format of these payloads pre-date most of the
  13. // GitHub v3 API, so there are lots of minor incompatibilities with the types
  14. // defined in the rest of the API. Therefore, several types are duplicated
  15. // here to account for these differences.
  16. //
  17. // GitHub API docs: https://help.github.com/articles/post-receive-hooks
  18. type WebHookPayload struct {
  19. After *string `json:"after,omitempty"`
  20. Before *string `json:"before,omitempty"`
  21. Commits []WebHookCommit `json:"commits,omitempty"`
  22. Compare *string `json:"compare,omitempty"`
  23. Created *bool `json:"created,omitempty"`
  24. Deleted *bool `json:"deleted,omitempty"`
  25. Forced *bool `json:"forced,omitempty"`
  26. HeadCommit *WebHookCommit `json:"head_commit,omitempty"`
  27. Pusher *User `json:"pusher,omitempty"`
  28. Ref *string `json:"ref,omitempty"`
  29. Repo *Repository `json:"repository,omitempty"`
  30. Sender *User `json:"sender,omitempty"`
  31. }
  32. func (w WebHookPayload) String() string {
  33. return Stringify(w)
  34. }
  35. // WebHookCommit represents the commit variant we receive from GitHub in a
  36. // WebHookPayload.
  37. type WebHookCommit struct {
  38. Added []string `json:"added,omitempty"`
  39. Author *WebHookAuthor `json:"author,omitempty"`
  40. Committer *WebHookAuthor `json:"committer,omitempty"`
  41. Distinct *bool `json:"distinct,omitempty"`
  42. ID *string `json:"id,omitempty"`
  43. Message *string `json:"message,omitempty"`
  44. Modified []string `json:"modified,omitempty"`
  45. Removed []string `json:"removed,omitempty"`
  46. Timestamp *time.Time `json:"timestamp,omitempty"`
  47. }
  48. func (w WebHookCommit) String() string {
  49. return Stringify(w)
  50. }
  51. // WebHookAuthor represents the author or committer of a commit, as specified
  52. // in a WebHookCommit. The commit author may not correspond to a GitHub User.
  53. type WebHookAuthor struct {
  54. Email *string `json:"email,omitempty"`
  55. Name *string `json:"name,omitempty"`
  56. Username *string `json:"username,omitempty"`
  57. }
  58. func (w WebHookAuthor) String() string {
  59. return Stringify(w)
  60. }
  61. // Hook represents a GitHub (web and service) hook for a repository.
  62. type Hook struct {
  63. CreatedAt *time.Time `json:"created_at,omitempty"`
  64. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  65. Name *string `json:"name,omitempty"`
  66. URL *string `json:"url,omitempty"`
  67. Events []string `json:"events,omitempty"`
  68. Active *bool `json:"active,omitempty"`
  69. Config map[string]interface{} `json:"config,omitempty"`
  70. ID *int64 `json:"id,omitempty"`
  71. }
  72. func (h Hook) String() string {
  73. return Stringify(h)
  74. }
  75. // CreateHook creates a Hook for the specified repository.
  76. // Name and Config are required fields.
  77. //
  78. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#create-a-hook
  79. func (s *RepositoriesService) CreateHook(ctx context.Context, owner, repo string, hook *Hook) (*Hook, *Response, error) {
  80. u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
  81. req, err := s.client.NewRequest("POST", u, hook)
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. h := new(Hook)
  86. resp, err := s.client.Do(ctx, req, h)
  87. if err != nil {
  88. return nil, resp, err
  89. }
  90. return h, resp, nil
  91. }
  92. // ListHooks lists all Hooks for the specified repository.
  93. //
  94. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#list
  95. func (s *RepositoriesService) ListHooks(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Hook, *Response, error) {
  96. u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
  97. u, err := addOptions(u, opt)
  98. if err != nil {
  99. return nil, nil, err
  100. }
  101. req, err := s.client.NewRequest("GET", u, nil)
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. var hooks []*Hook
  106. resp, err := s.client.Do(ctx, req, &hooks)
  107. if err != nil {
  108. return nil, resp, err
  109. }
  110. return hooks, resp, nil
  111. }
  112. // GetHook returns a single specified Hook.
  113. //
  114. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#get-single-hook
  115. func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, id int64) (*Hook, *Response, error) {
  116. u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
  117. req, err := s.client.NewRequest("GET", u, nil)
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. hook := new(Hook)
  122. resp, err := s.client.Do(ctx, req, hook)
  123. return hook, resp, err
  124. }
  125. // EditHook updates a specified Hook.
  126. //
  127. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#edit-a-hook
  128. func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
  129. u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
  130. req, err := s.client.NewRequest("PATCH", u, hook)
  131. if err != nil {
  132. return nil, nil, err
  133. }
  134. h := new(Hook)
  135. resp, err := s.client.Do(ctx, req, h)
  136. return h, resp, err
  137. }
  138. // DeleteHook deletes a specified Hook.
  139. //
  140. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#delete-a-hook
  141. func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  142. u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
  143. req, err := s.client.NewRequest("DELETE", u, nil)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return s.client.Do(ctx, req, nil)
  148. }
  149. // PingHook triggers a 'ping' event to be sent to the Hook.
  150. //
  151. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#ping-a-hook
  152. func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  153. u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
  154. req, err := s.client.NewRequest("POST", u, nil)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return s.client.Do(ctx, req, nil)
  159. }
  160. // TestHook triggers a test Hook by github.
  161. //
  162. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#test-a-push-hook
  163. func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  164. u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
  165. req, err := s.client.NewRequest("POST", u, nil)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return s.client.Do(ctx, req, nil)
  170. }