您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

repo_form.go 15KB

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