repo_hook.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gogs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. ErrInvalidReceiveHook = errors.New("Invalid JSON payload received over webhook")
  15. )
  16. type Hook struct {
  17. ID int64 `json:"id"`
  18. Type string `json:"type"`
  19. URL string `json:"-"`
  20. Config map[string]string `json:"config"`
  21. Events []string `json:"events"`
  22. Active bool `json:"active"`
  23. Updated time.Time `json:"updated_at"`
  24. Created time.Time `json:"created_at"`
  25. }
  26. func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
  27. hooks := make([]*Hook, 0, 10)
  28. return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
  29. }
  30. type CreateHookOption struct {
  31. Type string `json:"type" binding:"Required"`
  32. Config map[string]string `json:"config" binding:"Required"`
  33. Events []string `json:"events"`
  34. Active bool `json:"active"`
  35. }
  36. func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
  37. body, err := json.Marshal(&opt)
  38. if err != nil {
  39. return nil, err
  40. }
  41. h := new(Hook)
  42. return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
  43. }
  44. type EditHookOption struct {
  45. Config map[string]string `json:"config"`
  46. Events []string `json:"events"`
  47. Active *bool `json:"active"`
  48. }
  49. func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
  50. body, err := json.Marshal(&opt)
  51. if err != nil {
  52. return err
  53. }
  54. _, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
  55. return err
  56. }
  57. func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
  58. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
  59. return err
  60. }
  61. type Payloader interface {
  62. SetSecret(string)
  63. JSONPayload() ([]byte, error)
  64. }
  65. type PayloadUser struct {
  66. Name string `json:"name"`
  67. Email string `json:"email"`
  68. UserName string `json:"username"`
  69. }
  70. // FIXME: consider use same format as API when commits API are added.
  71. type PayloadCommit struct {
  72. ID string `json:"id"`
  73. Message string `json:"message"`
  74. URL string `json:"url"`
  75. Author *PayloadUser `json:"author"`
  76. Committer *PayloadUser `json:"committer"`
  77. Timestamp time.Time `json:"timestamp"`
  78. }
  79. var (
  80. _ Payloader = &CreatePayload{}
  81. _ Payloader = &PushPayload{}
  82. _ Payloader = &PullRequestPayload{}
  83. )
  84. // _________ __
  85. // \_ ___ \_______ ____ _____ _/ |_ ____
  86. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  87. // \ \____| | \/\ ___/ / __ \| | \ ___/
  88. // \______ /|__| \___ >____ /__| \___ >
  89. // \/ \/ \/ \/
  90. type CreatePayload struct {
  91. Secret string `json:"secret"`
  92. Ref string `json:"ref"`
  93. RefType string `json:"ref_type"`
  94. Repo *Repository `json:"repository"`
  95. Sender *User `json:"sender"`
  96. }
  97. func (p *CreatePayload) SetSecret(secret string) {
  98. p.Secret = secret
  99. }
  100. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  101. return json.MarshalIndent(p, "", " ")
  102. }
  103. // ParseCreateHook parses create event hook content.
  104. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  105. hook := new(CreatePayload)
  106. if err := json.Unmarshal(raw, hook); err != nil {
  107. return nil, err
  108. }
  109. // it is possible the JSON was parsed, however,
  110. // was not from Gogs (maybe was from Bitbucket)
  111. // So we'll check to be sure certain key fields
  112. // were populated
  113. switch {
  114. case hook.Repo == nil:
  115. return nil, ErrInvalidReceiveHook
  116. case len(hook.Ref) == 0:
  117. return nil, ErrInvalidReceiveHook
  118. }
  119. return hook, nil
  120. }
  121. // __________ .__
  122. // \______ \__ __ _____| |__
  123. // | ___/ | \/ ___/ | \
  124. // | | | | /\___ \| Y \
  125. // |____| |____//____ >___| /
  126. // \/ \/
  127. // PushPayload represents a payload information of push event.
  128. type PushPayload struct {
  129. Secret string `json:"secret"`
  130. Ref string `json:"ref"`
  131. Before string `json:"before"`
  132. After string `json:"after"`
  133. CompareURL string `json:"compare_url"`
  134. Commits []*PayloadCommit `json:"commits"`
  135. Repo *Repository `json:"repository"`
  136. Pusher *User `json:"pusher"`
  137. Sender *User `json:"sender"`
  138. }
  139. func (p *PushPayload) SetSecret(secret string) {
  140. p.Secret = secret
  141. }
  142. func (p *PushPayload) JSONPayload() ([]byte, error) {
  143. return json.MarshalIndent(p, "", " ")
  144. }
  145. // ParsePushHook parses push event hook content.
  146. func ParsePushHook(raw []byte) (*PushPayload, error) {
  147. hook := new(PushPayload)
  148. if err := json.Unmarshal(raw, hook); err != nil {
  149. return nil, err
  150. }
  151. switch {
  152. case hook.Repo == nil:
  153. return nil, ErrInvalidReceiveHook
  154. case len(hook.Ref) == 0:
  155. return nil, ErrInvalidReceiveHook
  156. }
  157. return hook, nil
  158. }
  159. // Branch returns branch name from a payload
  160. func (p *PushPayload) Branch() string {
  161. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  162. }
  163. // .___
  164. // | | ______ ________ __ ____
  165. // | |/ ___// ___/ | \_/ __ \
  166. // | |\___ \ \___ \| | /\ ___/
  167. // |___/____ >____ >____/ \___ >
  168. // \/ \/ \/
  169. type HookIssueAction string
  170. const (
  171. HOOK_ISSUE_OPENED HookIssueAction = "opened"
  172. HOOK_ISSUE_CLOSED HookIssueAction = "closed"
  173. HOOK_ISSUE_REOPENED HookIssueAction = "reopened"
  174. HOOK_ISSUE_EDITED HookIssueAction = "edited"
  175. HOOK_ISSUE_ASSIGNED HookIssueAction = "assigned"
  176. HOOK_ISSUE_UNASSIGNED HookIssueAction = "unassigned"
  177. HOOK_ISSUE_LABEL_UPDATED HookIssueAction = "label_updated"
  178. HOOK_ISSUE_LABEL_CLEARED HookIssueAction = "label_cleared"
  179. HOOK_ISSUE_SYNCHRONIZED HookIssueAction = "synchronized"
  180. )
  181. type ChangesFromPayload struct {
  182. From string `json:"from"`
  183. }
  184. type ChangesPayload struct {
  185. Title *ChangesFromPayload `json:"title,omitempty"`
  186. Body *ChangesFromPayload `json:"body,omitempty"`
  187. }
  188. // __________ .__ .__ __________ __
  189. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  190. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  191. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  192. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  193. // \/ \/ |__| \/ \/
  194. // PullRequestPayload represents a payload information of pull request event.
  195. type PullRequestPayload struct {
  196. Secret string `json:"secret"`
  197. Action HookIssueAction `json:"action"`
  198. Index int64 `json:"number"`
  199. Changes *ChangesPayload `json:"changes,omitempty"`
  200. PullRequest *PullRequest `json:"pull_request"`
  201. Repository *Repository `json:"repository"`
  202. Sender *User `json:"sender"`
  203. }
  204. func (p *PullRequestPayload) SetSecret(secret string) {
  205. p.Secret = secret
  206. }
  207. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  208. return json.MarshalIndent(p, "", " ")
  209. }