Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status filter to list pipeline API #4494

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/server/openapi/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,12 @@ const docTemplate = `{
"description": "filter pipelines by strings contained in ref",
"name": "ref",
"in": "query"
},
{
"type": "string",
"description": "filter pipelines by status",
"name": "status",
"in": "query"
}
],
"responses": {
Expand Down
10 changes: 10 additions & 0 deletions server/api/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func createTmpPipeline(event model.WebhookEvent, commit *model.Commit, user *mod
// @Param branch query string false "filter pipelines by branch"
// @Param event query string false "filter pipelines by webhook events (comma separated)"
// @Param ref query string false "filter pipelines by strings contained in ref"
// @Param status query string false "filter pipelines by status"
func GetPipelines(c *gin.Context) {
repo := session.Repo(c)

Expand All @@ -142,6 +143,15 @@ func GetPipelines(c *gin.Context) {
filter.Events = wel
}

if status := c.Query("status"); status != "" {
ps := model.StatusValue(status)
if err := ps.Validate(); err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}
filter.Status = ps
}

if before := c.Query("before"); before != "" {
beforeDt, err := time.Parse(time.RFC3339, before)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions server/model/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ const (
StatusCreated StatusValue = "created" // created / internal use only
)

var ErrInvalidStatusValue = errors.New("invalid status value")

func (s StatusValue) Validate() error {
switch s {
case StatusSkipped, StatusPending, StatusRunning, StatusSuccess, StatusFailure, StatusKilled, StatusError, StatusBlocked, StatusDeclined, StatusCreated:
return nil
default:
return fmt.Errorf("%w: %s", ErrInvalidStatusValue, s)
}
}

// SCMKind represent different version control systems.
type SCMKind string // @name SCMKind

Expand Down
1 change: 1 addition & 0 deletions server/model/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type PipelineFilter struct {
Branch string
Events []WebhookEvent
RefContains string
Status StatusValue
}

// IsMultiPipeline checks if step list contain more than one parent step.
Expand Down
4 changes: 4 additions & 0 deletions server/store/datastore/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (s storage) GetPipelineList(repo *model.Repo, p *model.ListOptions, f *mode
cond = cond.And(builder.Eq{"branch": f.Branch})
}

if f.Status != "" {
cond = cond.And(builder.Eq{"status": f.Status})
}

if len(f.Events) != 0 {
cond = cond.And(builder.In("event", f.Events))
}
Expand Down
29 changes: 29 additions & 0 deletions server/store/datastore/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,35 @@ func TestPipelines(t *testing.T) {
g.Assert(pipelines[0].ID).Equal(pipeline1.ID)
g.Assert(pipelines[0].RepoID).Equal(pipeline1.RepoID)
})

g.It("Should get pipelines filtered by status", func() {
pipeline1 := &model.Pipeline{
RepoID: repo.ID,
Status: model.StatusSuccess,
}
pipeline2 := &model.Pipeline{
RepoID: repo.ID,
Status: model.StatusFailure,
}
pipeline3 := &model.Pipeline{
RepoID: repo.ID,
Status: model.StatusRunning,
}
err1 := store.CreatePipeline(pipeline1, []*model.Step{}...)
g.Assert(err1).IsNil()
err2 := store.CreatePipeline(pipeline2, []*model.Step{}...)
g.Assert(err2).IsNil()
err3 := store.CreatePipeline(pipeline3, []*model.Step{}...)
g.Assert(err3).IsNil()

pipelines, err := store.GetPipelineList(&model.Repo{ID: 1}, nil, &model.PipelineFilter{
Status: model.StatusSuccess,
})
g.Assert(err).IsNil()
g.Assert(len(pipelines)).Equal(1)
g.Assert(pipelines[0].ID).Equal(pipeline1.ID)
g.Assert(pipelines[0].Status).Equal(model.StatusSuccess)
})
})
}

Expand Down