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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_name"`
  271. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  272. }
  273. // Validate validates the fields
  274. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  275. return validate(errs, ctx.Data, f, ctx.Locale)
  276. }
  277. // InitializeLabelsForm form for initializing labels
  278. type InitializeLabelsForm struct {
  279. TemplateName string `binding:"Required"`
  280. }
  281. // Validate validates the fields
  282. func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  283. return validate(errs, ctx.Data, f, ctx.Locale)
  284. }
  285. // __________ .__ .__ __________ __
  286. // \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
  287. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  288. // | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
  289. // |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
  290. // \/ \/ |__| \/ \/
  291. // MergePullRequestForm form for merging Pull Request
  292. type MergePullRequestForm struct {
  293. Do string `binding:"Required;In(merge,rebase,squash)"`
  294. MergeTitleField string
  295. MergeMessageField string
  296. }
  297. // Validate validates the fields
  298. func (f *MergePullRequestForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  299. return validate(errs, ctx.Data, f, ctx.Locale)
  300. }
  301. // __________ .__
  302. // \______ \ ____ | | ____ _____ ______ ____
  303. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  304. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  305. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  306. // \/ \/ \/ \/ \/ \/
  307. // NewReleaseForm form for creating release
  308. type NewReleaseForm struct {
  309. TagName string `binding:"Required"`
  310. Target string `form:"tag_target" binding:"Required"`
  311. Title string `binding:"Required"`
  312. Content string
  313. Draft string
  314. Prerelease bool
  315. Files []string
  316. }
  317. // Validate validates the fields
  318. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  319. return validate(errs, ctx.Data, f, ctx.Locale)
  320. }
  321. // EditReleaseForm form for changing release
  322. type EditReleaseForm struct {
  323. Title string `form:"title" binding:"Required"`
  324. Content string `form:"content"`
  325. Draft string `form:"draft"`
  326. Prerelease bool `form:"prerelease"`
  327. Files []string
  328. }
  329. // Validate validates the fields
  330. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  331. return validate(errs, ctx.Data, f, ctx.Locale)
  332. }
  333. // __ __.__ __ .__
  334. // / \ / \__| | _|__|
  335. // \ \/\/ / | |/ / |
  336. // \ /| | <| |
  337. // \__/\ / |__|__|_ \__|
  338. // \/ \/
  339. // NewWikiForm form for creating wiki
  340. type NewWikiForm struct {
  341. Title string `binding:"Required"`
  342. Content string `binding:"Required"`
  343. Message string
  344. }
  345. // Validate validates the fields
  346. // FIXME: use code generation to generate this method.
  347. func (f *NewWikiForm) 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. // EditRepoFileForm form for changing repository file
  357. type EditRepoFileForm struct {
  358. TreePath string `binding:"Required;MaxSize(500)"`
  359. Content string `binding:"Required"`
  360. CommitSummary string `binding:"MaxSize(100)"`
  361. CommitMessage string
  362. CommitChoice string `binding:"Required;MaxSize(50)"`
  363. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  364. LastCommit string
  365. }
  366. // Validate validates the fields
  367. func (f *EditRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  368. return validate(errs, ctx.Data, f, ctx.Locale)
  369. }
  370. // EditPreviewDiffForm form for changing preview diff
  371. type EditPreviewDiffForm struct {
  372. Content string
  373. }
  374. // Validate validates the fields
  375. func (f *EditPreviewDiffForm) 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. //
  385. // UploadRepoFileForm form for uploading repository file
  386. type UploadRepoFileForm struct {
  387. TreePath string `binding:"MaxSize(500)"`
  388. CommitSummary string `binding:"MaxSize(100)"`
  389. CommitMessage string
  390. CommitChoice string `binding:"Required;MaxSize(50)"`
  391. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  392. Files []string
  393. }
  394. // Validate validates the fields
  395. func (f *UploadRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  396. return validate(errs, ctx.Data, f, ctx.Locale)
  397. }
  398. // RemoveUploadFileForm form for removing uploaded file
  399. type RemoveUploadFileForm struct {
  400. File string `binding:"Required;MaxSize(50)"`
  401. }
  402. // Validate validates the fields
  403. func (f *RemoveUploadFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  404. return validate(errs, ctx.Data, f, ctx.Locale)
  405. }
  406. // ________ .__ __
  407. // \______ \ ____ | | _____/ |_ ____
  408. // | | \_/ __ \| | _/ __ \ __\/ __ \
  409. // | ` \ ___/| |_\ ___/| | \ ___/
  410. // /_______ /\___ >____/\___ >__| \___ >
  411. // \/ \/ \/ \/
  412. // DeleteRepoFileForm form for deleting repository file
  413. type DeleteRepoFileForm struct {
  414. CommitSummary string `binding:"MaxSize(100)"`
  415. CommitMessage string
  416. CommitChoice string `binding:"Required;MaxSize(50)"`
  417. NewBranchName string `binding:"GitRefName;MaxSize(100)"`
  418. }
  419. // Validate validates the fields
  420. func (f *DeleteRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  421. return validate(errs, ctx.Data, f, ctx.Locale)
  422. }
  423. // ___________.__ ___________ __
  424. // \__ ___/|__| _____ ____ \__ ___/___________ ____ | | __ ___________
  425. // | | | |/ \_/ __ \ | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \
  426. // | | | | Y Y \ ___/ | | | | \// __ \\ \___| <\ ___/| | \/
  427. // |____| |__|__|_| /\___ > |____| |__| (____ /\___ >__|_ \\___ >__|
  428. // \/ \/ \/ \/ \/ \/
  429. // AddTimeManuallyForm form that adds spent time manually.
  430. type AddTimeManuallyForm struct {
  431. Hours int `binding:"Range(0,1000)"`
  432. Minutes int `binding:"Range(0,1000)"`
  433. }
  434. // Validate validates the fields
  435. func (f *AddTimeManuallyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  436. return validate(errs, ctx.Data, f, ctx.Locale)
  437. }