repo.go 11 KB

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