Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_form.go 15KB

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