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

repo_form.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "net/url"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "github.com/Unknwon/com"
  10. "github.com/go-macaron/binding"
  11. macaron "gopkg.in/macaron.v1"
  12. )
  13. // _______________________________________ _________.______________________ _______________.___.
  14. // \______ \_ _____/\______ \_____ \ / _____/| \__ ___/\_____ \\______ \__ | |
  15. // | _/| __)_ | ___// | \ \_____ \ | | | | / | \| _// | |
  16. // | | \| \ | | / | \/ \| | | | / | \ | \\____ |
  17. // |____|_ /_______ / |____| \_______ /_______ /|___| |____| \_______ /____|_ // ______|
  18. // \/ \/ \/ \/ \/ \/ \/
  19. // CreateRepoForm form for creating repository
  20. type CreateRepoForm struct {
  21. UID int64 `binding:"Required"`
  22. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  23. Private bool
  24. Description string `binding:"MaxSize(255)"`
  25. AutoInit bool
  26. Gitignores string
  27. License string
  28. Readme string
  29. }
  30. // Validate valideates the fields
  31. func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  32. return validate(errs, ctx.Data, f, ctx.Locale)
  33. }
  34. // MigrateRepoForm form for migrating repository
  35. type MigrateRepoForm struct {
  36. CloneAddr string `json:"clone_addr" binding:"Required"`
  37. AuthUsername string `json:"auth_username"`
  38. AuthPassword string `json:"auth_password"`
  39. UID int64 `json:"uid" binding:"Required"`
  40. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  41. Mirror bool `json:"mirror"`
  42. Private bool `json:"private"`
  43. Description string `json:"description" binding:"MaxSize(255)"`
  44. }
  45. // Validate valideates the fields
  46. func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  47. return validate(errs, ctx.Data, f, ctx.Locale)
  48. }
  49. // ParseRemoteAddr checks if given remote address is valid,
  50. // and returns composed URL with needed username and password.
  51. // It also checks if given user has permission when remote address
  52. // is actually a local path.
  53. func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
  54. remoteAddr := strings.TrimSpace(f.CloneAddr)
  55. // Remote address can be HTTP/HTTPS/Git URL or local path.
  56. if strings.HasPrefix(remoteAddr, "http://") ||
  57. strings.HasPrefix(remoteAddr, "https://") ||
  58. strings.HasPrefix(remoteAddr, "git://") {
  59. u, err := url.Parse(remoteAddr)
  60. if err != nil {
  61. return "", models.ErrInvalidCloneAddr{IsURLError: true}
  62. }
  63. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  64. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  65. }
  66. remoteAddr = u.String()
  67. } else if !user.CanImportLocal() {
  68. return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
  69. } else if !com.IsDir(remoteAddr) {
  70. return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
  71. }
  72. return remoteAddr, nil
  73. }
  74. // RepoSettingForm form for changing repository settings
  75. type RepoSettingForm struct {
  76. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  77. Description string `binding:"MaxSize(255)"`
  78. Website string `binding:"Url;MaxSize(255)"`
  79. Interval int
  80. MirrorAddress string
  81. Private bool
  82. EnablePrune bool
  83. // Advanced settings
  84. EnableWiki bool
  85. EnableExternalWiki bool
  86. ExternalWikiURL string
  87. EnableIssues bool
  88. EnableExternalTracker bool
  89. ExternalTrackerURL string
  90. TrackerURLFormat string
  91. TrackerIssueStyle string
  92. EnablePulls bool
  93. }
  94. // Validate valideates the fields
  95. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  96. return validate(errs, ctx.Data, f, ctx.Locale)
  97. }
  98. // __ __ ___. .__ .__ __
  99. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  100. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  101. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  102. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  103. // \/ \/ \/ \/ \/ \/
  104. // WebhookForm form for changing web hook
  105. type WebhookForm struct {
  106. Events string
  107. Create bool
  108. Push bool
  109. PullRequest bool
  110. Active bool
  111. }
  112. // PushOnly if the hook will be triggered when push
  113. func (f WebhookForm) PushOnly() bool {
  114. return f.Events == "push_only"
  115. }
  116. // SendEverything if the hook will be triggered any event
  117. func (f WebhookForm) SendEverything() bool {
  118. return f.Events == "send_everything"
  119. }
  120. // ChooseEvents if the hook will be triggered choose events
  121. func (f WebhookForm) ChooseEvents() bool {
  122. return f.Events == "choose_events"
  123. }
  124. // NewWebhookForm form for creating web hook
  125. type NewWebhookForm struct {
  126. PayloadURL string `binding:"Required;Url"`
  127. ContentType int `binding:"Required"`
  128. Secret string
  129. WebhookForm
  130. }
  131. // Validate valideates the fields
  132. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  133. return validate(errs, ctx.Data, f, ctx.Locale)
  134. }
  135. // NewSlackHookForm form for creating slack hook
  136. type NewSlackHookForm struct {
  137. PayloadURL string `binding:"Required;Url"`
  138. Channel string `binding:"Required"`
  139. Username string
  140. IconURL string
  141. Color string
  142. WebhookForm
  143. }
  144. // Validate valideates the fields
  145. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  146. return validate(errs, ctx.Data, f, ctx.Locale)
  147. }
  148. // .___
  149. // | | ______ ________ __ ____
  150. // | |/ ___// ___/ | \_/ __ \
  151. // | |\___ \ \___ \| | /\ ___/
  152. // |___/____ >____ >____/ \___ >
  153. // \/ \/ \/
  154. // CreateIssueForm form for creating issue
  155. type CreateIssueForm struct {
  156. Title string `binding:"Required;MaxSize(255)"`
  157. LabelIDs string `form:"label_ids"`
  158. MilestoneID int64
  159. AssigneeID int64
  160. Content string
  161. Files []string
  162. }
  163. // Validate valideates the fields
  164. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  165. return validate(errs, ctx.Data, f, ctx.Locale)
  166. }
  167. // CreateCommentForm form for creating comment
  168. type CreateCommentForm struct {
  169. Content string
  170. Status string `binding:"OmitEmpty;In(reopen,close)"`
  171. Files []string
  172. }
  173. // Validate valideates the fields
  174. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  175. return validate(errs, ctx.Data, f, ctx.Locale)
  176. }
  177. // _____ .__.__ __
  178. // / \ |__| | ____ _______/ |_ ____ ____ ____
  179. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  180. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  181. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  182. // \/ \/ \/ \/ \/
  183. // CreateMilestoneForm form for creating milestone
  184. type CreateMilestoneForm struct {
  185. Title string `binding:"Required;MaxSize(50)"`
  186. Content string
  187. Deadline string
  188. }
  189. // Validate valideates the fields
  190. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  191. return validate(errs, ctx.Data, f, ctx.Locale)
  192. }
  193. // .____ ___. .__
  194. // | | _____ \_ |__ ____ | |
  195. // | | \__ \ | __ \_/ __ \| |
  196. // | |___ / __ \| \_\ \ ___/| |__
  197. // |_______ (____ /___ /\___ >____/
  198. // \/ \/ \/ \/
  199. // CreateLabelForm form for creating label
  200. type CreateLabelForm struct {
  201. ID int64
  202. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  203. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  204. }
  205. // Validate valideates the fields
  206. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  207. return validate(errs, ctx.Data, f, ctx.Locale)
  208. }
  209. // InitializeLabelsForm form for initializing labels
  210. type InitializeLabelsForm struct {
  211. TemplateName string `binding:"Required"`
  212. }
  213. // Validate valideates the fields
  214. func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  215. return validate(errs, ctx.Data, f, ctx.Locale)
  216. }
  217. // __________ .__
  218. // \______ \ ____ | | ____ _____ ______ ____
  219. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  220. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  221. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  222. // \/ \/ \/ \/ \/ \/
  223. // NewReleaseForm form for creating release
  224. type NewReleaseForm struct {
  225. TagName string `binding:"Required"`
  226. Target string `form:"tag_target" binding:"Required"`
  227. Title string `binding:"Required"`
  228. Content string
  229. Draft string
  230. Prerelease bool
  231. Files []string
  232. }
  233. // Validate valideates the fields
  234. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  235. return validate(errs, ctx.Data, f, ctx.Locale)
  236. }
  237. // EditReleaseForm form for changing release
  238. type EditReleaseForm struct {
  239. Title string `form:"title" binding:"Required"`
  240. Content string `form:"content"`
  241. Draft string `form:"draft"`
  242. Prerelease bool `form:"prerelease"`
  243. Files []string
  244. }
  245. // Validate valideates the fields
  246. func (f *EditReleaseForm) 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. // NewWikiForm form for creating wiki
  256. type NewWikiForm struct {
  257. OldTitle string
  258. Title string `binding:"Required"`
  259. Content string `binding:"Required"`
  260. Message string
  261. }
  262. // Validate valideates the fields
  263. // FIXME: use code generation to generate this method.
  264. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  265. return validate(errs, ctx.Data, f, ctx.Locale)
  266. }
  267. // ___________ .___.__ __
  268. // \_ _____/ __| _/|__|/ |_
  269. // | __)_ / __ | | \ __\
  270. // | \/ /_/ | | || |
  271. // /_______ /\____ | |__||__|
  272. // \/ \/
  273. // EditRepoFileForm form for changing repository file
  274. type EditRepoFileForm struct {
  275. TreePath string `binding:"Required;MaxSize(500)"`
  276. Content string `binding:"Required"`
  277. CommitSummary string `binding:"MaxSize(100)"`
  278. CommitMessage string
  279. CommitChoice string `binding:"Required;MaxSize(50)"`
  280. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  281. LastCommit string
  282. }
  283. // Validate valideates the fields
  284. func (f *EditRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  285. return validate(errs, ctx.Data, f, ctx.Locale)
  286. }
  287. // EditPreviewDiffForm form for changing preview diff
  288. type EditPreviewDiffForm struct {
  289. Content string
  290. }
  291. // Validate valideates the fields
  292. func (f *EditPreviewDiffForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  293. return validate(errs, ctx.Data, f, ctx.Locale)
  294. }
  295. // ____ ___ .__ .___
  296. // | | \______ | | _________ __| _/
  297. // | | /\____ \| | / _ \__ \ / __ |
  298. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  299. // |______/ | __/|____/\____(____ /\____ |
  300. // |__| \/ \/
  301. //
  302. // UploadRepoFileForm form for uploading repository file
  303. type UploadRepoFileForm struct {
  304. TreePath string `binding:"MaxSize(500)"`
  305. CommitSummary string `binding:"MaxSize(100)"`
  306. CommitMessage string
  307. CommitChoice string `binding:"Required;MaxSize(50)"`
  308. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  309. Files []string
  310. }
  311. // Validate valideates the fields
  312. func (f *UploadRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  313. return validate(errs, ctx.Data, f, ctx.Locale)
  314. }
  315. // RemoveUploadFileForm form for removing uploaded file
  316. type RemoveUploadFileForm struct {
  317. File string `binding:"Required;MaxSize(50)"`
  318. }
  319. // Validate valideates the fields
  320. func (f *RemoveUploadFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  321. return validate(errs, ctx.Data, f, ctx.Locale)
  322. }
  323. // ________ .__ __
  324. // \______ \ ____ | | _____/ |_ ____
  325. // | | \_/ __ \| | _/ __ \ __\/ __ \
  326. // | ` \ ___/| |_\ ___/| | \ ___/
  327. // /_______ /\___ >____/\___ >__| \___ >
  328. // \/ \/ \/ \/
  329. // DeleteRepoFileForm form for deleting repository file
  330. type DeleteRepoFileForm struct {
  331. CommitSummary string `binding:"MaxSize(100)"`
  332. CommitMessage string
  333. CommitChoice string `binding:"Required;MaxSize(50)"`
  334. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  335. }
  336. // Validate valideates the fields
  337. func (f *DeleteRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  338. return validate(errs, ctx.Data, f, ctx.Locale)
  339. }