repo.go 12 KB

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