aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api/v1
diff options
context:
space:
mode:
authormzroot <minoga_dot@mail.ru>2024-06-14 19:56:10 +0300
committerGitHub <noreply@github.com>2024-06-14 18:56:10 +0200
commitd4e4226c3cbfa62a6adf15f4466747468eb208c7 (patch)
treed98ea2048ea29a43ef9dcf67a7b57655a6fcd369 /routers/api/v1
parent4e7b067a7fdfb3e2c8dfdf87475e3938051fd400 (diff)
downloadgitea-d4e4226c3cbfa62a6adf15f4466747468eb208c7.tar.gz
gitea-d4e4226c3cbfa62a6adf15f4466747468eb208c7.zip
Add tag protection via rest api #17862 (#31295)
Add tag protection manage via rest API. --------- Co-authored-by: Alexander Kogay <kogay.a@citilink.ru> Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'routers/api/v1')
-rw-r--r--routers/api/v1/api.go9
-rw-r--r--routers/api/v1/repo/tag.go350
-rw-r--r--routers/api/v1/swagger/options.go6
-rw-r--r--routers/api/v1/swagger/repo.go14
4 files changed, 379 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index 74062c44ac..5363489939 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -1168,6 +1168,15 @@ func Routes() *web.Route {
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateTagOption{}), repo.CreateTag)
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteTag)
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true))
+ m.Group("/tag_protections", func() {
+ m.Combo("").Get(repo.ListTagProtection).
+ Post(bind(api.CreateTagProtectionOption{}), mustNotBeArchived, repo.CreateTagProtection)
+ m.Group("/{id}", func() {
+ m.Combo("").Get(repo.GetTagProtection).
+ Patch(bind(api.EditTagProtectionOption{}), mustNotBeArchived, repo.EditTagProtection).
+ Delete(repo.DeleteTagProtection)
+ })
+ }, reqToken(), reqAdmin())
m.Group("/actions", func() {
m.Get("/tasks", repo.ListActionTasks)
}, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true))
diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go
index 8577a0e896..f72034950f 100644
--- a/routers/api/v1/repo/tag.go
+++ b/routers/api/v1/repo/tag.go
@@ -7,9 +7,13 @@ import (
"errors"
"fmt"
"net/http"
+ "strings"
"code.gitea.io/gitea/models"
+ git_model "code.gitea.io/gitea/models/git"
+ "code.gitea.io/gitea/models/organization"
repo_model "code.gitea.io/gitea/models/repo"
+ user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -287,3 +291,349 @@ func DeleteTag(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent)
}
+
+// ListTagProtection lists tag protections for a repo
+func ListTagProtection(ctx *context.APIContext) {
+ // swagger:operation GET /repos/{owner}/{repo}/tag_protections repository repoListTagProtection
+ // ---
+ // summary: List tag protections for a repository
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // responses:
+ // "200":
+ // "$ref": "#/responses/TagProtectionList"
+
+ repo := ctx.Repo.Repository
+ pts, err := git_model.GetProtectedTags(ctx, repo.ID)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedTags", err)
+ return
+ }
+ apiPts := make([]*api.TagProtection, len(pts))
+ for i := range pts {
+ apiPts[i] = convert.ToTagProtection(ctx, pts[i], repo)
+ }
+
+ ctx.JSON(http.StatusOK, apiPts)
+}
+
+// GetTagProtection gets a tag protection
+func GetTagProtection(ctx *context.APIContext) {
+ // swagger:operation GET /repos/{owner}/{repo}/tag_protections/{id} repository repoGetTagProtection
+ // ---
+ // summary: Get a specific tag protection for the repository
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // - name: id
+ // in: path
+ // description: id of the tag protect to get
+ // type: integer
+ // required: true
+ // responses:
+ // "200":
+ // "$ref": "#/responses/TagProtection"
+ // "404":
+ // "$ref": "#/responses/notFound"
+
+ repo := ctx.Repo.Repository
+ id := ctx.ParamsInt64(":id")
+ pt, err := git_model.GetProtectedTagByID(ctx, id)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedTagByID", err)
+ return
+ }
+
+ if pt == nil || repo.ID != pt.RepoID {
+ ctx.NotFound()
+ return
+ }
+
+ ctx.JSON(http.StatusOK, convert.ToTagProtection(ctx, pt, repo))
+}
+
+// CreateTagProtection creates a tag protection for a repo
+func CreateTagProtection(ctx *context.APIContext) {
+ // swagger:operation POST /repos/{owner}/{repo}/tag_protections repository repoCreateTagProtection
+ // ---
+ // summary: Create a tag protections for a repository
+ // consumes:
+ // - application/json
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // - name: body
+ // in: body
+ // schema:
+ // "$ref": "#/definitions/CreateTagProtectionOption"
+ // responses:
+ // "201":
+ // "$ref": "#/responses/TagProtection"
+ // "403":
+ // "$ref": "#/responses/forbidden"
+ // "404":
+ // "$ref": "#/responses/notFound"
+ // "422":
+ // "$ref": "#/responses/validationError"
+ // "423":
+ // "$ref": "#/responses/repoArchivedError"
+
+ form := web.GetForm(ctx).(*api.CreateTagProtectionOption)
+ repo := ctx.Repo.Repository
+
+ namePattern := strings.TrimSpace(form.NamePattern)
+ if namePattern == "" {
+ ctx.Error(http.StatusBadRequest, "name_pattern are empty", "name_pattern are empty")
+ return
+ }
+
+ if len(form.WhitelistUsernames) == 0 && len(form.WhitelistTeams) == 0 {
+ ctx.Error(http.StatusBadRequest, "both whitelist_usernames and whitelist_teams are empty", "both whitelist_usernames and whitelist_teams are empty")
+ return
+ }
+
+ pt, err := git_model.GetProtectedTagByNamePattern(ctx, repo.ID, namePattern)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectTagOfRepo", err)
+ return
+ } else if pt != nil {
+ ctx.Error(http.StatusForbidden, "Create tag protection", "Tag protection already exist")
+ return
+ }
+
+ var whitelistUsers, whitelistTeams []int64
+ whitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.WhitelistUsernames, false)
+ if err != nil {
+ if user_model.IsErrUserNotExist(err) {
+ ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
+ return
+ }
+ ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
+ return
+ }
+
+ if repo.Owner.IsOrganization() {
+ whitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.WhitelistTeams, false)
+ if err != nil {
+ if organization.IsErrTeamNotExist(err) {
+ ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
+ return
+ }
+ ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
+ return
+ }
+ }
+
+ protectTag := &git_model.ProtectedTag{
+ RepoID: repo.ID,
+ NamePattern: strings.TrimSpace(namePattern),
+ AllowlistUserIDs: whitelistUsers,
+ AllowlistTeamIDs: whitelistTeams,
+ }
+ if err := git_model.InsertProtectedTag(ctx, protectTag); err != nil {
+ ctx.Error(http.StatusInternalServerError, "InsertProtectedTag", err)
+ return
+ }
+
+ pt, err = git_model.GetProtectedTagByID(ctx, protectTag.ID)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedTagByID", err)
+ return
+ }
+
+ if pt == nil || pt.RepoID != repo.ID {
+ ctx.Error(http.StatusInternalServerError, "New tag protection not found", err)
+ return
+ }
+
+ ctx.JSON(http.StatusCreated, convert.ToTagProtection(ctx, pt, repo))
+}
+
+// EditTagProtection edits a tag protection for a repo
+func EditTagProtection(ctx *context.APIContext) {
+ // swagger:operation PATCH /repos/{owner}/{repo}/tag_protections/{id} repository repoEditTagProtection
+ // ---
+ // summary: Edit a tag protections for a repository. Only fields that are set will be changed
+ // consumes:
+ // - application/json
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // - name: id
+ // in: path
+ // description: id of protected tag
+ // type: integer
+ // required: true
+ // - name: body
+ // in: body
+ // schema:
+ // "$ref": "#/definitions/EditTagProtectionOption"
+ // responses:
+ // "200":
+ // "$ref": "#/responses/TagProtection"
+ // "404":
+ // "$ref": "#/responses/notFound"
+ // "422":
+ // "$ref": "#/responses/validationError"
+ // "423":
+ // "$ref": "#/responses/repoArchivedError"
+
+ repo := ctx.Repo.Repository
+ form := web.GetForm(ctx).(*api.EditTagProtectionOption)
+
+ id := ctx.ParamsInt64(":id")
+ pt, err := git_model.GetProtectedTagByID(ctx, id)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedTagByID", err)
+ return
+ }
+
+ if pt == nil || pt.RepoID != repo.ID {
+ ctx.NotFound()
+ return
+ }
+
+ if form.NamePattern != nil {
+ pt.NamePattern = *form.NamePattern
+ }
+
+ var whitelistUsers, whitelistTeams []int64
+ if form.WhitelistTeams != nil {
+ if repo.Owner.IsOrganization() {
+ whitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.WhitelistTeams, false)
+ if err != nil {
+ if organization.IsErrTeamNotExist(err) {
+ ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
+ return
+ }
+ ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
+ return
+ }
+ }
+ pt.AllowlistTeamIDs = whitelistTeams
+ }
+
+ if form.WhitelistUsernames != nil {
+ whitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.WhitelistUsernames, false)
+ if err != nil {
+ if user_model.IsErrUserNotExist(err) {
+ ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
+ return
+ }
+ ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
+ return
+ }
+ pt.AllowlistUserIDs = whitelistUsers
+ }
+
+ err = git_model.UpdateProtectedTag(ctx, pt)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "UpdateProtectedTag", err)
+ return
+ }
+
+ pt, err = git_model.GetProtectedTagByID(ctx, id)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedTagByID", err)
+ return
+ }
+
+ if pt == nil || pt.RepoID != repo.ID {
+ ctx.Error(http.StatusInternalServerError, "New tag protection not found", "New tag protection not found")
+ return
+ }
+
+ ctx.JSON(http.StatusOK, convert.ToTagProtection(ctx, pt, repo))
+}
+
+// DeleteTagProtection
+func DeleteTagProtection(ctx *context.APIContext) {
+ // swagger:operation DELETE /repos/{owner}/{repo}/tag_protections/{id} repository repoDeleteTagProtection
+ // ---
+ // summary: Delete a specific tag protection for the repository
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // - name: id
+ // in: path
+ // description: id of protected tag
+ // type: integer
+ // required: true
+ // responses:
+ // "204":
+ // "$ref": "#/responses/empty"
+ // "404":
+ // "$ref": "#/responses/notFound"
+
+ repo := ctx.Repo.Repository
+ id := ctx.ParamsInt64(":id")
+ pt, err := git_model.GetProtectedTagByID(ctx, id)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedTagByID", err)
+ return
+ }
+
+ if pt == nil || pt.RepoID != repo.ID {
+ ctx.NotFound()
+ return
+ }
+
+ err = git_model.DeleteProtectedTag(ctx, pt)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "DeleteProtectedTag", err)
+ return
+ }
+
+ ctx.Status(http.StatusNoContent)
+}
diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go
index cd551cbdfa..1de58632d5 100644
--- a/routers/api/v1/swagger/options.go
+++ b/routers/api/v1/swagger/options.go
@@ -171,6 +171,12 @@ type swaggerParameterBodies struct {
CreateTagOption api.CreateTagOption
// in:body
+ CreateTagProtectionOption api.CreateTagProtectionOption
+
+ // in:body
+ EditTagProtectionOption api.EditTagProtectionOption
+
+ // in:body
CreateAccessTokenOption api.CreateAccessTokenOption
// in:body
diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go
index fcd34a63a9..345835f9a5 100644
--- a/routers/api/v1/swagger/repo.go
+++ b/routers/api/v1/swagger/repo.go
@@ -70,6 +70,20 @@ type swaggerResponseAnnotatedTag struct {
Body api.AnnotatedTag `json:"body"`
}
+// TagProtectionList
+// swagger:response TagProtectionList
+type swaggerResponseTagProtectionList struct {
+ // in:body
+ Body []api.TagProtection `json:"body"`
+}
+
+// TagProtection
+// swagger:response TagProtection
+type swaggerResponseTagProtection struct {
+ // in:body
+ Body api.TagProtection `json:"body"`
+}
+
// Reference
// swagger:response Reference
type swaggerResponseReference struct {