repo_hook.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. Added []string `json:"added"`
  77. Removed []string `json:"removed"`
  78. Modified []string `json:"modified"`
  79. Timestamp time.Time `json:"timestamp"`
  80. }
  81. var (
  82. _ Payloader = &CreatePayload{}
  83. _ Payloader = &DeletePayload{}
  84. _ Payloader = &ForkPayload{}
  85. _ Payloader = &PushPayload{}
  86. _ Payloader = &IssuesPayload{}
  87. _ Payloader = &IssueCommentPayload{}
  88. _ Payloader = &PullRequestPayload{}
  89. )
  90. // _________ __
  91. // \_ ___ \_______ ____ _____ _/ |_ ____
  92. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  93. // \ \____| | \/\ ___/ / __ \| | \ ___/
  94. // \______ /|__| \___ >____ /__| \___ >
  95. // \/ \/ \/ \/
  96. type CreatePayload struct {
  97. Ref string `json:"ref"`
  98. RefType string `json:"ref_type"`
  99. DefaultBranch string `json:"default_branch"`
  100. Repo *Repository `json:"repository"`
  101. Sender *User `json:"sender"`
  102. }
  103. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  104. return json.MarshalIndent(p, "", " ")
  105. }
  106. // ParseCreateHook parses create event hook content.
  107. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  108. hook := new(CreatePayload)
  109. if err := json.Unmarshal(raw, hook); err != nil {
  110. return nil, err
  111. }
  112. // it is possible the JSON was parsed, however,
  113. // was not from Gogs (maybe was from Bitbucket)
  114. // So we'll check to be sure certain key fields
  115. // were populated
  116. switch {
  117. case hook.Repo == nil:
  118. return nil, ErrInvalidReceiveHook
  119. case len(hook.Ref) == 0:
  120. return nil, ErrInvalidReceiveHook
  121. }
  122. return hook, nil
  123. }
  124. // ________ .__ __
  125. // \______ \ ____ | | _____/ |_ ____
  126. // | | \_/ __ \| | _/ __ \ __\/ __ \
  127. // | ` \ ___/| |_\ ___/| | \ ___/
  128. // /_______ /\___ >____/\___ >__| \___ >
  129. // \/ \/ \/ \/
  130. type PusherType string
  131. const (
  132. PUSHER_TYPE_USER PusherType = "user"
  133. )
  134. type DeletePayload struct {
  135. Ref string `json:"ref"`
  136. RefType string `json:"ref_type"`
  137. PusherType PusherType `json:"pusher_type"`
  138. Repo *Repository `json:"repository"`
  139. Sender *User `json:"sender"`
  140. }
  141. func (p *DeletePayload) JSONPayload() ([]byte, error) {
  142. return json.MarshalIndent(p, "", " ")
  143. }
  144. // ___________ __
  145. // \_ _____/__________| | __
  146. // | __)/ _ \_ __ \ |/ /
  147. // | \( <_> ) | \/ <
  148. // \___ / \____/|__| |__|_ \
  149. // \/ \/
  150. type ForkPayload struct {
  151. Forkee *Repository `json:"forkee"`
  152. Repo *Repository `json:"repository"`
  153. Sender *User `json:"sender"`
  154. }
  155. func (p *ForkPayload) JSONPayload() ([]byte, error) {
  156. return json.MarshalIndent(p, "", " ")
  157. }
  158. // __________ .__
  159. // \______ \__ __ _____| |__
  160. // | ___/ | \/ ___/ | \
  161. // | | | | /\___ \| Y \
  162. // |____| |____//____ >___| /
  163. // \/ \/
  164. // PushPayload represents a payload information of push event.
  165. type PushPayload struct {
  166. Ref string `json:"ref"`
  167. Before string `json:"before"`
  168. After string `json:"after"`
  169. CompareURL string `json:"compare_url"`
  170. Commits []*PayloadCommit `json:"commits"`
  171. Repo *Repository `json:"repository"`
  172. Pusher *User `json:"pusher"`
  173. Sender *User `json:"sender"`
  174. }
  175. func (p *PushPayload) JSONPayload() ([]byte, error) {
  176. return json.MarshalIndent(p, "", " ")
  177. }
  178. // ParsePushHook parses push event hook content.
  179. func ParsePushHook(raw []byte) (*PushPayload, error) {
  180. hook := new(PushPayload)
  181. if err := json.Unmarshal(raw, hook); err != nil {
  182. return nil, err
  183. }
  184. switch {
  185. case hook.Repo == nil:
  186. return nil, ErrInvalidReceiveHook
  187. case len(hook.Ref) == 0:
  188. return nil, ErrInvalidReceiveHook
  189. }
  190. return hook, nil
  191. }
  192. // Branch returns branch name from a payload
  193. func (p *PushPayload) Branch() string {
  194. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  195. }
  196. // .___
  197. // | | ______ ________ __ ____
  198. // | |/ ___// ___/ | \_/ __ \
  199. // | |\___ \ \___ \| | /\ ___/
  200. // |___/____ >____ >____/ \___ >
  201. // \/ \/ \/
  202. type HookIssueAction string
  203. const (
  204. HOOK_ISSUE_OPENED HookIssueAction = "opened"
  205. HOOK_ISSUE_CLOSED HookIssueAction = "closed"
  206. HOOK_ISSUE_REOPENED HookIssueAction = "reopened"
  207. HOOK_ISSUE_EDITED HookIssueAction = "edited"
  208. HOOK_ISSUE_ASSIGNED HookIssueAction = "assigned"
  209. HOOK_ISSUE_UNASSIGNED HookIssueAction = "unassigned"
  210. HOOK_ISSUE_LABEL_UPDATED HookIssueAction = "label_updated"
  211. HOOK_ISSUE_LABEL_CLEARED HookIssueAction = "label_cleared"
  212. HOOK_ISSUE_MILESTONED HookIssueAction = "milestoned"
  213. HOOK_ISSUE_DEMILESTONED HookIssueAction = "demilestoned"
  214. HOOK_ISSUE_SYNCHRONIZED HookIssueAction = "synchronized"
  215. )
  216. type ChangesFromPayload struct {
  217. From string `json:"from"`
  218. }
  219. type ChangesPayload struct {
  220. Title *ChangesFromPayload `json:"title,omitempty"`
  221. Body *ChangesFromPayload `json:"body,omitempty"`
  222. }
  223. // IssuesPayload represents a payload information of issues event.
  224. type IssuesPayload struct {
  225. Action HookIssueAction `json:"action"`
  226. Index int64 `json:"number"`
  227. Issue *Issue `json:"issue"`
  228. Changes *ChangesPayload `json:"changes,omitempty"`
  229. Repository *Repository `json:"repository"`
  230. Sender *User `json:"sender"`
  231. }
  232. func (p *IssuesPayload) JSONPayload() ([]byte, error) {
  233. return json.MarshalIndent(p, "", " ")
  234. }
  235. type HookIssueCommentAction string
  236. const (
  237. HOOK_ISSUE_COMMENT_CREATED HookIssueCommentAction = "created"
  238. HOOK_ISSUE_COMMENT_EDITED HookIssueCommentAction = "edited"
  239. HOOK_ISSUE_COMMENT_DELETED HookIssueCommentAction = "deleted"
  240. )
  241. // IssueCommentPayload represents a payload information of issue comment event.
  242. type IssueCommentPayload struct {
  243. Action HookIssueCommentAction `json:"action"`
  244. Issue *Issue `json:"issue"`
  245. Comment *Comment `json:"comment"`
  246. Changes *ChangesPayload `json:"changes,omitempty"`
  247. Repository *Repository `json:"repository"`
  248. Sender *User `json:"sender"`
  249. }
  250. func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
  251. return json.MarshalIndent(p, "", " ")
  252. }
  253. // __________ .__ .__ __________ __
  254. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  255. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  256. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  257. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  258. // \/ \/ |__| \/ \/
  259. // PullRequestPayload represents a payload information of pull request event.
  260. type PullRequestPayload struct {
  261. Action HookIssueAction `json:"action"`
  262. Index int64 `json:"number"`
  263. PullRequest *PullRequest `json:"pull_request"`
  264. Changes *ChangesPayload `json:"changes,omitempty"`
  265. Repository *Repository `json:"repository"`
  266. Sender *User `json:"sender"`
  267. }
  268. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  269. return json.MarshalIndent(p, "", " ")
  270. }
  271. // __________ .__
  272. // \______ \ ____ | | ____ _____ ______ ____
  273. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  274. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  275. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  276. // \/ \/ \/ \/ \/ \/
  277. type HookReleaseAction string
  278. const (
  279. HOOK_RELEASE_PUBLISHED HookReleaseAction = "published"
  280. )
  281. // ReleasePayload represents a payload information of release event.
  282. type ReleasePayload struct {
  283. Action HookReleaseAction `json:"action"`
  284. Release *Release `json:"release"`
  285. Repository *Repository `json:"repository"`
  286. Sender *User `json:"sender"`
  287. }
  288. func (p *ReleasePayload) JSONPayload() ([]byte, error) {
  289. return json.MarshalIndent(p, "", " ")
  290. }