|
|
@ -7,9 +7,13 @@ import (
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"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"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/web"
|
|
|
|
"code.gitea.io/gitea/modules/web"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
|
|
@ -287,3 +291,349 @@ func DeleteTag(ctx *context.APIContext) {
|
|
|
|
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|