repo_form.go 12 KB

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