You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_form.go 8.3KB

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