repo_form.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 ConvertRepoForm struct {
  33. Uid int64 `binding:"Required"`
  34. RepoId int64 `binding:"Required"`
  35. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  36. Private bool
  37. Description string `binding:"MaxSize(255)"`
  38. }
  39. func (f *ConvertRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  40. return validate(errs, ctx.Data, f, ctx.Locale)
  41. }
  42. type MigrateRepoForm struct {
  43. CloneAddr string `json:"clone_addr" binding:"Required"`
  44. AuthUsername string `json:"auth_username"`
  45. AuthPassword string `json:"auth_password"`
  46. Uid int64 `json:"uid" binding:"Required"`
  47. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  48. Mirror bool `json:"mirror"`
  49. Private bool `json:"private"`
  50. Description string `json:"description" binding:"MaxSize(255)"`
  51. }
  52. func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  53. return validate(errs, ctx.Data, f, ctx.Locale)
  54. }
  55. // ParseRemoteAddr checks if given remote address is valid,
  56. // and returns composed URL with needed username and passowrd.
  57. // It also checks if given user has permission when remote address
  58. // is actually a local path.
  59. func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
  60. remoteAddr := f.CloneAddr
  61. // Remote address can be HTTP/HTTPS/Git URL or local path.
  62. if strings.HasPrefix(remoteAddr, "http://") ||
  63. strings.HasPrefix(remoteAddr, "https://") ||
  64. strings.HasPrefix(remoteAddr, "git://") {
  65. u, err := url.Parse(remoteAddr)
  66. if err != nil {
  67. return "", models.ErrInvalidCloneAddr{IsURLError: true}
  68. }
  69. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  70. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  71. }
  72. remoteAddr = u.String()
  73. } else if !user.CanImportLocal() {
  74. return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
  75. } else if !com.IsDir(remoteAddr) {
  76. return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
  77. }
  78. return remoteAddr, nil
  79. }
  80. type RepoSettingForm struct {
  81. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  82. Description string `binding:"MaxSize(255)"`
  83. Website string `binding:"Url;MaxSize(100)"`
  84. Branch string
  85. Interval int
  86. MirrorAddress string
  87. Private bool
  88. // Advanced settings
  89. EnableWiki bool
  90. EnableExternalWiki bool
  91. ExternalWikiURL string
  92. EnableIssues bool
  93. EnableExternalTracker bool
  94. TrackerURLFormat string
  95. EnablePulls bool
  96. }
  97. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  98. return validate(errs, ctx.Data, f, ctx.Locale)
  99. }
  100. // __ __ ___. .__ .__ __
  101. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  102. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  103. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  104. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  105. // \/ \/ \/ \/ \/ \/
  106. type WebhookForm struct {
  107. Events string
  108. Create bool
  109. Push bool
  110. Active bool
  111. }
  112. func (f WebhookForm) PushOnly() bool {
  113. return f.Events == "push_only"
  114. }
  115. func (f WebhookForm) SendEverything() bool {
  116. return f.Events == "send_everything"
  117. }
  118. func (f WebhookForm) ChooseEvents() bool {
  119. return f.Events == "choose_events"
  120. }
  121. type NewWebhookForm struct {
  122. PayloadURL string `binding:"Required;Url"`
  123. ContentType int `binding:"Required"`
  124. Secret string
  125. WebhookForm
  126. }
  127. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  128. return validate(errs, ctx.Data, f, ctx.Locale)
  129. }
  130. type NewSlackHookForm struct {
  131. PayloadURL string `binding:"Required;Url`
  132. Channel string `binding:"Required"`
  133. Username string
  134. IconURL string
  135. Color string
  136. WebhookForm
  137. }
  138. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  139. return validate(errs, ctx.Data, f, ctx.Locale)
  140. }
  141. // .___
  142. // | | ______ ________ __ ____
  143. // | |/ ___// ___/ | \_/ __ \
  144. // | |\___ \ \___ \| | /\ ___/
  145. // |___/____ >____ >____/ \___ >
  146. // \/ \/ \/
  147. type CreateIssueForm struct {
  148. Title string `binding:"Required;MaxSize(255)"`
  149. LabelIDs string `form:"label_ids"`
  150. MilestoneID int64
  151. AssigneeID int64
  152. Content string
  153. Attachments []string
  154. }
  155. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  156. return validate(errs, ctx.Data, f, ctx.Locale)
  157. }
  158. type CreateCommentForm struct {
  159. Content string
  160. Status string `binding:"OmitEmpty;In(reopen,close)"`
  161. Attachments []string
  162. }
  163. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  164. return validate(errs, ctx.Data, f, ctx.Locale)
  165. }
  166. // _____ .__.__ __
  167. // / \ |__| | ____ _______/ |_ ____ ____ ____
  168. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  169. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  170. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  171. // \/ \/ \/ \/ \/
  172. type CreateMilestoneForm struct {
  173. Title string `binding:"Required;MaxSize(50)"`
  174. Content string
  175. Deadline string
  176. }
  177. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  178. return validate(errs, ctx.Data, f, ctx.Locale)
  179. }
  180. // .____ ___. .__
  181. // | | _____ \_ |__ ____ | |
  182. // | | \__ \ | __ \_/ __ \| |
  183. // | |___ / __ \| \_\ \ ___/| |__
  184. // |_______ (____ /___ /\___ >____/
  185. // \/ \/ \/ \/
  186. type CreateLabelForm struct {
  187. ID int64
  188. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  189. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  190. }
  191. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  192. return validate(errs, ctx.Data, f, ctx.Locale)
  193. }
  194. // __________ .__
  195. // \______ \ ____ | | ____ _____ ______ ____
  196. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  197. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  198. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  199. // \/ \/ \/ \/ \/ \/
  200. type NewReleaseForm struct {
  201. TagName string `binding:"Required"`
  202. Target string `form:"tag_target" binding:"Required"`
  203. Title string `binding:"Required"`
  204. Content string
  205. Draft string
  206. Prerelease bool
  207. }
  208. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  209. return validate(errs, ctx.Data, f, ctx.Locale)
  210. }
  211. type EditReleaseForm struct {
  212. Title string `form:"title" binding:"Required"`
  213. Content string `form:"content"`
  214. Draft string `form:"draft"`
  215. Prerelease bool `form:"prerelease"`
  216. }
  217. func (f *EditReleaseForm) 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 NewWikiForm struct {
  227. OldTitle string
  228. Title string `binding:"Required"`
  229. Content string `binding:"Required"`
  230. Message string
  231. }
  232. // FIXME: use code generation to generate this method.
  233. func (f *NewWikiForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  234. return validate(errs, ctx.Data, f, ctx.Locale)
  235. }