Add tag protection manage via rest API. --------- Co-authored-by: Alexander Kogay <kogay.a@citilink.ru> Co-authored-by: Giteabot <teabot@gitea.io> (cherry picked from commit d4e4226c3cbfa62a6adf15f4466747468eb208c7) Conflicts: modules/structs/repo_tag.go trivial context conflict templates/swagger/v1_json.tmpl fixed with make generate-swagger
This commit is contained in:
parent
7fa7ec0891
commit
ff43d02803
8 changed files with 778 additions and 0 deletions
|
@ -1112,6 +1112,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))
|
||||
|
|
|
@ -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"
|
||||
|
@ -314,3 +318,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)
|
||||
}
|
||||
|
|
|
@ -184,6 +184,12 @@ type swaggerParameterBodies struct {
|
|||
// in:body
|
||||
CreateTagOption api.CreateTagOption
|
||||
|
||||
// in:body
|
||||
CreateTagProtectionOption api.CreateTagProtectionOption
|
||||
|
||||
// in:body
|
||||
EditTagProtectionOption api.EditTagProtectionOption
|
||||
|
||||
// in:body
|
||||
CreateAccessTokenOption api.CreateAccessTokenOption
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue