repo_hook.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. JSONPayload() ([]byte, error)
  63. }
  64. type PayloadUser struct {
  65. Name string `json:"name"`
  66. Email string `json:"email"`
  67. UserName string `json:"username"`
  68. }
  69. // FIXME: consider use same format as API when commits API are added.
  70. type PayloadCommit struct {
  71. ID string `json:"id"`
  72. Message string `json:"message"`
  73. URL string `json:"url"`
  74. Author *PayloadUser `json:"author"`
  75. Committer *PayloadUser `json:"committer"`
  76. Timestamp time.Time `json:"timestamp"`
  77. }
  78. var (
  79. _ Payloader = &CreatePayload{}
  80. _ Payloader = &PushPayload{}
  81. _ Payloader = &PullRequestPayload{}
  82. )
  83. // _________ __
  84. // \_ ___ \_______ ____ _____ _/ |_ ____
  85. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  86. // \ \____| | \/\ ___/ / __ \| | \ ___/
  87. // \______ /|__| \___ >____ /__| \___ >
  88. // \/ \/ \/ \/
  89. type CreatePayload struct {
  90. Ref string `json:"ref"`
  91. RefType string `json:"ref_type"`
  92. Repo *Repository `json:"repository"`
  93. Sender *User `json:"sender"`
  94. }
  95. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  96. return json.MarshalIndent(p, "", " ")
  97. }
  98. // ParseCreateHook parses create event hook content.
  99. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  100. hook := new(CreatePayload)
  101. if err := json.Unmarshal(raw, hook); err != nil {
  102. return nil, err
  103. }
  104. // it is possible the JSON was parsed, however,
  105. // was not from Gogs (maybe was from Bitbucket)
  106. // So we'll check to be sure certain key fields
  107. // were populated
  108. switch {
  109. case hook.Repo == nil:
  110. return nil, ErrInvalidReceiveHook
  111. case len(hook.Ref) == 0:
  112. return nil, ErrInvalidReceiveHook
  113. }
  114. return hook, nil
  115. }
  116. // __________ .__
  117. // \______ \__ __ _____| |__
  118. // | ___/ | \/ ___/ | \
  119. // | | | | /\___ \| Y \
  120. // |____| |____//____ >___| /
  121. // \/ \/
  122. // PushPayload represents a payload information of push event.
  123. type PushPayload struct {
  124. Ref string `json:"ref"`
  125. Before string `json:"before"`
  126. After string `json:"after"`
  127. CompareURL string `json:"compare_url"`
  128. Commits []*PayloadCommit `json:"commits"`
  129. Repo *Repository `json:"repository"`
  130. Pusher *User `json:"pusher"`
  131. Sender *User `json:"sender"`
  132. }
  133. func (p *PushPayload) JSONPayload() ([]byte, error) {
  134. return json.MarshalIndent(p, "", " ")
  135. }
  136. // ParsePushHook parses push event hook content.
  137. func ParsePushHook(raw []byte) (*PushPayload, error) {
  138. hook := new(PushPayload)
  139. if err := json.Unmarshal(raw, hook); err != nil {
  140. return nil, err
  141. }
  142. switch {
  143. case hook.Repo == nil:
  144. return nil, ErrInvalidReceiveHook
  145. case len(hook.Ref) == 0:
  146. return nil, ErrInvalidReceiveHook
  147. }
  148. return hook, nil
  149. }
  150. // Branch returns branch name from a payload
  151. func (p *PushPayload) Branch() string {
  152. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  153. }
  154. // .___
  155. // | | ______ ________ __ ____
  156. // | |/ ___// ___/ | \_/ __ \
  157. // | |\___ \ \___ \| | /\ ___/
  158. // |___/____ >____ >____/ \___ >
  159. // \/ \/ \/
  160. type HookIssueAction string
  161. const (
  162. HOOK_ISSUE_OPENED HookIssueAction = "opened"
  163. HOOK_ISSUE_CLOSED HookIssueAction = "closed"
  164. HOOK_ISSUE_REOPENED HookIssueAction = "reopened"
  165. HOOK_ISSUE_EDITED HookIssueAction = "edited"
  166. HOOK_ISSUE_ASSIGNED HookIssueAction = "assigned"
  167. HOOK_ISSUE_UNASSIGNED HookIssueAction = "unassigned"
  168. HOOK_ISSUE_LABEL_UPDATED HookIssueAction = "label_updated"
  169. HOOK_ISSUE_LABEL_CLEARED HookIssueAction = "label_cleared"
  170. HOOK_ISSUE_SYNCHRONIZED HookIssueAction = "synchronized"
  171. )
  172. type ChangesFromPayload struct {
  173. From string `json:"from"`
  174. }
  175. type ChangesPayload struct {
  176. Title *ChangesFromPayload `json:"title,omitempty"`
  177. Body *ChangesFromPayload `json:"body,omitempty"`
  178. }
  179. // __________ .__ .__ __________ __
  180. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  181. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  182. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  183. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  184. // \/ \/ |__| \/ \/
  185. // PullRequestPayload represents a payload information of pull request event.
  186. type PullRequestPayload struct {
  187. Action HookIssueAction `json:"action"`
  188. Index int64 `json:"number"`
  189. Changes *ChangesPayload `json:"changes,omitempty"`
  190. PullRequest *PullRequest `json:"pull_request"`
  191. Repository *Repository `json:"repository"`
  192. Sender *User `json:"sender"`
  193. }
  194. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  195. return json.MarshalIndent(p, "", " ")
  196. }