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

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