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.

tag.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. git_model "code.gitea.io/gitea/models/git"
  9. "code.gitea.io/gitea/models/organization"
  10. "code.gitea.io/gitea/models/perm"
  11. access_model "code.gitea.io/gitea/models/perm/access"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/web"
  16. "code.gitea.io/gitea/services/forms"
  17. )
  18. // Tags render the page to protect tags
  19. func Tags(ctx *context.Context) {
  20. if setTagsContext(ctx) != nil {
  21. return
  22. }
  23. ctx.HTML(http.StatusOK, tplTags)
  24. }
  25. // NewProtectedTagPost handles creation of a protect tag
  26. func NewProtectedTagPost(ctx *context.Context) {
  27. if setTagsContext(ctx) != nil {
  28. return
  29. }
  30. if ctx.HasError() {
  31. ctx.HTML(http.StatusOK, tplTags)
  32. return
  33. }
  34. repo := ctx.Repo.Repository
  35. form := web.GetForm(ctx).(*forms.ProtectTagForm)
  36. pt := &git_model.ProtectedTag{
  37. RepoID: repo.ID,
  38. NamePattern: strings.TrimSpace(form.NamePattern),
  39. }
  40. if strings.TrimSpace(form.AllowlistUsers) != "" {
  41. pt.AllowlistUserIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistUsers, ","))
  42. }
  43. if strings.TrimSpace(form.AllowlistTeams) != "" {
  44. pt.AllowlistTeamIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistTeams, ","))
  45. }
  46. if err := git_model.InsertProtectedTag(ctx, pt); err != nil {
  47. ctx.ServerError("InsertProtectedTag", err)
  48. return
  49. }
  50. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  51. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath())
  52. }
  53. // EditProtectedTag render the page to edit a protect tag
  54. func EditProtectedTag(ctx *context.Context) {
  55. if setTagsContext(ctx) != nil {
  56. return
  57. }
  58. ctx.Data["PageIsEditProtectedTag"] = true
  59. pt := selectProtectedTagByContext(ctx)
  60. if pt == nil {
  61. return
  62. }
  63. ctx.Data["name_pattern"] = pt.NamePattern
  64. ctx.Data["allowlist_users"] = strings.Join(base.Int64sToStrings(pt.AllowlistUserIDs), ",")
  65. ctx.Data["allowlist_teams"] = strings.Join(base.Int64sToStrings(pt.AllowlistTeamIDs), ",")
  66. ctx.HTML(http.StatusOK, tplTags)
  67. }
  68. // EditProtectedTagPost handles creation of a protect tag
  69. func EditProtectedTagPost(ctx *context.Context) {
  70. if setTagsContext(ctx) != nil {
  71. return
  72. }
  73. ctx.Data["PageIsEditProtectedTag"] = true
  74. if ctx.HasError() {
  75. ctx.HTML(http.StatusOK, tplTags)
  76. return
  77. }
  78. pt := selectProtectedTagByContext(ctx)
  79. if pt == nil {
  80. return
  81. }
  82. form := web.GetForm(ctx).(*forms.ProtectTagForm)
  83. pt.NamePattern = strings.TrimSpace(form.NamePattern)
  84. pt.AllowlistUserIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistUsers, ","))
  85. pt.AllowlistTeamIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistTeams, ","))
  86. if err := git_model.UpdateProtectedTag(ctx, pt); err != nil {
  87. ctx.ServerError("UpdateProtectedTag", err)
  88. return
  89. }
  90. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  91. ctx.Redirect(ctx.Repo.Repository.Link() + "/settings/tags")
  92. }
  93. // DeleteProtectedTagPost handles deletion of a protected tag
  94. func DeleteProtectedTagPost(ctx *context.Context) {
  95. pt := selectProtectedTagByContext(ctx)
  96. if pt == nil {
  97. return
  98. }
  99. if err := git_model.DeleteProtectedTag(ctx, pt); err != nil {
  100. ctx.ServerError("DeleteProtectedTag", err)
  101. return
  102. }
  103. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  104. ctx.Redirect(ctx.Repo.Repository.Link() + "/settings/tags")
  105. }
  106. func setTagsContext(ctx *context.Context) error {
  107. ctx.Data["Title"] = ctx.Tr("repo.settings.tags")
  108. ctx.Data["PageIsSettingsTags"] = true
  109. protectedTags, err := git_model.GetProtectedTags(ctx, ctx.Repo.Repository.ID)
  110. if err != nil {
  111. ctx.ServerError("GetProtectedTags", err)
  112. return err
  113. }
  114. ctx.Data["ProtectedTags"] = protectedTags
  115. users, err := access_model.GetRepoReaders(ctx.Repo.Repository)
  116. if err != nil {
  117. ctx.ServerError("Repo.Repository.GetReaders", err)
  118. return err
  119. }
  120. ctx.Data["Users"] = users
  121. if ctx.Repo.Owner.IsOrganization() {
  122. teams, err := organization.OrgFromUser(ctx.Repo.Owner).TeamsWithAccessToRepo(ctx.Repo.Repository.ID, perm.AccessModeRead)
  123. if err != nil {
  124. ctx.ServerError("Repo.Owner.TeamsWithAccessToRepo", err)
  125. return err
  126. }
  127. ctx.Data["Teams"] = teams
  128. }
  129. return nil
  130. }
  131. func selectProtectedTagByContext(ctx *context.Context) *git_model.ProtectedTag {
  132. id := ctx.FormInt64("id")
  133. if id == 0 {
  134. id = ctx.ParamsInt64(":id")
  135. }
  136. tag, err := git_model.GetProtectedTagByID(ctx, id)
  137. if err != nil {
  138. ctx.ServerError("GetProtectedTagByID", err)
  139. return nil
  140. }
  141. if tag != nil && tag.RepoID == ctx.Repo.Repository.ID {
  142. return tag
  143. }
  144. ctx.NotFound("", fmt.Errorf("ProtectedTag[%v] not associated to repository %v", id, ctx.Repo.Repository))
  145. return nil
  146. }