repo_hook.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 = &DeletePayload{}
  81. _ Payloader = &PushPayload{}
  82. _ Payloader = &PullRequestPayload{}
  83. )
  84. // _________ __
  85. // \_ ___ \_______ ____ _____ _/ |_ ____
  86. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  87. // \ \____| | \/\ ___/ / __ \| | \ ___/
  88. // \______ /|__| \___ >____ /__| \___ >
  89. // \/ \/ \/ \/
  90. type CreatePayload struct {
  91. Ref string `json:"ref"`
  92. RefType string `json:"ref_type"`
  93. DefaultBranch string `json:"default_branch"`
  94. Repo *Repository `json:"repository"`
  95. Sender *User `json:"sender"`
  96. }
  97. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  98. return json.MarshalIndent(p, "", " ")
  99. }
  100. // ParseCreateHook parses create event hook content.
  101. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  102. hook := new(CreatePayload)
  103. if err := json.Unmarshal(raw, hook); err != nil {
  104. return nil, err
  105. }
  106. // it is possible the JSON was parsed, however,
  107. // was not from Gogs (maybe was from Bitbucket)
  108. // So we'll check to be sure certain key fields
  109. // were populated
  110. switch {
  111. case hook.Repo == nil:
  112. return nil, ErrInvalidReceiveHook
  113. case len(hook.Ref) == 0:
  114. return nil, ErrInvalidReceiveHook
  115. }
  116. return hook, nil
  117. }
  118. // ________ .__ __
  119. // \______ \ ____ | | _____/ |_ ____
  120. // | | \_/ __ \| | _/ __ \ __\/ __ \
  121. // | ` \ ___/| |_\ ___/| | \ ___/
  122. // /_______ /\___ >____/\___ >__| \___ >
  123. // \/ \/ \/ \/
  124. type PusherType string
  125. const (
  126. PUSHER_TYPE_USER PusherType = "user"
  127. )
  128. type DeletePayload struct {
  129. Ref string `json:"ref"`
  130. RefType string `json:"ref_type"`
  131. PusherType PusherType `json:"pusher_type"`
  132. Repo *Repository `json:"repository"`
  133. Sender *User `json:"sender"`
  134. }
  135. func (p *DeletePayload) JSONPayload() ([]byte, error) {
  136. return json.MarshalIndent(p, "", " ")
  137. }
  138. // ___________ __
  139. // \_ _____/__________| | __
  140. // | __)/ _ \_ __ \ |/ /
  141. // | \( <_> ) | \/ <
  142. // \___ / \____/|__| |__|_ \
  143. // \/ \/
  144. type ForkPayload struct {
  145. Forkee *Repository `json:"forkee"`
  146. Repo *Repository `json:"repository"`
  147. Sender *User `json:"sender"`
  148. }
  149. func (p *ForkPayload) JSONPayload() ([]byte, error) {
  150. return json.MarshalIndent(p, "", " ")
  151. }
  152. // __________ .__
  153. // \______ \__ __ _____| |__
  154. // | ___/ | \/ ___/ | \
  155. // | | | | /\___ \| Y \
  156. // |____| |____//____ >___| /
  157. // \/ \/
  158. // PushPayload represents a payload information of push event.
  159. type PushPayload struct {
  160. Ref string `json:"ref"`
  161. Before string `json:"before"`
  162. After string `json:"after"`
  163. CompareURL string `json:"compare_url"`
  164. Commits []*PayloadCommit `json:"commits"`
  165. Repo *Repository `json:"repository"`
  166. Pusher *User `json:"pusher"`
  167. Sender *User `json:"sender"`
  168. }
  169. func (p *PushPayload) JSONPayload() ([]byte, error) {
  170. return json.MarshalIndent(p, "", " ")
  171. }
  172. // ParsePushHook parses push event hook content.
  173. func ParsePushHook(raw []byte) (*PushPayload, error) {
  174. hook := new(PushPayload)
  175. if err := json.Unmarshal(raw, hook); err != nil {
  176. return nil, err
  177. }
  178. switch {
  179. case hook.Repo == nil:
  180. return nil, ErrInvalidReceiveHook
  181. case len(hook.Ref) == 0:
  182. return nil, ErrInvalidReceiveHook
  183. }
  184. return hook, nil
  185. }
  186. // Branch returns branch name from a payload
  187. func (p *PushPayload) Branch() string {
  188. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  189. }
  190. // .___
  191. // | | ______ ________ __ ____
  192. // | |/ ___// ___/ | \_/ __ \
  193. // | |\___ \ \___ \| | /\ ___/
  194. // |___/____ >____ >____/ \___ >
  195. // \/ \/ \/
  196. type HookIssueAction string
  197. const (
  198. HOOK_ISSUE_OPENED HookIssueAction = "opened"
  199. HOOK_ISSUE_CLOSED HookIssueAction = "closed"
  200. HOOK_ISSUE_REOPENED HookIssueAction = "reopened"
  201. HOOK_ISSUE_EDITED HookIssueAction = "edited"
  202. HOOK_ISSUE_ASSIGNED HookIssueAction = "assigned"
  203. HOOK_ISSUE_UNASSIGNED HookIssueAction = "unassigned"
  204. HOOK_ISSUE_LABEL_UPDATED HookIssueAction = "label_updated"
  205. HOOK_ISSUE_LABEL_CLEARED HookIssueAction = "label_cleared"
  206. HOOK_ISSUE_SYNCHRONIZED HookIssueAction = "synchronized"
  207. )
  208. type ChangesFromPayload struct {
  209. From string `json:"from"`
  210. }
  211. type ChangesPayload struct {
  212. Title *ChangesFromPayload `json:"title,omitempty"`
  213. Body *ChangesFromPayload `json:"body,omitempty"`
  214. }
  215. // __________ .__ .__ __________ __
  216. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  217. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  218. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  219. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  220. // \/ \/ |__| \/ \/
  221. // PullRequestPayload represents a payload information of pull request event.
  222. type PullRequestPayload struct {
  223. Action HookIssueAction `json:"action"`
  224. Index int64 `json:"number"`
  225. Changes *ChangesPayload `json:"changes,omitempty"`
  226. PullRequest *PullRequest `json:"pull_request"`
  227. Repository *Repository `json:"repository"`
  228. Sender *User `json:"sender"`
  229. }
  230. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  231. return json.MarshalIndent(p, "", " ")
  232. }