mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-27 19:00:35 +00:00
Use API error helpers and improve response codes (#2366)
This commit is contained in:
parent
3bee51dc7a
commit
3e563ef198
25 changed files with 117 additions and 156 deletions
|
@ -605,7 +605,7 @@ const docTemplate = `{
|
|||
},
|
||||
"/healthz": {
|
||||
"get": {
|
||||
"description": "If everything is fine, just a 200 will be returned, a 500 signals server state is unhealthy.",
|
||||
"description": "If everything is fine, just a 204 will be returned, a 500 signals server state is unhealthy.",
|
||||
"produces": [
|
||||
"text/plain"
|
||||
],
|
||||
|
@ -614,8 +614,8 @@ const docTemplate = `{
|
|||
],
|
||||
"summary": "Health information",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
"204": {
|
||||
"description": "No Content"
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal Server Error"
|
||||
|
@ -869,7 +869,7 @@ const docTemplate = `{
|
|||
"delete": {
|
||||
"description": "Deletes the given org. Requires admin rights.",
|
||||
"produces": [
|
||||
"application/json"
|
||||
"text/plain"
|
||||
],
|
||||
"tags": [
|
||||
"Orgs"
|
||||
|
@ -894,10 +894,7 @@ const docTemplate = `{
|
|||
],
|
||||
"responses": {
|
||||
"204": {
|
||||
"description": "No Content",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Org"
|
||||
}
|
||||
"description": "No Content"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1838,8 +1835,8 @@ const docTemplate = `{
|
|||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
"204": {
|
||||
"description": "No Content"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -1928,8 +1925,8 @@ const docTemplate = `{
|
|||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
"204": {
|
||||
"description": "No Content"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2667,8 +2664,8 @@ const docTemplate = `{
|
|||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
"204": {
|
||||
"description": "No Content"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -3307,7 +3304,7 @@ const docTemplate = `{
|
|||
"tags": [
|
||||
"User"
|
||||
],
|
||||
"summary": "Return the token of the current user as stringª",
|
||||
"summary": "Return the token of the current user as string",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
|
@ -3473,7 +3470,7 @@ const docTemplate = `{
|
|||
"delete": {
|
||||
"description": "Deletes the given user. Requires admin rights.",
|
||||
"produces": [
|
||||
"application/json"
|
||||
"text/plain"
|
||||
],
|
||||
"tags": [
|
||||
"Users"
|
||||
|
@ -3497,11 +3494,8 @@ const docTemplate = `{
|
|||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/User"
|
||||
}
|
||||
"204": {
|
||||
"description": "No Content"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -65,7 +65,7 @@ func GetAgent(c *gin.Context) {
|
|||
|
||||
agent, err := store.FromContext(c).AgentFind(agentID)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, agent)
|
||||
|
@ -89,7 +89,7 @@ func GetAgentTasks(c *gin.Context) {
|
|||
|
||||
agent, err := store.FromContext(c).AgentFind(agentID)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "Cannot find agent. %s", err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -132,7 +132,7 @@ func PatchAgent(c *gin.Context) {
|
|||
|
||||
agent, err := _store.AgentFind(agentID)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
agent.Name = in.Name
|
||||
|
@ -201,12 +201,12 @@ func DeleteAgent(c *gin.Context) {
|
|||
|
||||
agent, err := _store.AgentFind(agentID)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if err = _store.AgentDelete(agent); err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error deleting user. %s", err)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -62,11 +62,7 @@ func GetBadge(c *gin.Context) {
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -107,7 +103,7 @@ func GetCC(c *gin.Context) {
|
|||
_store := store.FromContext(c)
|
||||
repo, err := _store.GetRepoName(c.Param("owner") + "/" + c.Param("name"))
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
cronScheduler "github.com/woodpecker-ci/woodpecker/server/cron"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
|
@ -48,7 +49,7 @@ func GetCron(c *gin.Context) {
|
|||
|
||||
cron, err := store.FromContext(c).CronFind(repo, id)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, cron)
|
||||
|
@ -75,7 +76,7 @@ func RunCron(c *gin.Context) {
|
|||
|
||||
cron, err := _store.CronFind(repo, id)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -182,7 +183,7 @@ func PatchCron(c *gin.Context) {
|
|||
|
||||
cron, err := _store.CronFind(repo, id)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if in.Branch != "" {
|
||||
|
@ -245,7 +246,7 @@ func GetCronList(c *gin.Context) {
|
|||
// @Summary Delete a cron job by id
|
||||
// @Router /repos/{repo_id}/cron/{cron} [delete]
|
||||
// @Produce plain
|
||||
// @Success 200
|
||||
// @Success 204
|
||||
// @Tags Repository cron jobs
|
||||
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
||||
// @Param repo_id path int true "the repository id"
|
||||
|
@ -258,8 +259,8 @@ func DeleteCron(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
if err := store.FromContext(c).CronDelete(repo, id); err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
|
@ -61,7 +62,7 @@ func GetGlobalSecret(c *gin.Context) {
|
|||
name := c.Param("secret")
|
||||
secret, err := server.Config.Services.Secrets.GlobalSecretFind(name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, secret.Copy())
|
||||
|
@ -122,7 +123,7 @@ func PatchGlobalSecret(c *gin.Context) {
|
|||
|
||||
secret, err := server.Config.Services.Secrets.GlobalSecretFind(name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if in.Value != "" {
|
||||
|
@ -159,8 +160,8 @@ func PatchGlobalSecret(c *gin.Context) {
|
|||
func DeleteGlobalSecret(c *gin.Context) {
|
||||
name := c.Param("secret")
|
||||
if err := server.Config.Services.Secrets.GlobalSecretDelete(name); err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -35,13 +35,13 @@ func handlePipelineErr(c *gin.Context, err error) {
|
|||
} else if errors.Is(err, &pipeline.ErrBadRequest{}) {
|
||||
c.String(http.StatusBadRequest, "%s", err)
|
||||
} else if errors.Is(err, &pipeline.ErrFiltered{}) {
|
||||
c.String(http.StatusNoContent, "%s", err)
|
||||
c.Status(http.StatusNoContent)
|
||||
} else {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleDbGetError(c *gin.Context, err error) {
|
||||
func handleDbError(c *gin.Context, err error) {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
|
|
|
@ -143,23 +143,20 @@ func PostHook(c *gin.Context) {
|
|||
|
||||
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("failure to get repo %s from store", tmpRepo.FullName)
|
||||
log.Error().Err(err).Msg(msg)
|
||||
c.String(http.StatusNotFound, msg)
|
||||
log.Error().Err(err).Msgf("failure to get repo %s from store", tmpRepo.FullName)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if !repo.IsActive {
|
||||
msg := fmt.Sprintf("ignoring hook: repo %s is inactive", tmpRepo.FullName)
|
||||
log.Debug().Msg(msg)
|
||||
c.String(http.StatusNoContent, msg)
|
||||
log.Debug().Msgf("ignoring hook: repo %s is inactive", tmpRepo.FullName)
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
oldFullName := repo.FullName
|
||||
|
||||
if repo.UserID == 0 {
|
||||
msg := fmt.Sprintf("ignoring hook. repo %s has no owner.", repo.FullName)
|
||||
log.Warn().Msg(msg)
|
||||
c.String(http.StatusNoContent, msg)
|
||||
log.Warn().Msgf("ignoring hook. repo %s has no owner.", repo.FullName)
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -220,9 +217,8 @@ func PostHook(c *gin.Context) {
|
|||
//
|
||||
|
||||
if tmpPipeline.Event == model.EventPull && !repo.AllowPull {
|
||||
msg := "ignoring hook: pull requests are disabled for this repo in woodpecker"
|
||||
log.Debug().Str("repo", repo.FullName).Msg(msg)
|
||||
c.String(http.StatusNoContent, msg)
|
||||
log.Debug().Str("repo", repo.FullName).Msg("ignoring hook: pull requests are disabled for this repo in woodpecker")
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -210,7 +210,7 @@ func GetLoginToken(c *gin.Context) {
|
|||
|
||||
user, err := _store.GetUserLogin(login)
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusNotFound, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -43,14 +43,14 @@ func PromHandler() gin.HandlerFunc {
|
|||
header := c.Request.Header.Get("Authorization")
|
||||
|
||||
if header == "" {
|
||||
c.String(401, errInvalidToken.Error())
|
||||
c.String(http.StatusUnauthorized, errInvalidToken.Error())
|
||||
return
|
||||
}
|
||||
|
||||
bearer := fmt.Sprintf("Bearer %s", token)
|
||||
|
||||
if header != bearer {
|
||||
c.String(401, errInvalidToken.Error())
|
||||
c.String(http.StatusForbidden, errInvalidToken.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -15,19 +15,18 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetOrg
|
||||
|
@ -50,11 +49,7 @@ func GetOrg(c *gin.Context) {
|
|||
|
||||
org, err := _store.OrgGet(orgID)
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -127,12 +122,7 @@ func LookupOrg(c *gin.Context) {
|
|||
|
||||
org, err := _store.OrgFindByName(orgFullName)
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -151,7 +141,7 @@ func LookupOrg(c *gin.Context) {
|
|||
perm, err := server.Config.Services.Membership.Get(c, user, org.Name)
|
||||
if err != nil {
|
||||
log.Error().Msgf("Failed to check membership: %v", err)
|
||||
c.String(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
|
@ -46,7 +47,7 @@ func GetOrgSecret(c *gin.Context) {
|
|||
|
||||
secret, err := server.Config.Services.Secrets.OrgSecretFind(orgID, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, secret.Copy())
|
||||
|
@ -152,7 +153,7 @@ func PatchOrgSecret(c *gin.Context) {
|
|||
|
||||
secret, err := server.Config.Services.Secrets.OrgSecretFind(orgID, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if in.Value != "" {
|
||||
|
@ -196,8 +197,8 @@ func DeleteOrgSecret(c *gin.Context) {
|
|||
}
|
||||
|
||||
if err := server.Config.Services.Secrets.OrgSecretDelete(orgID, name); err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -38,10 +38,10 @@ import (
|
|||
func GetOrgs(c *gin.Context) {
|
||||
orgs, err := store.FromContext(c).OrgList(session.Pagination(c))
|
||||
if err != nil {
|
||||
c.String(500, "Error getting user list. %s", err)
|
||||
c.String(http.StatusInternalServerError, "Error getting user list. %s", err)
|
||||
return
|
||||
}
|
||||
c.JSON(200, orgs)
|
||||
c.JSON(http.StatusOK, orgs)
|
||||
}
|
||||
|
||||
// DeleteOrg
|
||||
|
@ -49,8 +49,8 @@ func GetOrgs(c *gin.Context) {
|
|||
// @Summary Delete an org
|
||||
// @Description Deletes the given org. Requires admin rights.
|
||||
// @Router /orgs/{id} [delete]
|
||||
// @Produce json
|
||||
// @Success 204 {object} Org
|
||||
// @Produce plain
|
||||
// @Success 204
|
||||
// @Tags Orgs
|
||||
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
||||
// @Param id path string true "the org's id"
|
||||
|
@ -65,8 +65,9 @@ func DeleteOrg(c *gin.Context) {
|
|||
|
||||
err = _store.OrgDelete(orgID)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error deleting org %d. %s", orgID, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@ import (
|
|||
"github.com/woodpecker-ci/woodpecker/server/pipeline"
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
||||
)
|
||||
|
||||
// CreatePipeline
|
||||
|
@ -109,10 +108,6 @@ func GetPipelines(c *gin.Context) {
|
|||
|
||||
pipelines, err := store.FromContext(c).GetPipelineList(repo, session.Pagination(c))
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
@ -145,11 +140,7 @@ func GetPipeline(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if pl.Workflows, err = _store.WorkflowGetTree(pl); err != nil {
|
||||
|
@ -167,7 +158,7 @@ func GetPipelineLast(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineLast(repo, branch)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -203,7 +194,7 @@ func GetStepLogs(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -215,7 +206,7 @@ func GetStepLogs(c *gin.Context) {
|
|||
|
||||
step, err := _store.StepLoad(stepID)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -227,7 +218,7 @@ func GetStepLogs(c *gin.Context) {
|
|||
|
||||
logs, err := _store.LogFind(step)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -255,7 +246,7 @@ func GetPipelineConfig(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -286,7 +277,7 @@ func CancelPipeline(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -317,7 +308,7 @@ func PostApproval(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -349,7 +340,7 @@ func PostDecline(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "%v", err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -404,25 +395,17 @@ func PostPipeline(c *gin.Context) {
|
|||
|
||||
user, err := _store.GetUser(repo.UserID)
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
if errors.Is(err, types.RecordNotExist) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// refresh the token to make sure, pipeline.ReStart can still obtain the pipeline config if necessary again
|
||||
// refresh the token to make sure, pipeline.Restart can still obtain the pipeline config if necessary again
|
||||
refreshUserToken(c, user)
|
||||
|
||||
// make Deploy overridable
|
||||
|
@ -472,7 +455,7 @@ func PostPipeline(c *gin.Context) {
|
|||
// @Summary Deletes log
|
||||
// @Router /repos/{repo_id}/logs/{number} [post]
|
||||
// @Produce plain
|
||||
// @Success 200
|
||||
// @Success 204
|
||||
// @Tags Pipeline logs
|
||||
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
||||
// @Param repo_id path int true "the repository id"
|
||||
|
@ -485,13 +468,13 @@ func DeletePipelineLogs(c *gin.Context) {
|
|||
|
||||
pl, err := _store.GetPipelineNumber(repo, num)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
steps, err := _store.StepList(pl)
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusNotFound, err)
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -511,5 +494,5 @@ func DeletePipelineLogs(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func GetRegistry(c *gin.Context) {
|
|||
)
|
||||
registry, err := server.Config.Services.Registries.RegistryFind(repo, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(200, registry.Copy())
|
||||
|
@ -110,7 +110,7 @@ func PatchRegistry(c *gin.Context) {
|
|||
|
||||
registry, err := server.Config.Services.Registries.RegistryFind(repo, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if in.Username != "" {
|
||||
|
@ -168,7 +168,7 @@ func GetRegistryList(c *gin.Context) {
|
|||
// @Summary Delete a named registry
|
||||
// @Router /repos/{repo_id}/registry/{registry} [delete]
|
||||
// @Produce plain
|
||||
// @Success 200
|
||||
// @Success 204
|
||||
// @Tags Repository registries
|
||||
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
||||
// @Param repo_id path int true "the repository id"
|
||||
|
@ -180,8 +180,8 @@ func DeleteRegistry(c *gin.Context) {
|
|||
)
|
||||
err := server.Config.Services.Registries.RegistryDelete(repo, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -198,7 +198,7 @@ func PatchRepo(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
if in.IsTrusted != nil && *in.IsTrusted != repo.IsTrusted && !user.Admin {
|
||||
log.Trace().Msgf("user '%s' wants to make repo trusted without being an instance admin ", user.Login)
|
||||
log.Trace().Msgf("user '%s' wants to make repo trusted without being an instance admin", user.Login)
|
||||
c.String(http.StatusForbidden, "Insufficient privileges")
|
||||
return
|
||||
}
|
||||
|
@ -384,7 +384,7 @@ func DeleteRepo(c *gin.Context) {
|
|||
|
||||
if remove {
|
||||
if err := _store.DeleteRepo(repo); err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -464,7 +464,7 @@ func RepairRepo(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
c.Writer.WriteHeader(http.StatusOK)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// MoveRepo
|
||||
|
@ -485,7 +485,7 @@ func MoveRepo(c *gin.Context) {
|
|||
|
||||
to, exists := c.GetQuery("to")
|
||||
if !exists {
|
||||
err := fmt.Errorf("Missing required to query value")
|
||||
err := fmt.Errorf("missing required to query value")
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
@ -548,5 +548,5 @@ func MoveRepo(c *gin.Context) {
|
|||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
c.Writer.WriteHeader(http.StatusOK)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ func GetSecret(c *gin.Context) {
|
|||
)
|
||||
secret, err := server.Config.Services.Secrets.SecretFind(repo, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, secret.Copy())
|
||||
|
@ -111,7 +111,7 @@ func PatchSecret(c *gin.Context) {
|
|||
|
||||
secret, err := server.Config.Services.Secrets.SecretFind(repo, name)
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if in.Value != "" {
|
||||
|
@ -178,8 +178,8 @@ func DeleteSecret(c *gin.Context) {
|
|||
name = c.Param("secret")
|
||||
)
|
||||
if err := server.Config.Services.Secrets.SecretDelete(repo, name); err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNoContent, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -138,7 +138,7 @@ func GetRepos(c *gin.Context) {
|
|||
|
||||
// PostToken
|
||||
//
|
||||
// @Summary Return the token of the current user as stringª
|
||||
// @Summary Return the token of the current user as string
|
||||
// @Router /user/token [post]
|
||||
// @Produce plain
|
||||
// @Success 200
|
||||
|
|
|
@ -40,10 +40,10 @@ import (
|
|||
func GetUsers(c *gin.Context) {
|
||||
users, err := store.FromContext(c).GetUserList(session.Pagination(c))
|
||||
if err != nil {
|
||||
c.String(500, "Error getting user list. %s", err)
|
||||
c.String(http.StatusInternalServerError, "Error getting user list. %s", err)
|
||||
return
|
||||
}
|
||||
c.JSON(200, users)
|
||||
c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
// GetUser
|
||||
|
@ -59,10 +59,10 @@ func GetUsers(c *gin.Context) {
|
|||
func GetUser(c *gin.Context) {
|
||||
user, err := store.FromContext(c).GetUserLogin(c.Param("login"))
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(200, user)
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
// PatchUser
|
||||
|
@ -89,7 +89,7 @@ func PatchUser(c *gin.Context) {
|
|||
|
||||
user, err := _store.GetUserLogin(c.Param("login"))
|
||||
if err != nil {
|
||||
handleDbGetError(c, err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -149,8 +149,8 @@ func PostUser(c *gin.Context) {
|
|||
// @Summary Delete a user
|
||||
// @Description Deletes the given user. Requires admin rights.
|
||||
// @Router /users/{login} [delete]
|
||||
// @Produce json
|
||||
// @Success 200 {object} User
|
||||
// @Produce plain
|
||||
// @Success 204
|
||||
// @Tags Users
|
||||
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
||||
// @Param login path string true "the user's login name"
|
||||
|
@ -159,12 +159,12 @@ func DeleteUser(c *gin.Context) {
|
|||
|
||||
user, err := _store.GetUserLogin(c.Param("login"))
|
||||
if err != nil {
|
||||
c.String(404, "Cannot find user. %s", err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
if err = _store.DeleteUser(user); err != nil {
|
||||
c.String(500, "Error deleting user. %s", err)
|
||||
handleDbError(c, err)
|
||||
return
|
||||
}
|
||||
c.String(200, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -28,10 +28,10 @@ import (
|
|||
// Health
|
||||
//
|
||||
// @Summary Health information
|
||||
// @Description If everything is fine, just a 200 will be returned, a 500 signals server state is unhealthy.
|
||||
// @Description If everything is fine, just a 204 will be returned, a 500 signals server state is unhealthy.
|
||||
// @Router /healthz [get]
|
||||
// @Produce plain
|
||||
// @Success 200
|
||||
// @Success 204
|
||||
// @Failure 500
|
||||
// @Tags System
|
||||
func Health(c *gin.Context) {
|
||||
|
@ -39,7 +39,7 @@ func Health(c *gin.Context) {
|
|||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, "")
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// Version
|
||||
|
|
|
@ -28,7 +28,7 @@ import (
|
|||
// wrapGet return error if err not nil or if requested entry do not exist
|
||||
func wrapGet(exist bool, err error) error {
|
||||
if !exist {
|
||||
err = types.RecordNotExist
|
||||
return types.RecordNotExist
|
||||
}
|
||||
if err != nil {
|
||||
// we only ask for the function's name if needed, as it's not as preformatted as to just execute it
|
||||
|
@ -41,7 +41,7 @@ func wrapGet(exist bool, err error) error {
|
|||
// wrapDelete return error if err not nil or if requested entry do not exist
|
||||
func wrapDelete(c int64, err error) error {
|
||||
if c == 0 {
|
||||
err = types.RecordNotExist
|
||||
return types.RecordNotExist
|
||||
}
|
||||
if err != nil {
|
||||
// we only ask for the function's name if needed, as it's not as preformatted as to just execute it
|
||||
|
|
|
@ -156,6 +156,5 @@ func deletePipeline(sess *xorm.Session, pipelineID int64) error {
|
|||
if _, err := sess.Where("pipeline_id = ?", pipelineID).Delete(new(model.PipelineConfig)); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := sess.ID(pipelineID).Delete(new(model.Pipeline))
|
||||
return err
|
||||
return wrapDelete(sess.ID(pipelineID).Delete(new(model.Pipeline)))
|
||||
}
|
||||
|
|
|
@ -48,6 +48,5 @@ func (s storage) RegistryDelete(repo *model.Repo, addr string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.engine.ID(registry.ID).Delete(new(model.Registry))
|
||||
return err
|
||||
return wrapDelete(s.engine.ID(registry.ID).Delete(new(model.Registry)))
|
||||
}
|
||||
|
|
|
@ -131,7 +131,7 @@ func (s storage) DeleteRepo(repo *model.Repo) error {
|
|||
}
|
||||
}
|
||||
|
||||
if _, err := sess.ID(repo.ID).Delete(new(model.Repo)); err != nil {
|
||||
if err := wrapDelete(sess.ID(repo.ID).Delete(new(model.Repo))); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -105,6 +105,5 @@ func deleteStep(sess *xorm.Session, stepID int64) error {
|
|||
if _, err := sess.Where("step_id = ?", stepID).Delete(new(model.LogEntry)); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := sess.ID(stepID).Delete(new(model.Step))
|
||||
return err
|
||||
return wrapDelete(sess.ID(stepID).Delete(new(model.Step)))
|
||||
}
|
||||
|
|
|
@ -15,8 +15,9 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"xorm.io/xorm"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
)
|
||||
|
||||
func (s storage) GetUser(id int64) (*model.User, error) {
|
||||
|
|
Loading…
Reference in a new issue