repo_hook.go 10 KB

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