repo_form.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 passowrd.
  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. TrackerURLFormat string
  86. TrackerIssueStyle string
  87. EnablePulls bool
  88. }
  89. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  90. return validate(errs, ctx.Data, f, ctx.Locale)
  91. }
  92. // __ __ ___. .__ .__ __
  93. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  94. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  95. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  96. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  97. // \/ \/ \/ \/ \/ \/
  98. type WebhookForm struct {
  99. Events string
  100. Create bool
  101. Push bool
  102. PullRequest bool
  103. Active bool
  104. }
  105. func (f WebhookForm) PushOnly() bool {
  106. return f.Events == "push_only"
  107. }
  108. func (f WebhookForm) SendEverything() bool {
  109. return f.Events == "send_everything"
  110. }
  111. func (f WebhookForm) ChooseEvents() bool {
  112. return f.Events == "choose_events"
  113. }
  114. type NewWebhookForm struct {
  115. PayloadURL string `binding:"Required;Url"`
  116. ContentType int `binding:"Required"`
  117. Secret string
  118. WebhookForm
  119. }
  120. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  121. return validate(errs, ctx.Data, f, ctx.Locale)
  122. }
  123. type NewSlackHookForm struct {
  124. PayloadURL string `binding:"Required;Url"`
  125. Channel string `binding:"Required"`
  126. Username string
  127. IconURL string
  128. Color string
  129. WebhookForm
  130. }
  131. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  132. return validate(errs, ctx.Data, f, ctx.Locale)
  133. }
  134. // .___
  135. // | | ______ ________ __ ____
  136. // | |/ ___// ___/ | \_/ __ \
  137. // | |\___ \ \___ \| | /\ ___/
  138. // |___/____ >____ >____/ \___ >
  139. // \/ \/ \/
  140. type CreateIssueForm struct {
  141. Title string `binding:"Required;MaxSize(255)"`
  142. LabelIDs string `form:"label_ids"`
  143. MilestoneID int64
  144. AssigneeID int64
  145. Content string
  146. Files []string
  147. }
  148. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  149. return validate(errs, ctx.Data, f, ctx.Locale)
  150. }
  151. type CreateCommentForm struct {
  152. Content string
  153. Status string `binding:"OmitEmpty;In(reopen,close)"`
  154. Files []string
  155. }
  156. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  157. return validate(errs, ctx.Data, f, ctx.Locale)
  158. }
  159. // _____ .__.__ __
  160. // / \ |__| | ____ _______/ |_ ____ ____ ____
  161. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  162. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  163. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  164. // \/ \/ \/ \/ \/
  165. type CreateMilestoneForm struct {
  166. Title string `binding:"Required;MaxSize(50)"`
  167. Content string
  168. Deadline string
  169. }
  170. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  171. return validate(errs, ctx.Data, f, ctx.Locale)
  172. }
  173. // .____ ___. .__
  174. // | | _____ \_ |__ ____ | |
  175. // | | \__ \ | __ \_/ __ \| |
  176. // | |___ / __ \| \_\ \ ___/| |__
  177. // |_______ (____ /___ /\___ >____/
  178. // \/ \/ \/ \/
  179. type CreateLabelForm struct {
  180. ID int64
  181. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  182. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  183. }
  184. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  185. return validate(errs, ctx.Data, f, ctx.Locale)
  186. }
  187. type InitializeLabelsForm struct {
  188. TemplateName string `binding:"Required"`
  189. }
  190. func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  191. return validate(errs, ctx.Data, f, ctx.Locale)
  192. }
  193. // __________ .__
  194. // \______ \ ____ | | ____ _____ ______ ____
  195. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  196. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  197. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  198. // \/ \/ \/ \/ \/ \/
  199. type NewReleaseForm struct {
  200. TagName string `binding:"Required"`
  201. Target string `form:"tag_target" binding:"Required"`
  202. Title string `binding:"Required"`
  203. Content string
  204. Draft string
  205. Prerelease bool
  206. }
  207. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  208. return validate(errs, ctx.Data, f, ctx.Locale)
  209. }
  210. type EditReleaseForm struct {
  211. Title string `form:"title" binding:"Required"`
  212. Content string `form:"content"`
  213. Draft string `form:"draft"`
  214. Prerelease bool `form:"prerelease"`
  215. }
  216. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  217. return validate(errs, ctx.Data, f, ctx.Locale)
  218. }
  219. // __ __.__ __ .__
  220. // / \ / \__| | _|__|
  221. // \ \/\/ / | |/ / |
  222. // \ /| | <| |
  223. // \__/\ / |__|__|_ \__|
  224. // \/ \/
  225. type NewWikiForm struct {
  226. OldTitle string
  227. Title string `binding:"Required"`
  228. Content string `binding:"Required"`
  229. Message string
  230. }
  231. // FIXME: use code generation to generate this method.
  232. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  233. return validate(errs, ctx.Data, f, ctx.Locale)
  234. }
  235. // ___________ .___.__ __
  236. // \_ _____/ __| _/|__|/ |_
  237. // | __)_ / __ | | \ __\
  238. // | \/ /_/ | | || |
  239. // /_______ /\____ | |__||__|
  240. // \/ \/
  241. type EditRepoFileForm struct {
  242. TreePath string `binding:"Required;MaxSize(500)"`
  243. Content string `binding:"Required"`
  244. CommitSummary string `binding:"MaxSize(100)`
  245. CommitMessage string
  246. CommitChoice string `binding:"Required;MaxSize(50)"`
  247. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  248. LastCommit string
  249. }
  250. func (f *EditRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  251. return validate(errs, ctx.Data, f, ctx.Locale)
  252. }
  253. type EditPreviewDiffForm struct {
  254. Content string
  255. }
  256. func (f *EditPreviewDiffForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  257. return validate(errs, ctx.Data, f, ctx.Locale)
  258. }
  259. // ____ ___ .__ .___
  260. // | | \______ | | _________ __| _/
  261. // | | /\____ \| | / _ \__ \ / __ |
  262. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  263. // |______/ | __/|____/\____(____ /\____ |
  264. // |__| \/ \/
  265. //
  266. type UploadRepoFileForm struct {
  267. TreePath string `binding:MaxSize(500)"`
  268. CommitSummary string `binding:"MaxSize(100)`
  269. CommitMessage string
  270. CommitChoice string `binding:"Required;MaxSize(50)"`
  271. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  272. Files []string
  273. }
  274. func (f *UploadRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  275. return validate(errs, ctx.Data, f, ctx.Locale)
  276. }
  277. type RemoveUploadFileForm struct {
  278. File string `binding:"Required;MaxSize(50)"`
  279. }
  280. func (f *RemoveUploadFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  281. return validate(errs, ctx.Data, f, ctx.Locale)
  282. }
  283. // ________ .__ __
  284. // \______ \ ____ | | _____/ |_ ____
  285. // | | \_/ __ \| | _/ __ \ __\/ __ \
  286. // | ` \ ___/| |_\ ___/| | \ ___/
  287. // /_______ /\___ >____/\___ >__| \___ >
  288. // \/ \/ \/ \/
  289. type DeleteRepoFileForm struct {
  290. CommitSummary string `binding:"MaxSize(100)`
  291. CommitMessage string
  292. CommitChoice string `binding:"Required;MaxSize(50)"`
  293. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  294. }
  295. func (f *DeleteRepoFileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  296. return validate(errs, ctx.Data, f, ctx.Locale)
  297. }