repo.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/go-macaron/binding"
  9. "github.com/unknwon/com"
  10. "gopkg.in/macaron.v1"
  11. "gogs.io/gogs/internal/db"
  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(512)"`
  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(512)"`
  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 *db.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 "", db.ErrInvalidCloneAddr{IsURLError: true}
  58. }
  59. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  60. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  61. }
  62. // To prevent CRLF injection in git protocol, see https://github.com/gogs/gogs/issues/6413
  63. if u.Scheme == "git" && (strings.Contains(remoteAddr, "%0d") || strings.Contains(remoteAddr, "%0a")) {
  64. return "", db.ErrInvalidCloneAddr{IsURLError: true}
  65. }
  66. remoteAddr = u.String()
  67. } else if !user.CanImportLocal() {
  68. return "", db.ErrInvalidCloneAddr{IsPermissionDenied: true}
  69. } else if !com.IsDir(remoteAddr) {
  70. return "", db.ErrInvalidCloneAddr{IsInvalidPath: true}
  71. }
  72. return remoteAddr, nil
  73. }
  74. type RepoSetting struct {
  75. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  76. Description string `binding:"MaxSize(512)"`
  77. Website string `binding:"Url;MaxSize(100)"`
  78. Branch string
  79. Interval int
  80. MirrorAddress string
  81. Private bool
  82. EnablePrune bool
  83. // Advanced settings
  84. EnableWiki bool
  85. AllowPublicWiki bool
  86. EnableExternalWiki bool
  87. ExternalWikiURL string
  88. EnableIssues bool
  89. AllowPublicIssues bool
  90. EnableExternalTracker bool
  91. ExternalTrackerURL string
  92. TrackerURLFormat string
  93. TrackerIssueStyle string
  94. EnablePulls bool
  95. PullsIgnoreWhitespace bool
  96. PullsAllowRebase bool
  97. }
  98. func (f *RepoSetting) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  99. return validate(errs, ctx.Data, f, ctx.Locale)
  100. }
  101. // __________ .__
  102. // \______ \____________ ____ ____ | |__
  103. // | | _/\_ __ \__ \ / \_/ ___\| | \
  104. // | | \ | | \// __ \| | \ \___| Y \
  105. // |______ / |__| (____ /___| /\___ >___| /
  106. // \/ \/ \/ \/ \/
  107. type ProtectBranch struct {
  108. Protected bool
  109. RequirePullRequest bool
  110. EnableWhitelist bool
  111. WhitelistUsers string
  112. WhitelistTeams string
  113. }
  114. func (f *ProtectBranch) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  115. return validate(errs, ctx.Data, f, ctx.Locale)
  116. }
  117. // __ __ ___. .__ .__ __
  118. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  119. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  120. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  121. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  122. // \/ \/ \/ \/ \/ \/
  123. type Webhook struct {
  124. Events string
  125. Create bool
  126. Delete bool
  127. Fork bool
  128. Push bool
  129. Issues bool
  130. IssueComment bool
  131. PullRequest bool
  132. Release bool
  133. Active bool
  134. }
  135. func (f Webhook) PushOnly() bool {
  136. return f.Events == "push_only"
  137. }
  138. func (f Webhook) SendEverything() bool {
  139. return f.Events == "send_everything"
  140. }
  141. func (f Webhook) ChooseEvents() bool {
  142. return f.Events == "choose_events"
  143. }
  144. type NewWebhook struct {
  145. PayloadURL string `binding:"Required;Url"`
  146. ContentType int `binding:"Required"`
  147. Secret string
  148. Webhook
  149. }
  150. func (f *NewWebhook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  151. return validate(errs, ctx.Data, f, ctx.Locale)
  152. }
  153. type NewSlackHook struct {
  154. PayloadURL string `binding:"Required;Url"`
  155. Channel string `binding:"Required"`
  156. Username string
  157. IconURL string
  158. Color string
  159. Webhook
  160. }
  161. func (f *NewSlackHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  162. return validate(errs, ctx.Data, f, ctx.Locale)
  163. }
  164. type NewDiscordHook struct {
  165. PayloadURL string `binding:"Required;Url"`
  166. Username string
  167. IconURL string
  168. Color string
  169. Webhook
  170. }
  171. func (f *NewDiscordHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  172. return validate(errs, ctx.Data, f, ctx.Locale)
  173. }
  174. type NewDingtalkHook struct {
  175. PayloadURL string `binding:"Required;Url"`
  176. Webhook
  177. }
  178. func (f *NewDingtalkHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  179. return validate(errs, ctx.Data, f, ctx.Locale)
  180. }
  181. // .___
  182. // | | ______ ________ __ ____
  183. // | |/ ___// ___/ | \_/ __ \
  184. // | |\___ \ \___ \| | /\ ___/
  185. // |___/____ >____ >____/ \___ >
  186. // \/ \/ \/
  187. type NewIssue struct {
  188. Title string `binding:"Required;MaxSize(255)"`
  189. LabelIDs string `form:"label_ids"`
  190. MilestoneID int64
  191. AssigneeID int64
  192. Content string
  193. Files []string
  194. }
  195. func (f *NewIssue) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  196. return validate(errs, ctx.Data, f, ctx.Locale)
  197. }
  198. type CreateComment struct {
  199. Content string
  200. Status string `binding:"OmitEmpty;In(reopen,close)"`
  201. Files []string
  202. }
  203. func (f *CreateComment) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  204. return validate(errs, ctx.Data, f, ctx.Locale)
  205. }
  206. // _____ .__.__ __
  207. // / \ |__| | ____ _______/ |_ ____ ____ ____
  208. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  209. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  210. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  211. // \/ \/ \/ \/ \/
  212. type CreateMilestone struct {
  213. Title string `binding:"Required;MaxSize(50)"`
  214. Content string
  215. Deadline string
  216. }
  217. func (f *CreateMilestone) 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 CreateLabel struct {
  227. ID int64
  228. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
  229. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  230. }
  231. func (f *CreateLabel) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  232. return validate(errs, ctx.Data, f, ctx.Locale)
  233. }
  234. type InitializeLabels struct {
  235. TemplateName string `binding:"Required"`
  236. }
  237. func (f *InitializeLabels) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  238. return validate(errs, ctx.Data, f, ctx.Locale)
  239. }
  240. // __________ .__
  241. // \______ \ ____ | | ____ _____ ______ ____
  242. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  243. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  244. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  245. // \/ \/ \/ \/ \/ \/
  246. type NewRelease struct {
  247. TagName string `binding:"Required"`
  248. Target string `form:"tag_target" binding:"Required"`
  249. Title string `binding:"Required"`
  250. Content string
  251. Draft string
  252. Prerelease bool
  253. Files []string
  254. }
  255. func (f *NewRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  256. return validate(errs, ctx.Data, f, ctx.Locale)
  257. }
  258. type EditRelease struct {
  259. Title string `binding:"Required"`
  260. Content string
  261. Draft string
  262. Prerelease bool
  263. Files []string
  264. }
  265. func (f *EditRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  266. return validate(errs, ctx.Data, f, ctx.Locale)
  267. }
  268. // __ __.__ __ .__
  269. // / \ / \__| | _|__|
  270. // \ \/\/ / | |/ / |
  271. // \ /| | <| |
  272. // \__/\ / |__|__|_ \__|
  273. // \/ \/
  274. type NewWiki struct {
  275. OldTitle string
  276. Title string `binding:"Required"`
  277. Content string `binding:"Required"`
  278. Message string
  279. }
  280. // FIXME: use code generation to generate this method.
  281. func (f *NewWiki) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  282. return validate(errs, ctx.Data, f, ctx.Locale)
  283. }
  284. // ___________ .___.__ __
  285. // \_ _____/ __| _/|__|/ |_
  286. // | __)_ / __ | | \ __\
  287. // | \/ /_/ | | || |
  288. // /_______ /\____ | |__||__|
  289. // \/ \/
  290. type EditRepoFile struct {
  291. TreePath string `binding:"Required;MaxSize(500)"`
  292. Content string `binding:"Required"`
  293. CommitSummary string `binding:"MaxSize(100)"`
  294. CommitMessage string
  295. CommitChoice string `binding:"Required;MaxSize(50)"`
  296. NewBranchName string `binding:"AlphaDashDotSlash;MaxSize(100)"`
  297. LastCommit string
  298. }
  299. func (f *EditRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  300. return validate(errs, ctx.Data, f, ctx.Locale)
  301. }
  302. func (f *EditRepoFile) IsNewBrnach() bool {
  303. return f.CommitChoice == "commit-to-new-branch"
  304. }
  305. type EditPreviewDiff struct {
  306. Content string
  307. }
  308. func (f *EditPreviewDiff) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  309. return validate(errs, ctx.Data, f, ctx.Locale)
  310. }
  311. // ____ ___ .__ .___
  312. // | | \______ | | _________ __| _/
  313. // | | /\____ \| | / _ \__ \ / __ |
  314. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  315. // |______/ | __/|____/\____(____ /\____ |
  316. // |__| \/ \/
  317. //
  318. type UploadRepoFile struct {
  319. TreePath string `binding:"MaxSize(500)"`
  320. CommitSummary string `binding:"MaxSize(100)"`
  321. CommitMessage string
  322. CommitChoice string `binding:"Required;MaxSize(50)"`
  323. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  324. Files []string
  325. }
  326. func (f *UploadRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  327. return validate(errs, ctx.Data, f, ctx.Locale)
  328. }
  329. func (f *UploadRepoFile) IsNewBrnach() bool {
  330. return f.CommitChoice == "commit-to-new-branch"
  331. }
  332. type RemoveUploadFile struct {
  333. File string `binding:"Required;MaxSize(50)"`
  334. }
  335. func (f *RemoveUploadFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  336. return validate(errs, ctx.Data, f, ctx.Locale)
  337. }
  338. // ________ .__ __
  339. // \______ \ ____ | | _____/ |_ ____
  340. // | | \_/ __ \| | _/ __ \ __\/ __ \
  341. // | ` \ ___/| |_\ ___/| | \ ___/
  342. // /_______ /\___ >____/\___ >__| \___ >
  343. // \/ \/ \/ \/
  344. type DeleteRepoFile struct {
  345. CommitSummary string `binding:"MaxSize(100)"`
  346. CommitMessage string
  347. CommitChoice string `binding:"Required;MaxSize(50)"`
  348. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  349. }
  350. func (f *DeleteRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  351. return validate(errs, ctx.Data, f, ctx.Locale)
  352. }
  353. func (f *DeleteRepoFile) IsNewBrnach() bool {
  354. return f.CommitChoice == "commit-to-new-branch"
  355. }