repo.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. type CreateRepo struct {
  16. UserID int64 `binding:"Required"`
  17. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  18. Private bool
  19. Description string `binding:"MaxSize(512)"`
  20. AutoInit bool
  21. Gitignores string
  22. License string
  23. Readme string
  24. }
  25. func (f *CreateRepo) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  26. return validate(errs, ctx.Data, f, ctx.Locale)
  27. }
  28. type MigrateRepo struct {
  29. CloneAddr string `json:"clone_addr" binding:"Required"`
  30. AuthUsername string `json:"auth_username"`
  31. AuthPassword string `json:"auth_password"`
  32. Uid int64 `json:"uid" binding:"Required"`
  33. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  34. Mirror bool `json:"mirror"`
  35. Private bool `json:"private"`
  36. Description string `json:"description" binding:"MaxSize(512)"`
  37. }
  38. func (f *MigrateRepo) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  39. return validate(errs, ctx.Data, f, ctx.Locale)
  40. }
  41. // ParseRemoteAddr checks if given remote address is valid,
  42. // and returns composed URL with needed username and password.
  43. // It also checks if given user has permission when remote address
  44. // is actually a local path.
  45. func (f MigrateRepo) ParseRemoteAddr(user *models.User) (string, error) {
  46. remoteAddr := strings.TrimSpace(f.CloneAddr)
  47. // Remote address can be HTTP/HTTPS/Git URL or local path.
  48. if strings.HasPrefix(remoteAddr, "http://") ||
  49. strings.HasPrefix(remoteAddr, "https://") ||
  50. strings.HasPrefix(remoteAddr, "git://") {
  51. u, err := url.Parse(remoteAddr)
  52. if err != nil {
  53. return "", models.ErrInvalidCloneAddr{IsURLError: true}
  54. }
  55. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  56. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  57. }
  58. remoteAddr = u.String()
  59. } else if !user.CanImportLocal() {
  60. return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
  61. } else if !com.IsDir(remoteAddr) {
  62. return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
  63. }
  64. return remoteAddr, nil
  65. }
  66. type RepoSetting struct {
  67. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  68. Description string `binding:"MaxSize(512)"`
  69. Website string `binding:"Url;MaxSize(100)"`
  70. DonateURL string `binding:"Url;MaxSize(100)"`
  71. DonateBadge string `binding:"Url;MaxSize(100)"`
  72. Branch string
  73. Interval int
  74. MirrorAddress string
  75. Private bool
  76. EnablePrune bool
  77. // Advanced settings
  78. EnableWiki bool
  79. AllowPublicWiki bool
  80. EnableExternalWiki bool
  81. ExternalWikiURL string
  82. EnableIssues bool
  83. AllowPublicIssues bool
  84. EnableExternalTracker bool
  85. ExternalTrackerURL string
  86. TrackerURLFormat string
  87. TrackerIssueStyle string
  88. EnablePulls bool
  89. PullsIgnoreWhitespace bool
  90. PullsAllowRebase 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. type ProtectBranch struct {
  96. Protected bool
  97. RequirePullRequest bool
  98. EnableWhitelist bool
  99. WhitelistUsers string
  100. WhitelistTeams string
  101. }
  102. func (f *ProtectBranch) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  103. return validate(errs, ctx.Data, f, ctx.Locale)
  104. }
  105. type Webhook struct {
  106. Events string
  107. Create bool
  108. Delete bool
  109. Fork bool
  110. Push bool
  111. Issues bool
  112. IssueComment bool
  113. PullRequest bool
  114. Release bool
  115. Active bool
  116. }
  117. func (f Webhook) PushOnly() bool {
  118. return f.Events == "push_only"
  119. }
  120. func (f Webhook) SendEverything() bool {
  121. return f.Events == "send_everything"
  122. }
  123. func (f Webhook) ChooseEvents() bool {
  124. return f.Events == "choose_events"
  125. }
  126. type NewWebhook struct {
  127. PayloadURL string `binding:"Required;Url"`
  128. ContentType int `binding:"Required"`
  129. Secret string
  130. Webhook
  131. }
  132. func (f *NewWebhook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  133. return validate(errs, ctx.Data, f, ctx.Locale)
  134. }
  135. type NewSlackHook struct {
  136. PayloadURL string `binding:"Required;Url"`
  137. Channel string `binding:"Required"`
  138. Username string
  139. IconURL string
  140. Color string
  141. Webhook
  142. }
  143. func (f *NewSlackHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  144. return validate(errs, ctx.Data, f, ctx.Locale)
  145. }
  146. type NewDiscordHook struct {
  147. PayloadURL string `binding:"Required;Url"`
  148. Username string
  149. IconURL string
  150. Color string
  151. Webhook
  152. }
  153. func (f *NewDiscordHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  154. return validate(errs, ctx.Data, f, ctx.Locale)
  155. }
  156. type NewIssue struct {
  157. Title string `binding:"Required;MaxSize(255)"`
  158. LabelIDs string `form:"label_ids"`
  159. MilestoneID int64
  160. AssigneeID int64
  161. Content string
  162. Files []string
  163. }
  164. func (f *NewIssue) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  165. return validate(errs, ctx.Data, f, ctx.Locale)
  166. }
  167. type CreateComment struct {
  168. Content string
  169. Status string `binding:"OmitEmpty;In(reopen,close)"`
  170. Files []string
  171. }
  172. func (f *CreateComment) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  173. return validate(errs, ctx.Data, f, ctx.Locale)
  174. }
  175. type CreateMilestone struct {
  176. Title string `binding:"Required;MaxSize(50)"`
  177. Content string
  178. Deadline string
  179. }
  180. func (f *CreateMilestone) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  181. return validate(errs, ctx.Data, f, ctx.Locale)
  182. }
  183. type CreateLabel struct {
  184. ID int64
  185. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
  186. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  187. }
  188. func (f *CreateLabel) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  189. return validate(errs, ctx.Data, f, ctx.Locale)
  190. }
  191. type InitializeLabels struct {
  192. TemplateName string `binding:"Required"`
  193. }
  194. func (f *InitializeLabels) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  195. return validate(errs, ctx.Data, f, ctx.Locale)
  196. }
  197. type NewRelease struct {
  198. TagName string `binding:"Required"`
  199. Target string `form:"tag_target" binding:"Required"`
  200. Title string `binding:"Required"`
  201. Content string
  202. Draft string
  203. Prerelease bool
  204. Files []string
  205. }
  206. func (f *NewRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  207. return validate(errs, ctx.Data, f, ctx.Locale)
  208. }
  209. type EditRelease struct {
  210. Title string `binding:"Required"`
  211. Content string
  212. Draft string
  213. Prerelease bool
  214. Files []string
  215. }
  216. func (f *EditRelease) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  217. return validate(errs, ctx.Data, f, ctx.Locale)
  218. }
  219. type NewWiki struct {
  220. OldTitle string
  221. Title string `binding:"Required"`
  222. Content string `binding:"Required"`
  223. Message string
  224. }
  225. // FIXME: use code generation to generate this method.
  226. func (f *NewWiki) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  227. return validate(errs, ctx.Data, f, ctx.Locale)
  228. }
  229. type EditRepoFile struct {
  230. TreePath string `binding:"Required;MaxSize(500)"`
  231. Content string `binding:"Required"`
  232. CommitSummary string `binding:"MaxSize(100)`
  233. CommitMessage string
  234. CommitChoice string `binding:"Required;MaxSize(50)"`
  235. NewBranchName string `binding:"AlphaDashDotSlash;MaxSize(100)"`
  236. LastCommit string
  237. }
  238. func (f *EditRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  239. return validate(errs, ctx.Data, f, ctx.Locale)
  240. }
  241. func (f *EditRepoFile) IsNewBrnach() bool {
  242. return f.CommitChoice == "commit-to-new-branch"
  243. }
  244. type EditPreviewDiff struct {
  245. Content string
  246. }
  247. func (f *EditPreviewDiff) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  248. return validate(errs, ctx.Data, f, ctx.Locale)
  249. }
  250. type UploadRepoFile struct {
  251. TreePath string `binding:MaxSize(500)"`
  252. CommitSummary string `binding:"MaxSize(100)`
  253. CommitMessage string
  254. CommitChoice string `binding:"Required;MaxSize(50)"`
  255. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  256. Files []string
  257. }
  258. func (f *UploadRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  259. return validate(errs, ctx.Data, f, ctx.Locale)
  260. }
  261. func (f *UploadRepoFile) IsNewBrnach() bool {
  262. return f.CommitChoice == "commit-to-new-branch"
  263. }
  264. type RemoveUploadFile struct {
  265. File string `binding:"Required;MaxSize(50)"`
  266. }
  267. func (f *RemoveUploadFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  268. return validate(errs, ctx.Data, f, ctx.Locale)
  269. }
  270. type DeleteRepoFile struct {
  271. CommitSummary string `binding:"MaxSize(100)`
  272. CommitMessage string
  273. CommitChoice string `binding:"Required;MaxSize(50)"`
  274. NewBranchName string `binding:"AlphaDashDot;MaxSize(100)"`
  275. }
  276. func (f *DeleteRepoFile) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  277. return validate(errs, ctx.Data, f, ctx.Locale)
  278. }
  279. func (f *DeleteRepoFile) IsNewBrnach() bool {
  280. return f.CommitChoice == "commit-to-new-branch"
  281. }