repo.go 12 KB

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