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