Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

hook.go 14KB

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