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 17 kB

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