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.

repo_form.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 auth
  6. import (
  7. "net/url"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "github.com/Unknwon/com"
  11. "github.com/go-macaron/binding"
  12. "gopkg.in/macaron.v1"
  13. )
  14. // _______________________________________ _________.______________________ _______________.___.
  15. // \______ \_ _____/\______ \_____ \ / _____/| \__ ___/\_____ \\______ \__ | |
  16. // | _/| __)_ | ___// | \ \_____ \ | | | | / | \| _// | |
  17. // | | \| \ | | / | \/ \| | | | / | \ | \\____ |
  18. // |____|_ /_______ / |____| \_______ /_______ /|___| |____| \_______ /____|_ // ______|
  19. // \/ \/ \/ \/ \/ \/ \/
  20. // CreateRepoForm form for creating repository
  21. type CreateRepoForm struct {
  22. UID int64 `binding:"Required"`
  23. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  24. Private bool
  25. Description string `binding:"MaxSize(255)"`
  26. AutoInit bool
  27. Gitignores string
  28. License string
  29. Readme string
  30. }
  31. // Validate validates the fields
  32. func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  33. return validate(errs, ctx.Data, f, ctx.Locale)
  34. }
  35. // MigrateRepoForm form for migrating repository
  36. type MigrateRepoForm struct {
  37. // required: true
  38. CloneAddr string `json:"clone_addr" binding:"Required"`
  39. AuthUsername string `json:"auth_username"`
  40. AuthPassword string `json:"auth_password"`
  41. // required: true
  42. UID int64 `json:"uid" binding:"Required"`
  43. // required: true
  44. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  45. Mirror bool `json:"mirror"`
  46. Private bool `json:"private"`
  47. Description string `json:"description" binding:"MaxSize(255)"`
  48. }
  49. // Validate validates the fields
  50. func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  51. return validate(errs, ctx.Data, f, ctx.Locale)
  52. }
  53. // ParseRemoteAddr checks if given remote address is valid,
  54. // and returns composed URL with needed username and password.
  55. // It also checks if given user has permission when remote address
  56. // is actually a local path.
  57. func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
  58. remoteAddr := strings.TrimSpace(f.CloneAddr)
  59. // Remote address can be HTTP/HTTPS/Git URL or local path.
  60. if strings.HasPrefix(remoteAddr, "http://") ||
  61. strings.HasPrefix(remoteAddr, "https://") ||
  62. strings.HasPrefix(remoteAddr, "git://") {
  63. u, err := url.Parse(remoteAddr)
  64. if err != nil {
  65. return "", models.ErrInvalidCloneAddr{IsURLError: true}
  66. }
  67. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  68. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  69. }
  70. remoteAddr = u.String()
  71. } else if !user.CanImportLocal() {
  72. return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
  73. } else if !com.IsDir(remoteAddr) {
  74. return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
  75. }
  76. return remoteAddr, nil
  77. }
  78. // RepoSettingForm form for changing repository settings
  79. type RepoSettingForm struct {
  80. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  81. Description string `binding:"MaxSize(255)"`
  82. Website string `binding:"ValidUrl;MaxSize(255)"`
  83. Interval string
  84. MirrorAddress string
  85. Private bool
  86. EnablePrune bool
  87. // Advanced settings
  88. EnableWiki bool
  89. EnableExternalWiki bool
  90. ExternalWikiURL string
  91. EnableIssues bool
  92. EnableExternalTracker bool
  93. ExternalTrackerURL string
  94. TrackerURLFormat string
  95. TrackerIssueStyle string
  96. EnablePulls bool
  97. PullsIgnoreWhitespace bool
  98. PullsAllowMerge bool
  99. PullsAllowRebase bool
  100. PullsAllowSquash bool
  101. EnableTimetracker bool
  102. AllowOnlyContributorsToTrackTime bool
  103. // Admin settings
  104. EnableHealthCheck bool
  105. }
  106. // Validate validates the fields
  107. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  108. return validate(errs, ctx.Data, f, ctx.Locale)
  109. }
  110. // __________ .__
  111. // \______ \____________ ____ ____ | |__
  112. // | | _/\_ __ \__ \ / \_/ ___\| | \
  113. // | | \ | | \// __ \| | \ \___| Y \
  114. // |______ / |__| (____ /___| /\___ >___| /
  115. // \/ \/ \/ \/ \/
  116. // ProtectBranchForm form for changing protected branch settings
  117. type ProtectBranchForm struct {
  118. Protected bool
  119. EnableWhitelist bool
  120. WhitelistUsers string
  121. WhitelistTeams string
  122. EnableMergeWhitelist bool
  123. MergeWhitelistUsers string
  124. MergeWhitelistTeams string
  125. }
  126. // Validate validates the fields
  127. func (f *ProtectBranchForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  128. return validate(errs, ctx.Data, f, ctx.Locale)
  129. }
  130. // __ __ ___. .__ .__ __
  131. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  132. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  133. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  134. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  135. // \/ \/ \/ \/ \/ \/
  136. // WebhookForm form for changing web hook
  137. type WebhookForm struct {
  138. Events string
  139. Create bool
  140. Push bool
  141. PullRequest bool
  142. Repository bool
  143. Active bool
  144. }
  145. // PushOnly if the hook will be triggered when push
  146. func (f WebhookForm) PushOnly() bool {
  147. return f.Events == "push_only"
  148. }
  149. // SendEverything if the hook will be triggered any event
  150. func (f WebhookForm) SendEverything() bool {
  151. return f.Events == "send_everything"
  152. }
  153. // ChooseEvents if the hook will be triggered choose events
  154. func (f WebhookForm) ChooseEvents() bool {
  155. return f.Events == "choose_events"
  156. }
  157. // NewWebhookForm form for creating web hook
  158. type NewWebhookForm struct {
  159. PayloadURL string `binding:"Required;ValidUrl"`
  160. ContentType int `binding:"Required"`
  161. Secret string
  162. WebhookForm
  163. }
  164. // Validate validates the fields
  165. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  166. return validate(errs, ctx.Data, f, ctx.Locale)
  167. }
  168. // NewGogshookForm form for creating gogs hook
  169. type NewGogshookForm struct {
  170. PayloadURL string `binding:"Required;ValidUrl"`
  171. ContentType int `binding:"Required"`
  172. Secret string
  173. WebhookForm
  174. }
  175. // Validate validates the fields
  176. func (f *NewGogshookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  177. return validate(errs, ctx.Data, f, ctx.Locale)
  178. }
  179. // NewSlackHookForm form for creating slack hook
  180. type NewSlackHookForm struct {
  181. PayloadURL string `binding:"Required;ValidUrl"`
  182. Channel string `binding:"Required"`
  183. Username string
  184. IconURL string
  185. Color string
  186. WebhookForm
  187. }
  188. // Validate validates the fields
  189. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  190. return validate(errs, ctx.Data, f, ctx.Locale)
  191. }
  192. // NewDiscordHookForm form for creating discord hook
  193. type NewDiscordHookForm struct {
  194. PayloadURL string `binding:"Required;ValidUrl"`
  195. Username string
  196. IconURL string
  197. WebhookForm
  198. }
  199. // Validate validates the fields
  200. func (f *NewDiscordHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  201. return validate(errs, ctx.Data, f, ctx.Locale)
  202. }
  203. // NewDingtalkHookForm form for creating dingtalk hook
  204. type NewDingtalkHookForm struct {
  205. PayloadURL string `binding:"Required;ValidUrl"`
  206. WebhookForm
  207. }
  208. // Validate validates the fields
  209. func (f *NewDingtalkHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  210. return validate(errs, ctx.Data, f, ctx.Locale)
  211. }
  212. // .___
  213. // | | ______ ________ __ ____
  214. // | |/ ___// ___/ | \_/ __ \
  215. // | |\___ \ \___ \| | /\ ___/
  216. // |___/____ >____ >____/ \___ >
  217. // \/ \/ \/
  218. // CreateIssueForm form for creating issue
  219. type CreateIssueForm struct {
  220. Title string `binding:"Required;MaxSize(255)"`
  221. LabelIDs string `form:"label_ids"`
  222. Ref string `form:"ref"`
  223. MilestoneID int64
  224. AssigneeID int64
  225. Content string
  226. Files []string
  227. }
  228. // Validate validates the fields
  229. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  230. return validate(errs, ctx.Data, f, ctx.Locale)
  231. }
  232. // CreateCommentForm form for creating comment
  233. type CreateCommentForm struct {
  234. Content string
  235. Status string `binding:"OmitEmpty;In(reopen,close)"`
  236. Files []string
  237. }
  238. // Validate validates the fields
  239. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  240. return validate(errs, ctx.Data, f, ctx.Locale)
  241. }
  242. // ReactionForm form for adding and removing reaction
  243. type ReactionForm struct {
  244. Content string `binding:"Required;In(+1,-1,laugh,confused,heart,hooray)"`
  245. }
  246. // Validate validates the fields
  247. func (f *ReactionForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  248. return validate(errs, ctx.Data, f, ctx.Locale)
  249. }
  250. // _____ .__.__ __
  251. // / \ |__| | ____ _______/ |_ ____ ____ ____
  252. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  253. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  254. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  255. // \/ \/ \/ \/ \/
  256. // CreateMilestoneForm form for creating milestone
  257. type CreateMilestoneForm struct {
  258. Title string `binding:"Required;MaxSize(50)"`
  259. Content string
  260. Deadline string
  261. }
  262. // Validate validates the fields
  263. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  264. return validate(errs, ctx.Data, f, ctx.Locale)
  265. }
  266. // .____ ___. .__
  267. // | | _____ \_ |__ ____ | |
  268. // | | \__ \ | __ \_/ __ \| |
  269. // | |___ / __ \| \_\ \ ___/| |__
  270. // |_______ (____ /___ /\___ >____/
  271. // \/ \/ \/ \/
  272. // CreateLabelForm form for creating label
  273. type CreateLabelForm struct {
  274. ID int64
  275. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
  276. Description string `binding:"MaxSize(200)" locale:"repo.issues.label_description"`
  277. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  278. }
  279. // Validate validates the fields
  280. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  281. return validate(errs, ctx.Data, f, ctx.Locale)
  282. }
  283. // InitializeLabelsForm form for initializing labels
  284. type InitializeLabelsForm struct {
  285. TemplateName string `binding:"Required"`
  286. }
  287. // Validate validates the fields
  288. func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  289. return validate(errs, ctx.Data, f, ctx.Locale)
  290. }
  291. // __________ .__ .__ __________ __
  292. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  293. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  294. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  295. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  296. // \/ \/ |__| \/ \/
  297. // MergePullRequestForm form for merging Pull Request
  298. type MergePullRequestForm struct {
  299. Do string `binding:"Required;In(merge,rebase,squash)"`
  300. MergeTitleField string
  301. MergeMessageField string
  302. }
  303. // Validate validates the fields
  304. func (f *MergePullRequestForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  305. return validate(errs, ctx.Data, f, ctx.Locale)
  306. }
  307. // __________ .__
  308. // \______ \ ____ | | ____ _____ ______ ____
  309. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  310. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  311. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  312. // \/ \/ \/ \/ \/ \/
  313. // NewReleaseForm form for creating release
  314. type NewReleaseForm struct {
  315. TagName string `binding:"Required"`
  316. Target string `form:"tag_target" binding:"Required"`
  317. Title string `binding:"Required"`
  318. Content string
  319. Draft string
  320. Prerelease bool
  321. Files []string
  322. }
  323. // Validate validates the fields
  324. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  325. return validate(errs, ctx.Data, f, ctx.Locale)
  326. }
  327. // EditReleaseForm form for changing release
  328. type EditReleaseForm struct {
  329. Title string `form:"title" binding:"Required"`
  330. Content string `form:"content"`
  331. Draft string `form:"draft"`
  332. Prerelease bool `form:"prerelease"`
  333. Files []string
  334. }
  335. // Validate validates the fields
  336. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  337. return validate(errs, ctx.Data, f, ctx.Locale)
  338. }
  339. // __ __.__ __ .__
  340. // / \ / \__| | _|__|
  341. // \ \/\/ / | |/ / |
  342. // \ /| | <| |
  343. // \__/\ / |__|__|_ \__|
  344. // \/ \/
  345. // NewWikiForm form for creating wiki
  346. type NewWikiForm struct {
  347. Title string `binding:"Required"`
  348. Content string `binding:"Required"`
  349. Message string
  350. }
  351. // Validate validates the fields
  352. // FIXME: use code generation to generate this method.
  353. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  354. return validate(errs, ctx.Data, f, ctx.Locale)
  355. }
  356. // ___________ .___.__ __
  357. // \_ _____/ __| _/|__|/ |_
  358. // | __)_ / __ | | \ __\
  359. // | \/ /_/ | | || |
  360. // /_______ /\____ | |__||__|
  361. // \/ \/
  362. // EditRepoFileForm form for changing repository file
  363. type EditRepoFileForm struct {
  364. TreePath string `binding:"Required;MaxSize(500)"`
  365. Content string `binding:"Required"`
  366. CommitSummary string `binding:"MaxSize(100)"`
  367. CommitMessage string
  368. CommitChoice string `binding:"Required;MaxSize(50)"`
  369. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  370. LastCommit string
  371. }
  372. // Validate validates the fields
  373. func (f *EditRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  374. return validate(errs, ctx.Data, f, ctx.Locale)
  375. }
  376. // EditPreviewDiffForm form for changing preview diff
  377. type EditPreviewDiffForm struct {
  378. Content string
  379. }
  380. // Validate validates the fields
  381. func (f *EditPreviewDiffForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  382. return validate(errs, ctx.Data, f, ctx.Locale)
  383. }
  384. // ____ ___ .__ .___
  385. // | | \______ | | _________ __| _/
  386. // | | /\____ \| | / _ \__ \ / __ |
  387. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  388. // |______/ | __/|____/\____(____ /\____ |
  389. // |__| \/ \/
  390. //
  391. // UploadRepoFileForm form for uploading repository file
  392. type UploadRepoFileForm struct {
  393. TreePath string `binding:"MaxSize(500)"`
  394. CommitSummary string `binding:"MaxSize(100)"`
  395. CommitMessage string
  396. CommitChoice string `binding:"Required;MaxSize(50)"`
  397. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  398. Files []string
  399. }
  400. // Validate validates the fields
  401. func (f *UploadRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  402. return validate(errs, ctx.Data, f, ctx.Locale)
  403. }
  404. // RemoveUploadFileForm form for removing uploaded file
  405. type RemoveUploadFileForm struct {
  406. File string `binding:"Required;MaxSize(50)"`
  407. }
  408. // Validate validates the fields
  409. func (f *RemoveUploadFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  410. return validate(errs, ctx.Data, f, ctx.Locale)
  411. }
  412. // ________ .__ __
  413. // \______ \ ____ | | _____/ |_ ____
  414. // | | \_/ __ \| | _/ __ \ __\/ __ \
  415. // | ` \ ___/| |_\ ___/| | \ ___/
  416. // /_______ /\___ >____/\___ >__| \___ >
  417. // \/ \/ \/ \/
  418. // DeleteRepoFileForm form for deleting repository file
  419. type DeleteRepoFileForm struct {
  420. CommitSummary string `binding:"MaxSize(100)"`
  421. CommitMessage string
  422. CommitChoice string `binding:"Required;MaxSize(50)"`
  423. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  424. }
  425. // Validate validates the fields
  426. func (f *DeleteRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  427. return validate(errs, ctx.Data, f, ctx.Locale)
  428. }
  429. // ___________.__ ___________ __
  430. // \__ ___/|__| _____ ____ \__ ___/___________ ____ | | __ ___________
  431. // | | | |/ \_/ __ \ | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \
  432. // | | | | Y Y \ ___/ | | | | \// __ \\ \___| <\ ___/| | \/
  433. // |____| |__|__|_| /\___ > |____| |__| (____ /\___ >__|_ \\___ >__|
  434. // \/ \/ \/ \/ \/ \/
  435. // AddTimeManuallyForm form that adds spent time manually.
  436. type AddTimeManuallyForm struct {
  437. Hours int `binding:"Range(0,1000)"`
  438. Minutes int `binding:"Range(0,1000)"`
  439. }
  440. // Validate validates the fields
  441. func (f *AddTimeManuallyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  442. return validate(errs, ctx.Data, f, ctx.Locale)
  443. }