code.gitea.io/gitea@v1.21.7/routers/web/repo/setting/protected_tag.go (about)

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