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