本站源代码
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

477 lignes
15KB

  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. Signer *PayloadUser `json:"signer"`
  86. Payload string `json:"payload"`
  87. }
  88. var (
  89. _ Payloader = &CreatePayload{}
  90. _ Payloader = &DeletePayload{}
  91. _ Payloader = &ForkPayload{}
  92. _ Payloader = &PushPayload{}
  93. _ Payloader = &IssuePayload{}
  94. _ Payloader = &IssueCommentPayload{}
  95. _ Payloader = &PullRequestPayload{}
  96. _ Payloader = &RepositoryPayload{}
  97. _ Payloader = &ReleasePayload{}
  98. )
  99. // _________ __
  100. // \_ ___ \_______ ____ _____ _/ |_ ____
  101. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  102. // \ \____| | \/\ ___/ / __ \| | \ ___/
  103. // \______ /|__| \___ >____ /__| \___ >
  104. // \/ \/ \/ \/
  105. // CreatePayload FIXME
  106. type CreatePayload struct {
  107. Secret string `json:"secret"`
  108. Sha string `json:"sha"`
  109. Ref string `json:"ref"`
  110. RefType string `json:"ref_type"`
  111. Repo *Repository `json:"repository"`
  112. Sender *User `json:"sender"`
  113. }
  114. // SetSecret modifies the secret of the CreatePayload
  115. func (p *CreatePayload) SetSecret(secret string) {
  116. p.Secret = secret
  117. }
  118. // JSONPayload return payload information
  119. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  120. return json.MarshalIndent(p, "", " ")
  121. }
  122. // ParseCreateHook parses create event hook content.
  123. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  124. hook := new(CreatePayload)
  125. if err := json.Unmarshal(raw, hook); err != nil {
  126. return nil, err
  127. }
  128. // it is possible the JSON was parsed, however,
  129. // was not from Gogs (maybe was from Bitbucket)
  130. // So we'll check to be sure certain key fields
  131. // were populated
  132. switch {
  133. case hook.Repo == nil:
  134. return nil, ErrInvalidReceiveHook
  135. case len(hook.Ref) == 0:
  136. return nil, ErrInvalidReceiveHook
  137. }
  138. return hook, nil
  139. }
  140. // ________ .__ __
  141. // \______ \ ____ | | _____/ |_ ____
  142. // | | \_/ __ \| | _/ __ \ __\/ __ \
  143. // | ` \ ___/| |_\ ___/| | \ ___/
  144. // /_______ /\___ >____/\___ >__| \___ >
  145. // \/ \/ \/ \/
  146. // PusherType define the type to push
  147. type PusherType string
  148. // describe all the PusherTypes
  149. const (
  150. PusherTypeUser PusherType = "user"
  151. )
  152. // DeletePayload represents delete payload
  153. type DeletePayload struct {
  154. Secret string `json:"secret"`
  155. Ref string `json:"ref"`
  156. RefType string `json:"ref_type"`
  157. PusherType PusherType `json:"pusher_type"`
  158. Repo *Repository `json:"repository"`
  159. Sender *User `json:"sender"`
  160. }
  161. // SetSecret modifies the secret of the DeletePayload
  162. func (p *DeletePayload) SetSecret(secret string) {
  163. p.Secret = secret
  164. }
  165. // JSONPayload implements Payload
  166. func (p *DeletePayload) JSONPayload() ([]byte, error) {
  167. return json.MarshalIndent(p, "", " ")
  168. }
  169. // ___________ __
  170. // \_ _____/__________| | __
  171. // | __)/ _ \_ __ \ |/ /
  172. // | \( <_> ) | \/ <
  173. // \___ / \____/|__| |__|_ \
  174. // \/ \/
  175. // ForkPayload represents fork payload
  176. type ForkPayload struct {
  177. Secret string `json:"secret"`
  178. Forkee *Repository `json:"forkee"`
  179. Repo *Repository `json:"repository"`
  180. Sender *User `json:"sender"`
  181. }
  182. // SetSecret modifies the secret of the ForkPayload
  183. func (p *ForkPayload) SetSecret(secret string) {
  184. p.Secret = secret
  185. }
  186. // JSONPayload implements Payload
  187. func (p *ForkPayload) JSONPayload() ([]byte, error) {
  188. return json.MarshalIndent(p, "", " ")
  189. }
  190. // HookIssueCommentAction defines hook issue comment action
  191. type HookIssueCommentAction string
  192. // all issue comment actions
  193. const (
  194. HookIssueCommentCreated HookIssueCommentAction = "created"
  195. HookIssueCommentEdited HookIssueCommentAction = "edited"
  196. HookIssueCommentDeleted HookIssueCommentAction = "deleted"
  197. )
  198. // IssueCommentPayload represents a payload information of issue comment event.
  199. type IssueCommentPayload struct {
  200. Secret string `json:"secret"`
  201. Action HookIssueCommentAction `json:"action"`
  202. Issue *Issue `json:"issue"`
  203. Comment *Comment `json:"comment"`
  204. Changes *ChangesPayload `json:"changes,omitempty"`
  205. Repository *Repository `json:"repository"`
  206. Sender *User `json:"sender"`
  207. IsPull bool `json:"is_pull"`
  208. }
  209. // SetSecret modifies the secret of the IssueCommentPayload
  210. func (p *IssueCommentPayload) SetSecret(secret string) {
  211. p.Secret = secret
  212. }
  213. // JSONPayload implements Payload
  214. func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
  215. return json.MarshalIndent(p, "", " ")
  216. }
  217. // __________ .__
  218. // \______ \ ____ | | ____ _____ ______ ____
  219. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  220. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  221. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  222. // \/ \/ \/ \/ \/ \/
  223. // HookReleaseAction defines hook release action type
  224. type HookReleaseAction string
  225. // all release actions
  226. const (
  227. HookReleasePublished HookReleaseAction = "published"
  228. HookReleaseUpdated HookReleaseAction = "updated"
  229. HookReleaseDeleted HookReleaseAction = "deleted"
  230. )
  231. // ReleasePayload represents a payload information of release event.
  232. type ReleasePayload struct {
  233. Secret string `json:"secret"`
  234. Action HookReleaseAction `json:"action"`
  235. Release *Release `json:"release"`
  236. Repository *Repository `json:"repository"`
  237. Sender *User `json:"sender"`
  238. }
  239. // SetSecret modifies the secret of the ReleasePayload
  240. func (p *ReleasePayload) SetSecret(secret string) {
  241. p.Secret = secret
  242. }
  243. // JSONPayload implements Payload
  244. func (p *ReleasePayload) JSONPayload() ([]byte, error) {
  245. return json.MarshalIndent(p, "", " ")
  246. }
  247. // __________ .__
  248. // \______ \__ __ _____| |__
  249. // | ___/ | \/ ___/ | \
  250. // | | | | /\___ \| Y \
  251. // |____| |____//____ >___| /
  252. // \/ \/
  253. // PushPayload represents a payload information of push event.
  254. type PushPayload struct {
  255. Secret string `json:"secret"`
  256. Ref string `json:"ref"`
  257. Before string `json:"before"`
  258. After string `json:"after"`
  259. CompareURL string `json:"compare_url"`
  260. Commits []*PayloadCommit `json:"commits"`
  261. HeadCommit *PayloadCommit `json:"head_commit"`
  262. Repo *Repository `json:"repository"`
  263. Pusher *User `json:"pusher"`
  264. Sender *User `json:"sender"`
  265. }
  266. // SetSecret modifies the secret of the PushPayload
  267. func (p *PushPayload) SetSecret(secret string) {
  268. p.Secret = secret
  269. }
  270. // JSONPayload FIXME
  271. func (p *PushPayload) JSONPayload() ([]byte, error) {
  272. return json.MarshalIndent(p, "", " ")
  273. }
  274. // ParsePushHook parses push event hook content.
  275. func ParsePushHook(raw []byte) (*PushPayload, error) {
  276. hook := new(PushPayload)
  277. if err := json.Unmarshal(raw, hook); err != nil {
  278. return nil, err
  279. }
  280. switch {
  281. case hook.Repo == nil:
  282. return nil, ErrInvalidReceiveHook
  283. case len(hook.Ref) == 0:
  284. return nil, ErrInvalidReceiveHook
  285. }
  286. return hook, nil
  287. }
  288. // Branch returns branch name from a payload
  289. func (p *PushPayload) Branch() string {
  290. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  291. }
  292. // .___
  293. // | | ______ ________ __ ____
  294. // | |/ ___// ___/ | \_/ __ \
  295. // | |\___ \ \___ \| | /\ ___/
  296. // |___/____ >____ >____/ \___ >
  297. // \/ \/ \/
  298. // HookIssueAction FIXME
  299. type HookIssueAction string
  300. const (
  301. // HookIssueOpened opened
  302. HookIssueOpened HookIssueAction = "opened"
  303. // HookIssueClosed closed
  304. HookIssueClosed HookIssueAction = "closed"
  305. // HookIssueReOpened reopened
  306. HookIssueReOpened HookIssueAction = "reopened"
  307. // HookIssueEdited edited
  308. HookIssueEdited HookIssueAction = "edited"
  309. // HookIssueAssigned assigned
  310. HookIssueAssigned HookIssueAction = "assigned"
  311. // HookIssueUnassigned unassigned
  312. HookIssueUnassigned HookIssueAction = "unassigned"
  313. // HookIssueLabelUpdated label_updated
  314. HookIssueLabelUpdated HookIssueAction = "label_updated"
  315. // HookIssueLabelCleared label_cleared
  316. HookIssueLabelCleared HookIssueAction = "label_cleared"
  317. // HookIssueSynchronized synchronized
  318. HookIssueSynchronized HookIssueAction = "synchronized"
  319. // HookIssueMilestoned is an issue action for when a milestone is set on an issue.
  320. HookIssueMilestoned HookIssueAction = "milestoned"
  321. // HookIssueDemilestoned is an issue action for when a milestone is cleared on an issue.
  322. HookIssueDemilestoned HookIssueAction = "demilestoned"
  323. )
  324. // IssuePayload represents the payload information that is sent along with an issue event.
  325. type IssuePayload struct {
  326. Secret string `json:"secret"`
  327. Action HookIssueAction `json:"action"`
  328. Index int64 `json:"number"`
  329. Changes *ChangesPayload `json:"changes,omitempty"`
  330. Issue *Issue `json:"issue"`
  331. Repository *Repository `json:"repository"`
  332. Sender *User `json:"sender"`
  333. }
  334. // SetSecret modifies the secret of the IssuePayload.
  335. func (p *IssuePayload) SetSecret(secret string) {
  336. p.Secret = secret
  337. }
  338. // JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces.
  339. func (p *IssuePayload) JSONPayload() ([]byte, error) {
  340. return json.MarshalIndent(p, "", " ")
  341. }
  342. // ChangesFromPayload FIXME
  343. type ChangesFromPayload struct {
  344. From string `json:"from"`
  345. }
  346. // ChangesPayload FIXME
  347. type ChangesPayload struct {
  348. Title *ChangesFromPayload `json:"title,omitempty"`
  349. Body *ChangesFromPayload `json:"body,omitempty"`
  350. }
  351. // __________ .__ .__ __________ __
  352. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  353. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  354. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  355. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  356. // \/ \/ |__| \/ \/
  357. // PullRequestPayload represents a payload information of pull request event.
  358. type PullRequestPayload struct {
  359. Secret string `json:"secret"`
  360. Action HookIssueAction `json:"action"`
  361. Index int64 `json:"number"`
  362. Changes *ChangesPayload `json:"changes,omitempty"`
  363. PullRequest *PullRequest `json:"pull_request"`
  364. Repository *Repository `json:"repository"`
  365. Sender *User `json:"sender"`
  366. Review *ReviewPayload `json:"review"`
  367. }
  368. // SetSecret modifies the secret of the PullRequestPayload.
  369. func (p *PullRequestPayload) SetSecret(secret string) {
  370. p.Secret = secret
  371. }
  372. // JSONPayload FIXME
  373. func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
  374. return json.MarshalIndent(p, "", " ")
  375. }
  376. // ReviewPayload FIXME
  377. type ReviewPayload struct {
  378. Type string `json:"type"`
  379. Content string `json:"content"`
  380. }
  381. //__________ .__ __
  382. //\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
  383. // | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
  384. // | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ |
  385. // |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____|
  386. // \/ \/|__| \/ \/
  387. // HookRepoAction an action that happens to a repo
  388. type HookRepoAction string
  389. const (
  390. // HookRepoCreated created
  391. HookRepoCreated HookRepoAction = "created"
  392. // HookRepoDeleted deleted
  393. HookRepoDeleted HookRepoAction = "deleted"
  394. )
  395. // RepositoryPayload payload for repository webhooks
  396. type RepositoryPayload struct {
  397. Secret string `json:"secret"`
  398. Action HookRepoAction `json:"action"`
  399. Repository *Repository `json:"repository"`
  400. Organization *User `json:"organization"`
  401. Sender *User `json:"sender"`
  402. }
  403. // SetSecret modifies the secret of the RepositoryPayload
  404. func (p *RepositoryPayload) SetSecret(secret string) {
  405. p.Secret = secret
  406. }
  407. // JSONPayload JSON representation of the payload
  408. func (p *RepositoryPayload) JSONPayload() ([]byte, error) {
  409. return json.MarshalIndent(p, "", " ")
  410. }
上海开阖软件有限公司 沪ICP备12045867号-1