repo_form.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 := 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. // Advanced settings
  79. EnableWiki bool
  80. EnableExternalWiki bool
  81. ExternalWikiURL string
  82. EnableIssues bool
  83. EnableExternalTracker bool
  84. TrackerURLFormat string
  85. EnablePulls bool
  86. }
  87. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  88. return validate(errs, ctx.Data, f, ctx.Locale)
  89. }
  90. // __ __ ___. .__ .__ __
  91. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  92. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  93. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  94. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  95. // \/ \/ \/ \/ \/ \/
  96. type WebhookForm struct {
  97. Events string
  98. Create bool
  99. Push bool
  100. Active bool
  101. }
  102. func (f WebhookForm) PushOnly() bool {
  103. return f.Events == "push_only"
  104. }
  105. func (f WebhookForm) SendEverything() bool {
  106. return f.Events == "send_everything"
  107. }
  108. func (f WebhookForm) ChooseEvents() bool {
  109. return f.Events == "choose_events"
  110. }
  111. type NewWebhookForm struct {
  112. PayloadURL string `binding:"Required;Url"`
  113. ContentType int `binding:"Required"`
  114. Secret string
  115. WebhookForm
  116. }
  117. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  118. return validate(errs, ctx.Data, f, ctx.Locale)
  119. }
  120. type NewSlackHookForm struct {
  121. PayloadURL string `binding:"Required;Url`
  122. Channel string `binding:"Required"`
  123. Username string
  124. IconURL string
  125. Color string
  126. WebhookForm
  127. }
  128. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  129. return validate(errs, ctx.Data, f, ctx.Locale)
  130. }
  131. // .___
  132. // | | ______ ________ __ ____
  133. // | |/ ___// ___/ | \_/ __ \
  134. // | |\___ \ \___ \| | /\ ___/
  135. // |___/____ >____ >____/ \___ >
  136. // \/ \/ \/
  137. type CreateIssueForm struct {
  138. Title string `binding:"Required;MaxSize(255)"`
  139. LabelIDs string `form:"label_ids"`
  140. MilestoneID int64
  141. AssigneeID int64
  142. Content string
  143. Attachments []string
  144. }
  145. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  146. return validate(errs, ctx.Data, f, ctx.Locale)
  147. }
  148. type CreateCommentForm struct {
  149. Content string
  150. Status string `binding:"OmitEmpty;In(reopen,close)"`
  151. Attachments []string
  152. }
  153. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  154. return validate(errs, ctx.Data, f, ctx.Locale)
  155. }
  156. // _____ .__.__ __
  157. // / \ |__| | ____ _______/ |_ ____ ____ ____
  158. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  159. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  160. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  161. // \/ \/ \/ \/ \/
  162. type CreateMilestoneForm struct {
  163. Title string `binding:"Required;MaxSize(50)"`
  164. Content string
  165. Deadline string
  166. }
  167. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  168. return validate(errs, ctx.Data, f, ctx.Locale)
  169. }
  170. // .____ ___. .__
  171. // | | _____ \_ |__ ____ | |
  172. // | | \__ \ | __ \_/ __ \| |
  173. // | |___ / __ \| \_\ \ ___/| |__
  174. // |_______ (____ /___ /\___ >____/
  175. // \/ \/ \/ \/
  176. type CreateLabelForm struct {
  177. ID int64
  178. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  179. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  180. }
  181. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  182. return validate(errs, ctx.Data, f, ctx.Locale)
  183. }
  184. // __________ .__
  185. // \______ \ ____ | | ____ _____ ______ ____
  186. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  187. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  188. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  189. // \/ \/ \/ \/ \/ \/
  190. type NewReleaseForm struct {
  191. TagName string `binding:"Required"`
  192. Target string `form:"tag_target" binding:"Required"`
  193. Title string `binding:"Required"`
  194. Content string
  195. Draft string
  196. Prerelease bool
  197. }
  198. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  199. return validate(errs, ctx.Data, f, ctx.Locale)
  200. }
  201. type EditReleaseForm struct {
  202. Title string `form:"title" binding:"Required"`
  203. Content string `form:"content" binding:"Required"`
  204. Draft string `form:"draft"`
  205. Prerelease bool `form:"prerelease"`
  206. }
  207. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  208. return validate(errs, ctx.Data, f, ctx.Locale)
  209. }
  210. // __ __.__ __ .__
  211. // / \ / \__| | _|__|
  212. // \ \/\/ / | |/ / |
  213. // \ /| | <| |
  214. // \__/\ / |__|__|_ \__|
  215. // \/ \/
  216. type NewWikiForm struct {
  217. OldTitle string
  218. Title string `binding:"Required"`
  219. Content string `binding:"Required"`
  220. Message string
  221. }
  222. // FIXME: use code generation to generate this method.
  223. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  224. return validate(errs, ctx.Data, f, ctx.Locale)
  225. }