repo.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 form
  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 CreateRepo struct {
  20. UserID 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 *CreateRepo) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  30. return validate(errs, ctx.Data, f, ctx.Locale)
  31. }
  32. type MigrateRepo 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 *MigrateRepo) 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 MigrateRepo) 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 RepoSetting 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 *RepoSetting) 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 ProtectBranch struct {
  100. Protected bool
  101. RequirePullRequest bool
  102. EnableWhitelist bool
  103. WhitelistUsers string
  104. WhitelistTeams string
  105. }
  106. func (f *ProtectBranch) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  107. return validate(errs, ctx.Data, f, ctx.Locale)
  108. }
  109. // __ __ ___. .__ .__ __
  110. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  111. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  112. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  113. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  114. // \/ \/ \/ \/ \/ \/
  115. type Webhook struct {
  116. Events string
  117. Create bool
  118. Delete bool
  119. Fork bool
  120. Push bool
  121. Issues bool
  122. IssueComment bool
  123. PullRequest bool
  124. Release bool
  125. Active bool
  126. }
  127. func (f Webhook) PushOnly() bool {
  128. return f.Events == "push_only"
  129. }
  130. func (f Webhook) SendEverything() bool {
  131. return f.Events == "send_everything"
  132. }
  133. func (f Webhook) ChooseEvents() bool {
  134. return f.Events == "choose_events"
  135. }
  136. type NewWebhook struct {
  137. PayloadURL string `binding:"Required;Url"`
  138. ContentType int `binding:"Required"`
  139. Secret string
  140. Webhook
  141. }
  142. func (f *NewWebhook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  143. return validate(errs, ctx.Data, f, ctx.Locale)
  144. }
  145. type NewSlackHook struct {
  146. PayloadURL string `binding:"Required;Url"`
  147. Channel string `binding:"Required"`
  148. Username string
  149. IconURL string
  150. Color string
  151. Webhook
  152. }
  153. func (f *NewSlackHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  154. return validate(errs, ctx.Data, f, ctx.Locale)
  155. }
  156. type NewDiscordHook struct {
  157. PayloadURL string `binding:"Required;Url"`
  158. Username string
  159. IconURL string
  160. Color string
  161. Webhook
  162. }
  163. func (f *NewDiscordHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  164. return validate(errs, ctx.Data, f, ctx.Locale)
  165. }
  166. // .___
  167. // | | ______ ________ __ ____
  168. // | |/ ___// ___/ | \_/ __ \
  169. // | |\___ \ \___ \| | /\ ___/
  170. // |___/____ >____ >____/ \___ >
  171. // \/ \/ \/
  172. type CreateIssue struct {
  173. Title string `binding:"Required;MaxSize(255)"`
  174. LabelIDs string `form:"label_ids"`
  175. MilestoneID int64
  176. AssigneeID int64
  177. Content string
  178. Files []string
  179. }
  180. func (f *CreateIssue) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  181. return validate(errs, ctx.Data, f, ctx.Locale)
  182. }
  183. type CreateComment struct {
  184. Content string
  185. Status string `binding:"OmitEmpty;In(reopen,close)"`
  186. Files []string
  187. }
  188. func (f *CreateComment) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  189. return validate(errs, ctx.Data, f, ctx.Locale)
  190. }
  191. // _____ .__.__ __
  192. // / \ |__| | ____ _______/ |_ ____ ____ ____
  193. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  194. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  195. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  196. // \/ \/ \/ \/ \/
  197. type CreateMilestone struct {
  198. Title string `binding:"Required;MaxSize(50)"`
  199. Content string
  200. Deadline string
  201. }
  202. func (f *CreateMilestone) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  203. return validate(errs, ctx.Data, f, ctx.Locale)
  204. }
  205. // .____ ___. .__
  206. // | | _____ \_ |__ ____ | |
  207. // | | \__ \ | __ \_/ __ \| |
  208. // | |___ / __ \| \_\ \ ___/| |__
  209. // |_______ (____ /___ /\___ >____/
  210. // \/ \/ \/ \/
  211. type CreateLabel struct {
  212. ID int64
  213. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  214. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  215. }
  216. func (f *CreateLabel) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  217. return validate(errs, ctx.Data, f, ctx.Locale)
  218. }
  219. type InitializeLabels struct {
  220. TemplateName string `binding:"Required"`
  221. }
  222. func (f *InitializeLabels) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  223. return validate(errs, ctx.Data, f, ctx.Locale)
  224. }
  225. // __________ .__
  226. // \______ \ ____ | | ____ _____ ______ ____
  227. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  228. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  229. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  230. // \/ \/ \/ \/ \/ \/
  231. type NewRelease struct {
  232. TagName string `binding:"Required"`
  233. Target string `form:"tag_target" binding:"Required"`
  234. Title string `binding:"Required"`
  235. Content string
  236. Draft string
  237. Prerelease bool
  238. }
  239. func (f *NewRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  240. return validate(errs, ctx.Data, f, ctx.Locale)
  241. }
  242. type EditRelease struct {
  243. Title string `binding:"Required"`
  244. Content string
  245. Draft string
  246. Prerelease bool
  247. }
  248. func (f *EditRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  249. return validate(errs, ctx.Data, f, ctx.Locale)
  250. }
  251. // __ __.__ __ .__
  252. // / \ / \__| | _|__|
  253. // \ \/\/ / | |/ / |
  254. // \ /| | <| |
  255. // \__/\ / |__|__|_ \__|
  256. // \/ \/
  257. type NewWiki struct {
  258. OldTitle string
  259. Title string `binding:"Required"`
  260. Content string `binding:"Required"`
  261. Message string
  262. }
  263. // FIXME: use code generation to generate this method.
  264. func (f *NewWiki) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  265. return validate(errs, ctx.Data, f, ctx.Locale)
  266. }
  267. // ___________ .___.__ __
  268. // \_ _____/ __| _/|__|/ |_
  269. // | __)_ / __ | | \ __\
  270. // | \/ /_/ | | || |
  271. // /_______ /\____ | |__||__|
  272. // \/ \/
  273. type EditRepoFile struct {
  274. TreePath string `binding:"Required;MaxSize(500)"`
  275. Content string `binding:"Required"`
  276. CommitSummary string `binding:"MaxSize(100)`
  277. CommitMessage string
  278. CommitChoice string `binding:"Required;MaxSize(50)"`
  279. NewBranchName string `binding:"AlphaDashDotSlash;MaxSize(100)"`
  280. LastCommit string
  281. }
  282. func (f *EditRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  283. return validate(errs, ctx.Data, f, ctx.Locale)
  284. }
  285. func (f *EditRepoFile) IsNewBrnach() bool {
  286. return f.CommitChoice == "commit-to-new-branch"
  287. }
  288. type EditPreviewDiff struct {
  289. Content string
  290. }
  291. func (f *EditPreviewDiff) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  292. return validate(errs, ctx.Data, f, ctx.Locale)
  293. }
  294. // ____ ___ .__ .___
  295. // | | \______ | | _________ __| _/
  296. // | | /\____ \| | / _ \__ \ / __ |
  297. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  298. // |______/ | __/|____/\____(____ /\____ |
  299. // |__| \/ \/
  300. //
  301. type UploadRepoFile struct {
  302. TreePath string `binding:MaxSize(500)"`
  303. CommitSummary string `binding:"MaxSize(100)`
  304. CommitMessage string
  305. CommitChoice string `binding:"Required;MaxSize(50)"`
  306. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  307. Files []string
  308. }
  309. func (f *UploadRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  310. return validate(errs, ctx.Data, f, ctx.Locale)
  311. }
  312. func (f *UploadRepoFile) IsNewBrnach() bool {
  313. return f.CommitChoice == "commit-to-new-branch"
  314. }
  315. type RemoveUploadFile struct {
  316. File string `binding:"Required;MaxSize(50)"`
  317. }
  318. func (f *RemoveUploadFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  319. return validate(errs, ctx.Data, f, ctx.Locale)
  320. }
  321. // ________ .__ __
  322. // \______ \ ____ | | _____/ |_ ____
  323. // | | \_/ __ \| | _/ __ \ __\/ __ \
  324. // | ` \ ___/| |_\ ___/| | \ ___/
  325. // /_______ /\___ >____/\___ >__| \___ >
  326. // \/ \/ \/ \/
  327. type DeleteRepoFile struct {
  328. CommitSummary string `binding:"MaxSize(100)`
  329. CommitMessage string
  330. CommitChoice string `binding:"Required;MaxSize(50)"`
  331. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  332. }
  333. func (f *DeleteRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  334. return validate(errs, ctx.Data, f, ctx.Locale)
  335. }
  336. func (f *DeleteRepoFile) IsNewBrnach() bool {
  337. return f.CommitChoice == "commit-to-new-branch"
  338. }