Skip to content

Commit

Permalink
get last 10 scheduled pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
marriva committed Aug 8, 2023
1 parent 8600fc5 commit 508fd27
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions cmd/projects/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package projects

import (
"fmt"
"math"
"os"
"regexp"
"strings"
"text/tabwriter"

go_sort "sort"

"github.com/flant/glaball/pkg/client"
"github.com/flant/glaball/pkg/limiter"
"github.com/flant/glaball/pkg/sort/v2"
Expand All @@ -19,6 +22,10 @@ import (
"github.com/xanzy/go-gitlab"
)

const (
pipelinesCount = 10
)

var (
listProjectsPipelinesOptions = gitlab.ListProjectsOptions{ListOptions: gitlab.ListOptions{PerPage: 100}}
active *bool
Expand Down Expand Up @@ -585,18 +592,21 @@ filter:
if active == nil && status == nil {
data <- sort.Element{
Host: h,
Struct: ProjectPipelineSchedule{project, nil},
Struct: ProjectPipelineSchedule{project, nil, nil},
Cached: resp.Header.Get("X-From-Cache") == "1"}
}
} else {
for _, v := range filteredList {
// get entire pipeline schedule to make lastpipeline struct accessible
// note: init new variables with the same names
wg.Lock()
v, resp, err := h.Client.PipelineSchedules.GetPipelineSchedule(project.ID, v.ID, options...)
if err != nil {
wg.Error(h, err)
wg.Unlock()
continue
}
wg.Unlock()
// check pipeline schedule state
if active != nil && v.Active != *active {
continue
Expand All @@ -606,10 +616,57 @@ filter:
if status != nil && (v.LastPipeline.Status == "" || v.LastPipeline.Status != *status) {
continue
}

// get last pipelines
perPage := 100

wg.Lock()
pipelines, resp, err := h.Client.PipelineSchedules.ListPipelinesTriggeredBySchedule(project.ID, v.ID, &gitlab.ListPipelinesTriggeredByScheduleOptions{PerPage: perPage}, options...)
if err != nil {
wg.Error(h, err)
wg.Unlock()
continue
}
wg.Unlock()

if resp.TotalPages > 1 {
// count last page
// https://gitlab.com/gitlab-org/gitlab/-/issues/369095
for perPage > pipelinesCount {
if resp.TotalItems%perPage > pipelinesCount {
break
}
perPage--
}
lastPage := math.Ceil(float64(resp.TotalItems) / float64(perPage))

wg.Lock()
pipelines, resp, err = h.Client.PipelineSchedules.ListPipelinesTriggeredBySchedule(project.ID, v.ID, &gitlab.ListPipelinesTriggeredByScheduleOptions{
Page: int(lastPage),
PerPage: perPage,
}, options...)
if err != nil {
wg.Error(h, err)
wg.Unlock()
continue
}
wg.Unlock()
}

if len(pipelines) > pipelinesCount {
pipelines = pipelines[len(pipelines)-pipelinesCount:]
}

// sort descending
// https://gitlab.com/gitlab-org/gitlab/-/issues/369095
go_sort.Slice(pipelines, func(i, j int) bool {
return pipelines[i].ID >= pipelines[j].ID
})

// push result to channel
data <- sort.Element{
Host: h,
Struct: ProjectPipelineSchedule{project, v},
Struct: ProjectPipelineSchedule{project, v, pipelines},
Cached: resp.Header.Get("X-From-Cache") == "1"}
}
}
Expand All @@ -622,8 +679,9 @@ filter:
}

type ProjectPipelineSchedule struct {
Project *gitlab.Project `json:"project,omitempty"`
Schedule *gitlab.PipelineSchedule `json:"schedule,omitempty"`
Project *gitlab.Project `json:"project,omitempty"`
Schedule *gitlab.PipelineSchedule `json:"schedule,omitempty"`
Pipelines []*gitlab.Pipeline `json:"pipelines,omitempty"`
}

type Schedules []*gitlab.PipelineSchedule
Expand Down

0 comments on commit 508fd27

Please sign in to comment.