repo_form.go 12 KB

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