repo_form.go 8.3 KB

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