repo.go 12 KB

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