repo.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package form
  7. import (
  8. "gitote/gitote/models"
  9. "net/url"
  10. "strings"
  11. "github.com/go-macaron/binding"
  12. "gitlab.com/gitote/com"
  13. "gopkg.in/macaron.v1"
  14. )
  15. // CreateRepo holds CreateRepo
  16. type CreateRepo struct {
  17. UserID int64 `binding:"Required"`
  18. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  19. Private bool
  20. Description string `binding:"MaxSize(512)"`
  21. AutoInit bool
  22. Gitignores string
  23. License string
  24. Readme string
  25. }
  26. // Validate validates fields
  27. func (f *CreateRepo) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  28. return validate(errs, ctx.Data, f, ctx.Locale)
  29. }
  30. // MigrateRepo holds MigrateRepo
  31. type MigrateRepo struct {
  32. CloneAddr string `json:"clone_addr" binding:"Required"`
  33. AuthUsername string `json:"auth_username"`
  34. AuthPassword string `json:"auth_password"`
  35. UID int64 `json:"uid" binding:"Required"`
  36. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  37. Mirror bool `json:"mirror"`
  38. Private bool `json:"private"`
  39. Description string `json:"description" binding:"MaxSize(512)"`
  40. }
  41. // Validate validates fields
  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. // RepoSetting holds RepoSetting
  71. type RepoSetting struct {
  72. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  73. Description string `binding:"MaxSize(512)"`
  74. Website string `binding:"Url;MaxSize(100)"`
  75. Branch string
  76. Interval int
  77. MirrorAddress string
  78. Private bool
  79. EnablePrune bool
  80. IsVerified bool
  81. // Advanced settings
  82. EnableWiki bool
  83. AllowPublicWiki bool
  84. EnableExternalWiki bool
  85. ExternalWikiURL string
  86. EnableIssues bool
  87. AllowPublicIssues bool
  88. EnableExternalTracker bool
  89. ExternalTrackerURL string
  90. TrackerURLFormat string
  91. TrackerIssueStyle string
  92. EnablePulls bool
  93. PullsIgnoreWhitespace bool
  94. PullsAllowRebase bool
  95. }
  96. // Validate validates fields
  97. func (f *RepoSetting) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  98. return validate(errs, ctx.Data, f, ctx.Locale)
  99. }
  100. // ProtectBranch holds ProtectBranch
  101. type ProtectBranch struct {
  102. Protected bool
  103. RequirePullRequest bool
  104. EnableWhitelist bool
  105. WhitelistUsers string
  106. WhitelistTeams string
  107. }
  108. // Validate validates fields
  109. func (f *ProtectBranch) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  110. return validate(errs, ctx.Data, f, ctx.Locale)
  111. }
  112. // Webhook holds Webhook
  113. type Webhook struct {
  114. Events string
  115. Create bool
  116. Delete bool
  117. Fork bool
  118. Push bool
  119. Issues bool
  120. IssueComment bool
  121. PullRequest bool
  122. Release bool
  123. Active bool
  124. }
  125. // PushOnly returns push_only
  126. func (f Webhook) PushOnly() bool {
  127. return f.Events == "push_only"
  128. }
  129. // SendEverything returns send_everything
  130. func (f Webhook) SendEverything() bool {
  131. return f.Events == "send_everything"
  132. }
  133. // ChooseEvents returns choose_events
  134. func (f Webhook) ChooseEvents() bool {
  135. return f.Events == "choose_events"
  136. }
  137. // NewWebhook holds NewWebhook
  138. type NewWebhook struct {
  139. PayloadURL string `binding:"Required;Url"`
  140. ContentType int `binding:"Required"`
  141. Secret string
  142. Webhook
  143. }
  144. // Validate validates fields
  145. func (f *NewWebhook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  146. return validate(errs, ctx.Data, f, ctx.Locale)
  147. }
  148. // NewSlackHook holds NewSlackHook
  149. type NewSlackHook struct {
  150. PayloadURL string `binding:"Required;Url"`
  151. Channel string `binding:"Required"`
  152. Username string
  153. IconURL string
  154. Color string
  155. Webhook
  156. }
  157. // Validate validates fields
  158. func (f *NewSlackHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  159. return validate(errs, ctx.Data, f, ctx.Locale)
  160. }
  161. // NewDiscordHook holds NewDiscordHook
  162. type NewDiscordHook struct {
  163. PayloadURL string `binding:"Required;Url"`
  164. Username string
  165. IconURL string
  166. Color string
  167. Webhook
  168. }
  169. // Validate validates fields
  170. func (f *NewDiscordHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  171. return validate(errs, ctx.Data, f, ctx.Locale)
  172. }
  173. // NewIssue holds NewIssue
  174. type NewIssue struct {
  175. Title string `binding:"Required;MaxSize(255)"`
  176. LabelIDs string `form:"label_ids"`
  177. MilestoneID int64
  178. AssigneeID int64
  179. Content string
  180. Files []string
  181. }
  182. // Validate validates fields
  183. func (f *NewIssue) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  184. return validate(errs, ctx.Data, f, ctx.Locale)
  185. }
  186. // CreateComment holds CreateComment
  187. type CreateComment struct {
  188. Content string
  189. Status string `binding:"OmitEmpty;In(reopen,close)"`
  190. Files []string
  191. }
  192. // Validate validates fields
  193. func (f *CreateComment) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  194. return validate(errs, ctx.Data, f, ctx.Locale)
  195. }
  196. // CreateMilestone holds CreateMilestone
  197. type CreateMilestone struct {
  198. Title string `binding:"Required;MaxSize(50)"`
  199. Content string
  200. Deadline string
  201. }
  202. // Validate validates fields
  203. func (f *CreateMilestone) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  204. return validate(errs, ctx.Data, f, ctx.Locale)
  205. }
  206. // CreateLabel holds CreateLabel
  207. type CreateLabel struct {
  208. ID int64
  209. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
  210. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  211. }
  212. // Validate validates fields
  213. func (f *CreateLabel) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  214. return validate(errs, ctx.Data, f, ctx.Locale)
  215. }
  216. // InitializeLabels holds InitializeLabels
  217. type InitializeLabels struct {
  218. TemplateName string `binding:"Required"`
  219. }
  220. // Validate validates fields
  221. func (f *InitializeLabels) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  222. return validate(errs, ctx.Data, f, ctx.Locale)
  223. }
  224. // NewRelease holds NewRelease
  225. type NewRelease struct {
  226. TagName string `binding:"Required"`
  227. Target string `form:"tag_target" binding:"Required"`
  228. Title string `binding:"Required"`
  229. Content string
  230. Draft string
  231. Prerelease bool
  232. Files []string
  233. }
  234. // Validate validates fields
  235. func (f *NewRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  236. return validate(errs, ctx.Data, f, ctx.Locale)
  237. }
  238. // EditRelease holds EditRelease
  239. type EditRelease struct {
  240. Title string `binding:"Required"`
  241. Content string
  242. Draft string
  243. Prerelease bool
  244. Files []string
  245. }
  246. // Validate validates fields
  247. func (f *EditRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  248. return validate(errs, ctx.Data, f, ctx.Locale)
  249. }
  250. // NewWiki holds NewWiki
  251. type NewWiki struct {
  252. OldTitle string
  253. Title string `binding:"Required"`
  254. Content string `binding:"Required"`
  255. Message string
  256. }
  257. // Validate validates fields
  258. // FIXME: use code generation to generate this method.
  259. func (f *NewWiki) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  260. return validate(errs, ctx.Data, f, ctx.Locale)
  261. }
  262. // EditRepoFile holds EditRepoFile
  263. type EditRepoFile struct {
  264. TreePath string `binding:"Required;MaxSize(500)"`
  265. Content string `binding:"Required"`
  266. CommitSummary string `binding:"MaxSize(100)"`
  267. CommitMessage string
  268. CommitChoice string `binding:"Required;MaxSize(50)"`
  269. NewBranchName string `binding:"AlphaDashDotSlash;MaxSize(100)"`
  270. LastCommit string
  271. }
  272. // Validate validates fields
  273. func (f *EditRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  274. return validate(errs, ctx.Data, f, ctx.Locale)
  275. }
  276. // IsNewBranch returns commit-to-new-branch
  277. func (f *EditRepoFile) IsNewBranch() bool {
  278. return f.CommitChoice == "commit-to-new-branch"
  279. }
  280. // EditPreviewDiff holds EditPreviewDiff
  281. type EditPreviewDiff struct {
  282. Content string
  283. }
  284. // Validate validates fields
  285. func (f *EditPreviewDiff) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  286. return validate(errs, ctx.Data, f, ctx.Locale)
  287. }
  288. // UploadRepoFile holds UploadRepoFile
  289. type UploadRepoFile struct {
  290. TreePath string `binding:"MaxSize(500)"`
  291. CommitSummary string `binding:"MaxSize(100)"`
  292. CommitMessage string
  293. CommitChoice string `binding:"Required;MaxSize(50)"`
  294. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  295. Files []string
  296. }
  297. // Validate validates fields
  298. func (f *UploadRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  299. return validate(errs, ctx.Data, f, ctx.Locale)
  300. }
  301. // IsNewBranch returns commit-to-new-branch
  302. func (f *UploadRepoFile) IsNewBranch() bool {
  303. return f.CommitChoice == "commit-to-new-branch"
  304. }
  305. // RemoveUploadFile holds RemoveUploadFile
  306. type RemoveUploadFile struct {
  307. File string `binding:"Required;MaxSize(50)"`
  308. }
  309. // Validate validates fields
  310. func (f *RemoveUploadFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  311. return validate(errs, ctx.Data, f, ctx.Locale)
  312. }
  313. // DeleteRepoFile holds DeleteRepoFile
  314. type DeleteRepoFile struct {
  315. CommitSummary string `binding:"MaxSize(100)"`
  316. CommitMessage string
  317. CommitChoice string `binding:"Required;MaxSize(50)"`
  318. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  319. }
  320. // Validate validates fields
  321. func (f *DeleteRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  322. return validate(errs, ctx.Data, f, ctx.Locale)
  323. }
  324. // IsNewBranch returns commit-to-new-branch
  325. func (f *DeleteRepoFile) IsNewBranch() bool {
  326. return f.CommitChoice == "commit-to-new-branch"
  327. }