repo.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. Unlisted bool
  24. Description string `binding:"MaxSize(512)"`
  25. AutoInit bool
  26. Gitignores string
  27. License string
  28. Readme string
  29. }
  30. func (f *CreateRepo) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  31. return validate(errs, ctx.Data, f, ctx.Locale)
  32. }
  33. type MigrateRepo struct {
  34. CloneAddr string `json:"clone_addr" binding:"Required"`
  35. AuthUsername string `json:"auth_username"`
  36. AuthPassword string `json:"auth_password"`
  37. Uid int64 `json:"uid" binding:"Required"`
  38. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  39. Mirror bool `json:"mirror"`
  40. Private bool `json:"private"`
  41. Unlisted bool `json:"unlisted"`
  42. Description string `json:"description" binding:"MaxSize(512)"`
  43. }
  44. func (f *MigrateRepo) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  45. return validate(errs, ctx.Data, f, ctx.Locale)
  46. }
  47. // ParseRemoteAddr checks if given remote address is valid,
  48. // and returns composed URL with needed username and password.
  49. // It also checks if given user has permission when remote address
  50. // is actually a local path.
  51. func (f MigrateRepo) ParseRemoteAddr(user *db.User) (string, error) {
  52. remoteAddr := strings.TrimSpace(f.CloneAddr)
  53. // Remote address can be HTTP/HTTPS/Git URL or local path.
  54. if strings.HasPrefix(remoteAddr, "http://") ||
  55. strings.HasPrefix(remoteAddr, "https://") ||
  56. strings.HasPrefix(remoteAddr, "git://") {
  57. u, err := url.Parse(remoteAddr)
  58. if err != nil {
  59. return "", db.ErrInvalidCloneAddr{IsURLError: true}
  60. }
  61. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  62. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  63. }
  64. remoteAddr = u.String()
  65. } else if !user.CanImportLocal() {
  66. return "", db.ErrInvalidCloneAddr{IsPermissionDenied: true}
  67. } else if !com.IsDir(remoteAddr) {
  68. return "", db.ErrInvalidCloneAddr{IsInvalidPath: true}
  69. }
  70. return remoteAddr, nil
  71. }
  72. type RepoSetting struct {
  73. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  74. Description string `binding:"MaxSize(512)"`
  75. Website string `binding:"Url;MaxSize(100)"`
  76. Branch string
  77. Interval int
  78. MirrorAddress string
  79. Private bool
  80. Unlisted bool
  81. EnablePrune bool
  82. // Advanced settings
  83. EnableWiki bool
  84. AllowPublicWiki bool
  85. EnableExternalWiki bool
  86. ExternalWikiURL string
  87. EnableIssues bool
  88. AllowPublicIssues bool
  89. EnableExternalTracker bool
  90. ExternalTrackerURL string
  91. TrackerURLFormat string
  92. TrackerIssueStyle string
  93. EnablePulls bool
  94. PullsIgnoreWhitespace bool
  95. PullsAllowRebase bool
  96. }
  97. func (f *RepoSetting) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  98. return validate(errs, ctx.Data, f, ctx.Locale)
  99. }
  100. // __________ .__
  101. // \______ \____________ ____ ____ | |__
  102. // | | _/\_ __ \__ \ / \_/ ___\| | \
  103. // | | \ | | \// __ \| | \ \___| Y \
  104. // |______ / |__| (____ /___| /\___ >___| /
  105. // \/ \/ \/ \/ \/
  106. type ProtectBranch struct {
  107. Protected bool
  108. RequirePullRequest bool
  109. EnableWhitelist bool
  110. WhitelistUsers string
  111. WhitelistTeams string
  112. }
  113. func (f *ProtectBranch) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  114. return validate(errs, ctx.Data, f, ctx.Locale)
  115. }
  116. // __ __ ___. .__ .__ __
  117. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  118. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  119. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  120. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  121. // \/ \/ \/ \/ \/ \/
  122. type Webhook struct {
  123. Events string
  124. Create bool
  125. Delete bool
  126. Fork bool
  127. Push bool
  128. Issues bool
  129. IssueComment bool
  130. PullRequest bool
  131. Release bool
  132. Active bool
  133. }
  134. func (f Webhook) PushOnly() bool {
  135. return f.Events == "push_only"
  136. }
  137. func (f Webhook) SendEverything() bool {
  138. return f.Events == "send_everything"
  139. }
  140. func (f Webhook) ChooseEvents() bool {
  141. return f.Events == "choose_events"
  142. }
  143. type NewWebhook struct {
  144. PayloadURL string `binding:"Required;Url"`
  145. ContentType int `binding:"Required"`
  146. Secret string
  147. Webhook
  148. }
  149. func (f *NewWebhook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  150. return validate(errs, ctx.Data, f, ctx.Locale)
  151. }
  152. type NewSlackHook struct {
  153. PayloadURL string `binding:"Required;Url"`
  154. Channel string `binding:"Required"`
  155. Username string
  156. IconURL string
  157. Color string
  158. Webhook
  159. }
  160. func (f *NewSlackHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  161. return validate(errs, ctx.Data, f, ctx.Locale)
  162. }
  163. type NewDiscordHook struct {
  164. PayloadURL string `binding:"Required;Url"`
  165. Username string
  166. IconURL string
  167. Color string
  168. Webhook
  169. }
  170. func (f *NewDiscordHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  171. return validate(errs, ctx.Data, f, ctx.Locale)
  172. }
  173. type NewDingtalkHook struct {
  174. PayloadURL string `binding:"Required;Url"`
  175. Webhook
  176. }
  177. func (f *NewDingtalkHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  178. return validate(errs, ctx.Data, f, ctx.Locale)
  179. }
  180. // .___
  181. // | | ______ ________ __ ____
  182. // | |/ ___// ___/ | \_/ __ \
  183. // | |\___ \ \___ \| | /\ ___/
  184. // |___/____ >____ >____/ \___ >
  185. // \/ \/ \/
  186. type NewIssue struct {
  187. Title string `binding:"Required;MaxSize(255)"`
  188. LabelIDs string `form:"label_ids"`
  189. MilestoneID int64
  190. AssigneeID int64
  191. Content string
  192. Files []string
  193. }
  194. func (f *NewIssue) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  195. return validate(errs, ctx.Data, f, ctx.Locale)
  196. }
  197. type CreateComment struct {
  198. Content string
  199. Status string `binding:"OmitEmpty;In(reopen,close)"`
  200. Files []string
  201. }
  202. func (f *CreateComment) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  203. return validate(errs, ctx.Data, f, ctx.Locale)
  204. }
  205. // _____ .__.__ __
  206. // / \ |__| | ____ _______/ |_ ____ ____ ____
  207. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  208. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  209. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  210. // \/ \/ \/ \/ \/
  211. type CreateMilestone struct {
  212. Title string `binding:"Required;MaxSize(50)"`
  213. Content string
  214. Deadline string
  215. }
  216. func (f *CreateMilestone) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  217. return validate(errs, ctx.Data, f, ctx.Locale)
  218. }
  219. // .____ ___. .__
  220. // | | _____ \_ |__ ____ | |
  221. // | | \__ \ | __ \_/ __ \| |
  222. // | |___ / __ \| \_\ \ ___/| |__
  223. // |_______ (____ /___ /\___ >____/
  224. // \/ \/ \/ \/
  225. type CreateLabel struct {
  226. ID int64
  227. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
  228. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  229. }
  230. func (f *CreateLabel) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  231. return validate(errs, ctx.Data, f, ctx.Locale)
  232. }
  233. type InitializeLabels struct {
  234. TemplateName string `binding:"Required"`
  235. }
  236. func (f *InitializeLabels) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  237. return validate(errs, ctx.Data, f, ctx.Locale)
  238. }
  239. // __________ .__
  240. // \______ \ ____ | | ____ _____ ______ ____
  241. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  242. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  243. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  244. // \/ \/ \/ \/ \/ \/
  245. type NewRelease struct {
  246. TagName string `binding:"Required"`
  247. Target string `form:"tag_target" binding:"Required"`
  248. Title string `binding:"Required"`
  249. Content string
  250. Draft string
  251. Prerelease bool
  252. Files []string
  253. }
  254. func (f *NewRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  255. return validate(errs, ctx.Data, f, ctx.Locale)
  256. }
  257. type EditRelease struct {
  258. Title string `binding:"Required"`
  259. Content string
  260. Draft string
  261. Prerelease bool
  262. Files []string
  263. }
  264. func (f *EditRelease) 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 NewWiki struct {
  274. OldTitle string
  275. Title string `binding:"Required"`
  276. Content string `binding:"Required"`
  277. Message string
  278. }
  279. // FIXME: use code generation to generate this method.
  280. func (f *NewWiki) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  281. return validate(errs, ctx.Data, f, ctx.Locale)
  282. }
  283. // ___________ .___.__ __
  284. // \_ _____/ __| _/|__|/ |_
  285. // | __)_ / __ | | \ __\
  286. // | \/ /_/ | | || |
  287. // /_______ /\____ | |__||__|
  288. // \/ \/
  289. type EditRepoFile struct {
  290. TreePath string `binding:"Required;MaxSize(500)"`
  291. Content string `binding:"Required"`
  292. CommitSummary string `binding:"MaxSize(100)"`
  293. CommitMessage string
  294. CommitChoice string `binding:"Required;MaxSize(50)"`
  295. NewBranchName string `binding:"AlphaDashDotSlash;MaxSize(100)"`
  296. LastCommit string
  297. }
  298. func (f *EditRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  299. return validate(errs, ctx.Data, f, ctx.Locale)
  300. }
  301. func (f *EditRepoFile) IsNewBrnach() bool {
  302. return f.CommitChoice == "commit-to-new-branch"
  303. }
  304. type EditPreviewDiff struct {
  305. Content string
  306. }
  307. func (f *EditPreviewDiff) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  308. return validate(errs, ctx.Data, f, ctx.Locale)
  309. }
  310. // ____ ___ .__ .___
  311. // | | \______ | | _________ __| _/
  312. // | | /\____ \| | / _ \__ \ / __ |
  313. // | | / | |_> > |_( <_> ) __ \_/ /_/ |
  314. // |______/ | __/|____/\____(____ /\____ |
  315. // |__| \/ \/
  316. //
  317. type UploadRepoFile struct {
  318. TreePath string `binding:"MaxSize(500)"`
  319. CommitSummary string `binding:"MaxSize(100)"`
  320. CommitMessage string
  321. CommitChoice string `binding:"Required;MaxSize(50)"`
  322. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  323. Files []string
  324. }
  325. func (f *UploadRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  326. return validate(errs, ctx.Data, f, ctx.Locale)
  327. }
  328. func (f *UploadRepoFile) IsNewBrnach() bool {
  329. return f.CommitChoice == "commit-to-new-branch"
  330. }
  331. type RemoveUploadFile struct {
  332. File string `binding:"Required;MaxSize(50)"`
  333. }
  334. func (f *RemoveUploadFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  335. return validate(errs, ctx.Data, f, ctx.Locale)
  336. }
  337. // ________ .__ __
  338. // \______ \ ____ | | _____/ |_ ____
  339. // | | \_/ __ \| | _/ __ \ __\/ __ \
  340. // | ` \ ___/| |_\ ___/| | \ ___/
  341. // /_______ /\___ >____/\___ >__| \___ >
  342. // \/ \/ \/ \/
  343. type DeleteRepoFile struct {
  344. CommitSummary string `binding:"MaxSize(100)"`
  345. CommitMessage string
  346. CommitChoice string `binding:"Required;MaxSize(50)"`
  347. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  348. }
  349. func (f *DeleteRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  350. return validate(errs, ctx.Data, f, ctx.Locale)
  351. }
  352. func (f *DeleteRepoFile) IsNewBrnach() bool {
  353. return f.CommitChoice == "commit-to-new-branch"
  354. }