repo.go 8.9 KB

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