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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. }
  104. // Validate validates the fields
  105. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  106. return validate(errs, ctx.Data, f, ctx.Locale)
  107. }
  108. // __________ .__
  109. // \______ \____________ ____ ____ | |__
  110. // | | _/\_ __ \__ \ / \_/ ___\| | \
  111. // | | \ | | \// __ \| | \ \___| Y \
  112. // |______ / |__| (____ /___| /\___ >___| /
  113. // \/ \/ \/ \/ \/
  114. // ProtectBranchForm form for changing protected branch settings
  115. type ProtectBranchForm struct {
  116. Protected bool
  117. EnableWhitelist bool
  118. WhitelistUsers string
  119. WhitelistTeams string
  120. }
  121. // Validate validates the fields
  122. func (f *ProtectBranchForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  123. return validate(errs, ctx.Data, f, ctx.Locale)
  124. }
  125. // __ __ ___. .__ .__ __
  126. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  127. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  128. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  129. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  130. // \/ \/ \/ \/ \/ \/
  131. // WebhookForm form for changing web hook
  132. type WebhookForm struct {
  133. Events string
  134. Create bool
  135. Push bool
  136. PullRequest bool
  137. Repository bool
  138. Active bool
  139. }
  140. // PushOnly if the hook will be triggered when push
  141. func (f WebhookForm) PushOnly() bool {
  142. return f.Events == "push_only"
  143. }
  144. // SendEverything if the hook will be triggered any event
  145. func (f WebhookForm) SendEverything() bool {
  146. return f.Events == "send_everything"
  147. }
  148. // ChooseEvents if the hook will be triggered choose events
  149. func (f WebhookForm) ChooseEvents() bool {
  150. return f.Events == "choose_events"
  151. }
  152. // NewWebhookForm form for creating web hook
  153. type NewWebhookForm struct {
  154. PayloadURL string `binding:"Required;ValidUrl"`
  155. ContentType int `binding:"Required"`
  156. Secret string
  157. WebhookForm
  158. }
  159. // Validate validates the fields
  160. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  161. return validate(errs, ctx.Data, f, ctx.Locale)
  162. }
  163. // NewGogshookForm form for creating gogs hook
  164. type NewGogshookForm struct {
  165. PayloadURL string `binding:"Required;ValidUrl"`
  166. ContentType int `binding:"Required"`
  167. Secret string
  168. WebhookForm
  169. }
  170. // Validate validates the fields
  171. func (f *NewGogshookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  172. return validate(errs, ctx.Data, f, ctx.Locale)
  173. }
  174. // NewSlackHookForm form for creating slack hook
  175. type NewSlackHookForm struct {
  176. PayloadURL string `binding:"Required;ValidUrl"`
  177. Channel string `binding:"Required"`
  178. Username string
  179. IconURL string
  180. Color string
  181. WebhookForm
  182. }
  183. // Validate validates the fields
  184. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  185. return validate(errs, ctx.Data, f, ctx.Locale)
  186. }
  187. // NewDiscordHookForm form for creating discord hook
  188. type NewDiscordHookForm struct {
  189. PayloadURL string `binding:"Required;ValidUrl"`
  190. Username string
  191. IconURL string
  192. WebhookForm
  193. }
  194. // Validate validates the fields
  195. func (f *NewDiscordHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  196. return validate(errs, ctx.Data, f, ctx.Locale)
  197. }
  198. // NewDingtalkHookForm form for creating dingtalk hook
  199. type NewDingtalkHookForm struct {
  200. PayloadURL string `binding:"Required;ValidUrl"`
  201. WebhookForm
  202. }
  203. // Validate validates the fields
  204. func (f *NewDingtalkHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  205. return validate(errs, ctx.Data, f, ctx.Locale)
  206. }
  207. // .___
  208. // | | ______ ________ __ ____
  209. // | |/ ___// ___/ | \_/ __ \
  210. // | |\___ \ \___ \| | /\ ___/
  211. // |___/____ >____ >____/ \___ >
  212. // \/ \/ \/
  213. // CreateIssueForm form for creating issue
  214. type CreateIssueForm struct {
  215. Title string `binding:"Required;MaxSize(255)"`
  216. LabelIDs string `form:"label_ids"`
  217. Ref string `form:"ref"`
  218. MilestoneID int64
  219. AssigneeID int64
  220. Content string
  221. Files []string
  222. }
  223. // Validate validates the fields
  224. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  225. return validate(errs, ctx.Data, f, ctx.Locale)
  226. }
  227. // CreateCommentForm form for creating comment
  228. type CreateCommentForm struct {
  229. Content string
  230. Status string `binding:"OmitEmpty;In(reopen,close)"`
  231. Files []string
  232. }
  233. // Validate validates the fields
  234. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  235. return validate(errs, ctx.Data, f, ctx.Locale)
  236. }
  237. // ReactionForm form for adding and removing reaction
  238. type ReactionForm struct {
  239. Content string `binding:"Required;In(+1,-1,laugh,confused,heart,hooray)"`
  240. }
  241. // Validate validates the fields
  242. func (f *ReactionForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  243. return validate(errs, ctx.Data, f, ctx.Locale)
  244. }
  245. // _____ .__.__ __
  246. // / \ |__| | ____ _______/ |_ ____ ____ ____
  247. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  248. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  249. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  250. // \/ \/ \/ \/ \/
  251. // CreateMilestoneForm form for creating milestone
  252. type CreateMilestoneForm struct {
  253. Title string `binding:"Required;MaxSize(50)"`
  254. Content string
  255. Deadline string
  256. }
  257. // Validate validates the fields
  258. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  259. return validate(errs, ctx.Data, f, ctx.Locale)
  260. }
  261. // .____ ___. .__
  262. // | | _____ \_ |__ ____ | |
  263. // | | \__ \ | __ \_/ __ \| |
  264. // | |___ / __ \| \_\ \ ___/| |__
  265. // |_______ (____ /___ /\___ >____/
  266. // \/ \/ \/ \/
  267. // CreateLabelForm form for creating label
  268. type CreateLabelForm struct {
  269. ID int64
  270. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
  271. Description string `binding:"MaxSize(200)" locale:"repo.issues.label_description"`
  272. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  273. }
  274. // Validate validates the fields
  275. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  276. return validate(errs, ctx.Data, f, ctx.Locale)
  277. }
  278. // InitializeLabelsForm form for initializing labels
  279. type InitializeLabelsForm struct {
  280. TemplateName string `binding:"Required"`
  281. }
  282. // Validate validates the fields
  283. func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  284. return validate(errs, ctx.Data, f, ctx.Locale)
  285. }
  286. // __________ .__ .__ __________ __
  287. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  288. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  289. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  290. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  291. // \/ \/ |__| \/ \/
  292. // MergePullRequestForm form for merging Pull Request
  293. type MergePullRequestForm struct {
  294. Do string `binding:"Required;In(merge,rebase,squash)"`
  295. MergeTitleField string
  296. MergeMessageField string
  297. }
  298. // Validate validates the fields
  299. func (f *MergePullRequestForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  300. return validate(errs, ctx.Data, f, ctx.Locale)
  301. }
  302. // __________ .__
  303. // \______ \ ____ | | ____ _____ ______ ____
  304. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  305. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  306. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  307. // \/ \/ \/ \/ \/ \/
  308. // NewReleaseForm form for creating release
  309. type NewReleaseForm struct {
  310. TagName string `binding:"Required"`
  311. Target string `form:"tag_target" binding:"Required"`
  312. Title string `binding:"Required"`
  313. Content string
  314. Draft string
  315. Prerelease bool
  316. Files []string
  317. }
  318. // Validate validates the fields
  319. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  320. return validate(errs, ctx.Data, f, ctx.Locale)
  321. }
  322. // EditReleaseForm form for changing release
  323. type EditReleaseForm struct {
  324. Title string `form:"title" binding:"Required"`
  325. Content string `form:"content"`
  326. Draft string `form:"draft"`
  327. Prerelease bool `form:"prerelease"`
  328. Files []string
  329. }
  330. // Validate validates the fields
  331. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  332. return validate(errs, ctx.Data, f, ctx.Locale)
  333. }
  334. // __ __.__ __ .__
  335. // / \ / \__| | _|__|
  336. // \ \/\/ / | |/ / |
  337. // \ /| | <| |
  338. // \__/\ / |__|__|_ \__|
  339. // \/ \/
  340. // NewWikiForm form for creating wiki
  341. type NewWikiForm struct {
  342. Title string `binding:"Required"`
  343. Content string `binding:"Required"`
  344. Message string
  345. }
  346. // Validate validates the fields
  347. // FIXME: use code generation to generate this method.
  348. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  349. return validate(errs, ctx.Data, f, ctx.Locale)
  350. }
  351. // ___________ .___.__ __
  352. // \_ _____/ __| _/|__|/ |_
  353. // | __)_ / __ | | \ __\
  354. // | \/ /_/ | | || |
  355. // /_______ /\____ | |__||__|
  356. // \/ \/
  357. // EditRepoFileForm form for changing repository file
  358. type EditRepoFileForm struct {
  359. TreePath string `binding:"Required;MaxSize(500)"`
  360. Content string `binding:"Required"`
  361. CommitSummary string `binding:"MaxSize(100)"`
  362. CommitMessage string
  363. CommitChoice string `binding:"Required;MaxSize(50)"`
  364. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  365. LastCommit string
  366. }
  367. // Validate validates the fields
  368. func (f *EditRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  369. return validate(errs, ctx.Data, f, ctx.Locale)
  370. }
  371. // EditPreviewDiffForm form for changing preview diff
  372. type EditPreviewDiffForm struct {
  373. Content string
  374. }
  375. // Validate validates the fields
  376. func (f *EditPreviewDiffForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  377. return validate(errs, ctx.Data, f, ctx.Locale)
  378. }
  379. // ____ ___ .__ .___
  380. // | | \______ | | _________ __| _/
  381. // | | /\____ \| | / _ \__ \ / __ |
  382. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  383. // |______/ | __/|____/\____(____ /\____ |
  384. // |__| \/ \/
  385. //
  386. // UploadRepoFileForm form for uploading repository file
  387. type UploadRepoFileForm struct {
  388. TreePath string `binding:"MaxSize(500)"`
  389. CommitSummary string `binding:"MaxSize(100)"`
  390. CommitMessage string
  391. CommitChoice string `binding:"Required;MaxSize(50)"`
  392. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  393. Files []string
  394. }
  395. // Validate validates the fields
  396. func (f *UploadRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  397. return validate(errs, ctx.Data, f, ctx.Locale)
  398. }
  399. // RemoveUploadFileForm form for removing uploaded file
  400. type RemoveUploadFileForm struct {
  401. File string `binding:"Required;MaxSize(50)"`
  402. }
  403. // Validate validates the fields
  404. func (f *RemoveUploadFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  405. return validate(errs, ctx.Data, f, ctx.Locale)
  406. }
  407. // ________ .__ __
  408. // \______ \ ____ | | _____/ |_ ____
  409. // | | \_/ __ \| | _/ __ \ __\/ __ \
  410. // | ` \ ___/| |_\ ___/| | \ ___/
  411. // /_______ /\___ >____/\___ >__| \___ >
  412. // \/ \/ \/ \/
  413. // DeleteRepoFileForm form for deleting repository file
  414. type DeleteRepoFileForm struct {
  415. CommitSummary string `binding:"MaxSize(100)"`
  416. CommitMessage string
  417. CommitChoice string `binding:"Required;MaxSize(50)"`
  418. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  419. }
  420. // Validate validates the fields
  421. func (f *DeleteRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  422. return validate(errs, ctx.Data, f, ctx.Locale)
  423. }
  424. // ___________.__ ___________ __
  425. // \__ ___/|__| _____ ____ \__ ___/___________ ____ | | __ ___________
  426. // | | | |/ \_/ __ \ | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \
  427. // | | | | Y Y \ ___/ | | | | \// __ \\ \___| <\ ___/| | \/
  428. // |____| |__|__|_| /\___ > |____| |__| (____ /\___ >__|_ \\___ >__|
  429. // \/ \/ \/ \/ \/ \/
  430. // AddTimeManuallyForm form that adds spent time manually.
  431. type AddTimeManuallyForm struct {
  432. Hours int `binding:"Range(0,1000)"`
  433. Minutes int `binding:"Range(0,1000)"`
  434. }
  435. // Validate validates the fields
  436. func (f *AddTimeManuallyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  437. return validate(errs, ctx.Data, f, ctx.Locale)
  438. }