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

repo_form.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. CloneAddr string `json:"clone_addr" binding:"Required"`
  38. AuthUsername string `json:"auth_username"`
  39. AuthPassword string `json:"auth_password"`
  40. UID int64 `json:"uid" binding:"Required"`
  41. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  42. Mirror bool `json:"mirror"`
  43. Private bool `json:"private"`
  44. Description string `json:"description" binding:"MaxSize(255)"`
  45. }
  46. // Validate validates the fields
  47. func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  48. return validate(errs, ctx.Data, f, ctx.Locale)
  49. }
  50. // ParseRemoteAddr checks if given remote address is valid,
  51. // and returns composed URL with needed username and password.
  52. // It also checks if given user has permission when remote address
  53. // is actually a local path.
  54. func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
  55. remoteAddr := strings.TrimSpace(f.CloneAddr)
  56. // Remote address can be HTTP/HTTPS/Git URL or local path.
  57. if strings.HasPrefix(remoteAddr, "http://") ||
  58. strings.HasPrefix(remoteAddr, "https://") ||
  59. strings.HasPrefix(remoteAddr, "git://") {
  60. u, err := url.Parse(remoteAddr)
  61. if err != nil {
  62. return "", models.ErrInvalidCloneAddr{IsURLError: true}
  63. }
  64. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  65. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  66. }
  67. remoteAddr = u.String()
  68. } else if !user.CanImportLocal() {
  69. return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
  70. } else if !com.IsDir(remoteAddr) {
  71. return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
  72. }
  73. return remoteAddr, nil
  74. }
  75. // RepoSettingForm form for changing repository settings
  76. type RepoSettingForm struct {
  77. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  78. Description string `binding:"MaxSize(255)"`
  79. Website string `binding:"ValidUrl;MaxSize(255)"`
  80. Interval string
  81. MirrorAddress string
  82. Private bool
  83. EnablePrune bool
  84. // Advanced settings
  85. EnableWiki bool
  86. EnableExternalWiki bool
  87. ExternalWikiURL string
  88. EnableIssues bool
  89. EnableExternalTracker bool
  90. ExternalTrackerURL string
  91. TrackerURLFormat string
  92. TrackerIssueStyle string
  93. EnablePulls bool
  94. EnableTimetracker bool
  95. AllowOnlyContributorsToTrackTime bool
  96. }
  97. // Validate validates the fields
  98. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  99. return validate(errs, ctx.Data, f, ctx.Locale)
  100. }
  101. // __ __ ___. .__ .__ __
  102. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  103. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  104. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  105. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  106. // \/ \/ \/ \/ \/ \/
  107. // WebhookForm form for changing web hook
  108. type WebhookForm struct {
  109. Events string
  110. Create bool
  111. Push bool
  112. PullRequest bool
  113. Repository bool
  114. Active bool
  115. }
  116. // PushOnly if the hook will be triggered when push
  117. func (f WebhookForm) PushOnly() bool {
  118. return f.Events == "push_only"
  119. }
  120. // SendEverything if the hook will be triggered any event
  121. func (f WebhookForm) SendEverything() bool {
  122. return f.Events == "send_everything"
  123. }
  124. // ChooseEvents if the hook will be triggered choose events
  125. func (f WebhookForm) ChooseEvents() bool {
  126. return f.Events == "choose_events"
  127. }
  128. // NewWebhookForm form for creating web hook
  129. type NewWebhookForm struct {
  130. PayloadURL string `binding:"Required;ValidUrl"`
  131. ContentType int `binding:"Required"`
  132. Secret string
  133. WebhookForm
  134. }
  135. // Validate validates the fields
  136. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  137. return validate(errs, ctx.Data, f, ctx.Locale)
  138. }
  139. // NewGogshookForm form for creating gogs hook
  140. type NewGogshookForm struct {
  141. PayloadURL string `binding:"Required;ValidUrl"`
  142. ContentType int `binding:"Required"`
  143. Secret string
  144. WebhookForm
  145. }
  146. // Validate validates the fields
  147. func (f *NewGogshookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  148. return validate(errs, ctx.Data, f, ctx.Locale)
  149. }
  150. // NewSlackHookForm form for creating slack hook
  151. type NewSlackHookForm struct {
  152. PayloadURL string `binding:"Required;ValidUrl"`
  153. Channel string `binding:"Required"`
  154. Username string
  155. IconURL string
  156. Color string
  157. WebhookForm
  158. }
  159. // Validate validates the fields
  160. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  161. return validate(errs, ctx.Data, f, ctx.Locale)
  162. }
  163. // NewDiscordHookForm form for creating discord hook
  164. type NewDiscordHookForm struct {
  165. PayloadURL string `binding:"Required;ValidUrl"`
  166. Username string
  167. IconURL string
  168. WebhookForm
  169. }
  170. // Validate validates the fields
  171. func (f *NewDiscordHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  172. return validate(errs, ctx.Data, f, ctx.Locale)
  173. }
  174. // .___
  175. // | | ______ ________ __ ____
  176. // | |/ ___// ___/ | \_/ __ \
  177. // | |\___ \ \___ \| | /\ ___/
  178. // |___/____ >____ >____/ \___ >
  179. // \/ \/ \/
  180. // CreateIssueForm form for creating issue
  181. type CreateIssueForm struct {
  182. Title string `binding:"Required;MaxSize(255)"`
  183. LabelIDs string `form:"label_ids"`
  184. Ref string `form:"ref"`
  185. MilestoneID int64
  186. AssigneeID int64
  187. Content string
  188. Files []string
  189. }
  190. // Validate validates the fields
  191. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  192. return validate(errs, ctx.Data, f, ctx.Locale)
  193. }
  194. // CreateCommentForm form for creating comment
  195. type CreateCommentForm struct {
  196. Content string
  197. Status string `binding:"OmitEmpty;In(reopen,close)"`
  198. Files []string
  199. }
  200. // Validate validates the fields
  201. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  202. return validate(errs, ctx.Data, f, ctx.Locale)
  203. }
  204. // _____ .__.__ __
  205. // / \ |__| | ____ _______/ |_ ____ ____ ____
  206. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  207. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  208. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  209. // \/ \/ \/ \/ \/
  210. // CreateMilestoneForm form for creating milestone
  211. type CreateMilestoneForm struct {
  212. Title string `binding:"Required;MaxSize(50)"`
  213. Content string
  214. Deadline string
  215. }
  216. // Validate validates the fields
  217. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  218. return validate(errs, ctx.Data, f, ctx.Locale)
  219. }
  220. // .____ ___. .__
  221. // | | _____ \_ |__ ____ | |
  222. // | | \__ \ | __ \_/ __ \| |
  223. // | |___ / __ \| \_\ \ ___/| |__
  224. // |_______ (____ /___ /\___ >____/
  225. // \/ \/ \/ \/
  226. // CreateLabelForm form for creating label
  227. type CreateLabelForm struct {
  228. ID int64
  229. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  230. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  231. }
  232. // Validate validates the fields
  233. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  234. return validate(errs, ctx.Data, f, ctx.Locale)
  235. }
  236. // InitializeLabelsForm form for initializing labels
  237. type InitializeLabelsForm struct {
  238. TemplateName string `binding:"Required"`
  239. }
  240. // Validate validates the fields
  241. func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  242. return validate(errs, ctx.Data, f, ctx.Locale)
  243. }
  244. // __________ .__
  245. // \______ \ ____ | | ____ _____ ______ ____
  246. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  247. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  248. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  249. // \/ \/ \/ \/ \/ \/
  250. // NewReleaseForm form for creating release
  251. type NewReleaseForm struct {
  252. TagName string `binding:"Required"`
  253. Target string `form:"tag_target" binding:"Required"`
  254. Title string `binding:"Required"`
  255. Content string
  256. Draft string
  257. Prerelease bool
  258. Files []string
  259. }
  260. // Validate validates the fields
  261. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  262. return validate(errs, ctx.Data, f, ctx.Locale)
  263. }
  264. // EditReleaseForm form for changing release
  265. type EditReleaseForm struct {
  266. Title string `form:"title" binding:"Required"`
  267. Content string `form:"content"`
  268. Draft string `form:"draft"`
  269. Prerelease bool `form:"prerelease"`
  270. Files []string
  271. }
  272. // Validate validates the fields
  273. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  274. return validate(errs, ctx.Data, f, ctx.Locale)
  275. }
  276. // __ __.__ __ .__
  277. // / \ / \__| | _|__|
  278. // \ \/\/ / | |/ / |
  279. // \ /| | <| |
  280. // \__/\ / |__|__|_ \__|
  281. // \/ \/
  282. // NewWikiForm form for creating wiki
  283. type NewWikiForm struct {
  284. OldTitle string
  285. Title string `binding:"Required"`
  286. Content string `binding:"Required"`
  287. Message string
  288. }
  289. // Validate validates the fields
  290. // FIXME: use code generation to generate this method.
  291. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  292. return validate(errs, ctx.Data, f, ctx.Locale)
  293. }
  294. // ___________ .___.__ __
  295. // \_ _____/ __| _/|__|/ |_
  296. // | __)_ / __ | | \ __\
  297. // | \/ /_/ | | || |
  298. // /_______ /\____ | |__||__|
  299. // \/ \/
  300. // EditRepoFileForm form for changing repository file
  301. type EditRepoFileForm struct {
  302. TreePath string `binding:"Required;MaxSize(500)"`
  303. Content string `binding:"Required"`
  304. CommitSummary string `binding:"MaxSize(100)"`
  305. CommitMessage string
  306. CommitChoice string `binding:"Required;MaxSize(50)"`
  307. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  308. LastCommit string
  309. }
  310. // Validate validates the fields
  311. func (f *EditRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  312. return validate(errs, ctx.Data, f, ctx.Locale)
  313. }
  314. // EditPreviewDiffForm form for changing preview diff
  315. type EditPreviewDiffForm struct {
  316. Content string
  317. }
  318. // Validate validates the fields
  319. func (f *EditPreviewDiffForm) 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. //
  329. // UploadRepoFileForm form for uploading repository file
  330. type UploadRepoFileForm struct {
  331. TreePath string `binding:"MaxSize(500)"`
  332. CommitSummary string `binding:"MaxSize(100)"`
  333. CommitMessage string
  334. CommitChoice string `binding:"Required;MaxSize(50)"`
  335. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  336. Files []string
  337. }
  338. // Validate validates the fields
  339. func (f *UploadRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  340. return validate(errs, ctx.Data, f, ctx.Locale)
  341. }
  342. // RemoveUploadFileForm form for removing uploaded file
  343. type RemoveUploadFileForm struct {
  344. File string `binding:"Required;MaxSize(50)"`
  345. }
  346. // Validate validates the fields
  347. func (f *RemoveUploadFileForm) 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. // DeleteRepoFileForm form for deleting repository file
  357. type DeleteRepoFileForm struct {
  358. CommitSummary string `binding:"MaxSize(100)"`
  359. CommitMessage string
  360. CommitChoice string `binding:"Required;MaxSize(50)"`
  361. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  362. }
  363. // Validate validates the fields
  364. func (f *DeleteRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  365. return validate(errs, ctx.Data, f, ctx.Locale)
  366. }
  367. // ___________.__ ___________ __
  368. // \__ ___/|__| _____ ____ \__ ___/___________ ____ | | __ ___________
  369. // | | | |/ \_/ __ \ | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \
  370. // | | | | Y Y \ ___/ | | | | \// __ \\ \___| <\ ___/| | \/
  371. // |____| |__|__|_| /\___ > |____| |__| (____ /\___ >__|_ \\___ >__|
  372. // \/ \/ \/ \/ \/ \/
  373. // AddTimeManuallyForm form that adds spent time manually.
  374. type AddTimeManuallyForm struct {
  375. Hours int `binding:"Range(0,1000)"`
  376. Minutes int `binding:"Range(0,1000)"`
  377. }
  378. // Validate validates the fields
  379. func (f *AddTimeManuallyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  380. return validate(errs, ctx.Data, f, ctx.Locale)
  381. }