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 14KB

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