repo.go 9.0 KB

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