repo.go 8.9 KB

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