本站源代码
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.

227 lines
7.1KB

  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. URL *string `json:"url,omitempty"`
  66. ID *int64 `json:"id,omitempty"`
  67. // Only the following fields are used when creating a hook.
  68. // Config is required.
  69. Config map[string]interface{} `json:"config,omitempty"`
  70. Events []string `json:"events,omitempty"`
  71. Active *bool `json:"active,omitempty"`
  72. }
  73. func (h Hook) String() string {
  74. return Stringify(h)
  75. }
  76. // createHookRequest is a subset of Hook and is used internally
  77. // by CreateHook to pass only the known fields for the endpoint.
  78. //
  79. // See https://github.com/google/go-github/issues/1015 for more
  80. // information.
  81. type createHookRequest struct {
  82. // Config is required.
  83. Name string `json:"name"`
  84. Config map[string]interface{} `json:"config,omitempty"`
  85. Events []string `json:"events,omitempty"`
  86. Active *bool `json:"active,omitempty"`
  87. }
  88. // CreateHook creates a Hook for the specified repository.
  89. // Config is a required field.
  90. //
  91. // Note that only a subset of the hook fields are used and hook must
  92. // not be nil.
  93. //
  94. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#create-a-hook
  95. func (s *RepositoriesService) CreateHook(ctx context.Context, owner, repo string, hook *Hook) (*Hook, *Response, error) {
  96. u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
  97. hookReq := &createHookRequest{
  98. Name: "web",
  99. Events: hook.Events,
  100. Active: hook.Active,
  101. Config: hook.Config,
  102. }
  103. req, err := s.client.NewRequest("POST", u, hookReq)
  104. if err != nil {
  105. return nil, nil, err
  106. }
  107. h := new(Hook)
  108. resp, err := s.client.Do(ctx, req, h)
  109. if err != nil {
  110. return nil, resp, err
  111. }
  112. return h, resp, nil
  113. }
  114. // ListHooks lists all Hooks for the specified repository.
  115. //
  116. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#list
  117. func (s *RepositoriesService) ListHooks(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Hook, *Response, error) {
  118. u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
  119. u, err := addOptions(u, opt)
  120. if err != nil {
  121. return nil, nil, err
  122. }
  123. req, err := s.client.NewRequest("GET", u, nil)
  124. if err != nil {
  125. return nil, nil, err
  126. }
  127. var hooks []*Hook
  128. resp, err := s.client.Do(ctx, req, &hooks)
  129. if err != nil {
  130. return nil, resp, err
  131. }
  132. return hooks, resp, nil
  133. }
  134. // GetHook returns a single specified Hook.
  135. //
  136. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#get-single-hook
  137. func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, id int64) (*Hook, *Response, error) {
  138. u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
  139. req, err := s.client.NewRequest("GET", u, nil)
  140. if err != nil {
  141. return nil, nil, err
  142. }
  143. h := new(Hook)
  144. resp, err := s.client.Do(ctx, req, h)
  145. if err != nil {
  146. return nil, resp, err
  147. }
  148. return h, resp, nil
  149. }
  150. // EditHook updates a specified Hook.
  151. //
  152. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#edit-a-hook
  153. func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
  154. u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
  155. req, err := s.client.NewRequest("PATCH", u, hook)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. h := new(Hook)
  160. resp, err := s.client.Do(ctx, req, h)
  161. if err != nil {
  162. return nil, resp, err
  163. }
  164. return h, resp, nil
  165. }
  166. // DeleteHook deletes a specified Hook.
  167. //
  168. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#delete-a-hook
  169. func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  170. u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
  171. req, err := s.client.NewRequest("DELETE", u, nil)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return s.client.Do(ctx, req, nil)
  176. }
  177. // PingHook triggers a 'ping' event to be sent to the Hook.
  178. //
  179. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#ping-a-hook
  180. func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  181. u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
  182. req, err := s.client.NewRequest("POST", u, nil)
  183. if err != nil {
  184. return nil, err
  185. }
  186. return s.client.Do(ctx, req, nil)
  187. }
  188. // TestHook triggers a test Hook by github.
  189. //
  190. // GitHub API docs: https://developer.github.com/v3/repos/hooks/#test-a-push-hook
  191. func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  192. u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
  193. req, err := s.client.NewRequest("POST", u, nil)
  194. if err != nil {
  195. return nil, err
  196. }
  197. return s.client.Do(ctx, req, nil)
  198. }
上海开阖软件有限公司 沪ICP备12045867号-1