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