You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo.go 15 kB

11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
6 years ago
11 years ago
11 years ago
10 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
6 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
6 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/migrations"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/task"
  19. "code.gitea.io/gitea/modules/util"
  20. repo_service "code.gitea.io/gitea/services/repository"
  21. "github.com/unknwon/com"
  22. )
  23. const (
  24. tplCreate base.TplName = "repo/create"
  25. tplMigrate base.TplName = "repo/migrate"
  26. )
  27. // MustBeNotEmpty render when a repo is a empty git dir
  28. func MustBeNotEmpty(ctx *context.Context) {
  29. if ctx.Repo.Repository.IsEmpty {
  30. ctx.NotFound("MustBeNotEmpty", nil)
  31. }
  32. }
  33. // MustBeEditable check that repo can be edited
  34. func MustBeEditable(ctx *context.Context) {
  35. if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
  36. ctx.NotFound("", nil)
  37. return
  38. }
  39. }
  40. // MustBeAbleToUpload check that repo can be uploaded to
  41. func MustBeAbleToUpload(ctx *context.Context) {
  42. if !setting.Repository.Upload.Enabled {
  43. ctx.NotFound("", nil)
  44. }
  45. }
  46. func checkContextUser(ctx *context.Context, uid int64) *models.User {
  47. orgs, err := models.GetOrgsCanCreateRepoByUserID(ctx.User.ID)
  48. if err != nil {
  49. ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
  50. return nil
  51. }
  52. ctx.Data["Orgs"] = orgs
  53. // Not equal means current user is an organization.
  54. if uid == ctx.User.ID || uid == 0 {
  55. return ctx.User
  56. }
  57. org, err := models.GetUserByID(uid)
  58. if models.IsErrUserNotExist(err) {
  59. return ctx.User
  60. }
  61. if err != nil {
  62. ctx.ServerError("GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
  63. return nil
  64. }
  65. // Check ownership of organization.
  66. if !org.IsOrganization() {
  67. ctx.Error(403)
  68. return nil
  69. }
  70. if !ctx.User.IsAdmin {
  71. canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
  72. if err != nil {
  73. ctx.ServerError("CanCreateOrgRepo", err)
  74. return nil
  75. } else if !canCreate {
  76. ctx.Error(403)
  77. return nil
  78. }
  79. }
  80. return org
  81. }
  82. func getRepoPrivate(ctx *context.Context) bool {
  83. switch strings.ToLower(setting.Repository.DefaultPrivate) {
  84. case setting.RepoCreatingLastUserVisibility:
  85. return ctx.User.LastRepoVisibility
  86. case setting.RepoCreatingPrivate:
  87. return true
  88. case setting.RepoCreatingPublic:
  89. return false
  90. default:
  91. return ctx.User.LastRepoVisibility
  92. }
  93. }
  94. // Create render creating repository page
  95. func Create(ctx *context.Context) {
  96. if !ctx.User.CanCreateRepo() {
  97. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit()), tplCreate, nil)
  98. }
  99. ctx.Data["Title"] = ctx.Tr("new_repo")
  100. // Give default value for template to render.
  101. ctx.Data["Gitignores"] = models.Gitignores
  102. ctx.Data["LabelTemplates"] = models.LabelTemplates
  103. ctx.Data["Licenses"] = models.Licenses
  104. ctx.Data["Readmes"] = models.Readmes
  105. ctx.Data["readme"] = "Default"
  106. ctx.Data["private"] = getRepoPrivate(ctx)
  107. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  108. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  109. if ctx.Written() {
  110. return
  111. }
  112. ctx.Data["ContextUser"] = ctxUser
  113. ctx.Data["repo_template_name"] = ctx.Tr("repo.template_select")
  114. templateID := ctx.QueryInt64("template_id")
  115. if templateID > 0 {
  116. templateRepo, err := models.GetRepositoryByID(templateID)
  117. if err == nil && templateRepo.CheckUnitUser(ctxUser.ID, ctxUser.IsAdmin, models.UnitTypeCode) {
  118. ctx.Data["repo_template"] = templateID
  119. ctx.Data["repo_template_name"] = templateRepo.Name
  120. }
  121. }
  122. ctx.HTML(200, tplCreate)
  123. }
  124. func handleCreateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form interface{}) {
  125. switch {
  126. case models.IsErrReachLimitOfRepo(err):
  127. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form)
  128. case models.IsErrRepoAlreadyExist(err):
  129. ctx.Data["Err_RepoName"] = true
  130. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
  131. case models.IsErrNameReserved(err):
  132. ctx.Data["Err_RepoName"] = true
  133. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
  134. case models.IsErrNamePatternNotAllowed(err):
  135. ctx.Data["Err_RepoName"] = true
  136. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
  137. default:
  138. ctx.ServerError(name, err)
  139. }
  140. }
  141. // CreatePost response for creating repository
  142. func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
  143. ctx.Data["Title"] = ctx.Tr("new_repo")
  144. ctx.Data["Gitignores"] = models.Gitignores
  145. ctx.Data["LabelTemplates"] = models.LabelTemplates
  146. ctx.Data["Licenses"] = models.Licenses
  147. ctx.Data["Readmes"] = models.Readmes
  148. ctxUser := checkContextUser(ctx, form.UID)
  149. if ctx.Written() {
  150. return
  151. }
  152. ctx.Data["ContextUser"] = ctxUser
  153. if ctx.HasError() {
  154. ctx.HTML(200, tplCreate)
  155. return
  156. }
  157. var err error
  158. if form.RepoTemplate > 0 {
  159. opts := models.GenerateRepoOptions{
  160. Name: form.RepoName,
  161. Description: form.Description,
  162. Private: form.Private,
  163. GitContent: form.GitContent,
  164. Topics: form.Topics,
  165. GitHooks: form.GitHooks,
  166. Webhooks: form.Webhooks,
  167. Avatar: form.Avatar,
  168. IssueLabels: form.Labels,
  169. }
  170. if !opts.IsValid() {
  171. ctx.RenderWithErr(ctx.Tr("repo.template.one_item"), tplCreate, form)
  172. return
  173. }
  174. templateRepo := getRepository(ctx, form.RepoTemplate)
  175. if ctx.Written() {
  176. return
  177. }
  178. if !templateRepo.IsTemplate {
  179. ctx.RenderWithErr(ctx.Tr("repo.template.invalid"), tplCreate, form)
  180. return
  181. }
  182. repo, err := repo_service.GenerateRepository(ctx.User, ctxUser, templateRepo, opts)
  183. if err == nil {
  184. log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
  185. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  186. return
  187. }
  188. } else {
  189. repo, err := repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
  190. Name: form.RepoName,
  191. Description: form.Description,
  192. Gitignores: form.Gitignores,
  193. IssueLabels: form.IssueLabels,
  194. License: form.License,
  195. Readme: form.Readme,
  196. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  197. AutoInit: form.AutoInit,
  198. })
  199. if err == nil {
  200. log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
  201. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  202. return
  203. }
  204. }
  205. handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form)
  206. }
  207. // Migrate render migration of repository page
  208. func Migrate(ctx *context.Context) {
  209. ctx.Data["Title"] = ctx.Tr("new_migrate")
  210. ctx.Data["private"] = getRepoPrivate(ctx)
  211. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  212. ctx.Data["mirror"] = ctx.Query("mirror") == "1"
  213. ctx.Data["wiki"] = ctx.Query("wiki") == "1"
  214. ctx.Data["milestones"] = ctx.Query("milestones") == "1"
  215. ctx.Data["labels"] = ctx.Query("labels") == "1"
  216. ctx.Data["issues"] = ctx.Query("issues") == "1"
  217. ctx.Data["pull_requests"] = ctx.Query("pull_requests") == "1"
  218. ctx.Data["releases"] = ctx.Query("releases") == "1"
  219. ctx.Data["LFSActive"] = setting.LFS.StartServer
  220. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  221. if ctx.Written() {
  222. return
  223. }
  224. ctx.Data["ContextUser"] = ctxUser
  225. ctx.HTML(200, tplMigrate)
  226. }
  227. func handleMigrateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form *auth.MigrateRepoForm) {
  228. switch {
  229. case migrations.IsRateLimitError(err):
  230. ctx.RenderWithErr(ctx.Tr("form.visit_rate_limit"), tpl, form)
  231. case migrations.IsTwoFactorAuthError(err):
  232. ctx.RenderWithErr(ctx.Tr("form.2fa_auth_required"), tpl, form)
  233. case models.IsErrReachLimitOfRepo(err):
  234. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form)
  235. case models.IsErrRepoAlreadyExist(err):
  236. ctx.Data["Err_RepoName"] = true
  237. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
  238. case models.IsErrNameReserved(err):
  239. ctx.Data["Err_RepoName"] = true
  240. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
  241. case models.IsErrNamePatternNotAllowed(err):
  242. ctx.Data["Err_RepoName"] = true
  243. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
  244. default:
  245. remoteAddr, _ := form.ParseRemoteAddr(owner)
  246. err = util.URLSanitizedError(err, remoteAddr)
  247. if strings.Contains(err.Error(), "Authentication failed") ||
  248. strings.Contains(err.Error(), "Bad credentials") ||
  249. strings.Contains(err.Error(), "could not read Username") {
  250. ctx.Data["Err_Auth"] = true
  251. ctx.RenderWithErr(ctx.Tr("form.auth_failed", err.Error()), tpl, form)
  252. } else if strings.Contains(err.Error(), "fatal:") {
  253. ctx.Data["Err_CloneAddr"] = true
  254. ctx.RenderWithErr(ctx.Tr("repo.migrate.failed", err.Error()), tpl, form)
  255. } else {
  256. ctx.ServerError(name, err)
  257. }
  258. }
  259. }
  260. // MigratePost response for migrating from external git repository
  261. func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
  262. ctx.Data["Title"] = ctx.Tr("new_migrate")
  263. ctxUser := checkContextUser(ctx, form.UID)
  264. if ctx.Written() {
  265. return
  266. }
  267. ctx.Data["ContextUser"] = ctxUser
  268. if ctx.HasError() {
  269. ctx.HTML(200, tplMigrate)
  270. return
  271. }
  272. remoteAddr, err := form.ParseRemoteAddr(ctx.User)
  273. if err != nil {
  274. if models.IsErrInvalidCloneAddr(err) {
  275. ctx.Data["Err_CloneAddr"] = true
  276. addrErr := err.(models.ErrInvalidCloneAddr)
  277. switch {
  278. case addrErr.IsURLError:
  279. ctx.RenderWithErr(ctx.Tr("form.url_error"), tplMigrate, &form)
  280. case addrErr.IsPermissionDenied:
  281. ctx.RenderWithErr(ctx.Tr("repo.migrate.permission_denied"), tplMigrate, &form)
  282. case addrErr.IsInvalidPath:
  283. ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), tplMigrate, &form)
  284. default:
  285. ctx.ServerError("Unknown error", err)
  286. }
  287. } else {
  288. ctx.ServerError("ParseRemoteAddr", err)
  289. }
  290. return
  291. }
  292. var opts = migrations.MigrateOptions{
  293. OriginalURL: form.CloneAddr,
  294. CloneAddr: remoteAddr,
  295. RepoName: form.RepoName,
  296. Description: form.Description,
  297. Private: form.Private || setting.Repository.ForcePrivate,
  298. Mirror: form.Mirror,
  299. AuthUsername: form.AuthUsername,
  300. AuthPassword: form.AuthPassword,
  301. Wiki: form.Wiki,
  302. Issues: form.Issues,
  303. Milestones: form.Milestones,
  304. Labels: form.Labels,
  305. Comments: true,
  306. PullRequests: form.PullRequests,
  307. Releases: form.Releases,
  308. }
  309. if opts.Mirror {
  310. opts.Issues = false
  311. opts.Milestones = false
  312. opts.Labels = false
  313. opts.Comments = false
  314. opts.PullRequests = false
  315. opts.Releases = false
  316. }
  317. err = models.CheckCreateRepository(ctx.User, ctxUser, opts.RepoName)
  318. if err != nil {
  319. handleMigrateError(ctx, ctxUser, err, "MigratePost", tplMigrate, &form)
  320. return
  321. }
  322. err = task.MigrateRepository(ctx.User, ctxUser, opts)
  323. if err == nil {
  324. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + opts.RepoName)
  325. return
  326. }
  327. handleMigrateError(ctx, ctxUser, err, "MigratePost", tplMigrate, &form)
  328. }
  329. // Action response for actions to a repository
  330. func Action(ctx *context.Context) {
  331. var err error
  332. switch ctx.Params(":action") {
  333. case "watch":
  334. err = models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  335. case "unwatch":
  336. err = models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  337. case "star":
  338. err = models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  339. case "unstar":
  340. err = models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  341. case "desc": // FIXME: this is not used
  342. if !ctx.Repo.IsOwner() {
  343. ctx.Error(404)
  344. return
  345. }
  346. ctx.Repo.Repository.Description = ctx.Query("desc")
  347. ctx.Repo.Repository.Website = ctx.Query("site")
  348. err = models.UpdateRepository(ctx.Repo.Repository, false)
  349. }
  350. if err != nil {
  351. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  352. return
  353. }
  354. ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink)
  355. }
  356. // RedirectDownload return a file based on the following infos:
  357. func RedirectDownload(ctx *context.Context) {
  358. var (
  359. vTag = ctx.Params("vTag")
  360. fileName = ctx.Params("fileName")
  361. )
  362. tagNames := []string{vTag}
  363. curRepo := ctx.Repo.Repository
  364. releases, err := models.GetReleasesByRepoIDAndNames(curRepo.ID, tagNames)
  365. if err != nil {
  366. if models.IsErrAttachmentNotExist(err) {
  367. ctx.Error(404)
  368. return
  369. }
  370. ctx.ServerError("RedirectDownload", err)
  371. return
  372. }
  373. if len(releases) == 1 {
  374. release := releases[0]
  375. att, err := models.GetAttachmentByReleaseIDFileName(release.ID, fileName)
  376. if err != nil {
  377. ctx.Error(404)
  378. return
  379. }
  380. if att != nil {
  381. ctx.Redirect(setting.AppSubURL + "/attachments/" + att.UUID)
  382. return
  383. }
  384. }
  385. ctx.Error(404)
  386. }
  387. // Download download an archive of a repository
  388. func Download(ctx *context.Context) {
  389. var (
  390. uri = ctx.Params("*")
  391. refName string
  392. ext string
  393. archivePath string
  394. archiveType git.ArchiveType
  395. )
  396. switch {
  397. case strings.HasSuffix(uri, ".zip"):
  398. ext = ".zip"
  399. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  400. archiveType = git.ZIP
  401. case strings.HasSuffix(uri, ".tar.gz"):
  402. ext = ".tar.gz"
  403. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  404. archiveType = git.TARGZ
  405. default:
  406. log.Trace("Unknown format: %s", uri)
  407. ctx.Error(404)
  408. return
  409. }
  410. refName = strings.TrimSuffix(uri, ext)
  411. if !com.IsDir(archivePath) {
  412. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  413. ctx.ServerError("Download -> os.MkdirAll(archivePath)", err)
  414. return
  415. }
  416. }
  417. // Get corresponding commit.
  418. var (
  419. commit *git.Commit
  420. err error
  421. )
  422. gitRepo := ctx.Repo.GitRepo
  423. if gitRepo.IsBranchExist(refName) {
  424. commit, err = gitRepo.GetBranchCommit(refName)
  425. if err != nil {
  426. ctx.ServerError("GetBranchCommit", err)
  427. return
  428. }
  429. } else if gitRepo.IsTagExist(refName) {
  430. commit, err = gitRepo.GetTagCommit(refName)
  431. if err != nil {
  432. ctx.ServerError("GetTagCommit", err)
  433. return
  434. }
  435. } else if len(refName) >= 4 && len(refName) <= 40 {
  436. commit, err = gitRepo.GetCommit(refName)
  437. if err != nil {
  438. ctx.NotFound("GetCommit", nil)
  439. return
  440. }
  441. } else {
  442. ctx.NotFound("Download", nil)
  443. return
  444. }
  445. archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext)
  446. if !com.IsFile(archivePath) {
  447. if err := commit.CreateArchive(archivePath, archiveType); err != nil {
  448. ctx.ServerError("Download -> CreateArchive "+archivePath, err)
  449. return
  450. }
  451. }
  452. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+refName+ext)
  453. }
  454. // Status returns repository's status
  455. func Status(ctx *context.Context) {
  456. task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
  457. if err != nil {
  458. ctx.JSON(500, map[string]interface{}{
  459. "err": err,
  460. })
  461. return
  462. }
  463. ctx.JSON(200, map[string]interface{}{
  464. "status": ctx.Repo.Repository.Status,
  465. "err": task.Errors,
  466. })
  467. }