You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package structs
  6. import (
  7. "encoding/json"
  8. "errors"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. // ErrInvalidReceiveHook FIXME
  14. ErrInvalidReceiveHook = errors.New("Invalid JSON payload received over webhook")
  15. )
  16. // Hook a hook is a web hook when one repository changed
  17. type Hook struct {
  18. ID int64 `json:"id"`
  19. Type string `json:"type"`
  20. URL string `json:"-"`
  21. Config map[string]string `json:"config"`
  22. Events []string `json:"events"`
  23. Active bool `json:"active"`
  24. // swagger:strfmt date-time
  25. Updated time.Time `json:"updated_at"`
  26. // swagger:strfmt date-time
  27. Created time.Time `json:"created_at"`
  28. }
  29. // HookList represents a list of API hook.
  30. type HookList []*Hook
  31. // CreateHookOption options when create a hook
  32. type CreateHookOption struct {
  33. // required: true
  34. // enum: gitea,gogs,slack,discord
  35. Type string `json:"type" binding:"Required"`
  36. // required: true
  37. Config map[string]string `json:"config" binding:"Required"`
  38. Events []string `json:"events"`
  39. BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
  40. // default: false
  41. Active bool `json:"active"`
  42. }
  43. // EditHookOption options when modify one hook
  44. type EditHookOption struct {
  45. Config map[string]string `json:"config"`
  46. Events []string `json:"events"`
  47. BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
  48. Active *bool `json:"active"`
  49. }
  50. // Payloader payload is some part of one hook
  51. type Payloader interface {
  52. SetSecret(string)
  53. JSONPayload() ([]byte, error)
  54. }
  55. // PayloadUser represents the author or committer of a commit
  56. type PayloadUser struct {
  57. // Full name of the commit author
  58. Name string `json:"name"`
  59. // swagger:strfmt email
  60. Email string `json:"email"`
  61. UserName string `json:"username"`
  62. }
  63. // FIXME: consider using same format as API when commits API are added.
  64. // applies to PayloadCommit and PayloadCommitVerification
  65. // PayloadCommit represents a commit
  66. type PayloadCommit struct {
  67. // sha1 hash of the commit
  68. ID string `json:"id"`
  69. Message string `json:"message"`
  70. URL string `json:"url"`
  71. Author *PayloadUser `json:"author"`
  72. Committer *PayloadUser `json:"committer"`
  73. Verification *PayloadCommitVerification `json:"verification"`
  74. // swagger:strfmt date-time
  75. Timestamp time.Time `json:"timestamp"`
  76. Added []string `json:"added"`
  77. Removed []string `json:"removed"`
  78. Modified []string `json:"modified"`
  79. }
  80. // PayloadCommitVerification represents the GPG verification of a commit
  81. type PayloadCommitVerification struct {
  82. Verified bool `json:"verified"`
  83. Reason string `json:"reason"`
  84. Signature string `json:"signature"`
  85. Payload string `json:"payload"`
  86. }
  87. var (
  88. _ Payloader = &CreatePayload{}
  89. _ Payloader = &DeletePayload{}
  90. _ Payloader = &ForkPayload{}
  91. _ Payloader = &PushPayload{}
  92. _ Payloader = &IssuePayload{}
  93. _ Payloader = &IssueCommentPayload{}
  94. _ Payloader = &PullRequestPayload{}
  95. _ Payloader = &RepositoryPayload{}
  96. _ Payloader = &ReleasePayload{}
  97. )
  98. // _________ __
  99. // \_ ___ \_______ ____ _____ _/ |_ ____
  100. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  101. // \ \____| | \/\ ___/ / __ \| | \ ___/
  102. // \______ /|__| \___ >____ /__| \___ >
  103. // \/ \/ \/ \/
  104. // CreatePayload FIXME
  105. type CreatePayload struct {
  106. Secret string `json:"secret"`
  107. Sha string `json:"sha"`
  108. Ref string `json:"ref"`
  109. RefType string `json:"ref_type"`
  110. Repo *Repository `json:"repository"`
  111. Sender *User `json:"sender"`
  112. }
  113. // SetSecret modifies the secret of the CreatePayload
  114. func (p *CreatePayload) SetSecret(secret string) {
  115. p.Secret = secret
  116. }
  117. // JSONPayload return payload information
  118. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  119. return json.MarshalIndent(p, "", " ")
  120. }
  121. // ParseCreateHook parses create event hook content.
  122. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  123. hook := new(CreatePayload)
  124. if err := json.Unmarshal(raw, hook); err != nil {
  125. return nil, err
  126. }
  127. // it is possible the JSON was parsed, however,
  128. // was not from Gogs (maybe was from Bitbucket)
  129. // So we'll check to be sure certain key fields
  130. // were populated
  131. switch {
  132. case hook.Repo == nil:
  133. return nil, ErrInvalidReceiveHook
  134. case len(hook.Ref) == 0:
  135. return nil, ErrInvalidReceiveHook
  136. }
  137. return hook, nil
  138. }
  139. // ________ .__ __
  140. // \______ \ ____ | | _____/ |_ ____
  141. // | | \_/ __ \| | _/ __ \ __\/ __ \
  142. // | ` \ ___/| |_\ ___/| | \ ___/
  143. // /_______ /\___ >____/\___ >__| \___ >
  144. // \/ \/ \/ \/
  145. // PusherType define the type to push
  146. type PusherType string
  147. // describe all the PusherTypes
  148. const (
  149. PusherTypeUser PusherType = "user"
  150. )
  151. // DeletePayload represents delete payload
  152. type DeletePayload struct {
  153. Secret string `json:"secret"`
  154. Ref string `json:"ref"`
  155. RefType string `json:"ref_type"`
  156. PusherType PusherType `json:"pusher_type"`
  157. Repo *Repository `json:"repository"`
  158. Sender *User `json:"sender"`
  159. }
  160. // SetSecret modifies the secret of the DeletePayload
  161. func (p *DeletePayload) SetSecret(secret string) {
  162. p.Secret = secret
  163. }
  164. // JSONPayload implements Payload
  165. func (p *DeletePayload) JSONPayload() ([]byte, error) {
  166. return json.MarshalIndent(p, "", " ")
  167. }
  168. // ___________ __
  169. // \_ _____/__________| | __
  170. // | __)/ _ \_ __ \ |/ /
  171. // | \( <_> ) | \/ <
  172. // \___ / \____/|__| |__|_ \
  173. // \/ \/
  174. // ForkPayload represents fork payload
  175. type ForkPayload struct {
  176. Secret string `json:"secret"`
  177. Forkee *Repository `json:"forkee"`
  178. Repo *Repository `json:"repository"`
  179. Sender *User `json:"sender"`
  180. }
  181. // SetSecret modifies the secret of the ForkPayload
  182. func (p *ForkPayload) SetSecret(secret string) {
  183. p.Secret = secret
  184. }
  185. // JSONPayload implements Payload
  186. func (p *ForkPayload) JSONPayload() ([]byte, error) {
  187. return json.MarshalIndent(p, "", " ")
  188. }
  189. // HookIssueCommentAction defines hook issue comment action
  190. type HookIssueCommentAction string
  191. // all issue comment actions
  192. const (
  193. HookIssueCommentCreated HookIssueCommentAction = "created"
  194. HookIssueCommentEdited HookIssueCommentAction = "edited"
  195. HookIssueCommentDeleted HookIssueCommentAction = "deleted"
  196. )
  197. // IssueCommentPayload represents a payload information of issue comment event.
  198. type IssueCommentPayload struct {
  199. Secret string `json:"secret"`
  200. Action HookIssueCommentAction `json:"action"`
  201. Issue *Issue `json:"issue"`
  202. Comment *Comment `json:"comment"`
  203. Changes *ChangesPayload `json:"changes,omitempty"`
  204. Repository *Repository `json:"repository"`
  205. Sender *User `json:"sender"`
  206. }
  207. // SetSecret modifies the secret of the IssueCommentPayload
  208. func (p *IssueCommentPayload) SetSecret(secret string) {
  209. p.Secret = secret
  210. }
  211. // JSONPayload implements Payload
  212. func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
  213. return json.MarshalIndent(p, "", " ")
  214. }
  215. // __________ .__
  216. // \______ \ ____ | | ____ _____ ______ ____
  217. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  218. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  219. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  220. // \/ \/ \/ \/ \/ \/
  221. // HookReleaseAction defines hook release action type
  222. type HookReleaseAction string
  223. // all release actions
  224. const (
  225. HookReleasePublished HookReleaseAction = "published"
  226. HookReleaseUpdated HookReleaseAction = "updated"
  227. HookReleaseDeleted HookReleaseAction = "deleted"
  228. )
  229. // ReleasePayload represents a payload information of release event.
  230. type ReleasePayload struct {
  231. Secret string `json:"secret"`
  232. Action HookReleaseAction `json:"action"`
  233. Release *Release `json:"release"`
  234. Repository *Repository `json:"repository"`
  235. Sender *User `json:"sender"`
  236. }
  237. // SetSecret modifies the secret of the ReleasePayload
  238. func (p *ReleasePayload) SetSecret(secret string) {
  239. p.Secret = secret
  240. }
  241. // JSONPayload implements Payload
  242. func (p *ReleasePayload) JSONPayload() ([]byte, error) {
  243. return json.MarshalIndent(p, "", " ")
  244. }
  245. // __________ .__
  246. // \______ \__ __ _____| |__
  247. // | ___/ | \/ ___/ | \
  248. // | | | | /\___ \| Y \
  249. // |____| |____//____ >___| /
  250. // \/ \/
  251. // PushPayload represents a payload information of push event.
  252. type PushPayload struct {
  253. Secret string `json:"secret"`
  254. Ref string `json:"ref"`
  255. Before string `json:"before"`
  256. After string `json:"after"`
  257. CompareURL string `json:"compare_url"`
  258. Commits []*PayloadCommit `json:"commits"`
  259. HeadCommit *PayloadCommit `json:"head_commit"`
  260. Repo *Repository `json:"repository"`
  261. Pusher *User `json:"pusher"`
  262. Sender *User `json:"sender"`
  263. }
  264. // SetSecret modifies the secret of the PushPayload
  265. func (p *PushPayload) SetSecret(secret string) {
  266. p.Secret = secret
  267. }
  268. // JSONPayload FIXME
  269. func (p *PushPayload) JSONPayload() ([]byte, error) {
  270. return json.MarshalIndent(p, "", " ")
  271. }
  272. // ParsePushHook parses push event hook content.
  273. func ParsePushHook(raw []byte) (*PushPayload, error) {
  274. hook := new(PushPayload)
  275. if err := json.Unmarshal(raw, hook); err != nil {
  276. return nil, err
  277. }
  278. switch {
  279. case hook.Repo == nil:
  280. return nil, ErrInvalidReceiveHook
  281. case len(hook.Ref) == 0:
  282. return nil, ErrInvalidReceiveHook
  283. }
  284. return hook, nil
  285. }
  286. // Branch returns branch name from a payload
  287. func (p *PushPayload) Branch() string {
  288. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  289. }
  290. // .___
  291. // | | ______ ________ __ ____
  292. // | |/ ___// ___/ | \_/ __ \
  293. // | |\___ \ \___ \| | /\ ___/
  294. // |___/____ >____ >____/ \___ >
  295. // \/ \/ \/
  296. // HookIssueAction FIXME
  297. type HookIssueAction string
  298. const (
  299. // HookIssueOpened opened
  300. HookIssueOpened HookIssueAction = "opened"
  301. // HookIssueClosed closed
  302. HookIssueClosed HookIssueAction = "closed"
  303. // HookIssueReOpened reopened
  304. HookIssueReOpened HookIssueAction = "reopened"
  305. // HookIssueEdited edited
  306. HookIssueEdited HookIssueAction = "edited"
  307. // HookIssueAssigned assigned
  308. HookIssueAssigned HookIssueAction = "assigned"
  309. // HookIssueUnassigned unassigned
  310. HookIssueUnassigned HookIssueAction = "unassigned"
  311. // HookIssueLabelUpdated label_updated
  312. HookIssueLabelUpdated HookIssueAction = "label_updated"
  313. // HookIssueLabelCleared label_cleared
  314. HookIssueLabelCleared HookIssueAction = "label_cleared"
  315. // HookIssueSynchronized synchronized
  316. HookIssueSynchronized HookIssueAction = "synchronized"
  317. // HookIssueMilestoned is an issue action for when a milestone is set on an issue.
  318. HookIssueMilestoned HookIssueAction = "milestoned"
  319. // HookIssueDemilestoned is an issue action for when a milestone is cleared on an issue.
  320. HookIssueDemilestoned HookIssueAction = "demilestoned"
  321. )
  322. // IssuePayload represents the payload information that is sent along with an issue event.
  323. type IssuePayload struct {
  324. Secret string `json:"secret"`
  325. Action HookIssueAction `json:"action"`
  326. Index int64 `json:"number"`
  327. Changes *ChangesPayload `json:"changes,omitempty"`
  328. Issue *Issue `json:"issue"`
  329. Repository *Repository `json:"repository"`
  330. Sender *User `json:"sender"`
  331. }
  332. // SetSecret modifies the secret of the IssuePayload.
  333. func (p *IssuePayload) SetSecret(secret string) {
  334. p.Secret = secret
  335. }
  336. // JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces.
  337. func (p *IssuePayload) JSONPayload() ([]byte, error) {
  338. return json.MarshalIndent(p, "", " ")
  339. }
  340. // ChangesFromPayload FIXME
  341. type ChangesFromPayload struct {
  342. From string `json:"from"`
  343. }
  344. // ChangesPayload FIXME
  345. type ChangesPayload struct {
  346. Title *ChangesFromPayload `json:"title,omitempty"`
  347. Body *ChangesFromPayload `json:"body,omitempty"`
  348. }
  349. // __________ .__ .__ __________ __
  350. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  351. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  352. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  353. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  354. // \/ \/ |__| \/ \/
  355. // PullRequestPayload represents a payload information of pull request event.
  356. type PullRequestPayload struct {
  357. Secret string `json:"secret"`
  358. Action HookIssueAction `json:"action"`
  359. Index int64 `json:"number"`
  360. Changes *ChangesPayload `json:"changes,omitempty"`
  361. PullRequest *PullRequest `json:"pull_request"`
  362. Repository *Repository `json:"repository"`
  363. Sender *User `json:"sender"`
  364. }
  365. // SetSecret modifies the secret of the PullRequestPayload.
  366. func (p *PullRequestPayload) SetSecret(secret string) {
  367. p.Secret = secret
  368. }
  369. // JSONPayload FIXME
  370. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  371. return json.MarshalIndent(p, "", " ")
  372. }
  373. //__________ .__ __
  374. //\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
  375. // | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
  376. // | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ |
  377. // |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____|
  378. // \/ \/|__| \/ \/
  379. // HookRepoAction an action that happens to a repo
  380. type HookRepoAction string
  381. const (
  382. // HookRepoCreated created
  383. HookRepoCreated HookRepoAction = "created"
  384. // HookRepoDeleted deleted
  385. HookRepoDeleted HookRepoAction = "deleted"
  386. )
  387. // RepositoryPayload payload for repository webhooks
  388. type RepositoryPayload struct {
  389. Secret string `json:"secret"`
  390. Action HookRepoAction `json:"action"`
  391. Repository *Repository `json:"repository"`
  392. Organization *User `json:"organization"`
  393. Sender *User `json:"sender"`
  394. }
  395. // SetSecret modifies the secret of the RepositoryPayload
  396. func (p *RepositoryPayload) SetSecret(secret string) {
  397. p.Secret = secret
  398. }
  399. // JSONPayload JSON representation of the payload
  400. func (p *RepositoryPayload) JSONPayload() ([]byte, error) {
  401. return json.MarshalIndent(p, "", " ")
  402. }