Skip to content

Commit 32c7a35

Browse files
author
Alexander Kogay
committed
Add tag protection via rest api go-gitea#17862
1 parent 0188d82 commit 32c7a35

File tree

8 files changed

+778
-0
lines changed

8 files changed

+778
-0
lines changed

models/git/protected_tag.go

+13
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ func GetProtectedTagByID(ctx context.Context, id int64) (*ProtectedTag, error) {
110110
return tag, nil
111111
}
112112

113+
// GetProtectedTagByNamePattern getting protected tag by name_pattern
114+
func GetProtectedTagByNamePattern(ctx context.Context, repoID int64, pattern string) (*ProtectedTag, error) {
115+
tag := &ProtectedTag{NamePattern: pattern, RepoID: repoID}
116+
has, err := db.GetEngine(ctx).Get(tag)
117+
if err != nil {
118+
return nil, err
119+
}
120+
if !has {
121+
return nil, nil
122+
}
123+
return tag, nil
124+
}
125+
113126
// IsUserAllowedToControlTag checks if a user can control the specific tag.
114127
// It returns true if the tag name is not protected or the user is allowed to control it.
115128
func IsUserAllowedToControlTag(ctx context.Context, tags []*ProtectedTag, tagName string, userID int64) (bool, error) {

modules/structs/repo_tag.go

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package structs
55

6+
import "time"
7+
68
// Tag represents a repository tag
79
type Tag struct {
810
Name string `json:"name"`
@@ -38,3 +40,29 @@ type CreateTagOption struct {
3840
Message string `json:"message"`
3941
Target string `json:"target"`
4042
}
43+
44+
// TagProtection represents an tag protection
45+
type TagProtection struct {
46+
ID int64 `json:"id"`
47+
NamePattern string `json:"name_pattern"`
48+
WhitelistUsernames []string `json:"whitelist_usernames"`
49+
WhitelistTeams []string `json:"whitelist_teams"`
50+
// swagger:strfmt date-time
51+
Created time.Time `json:"created_at"`
52+
// swagger:strfmt date-time
53+
Updated time.Time `json:"updated_at"`
54+
}
55+
56+
// CreateTagProtectionOption options for creating a tag protection
57+
type CreateTagProtectionOption struct {
58+
NamePattern string `json:"name_pattern"`
59+
WhitelistUsernames []string `json:"whitelist_usernames"`
60+
WhitelistTeams []string `json:"whitelist_teams"`
61+
}
62+
63+
// EditTagProtectionOption options for editing a tag protection
64+
type EditTagProtectionOption struct {
65+
NamePattern *string `json:"name_pattern"`
66+
WhitelistUsernames []string `json:"whitelist_usernames"`
67+
WhitelistTeams []string `json:"whitelist_teams"`
68+
}

routers/api/v1/api.go

+9
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,15 @@ func Routes() *web.Route {
11681168
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateTagOption{}), repo.CreateTag)
11691169
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteTag)
11701170
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true))
1171+
m.Group("/tag_protections", func() {
1172+
m.Combo("").Get(repo.ListTagProtection).
1173+
Post(bind(api.CreateTagProtectionOption{}), mustNotBeArchived, repo.CreateTagProtection)
1174+
m.Group("/{id}", func() {
1175+
m.Combo("").Get(repo.GetTagProtection).
1176+
Patch(bind(api.EditTagProtectionOption{}), mustNotBeArchived, repo.EditTagProtection).
1177+
Delete(repo.DeleteTagProtection)
1178+
})
1179+
}, reqToken(), reqAdmin())
11711180
m.Group("/actions", func() {
11721181
m.Get("/tasks", repo.ListActionTasks)
11731182
}, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true))

0 commit comments

Comments
 (0)