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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. "errors"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/modules/json"
  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. // CreateHookOptionConfig has all config options in it
  32. // required are "content_type" and "url" Required
  33. type CreateHookOptionConfig map[string]string
  34. // CreateHookOption options when create a hook
  35. type CreateHookOption struct {
  36. // required: true
  37. // enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu,wechatwork
  38. Type string `json:"type" binding:"Required"`
  39. // required: true
  40. Config CreateHookOptionConfig `json:"config" binding:"Required"`
  41. Events []string `json:"events"`
  42. BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
  43. // default: false
  44. Active bool `json:"active"`
  45. }
  46. // EditHookOption options when modify one hook
  47. type EditHookOption struct {
  48. Config map[string]string `json:"config"`
  49. Events []string `json:"events"`
  50. BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
  51. Active *bool `json:"active"`
  52. }
  53. // Payloader payload is some part of one hook
  54. type Payloader interface {
  55. JSONPayload() ([]byte, error)
  56. }
  57. // PayloadUser represents the author or committer of a commit
  58. type PayloadUser struct {
  59. // Full name of the commit author
  60. Name string `json:"name"`
  61. // swagger:strfmt email
  62. Email string `json:"email"`
  63. UserName string `json:"username"`
  64. }
  65. // FIXME: consider using same format as API when commits API are added.
  66. // applies to PayloadCommit and PayloadCommitVerification
  67. // PayloadCommit represents a commit
  68. type PayloadCommit struct {
  69. // sha1 hash of the commit
  70. ID string `json:"id"`
  71. Message string `json:"message"`
  72. URL string `json:"url"`
  73. Author *PayloadUser `json:"author"`
  74. Committer *PayloadUser `json:"committer"`
  75. Verification *PayloadCommitVerification `json:"verification"`
  76. // swagger:strfmt date-time
  77. Timestamp time.Time `json:"timestamp"`
  78. Added []string `json:"added"`
  79. Removed []string `json:"removed"`
  80. Modified []string `json:"modified"`
  81. }
  82. // PayloadCommitVerification represents the GPG verification of a commit
  83. type PayloadCommitVerification struct {
  84. Verified bool `json:"verified"`
  85. Reason string `json:"reason"`
  86. Signature string `json:"signature"`
  87. Signer *PayloadUser `json:"signer"`
  88. Payload string `json:"payload"`
  89. }
  90. var (
  91. _ Payloader = &CreatePayload{}
  92. _ Payloader = &DeletePayload{}
  93. _ Payloader = &ForkPayload{}
  94. _ Payloader = &PushPayload{}
  95. _ Payloader = &IssuePayload{}
  96. _ Payloader = &IssueCommentPayload{}
  97. _ Payloader = &PullRequestPayload{}
  98. _ Payloader = &RepositoryPayload{}
  99. _ Payloader = &ReleasePayload{}
  100. )
  101. // _________ __
  102. // \_ ___ \_______ ____ _____ _/ |_ ____
  103. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  104. // \ \____| | \/\ ___/ / __ \| | \ ___/
  105. // \______ /|__| \___ >____ /__| \___ >
  106. // \/ \/ \/ \/
  107. // CreatePayload FIXME
  108. type CreatePayload struct {
  109. Sha string `json:"sha"`
  110. Ref string `json:"ref"`
  111. RefType string `json:"ref_type"`
  112. Repo *Repository `json:"repository"`
  113. Sender *User `json:"sender"`
  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. Ref string `json:"ref"`
  152. RefType string `json:"ref_type"`
  153. PusherType PusherType `json:"pusher_type"`
  154. Repo *Repository `json:"repository"`
  155. Sender *User `json:"sender"`
  156. }
  157. // JSONPayload implements Payload
  158. func (p *DeletePayload) JSONPayload() ([]byte, error) {
  159. return json.MarshalIndent(p, "", " ")
  160. }
  161. // ___________ __
  162. // \_ _____/__________| | __
  163. // | __)/ _ \_ __ \ |/ /
  164. // | \( <_> ) | \/ <
  165. // \___ / \____/|__| |__|_ \
  166. // \/ \/
  167. // ForkPayload represents fork payload
  168. type ForkPayload struct {
  169. Forkee *Repository `json:"forkee"`
  170. Repo *Repository `json:"repository"`
  171. Sender *User `json:"sender"`
  172. }
  173. // JSONPayload implements Payload
  174. func (p *ForkPayload) JSONPayload() ([]byte, error) {
  175. return json.MarshalIndent(p, "", " ")
  176. }
  177. // HookIssueCommentAction defines hook issue comment action
  178. type HookIssueCommentAction string
  179. // all issue comment actions
  180. const (
  181. HookIssueCommentCreated HookIssueCommentAction = "created"
  182. HookIssueCommentEdited HookIssueCommentAction = "edited"
  183. HookIssueCommentDeleted HookIssueCommentAction = "deleted"
  184. )
  185. // IssueCommentPayload represents a payload information of issue comment event.
  186. type IssueCommentPayload struct {
  187. Action HookIssueCommentAction `json:"action"`
  188. Issue *Issue `json:"issue"`
  189. Comment *Comment `json:"comment"`
  190. Changes *ChangesPayload `json:"changes,omitempty"`
  191. Repository *Repository `json:"repository"`
  192. Sender *User `json:"sender"`
  193. IsPull bool `json:"is_pull"`
  194. }
  195. // JSONPayload implements Payload
  196. func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
  197. return json.MarshalIndent(p, "", " ")
  198. }
  199. // __________ .__
  200. // \______ \ ____ | | ____ _____ ______ ____
  201. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  202. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  203. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  204. // \/ \/ \/ \/ \/ \/
  205. // HookReleaseAction defines hook release action type
  206. type HookReleaseAction string
  207. // all release actions
  208. const (
  209. HookReleasePublished HookReleaseAction = "published"
  210. HookReleaseUpdated HookReleaseAction = "updated"
  211. HookReleaseDeleted HookReleaseAction = "deleted"
  212. )
  213. // ReleasePayload represents a payload information of release event.
  214. type ReleasePayload struct {
  215. Action HookReleaseAction `json:"action"`
  216. Release *Release `json:"release"`
  217. Repository *Repository `json:"repository"`
  218. Sender *User `json:"sender"`
  219. }
  220. // JSONPayload implements Payload
  221. func (p *ReleasePayload) JSONPayload() ([]byte, error) {
  222. return json.MarshalIndent(p, "", " ")
  223. }
  224. // __________ .__
  225. // \______ \__ __ _____| |__
  226. // | ___/ | \/ ___/ | \
  227. // | | | | /\___ \| Y \
  228. // |____| |____//____ >___| /
  229. // \/ \/
  230. // PushPayload represents a payload information of push event.
  231. type PushPayload struct {
  232. Ref string `json:"ref"`
  233. Before string `json:"before"`
  234. After string `json:"after"`
  235. CompareURL string `json:"compare_url"`
  236. Commits []*PayloadCommit `json:"commits"`
  237. HeadCommit *PayloadCommit `json:"head_commit"`
  238. Repo *Repository `json:"repository"`
  239. Pusher *User `json:"pusher"`
  240. Sender *User `json:"sender"`
  241. }
  242. // JSONPayload FIXME
  243. func (p *PushPayload) JSONPayload() ([]byte, error) {
  244. return json.MarshalIndent(p, "", " ")
  245. }
  246. // ParsePushHook parses push event hook content.
  247. func ParsePushHook(raw []byte) (*PushPayload, error) {
  248. hook := new(PushPayload)
  249. if err := json.Unmarshal(raw, hook); err != nil {
  250. return nil, err
  251. }
  252. switch {
  253. case hook.Repo == nil:
  254. return nil, ErrInvalidReceiveHook
  255. case len(hook.Ref) == 0:
  256. return nil, ErrInvalidReceiveHook
  257. }
  258. return hook, nil
  259. }
  260. // Branch returns branch name from a payload
  261. func (p *PushPayload) Branch() string {
  262. return strings.ReplaceAll(p.Ref, "refs/heads/", "")
  263. }
  264. // .___
  265. // | | ______ ________ __ ____
  266. // | |/ ___// ___/ | \_/ __ \
  267. // | |\___ \ \___ \| | /\ ___/
  268. // |___/____ >____ >____/ \___ >
  269. // \/ \/ \/
  270. // HookIssueAction FIXME
  271. type HookIssueAction string
  272. const (
  273. // HookIssueOpened opened
  274. HookIssueOpened HookIssueAction = "opened"
  275. // HookIssueClosed closed
  276. HookIssueClosed HookIssueAction = "closed"
  277. // HookIssueReOpened reopened
  278. HookIssueReOpened HookIssueAction = "reopened"
  279. // HookIssueEdited edited
  280. HookIssueEdited HookIssueAction = "edited"
  281. // HookIssueAssigned assigned
  282. HookIssueAssigned HookIssueAction = "assigned"
  283. // HookIssueUnassigned unassigned
  284. HookIssueUnassigned HookIssueAction = "unassigned"
  285. // HookIssueLabelUpdated label_updated
  286. HookIssueLabelUpdated HookIssueAction = "label_updated"
  287. // HookIssueLabelCleared label_cleared
  288. HookIssueLabelCleared HookIssueAction = "label_cleared"
  289. // HookIssueSynchronized synchronized
  290. HookIssueSynchronized HookIssueAction = "synchronized"
  291. // HookIssueMilestoned is an issue action for when a milestone is set on an issue.
  292. HookIssueMilestoned HookIssueAction = "milestoned"
  293. // HookIssueDemilestoned is an issue action for when a milestone is cleared on an issue.
  294. HookIssueDemilestoned HookIssueAction = "demilestoned"
  295. // HookIssueReviewed is an issue action for when a pull request is reviewed
  296. HookIssueReviewed HookIssueAction = "reviewed"
  297. )
  298. // IssuePayload represents the payload information that is sent along with an issue event.
  299. type IssuePayload struct {
  300. Action HookIssueAction `json:"action"`
  301. Index int64 `json:"number"`
  302. Changes *ChangesPayload `json:"changes,omitempty"`
  303. Issue *Issue `json:"issue"`
  304. Repository *Repository `json:"repository"`
  305. Sender *User `json:"sender"`
  306. }
  307. // JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces.
  308. func (p *IssuePayload) JSONPayload() ([]byte, error) {
  309. return json.MarshalIndent(p, "", " ")
  310. }
  311. // ChangesFromPayload FIXME
  312. type ChangesFromPayload struct {
  313. From string `json:"from"`
  314. }
  315. // ChangesPayload represents the payload information of issue change
  316. type ChangesPayload struct {
  317. Title *ChangesFromPayload `json:"title,omitempty"`
  318. Body *ChangesFromPayload `json:"body,omitempty"`
  319. Ref *ChangesFromPayload `json:"ref,omitempty"`
  320. }
  321. // __________ .__ .__ __________ __
  322. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  323. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  324. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  325. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  326. // \/ \/ |__| \/ \/
  327. // PullRequestPayload represents a payload information of pull request event.
  328. type PullRequestPayload struct {
  329. Action HookIssueAction `json:"action"`
  330. Index int64 `json:"number"`
  331. Changes *ChangesPayload `json:"changes,omitempty"`
  332. PullRequest *PullRequest `json:"pull_request"`
  333. Repository *Repository `json:"repository"`
  334. Sender *User `json:"sender"`
  335. Review *ReviewPayload `json:"review"`
  336. }
  337. // JSONPayload FIXME
  338. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  339. return json.MarshalIndent(p, "", " ")
  340. }
  341. // ReviewPayload FIXME
  342. type ReviewPayload struct {
  343. Type string `json:"type"`
  344. Content string `json:"content"`
  345. }
  346. //__________ .__ __
  347. //\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
  348. // | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
  349. // | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ |
  350. // |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____|
  351. // \/ \/|__| \/ \/
  352. // HookRepoAction an action that happens to a repo
  353. type HookRepoAction string
  354. const (
  355. // HookRepoCreated created
  356. HookRepoCreated HookRepoAction = "created"
  357. // HookRepoDeleted deleted
  358. HookRepoDeleted HookRepoAction = "deleted"
  359. )
  360. // RepositoryPayload payload for repository webhooks
  361. type RepositoryPayload struct {
  362. Action HookRepoAction `json:"action"`
  363. Repository *Repository `json:"repository"`
  364. Organization *User `json:"organization"`
  365. Sender *User `json:"sender"`
  366. }
  367. // JSONPayload JSON representation of the payload
  368. func (p *RepositoryPayload) JSONPayload() ([]byte, error) {
  369. return json.MarshalIndent(p, "", " ")
  370. }