repo.go 9.2 KB

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