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

406 lines
11 KiB

10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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>
4 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>
4 years ago
10 years ago
10 years ago
8 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>
4 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>
4 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>
4 years ago
Add configurable Trust Models (#11712) * Add configurable Trust Models Gitea's default signature verification model differs from GitHub. GitHub uses signatures to verify that the committer is who they say they are - meaning that when GitHub makes a signed commit it must be the committer. The GitHub model prevents re-publishing of commits after revocation of a key and prevents re-signing of other people's commits to create a completely trusted repository signed by one key or a set of trusted keys. The default behaviour of Gitea in contrast is to always display the avatar and information related to a signature. This allows signatures to be decoupled from the committer. That being said, allowing arbitary users to present other peoples commits as theirs is not necessarily desired therefore we have a trust model whereby signatures from collaborators are marked trusted, signatures matching the commit line are marked untrusted and signatures that match a user in the db but not the committer line are marked unmatched. The problem with this model is that this conflicts with Github therefore we need to provide an option to allow users to choose the Github model should they wish to. Signed-off-by: Andrew Thornton <art27@cantab.net> * Adjust locale strings Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Co-authored-by: 6543 <6543@obermui.de> * Update models/gpg_key.go * Add migration for repository Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
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>
4 years ago
10 years ago
9 years ago
9 years ago
9 years ago
8 years ago
10 years ago
10 years ago
  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. "os"
  9. "path"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. repo_service "code.gitea.io/gitea/services/repository"
  19. "github.com/unknwon/com"
  20. )
  21. const (
  22. tplCreate base.TplName = "repo/create"
  23. )
  24. // MustBeNotEmpty render when a repo is a empty git dir
  25. func MustBeNotEmpty(ctx *context.Context) {
  26. if ctx.Repo.Repository.IsEmpty {
  27. ctx.NotFound("MustBeNotEmpty", nil)
  28. }
  29. }
  30. // MustBeEditable check that repo can be edited
  31. func MustBeEditable(ctx *context.Context) {
  32. if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
  33. ctx.NotFound("", nil)
  34. return
  35. }
  36. }
  37. // MustBeAbleToUpload check that repo can be uploaded to
  38. func MustBeAbleToUpload(ctx *context.Context) {
  39. if !setting.Repository.Upload.Enabled {
  40. ctx.NotFound("", nil)
  41. }
  42. }
  43. func checkContextUser(ctx *context.Context, uid int64) *models.User {
  44. orgs, err := models.GetOrgsCanCreateRepoByUserID(ctx.User.ID)
  45. if err != nil {
  46. ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
  47. return nil
  48. }
  49. if !ctx.User.IsAdmin {
  50. orgsAvailable := []*models.User{}
  51. for i := 0; i < len(orgs); i++ {
  52. if orgs[i].CanCreateRepo() {
  53. orgsAvailable = append(orgsAvailable, orgs[i])
  54. }
  55. }
  56. ctx.Data["Orgs"] = orgsAvailable
  57. } else {
  58. ctx.Data["Orgs"] = orgs
  59. }
  60. // Not equal means current user is an organization.
  61. if uid == ctx.User.ID || uid == 0 {
  62. return ctx.User
  63. }
  64. org, err := models.GetUserByID(uid)
  65. if models.IsErrUserNotExist(err) {
  66. return ctx.User
  67. }
  68. if err != nil {
  69. ctx.ServerError("GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
  70. return nil
  71. }
  72. // Check ownership of organization.
  73. if !org.IsOrganization() {
  74. ctx.Error(403)
  75. return nil
  76. }
  77. if !ctx.User.IsAdmin {
  78. canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
  79. if err != nil {
  80. ctx.ServerError("CanCreateOrgRepo", err)
  81. return nil
  82. } else if !canCreate {
  83. ctx.Error(403)
  84. return nil
  85. }
  86. } else {
  87. ctx.Data["Orgs"] = orgs
  88. }
  89. return org
  90. }
  91. func getRepoPrivate(ctx *context.Context) bool {
  92. switch strings.ToLower(setting.Repository.DefaultPrivate) {
  93. case setting.RepoCreatingLastUserVisibility:
  94. return ctx.User.LastRepoVisibility
  95. case setting.RepoCreatingPrivate:
  96. return true
  97. case setting.RepoCreatingPublic:
  98. return false
  99. default:
  100. return ctx.User.LastRepoVisibility
  101. }
  102. }
  103. // Create render creating repository page
  104. func Create(ctx *context.Context) {
  105. ctx.Data["Title"] = ctx.Tr("new_repo")
  106. // Give default value for template to render.
  107. ctx.Data["Gitignores"] = models.Gitignores
  108. ctx.Data["LabelTemplates"] = models.LabelTemplates
  109. ctx.Data["Licenses"] = models.Licenses
  110. ctx.Data["Readmes"] = models.Readmes
  111. ctx.Data["readme"] = "Default"
  112. ctx.Data["private"] = getRepoPrivate(ctx)
  113. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  114. ctx.Data["default_branch"] = setting.Repository.DefaultBranch
  115. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  116. if ctx.Written() {
  117. return
  118. }
  119. ctx.Data["ContextUser"] = ctxUser
  120. ctx.Data["repo_template_name"] = ctx.Tr("repo.template_select")
  121. templateID := ctx.QueryInt64("template_id")
  122. if templateID > 0 {
  123. templateRepo, err := models.GetRepositoryByID(templateID)
  124. if err == nil && templateRepo.CheckUnitUser(ctxUser, models.UnitTypeCode) {
  125. ctx.Data["repo_template"] = templateID
  126. ctx.Data["repo_template_name"] = templateRepo.Name
  127. }
  128. }
  129. if !ctx.User.CanCreateRepo() {
  130. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit()), tplCreate, nil)
  131. } else {
  132. ctx.HTML(200, tplCreate)
  133. }
  134. }
  135. func handleCreateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form interface{}) {
  136. switch {
  137. case models.IsErrReachLimitOfRepo(err):
  138. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form)
  139. case models.IsErrRepoAlreadyExist(err):
  140. ctx.Data["Err_RepoName"] = true
  141. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
  142. case models.IsErrNameReserved(err):
  143. ctx.Data["Err_RepoName"] = true
  144. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
  145. case models.IsErrNamePatternNotAllowed(err):
  146. ctx.Data["Err_RepoName"] = true
  147. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
  148. default:
  149. ctx.ServerError(name, err)
  150. }
  151. }
  152. // CreatePost response for creating repository
  153. func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
  154. ctx.Data["Title"] = ctx.Tr("new_repo")
  155. ctx.Data["Gitignores"] = models.Gitignores
  156. ctx.Data["LabelTemplates"] = models.LabelTemplates
  157. ctx.Data["Licenses"] = models.Licenses
  158. ctx.Data["Readmes"] = models.Readmes
  159. ctxUser := checkContextUser(ctx, form.UID)
  160. if ctx.Written() {
  161. return
  162. }
  163. ctx.Data["ContextUser"] = ctxUser
  164. if ctx.HasError() {
  165. ctx.HTML(200, tplCreate)
  166. return
  167. }
  168. var repo *models.Repository
  169. var err error
  170. if form.RepoTemplate > 0 {
  171. opts := models.GenerateRepoOptions{
  172. Name: form.RepoName,
  173. Description: form.Description,
  174. Private: form.Private,
  175. GitContent: form.GitContent,
  176. Topics: form.Topics,
  177. GitHooks: form.GitHooks,
  178. Webhooks: form.Webhooks,
  179. Avatar: form.Avatar,
  180. IssueLabels: form.Labels,
  181. }
  182. if !opts.IsValid() {
  183. ctx.RenderWithErr(ctx.Tr("repo.template.one_item"), tplCreate, form)
  184. return
  185. }
  186. templateRepo := getRepository(ctx, form.RepoTemplate)
  187. if ctx.Written() {
  188. return
  189. }
  190. if !templateRepo.IsTemplate {
  191. ctx.RenderWithErr(ctx.Tr("repo.template.invalid"), tplCreate, form)
  192. return
  193. }
  194. repo, err = repo_service.GenerateRepository(ctx.User, ctxUser, templateRepo, opts)
  195. if err == nil {
  196. log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
  197. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  198. return
  199. }
  200. } else {
  201. repo, err = repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
  202. Name: form.RepoName,
  203. Description: form.Description,
  204. Gitignores: form.Gitignores,
  205. IssueLabels: form.IssueLabels,
  206. License: form.License,
  207. Readme: form.Readme,
  208. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  209. DefaultBranch: form.DefaultBranch,
  210. AutoInit: form.AutoInit,
  211. TrustModel: models.ToTrustModel(form.TrustModel),
  212. })
  213. if err == nil {
  214. log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
  215. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  216. return
  217. }
  218. }
  219. handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form)
  220. }
  221. // Action response for actions to a repository
  222. func Action(ctx *context.Context) {
  223. var err error
  224. switch ctx.Params(":action") {
  225. case "watch":
  226. err = models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  227. case "unwatch":
  228. err = models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  229. case "star":
  230. err = models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  231. case "unstar":
  232. err = models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  233. case "desc": // FIXME: this is not used
  234. if !ctx.Repo.IsOwner() {
  235. ctx.Error(404)
  236. return
  237. }
  238. ctx.Repo.Repository.Description = ctx.Query("desc")
  239. ctx.Repo.Repository.Website = ctx.Query("site")
  240. err = models.UpdateRepository(ctx.Repo.Repository, false)
  241. }
  242. if err != nil {
  243. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  244. return
  245. }
  246. ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink)
  247. }
  248. // RedirectDownload return a file based on the following infos:
  249. func RedirectDownload(ctx *context.Context) {
  250. var (
  251. vTag = ctx.Params("vTag")
  252. fileName = ctx.Params("fileName")
  253. )
  254. tagNames := []string{vTag}
  255. curRepo := ctx.Repo.Repository
  256. releases, err := models.GetReleasesByRepoIDAndNames(models.DefaultDBContext(), curRepo.ID, tagNames)
  257. if err != nil {
  258. if models.IsErrAttachmentNotExist(err) {
  259. ctx.Error(404)
  260. return
  261. }
  262. ctx.ServerError("RedirectDownload", err)
  263. return
  264. }
  265. if len(releases) == 1 {
  266. release := releases[0]
  267. att, err := models.GetAttachmentByReleaseIDFileName(release.ID, fileName)
  268. if err != nil {
  269. ctx.Error(404)
  270. return
  271. }
  272. if att != nil {
  273. ctx.Redirect(att.DownloadURL())
  274. return
  275. }
  276. }
  277. ctx.Error(404)
  278. }
  279. // Download download an archive of a repository
  280. func Download(ctx *context.Context) {
  281. var (
  282. uri = ctx.Params("*")
  283. refName string
  284. ext string
  285. archivePath string
  286. archiveType git.ArchiveType
  287. )
  288. switch {
  289. case strings.HasSuffix(uri, ".zip"):
  290. ext = ".zip"
  291. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  292. archiveType = git.ZIP
  293. case strings.HasSuffix(uri, ".tar.gz"):
  294. ext = ".tar.gz"
  295. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  296. archiveType = git.TARGZ
  297. default:
  298. log.Trace("Unknown format: %s", uri)
  299. ctx.Error(404)
  300. return
  301. }
  302. refName = strings.TrimSuffix(uri, ext)
  303. if !com.IsDir(archivePath) {
  304. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  305. ctx.ServerError("Download -> os.MkdirAll(archivePath)", err)
  306. return
  307. }
  308. }
  309. // Get corresponding commit.
  310. var (
  311. commit *git.Commit
  312. err error
  313. )
  314. gitRepo := ctx.Repo.GitRepo
  315. if gitRepo.IsBranchExist(refName) {
  316. commit, err = gitRepo.GetBranchCommit(refName)
  317. if err != nil {
  318. ctx.ServerError("GetBranchCommit", err)
  319. return
  320. }
  321. } else if gitRepo.IsTagExist(refName) {
  322. commit, err = gitRepo.GetTagCommit(refName)
  323. if err != nil {
  324. ctx.ServerError("GetTagCommit", err)
  325. return
  326. }
  327. } else if len(refName) >= 4 && len(refName) <= 40 {
  328. commit, err = gitRepo.GetCommit(refName)
  329. if err != nil {
  330. ctx.NotFound("GetCommit", nil)
  331. return
  332. }
  333. } else {
  334. ctx.NotFound("Download", nil)
  335. return
  336. }
  337. archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext)
  338. if !com.IsFile(archivePath) {
  339. if err := commit.CreateArchive(ctx.Req.Context(), archivePath, git.CreateArchiveOpts{
  340. Format: archiveType,
  341. Prefix: setting.Repository.PrefixArchiveFiles,
  342. }); err != nil {
  343. ctx.ServerError("Download -> CreateArchive "+archivePath, err)
  344. return
  345. }
  346. }
  347. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+refName+ext)
  348. }
  349. // Status returns repository's status
  350. func Status(ctx *context.Context) {
  351. task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
  352. if err != nil {
  353. ctx.JSON(500, map[string]interface{}{
  354. "err": err,
  355. })
  356. return
  357. }
  358. ctx.JSON(200, map[string]interface{}{
  359. "status": ctx.Repo.Repository.Status,
  360. "err": task.Errors,
  361. })
  362. }