Skip to content

Commit

Permalink
Merge pull request #198 from xuzhu-591/fix-500
Browse files Browse the repository at this point in the history
fix: got http code 500 if resoureces have not been fetched
  • Loading branch information
xuzhu-591 authored Oct 19, 2023
2 parents f265604 + 4f2b1ad commit 2e4b1ad
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 295 deletions.
12 changes: 11 additions & 1 deletion core/controller/pipelinerun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/horizoncd/horizon/pkg/config/token"
envmanager "github.com/horizoncd/horizon/pkg/environment/manager"
perror "github.com/horizoncd/horizon/pkg/errors"
eventmodels "github.com/horizoncd/horizon/pkg/event/models"
eventservice "github.com/horizoncd/horizon/pkg/event/service"
membermanager "github.com/horizoncd/horizon/pkg/member"
"github.com/horizoncd/horizon/pkg/param"
prmanager "github.com/horizoncd/horizon/pkg/pr/manager"
Expand Down Expand Up @@ -93,6 +95,7 @@ type controller struct {
commitGetter code.GitGetter
clusterGitRepo gitrepo.ClusterGitRepo
userMgr usermanager.Manager
eventSvc eventservice.Service
}

var _ Controller = (*controller)(nil)
Expand All @@ -113,6 +116,7 @@ func NewController(config *config.Config, param *param.Param) Controller {
clusterGitRepo: param.ClusterGitRepo,
userMgr: param.UserMgr,
templateReleaseMgr: param.TemplateReleaseMgr,
eventSvc: param.EventSvc,
}
}

Expand Down Expand Up @@ -496,7 +500,13 @@ func (c *controller) Cancel(ctx context.Context, pipelinerunID uint) error {
if pr.Status != string(prmodels.StatusPending) && pr.Status != string(prmodels.StatusReady) {
return perror.Wrapf(herrors.ErrParamInvalid, "pipelinerun is not pending or ready to cancel")
}
return c.prMgr.PipelineRun.UpdateStatusByID(ctx, pipelinerunID, prmodels.StatusCancelled)
err = c.prMgr.PipelineRun.UpdateStatusByID(ctx, pipelinerunID, prmodels.StatusCancelled)
if err != nil {
return err
}
c.eventSvc.CreateEventIgnoreError(ctx, common.ResourcePipelinerun, pipelinerunID,
eventmodels.PipelinerunCancelled, nil)
return nil
}

func (c *controller) ListCheckRuns(ctx context.Context, pipelinerunID uint) ([]*prmodels.CheckRun, error) {
Expand Down
7 changes: 7 additions & 0 deletions core/http/api/v1/pipelinerun/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (

"github.com/horizoncd/horizon/core/common"
prctl "github.com/horizoncd/horizon/core/controller/pipelinerun"
herrors "github.com/horizoncd/horizon/core/errors"
"github.com/horizoncd/horizon/lib/q"
"github.com/horizoncd/horizon/pkg/cluster/tekton/collector"
perror "github.com/horizoncd/horizon/pkg/errors"
"github.com/horizoncd/horizon/pkg/server/request"
"github.com/horizoncd/horizon/pkg/server/response"
"github.com/horizoncd/horizon/pkg/server/rpcerror"
"github.com/horizoncd/horizon/pkg/util/errors"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -119,6 +122,10 @@ func (a *API) GetDiff(c *gin.Context) {
}
diff, err := a.prCtl.GetDiff(c, uint(pipelinerunID))
if err != nil {
if _, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok {
response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error()))
return
}
response.AbortWithError(c, err)
return
}
Expand Down
4 changes: 4 additions & 0 deletions core/http/api/v2/pipelinerun/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func (a *API) GetDiff(c *gin.Context) {
a.withPipelinerunID(c, func(pipelinerunID uint) {
diff, err := a.prCtl.GetDiff(c, uint(pipelinerunID))
if err != nil {
if _, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok {
response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error()))
return
}
response.AbortWithError(c, err)
return
}
Expand Down
6 changes: 6 additions & 0 deletions core/http/api/v2/tag/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func (a *API) Update(c *gin.Context) {
response.AbortWithRPCError(c, rpcerror.ParamError.WithErrMsg(err.Error()))
return
}
if e, ok := perror.Cause(err).(*herrors.HorizonErrNotFound); ok {
if e.Source == herrors.ClusterInDB || e.Source == herrors.ApplicationInDB {
response.AbortWithRPCError(c, rpcerror.NotFoundError.WithErrMsg(err.Error()))
return
}
}
log.WithFiled(c, "op", op).Errorf("%+v", err)
response.AbortWithRPCError(c, rpcerror.InternalError.WithErrMsg(err.Error()))
return
Expand Down
80 changes: 40 additions & 40 deletions openapi/v2/restful/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ paths:
content:
application/json:
schema:
properties:
properties:
data:
$ref: "#/components/schemas/PipelinerunIDResponse"
default:
Expand Down Expand Up @@ -1121,7 +1121,7 @@ paths:
description: OK
content:
application/json:
schema: {}
schema: { }
delete:
summary: Remove cluster from favorites
operationId: removeFavoriteCluster
Expand All @@ -1130,7 +1130,7 @@ paths:
description: OK
content:
application/json:
schema: {}
schema: { }
/apis/core/v2/clusters/{clusterId}/pipelineruns:
parameters:
- name: clusterId
Expand All @@ -1141,41 +1141,41 @@ paths:
type: integer
example: 60517
post:
summary: Create a pipelinerun for a cluster
operationId: createPipelinerun
tags: [cluster]
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
action:
type: string
enum: [builddeploy, deploy, rollback]
description: type of pipelinerun
title:
type: string
description: title of pipelinerun
description:
type: string
description: description of pipelinerun
git:
type: object
properties:
branch:
type: string
description: branch of git repository
imageTag:
type: string
description: image tag of a image
pipelinerunID:
type: number
description: id of pipelinerun
responses:
'200':
description: OK
summary: Create a pipelinerun for a cluster
operationId: createPipelinerun
tags: [ cluster ]
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
action:
type: string
enum: [ builddeploy, deploy, rollback ]
description: type of pipelinerun
title:
type: string
description: title of pipelinerun
description:
type: string
description: description of pipelinerun
git:
type: object
properties:
branch:
type: string
description: branch of git repository
imageTag:
type: string
description: image tag of a image
pipelinerunID:
type: number
description: id of pipelinerun
responses:
'200':
description: OK


components:
Expand Down Expand Up @@ -1339,7 +1339,7 @@ components:
$ref: "#/components/schemas/GitResponse"
type:
type: string
enum: [workload, database, middleware, other, v1]
enum: [ workload, database, middleware, other, v1 ]
description: type of template related to cluster
tags:
$ref: "#/components/schemas/Tags"
Expand Down Expand Up @@ -1497,7 +1497,7 @@ components:
properties:
pipelinerunID:
$ref: "#/components/schemas/PipelinerunID"

DeployRequest:
type: object
properties:
Expand Down
Loading

0 comments on commit 2e4b1ad

Please sign in to comment.