Skip to content

Commit

Permalink
Sort Buildkite pipelines and branches alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
jmks committed Oct 7, 2019
1 parent c5b36d8 commit 61bfd97
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 61 deletions.
120 changes: 120 additions & 0 deletions modules/buildkite/pipelines_display_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package buildkite

import (
"fmt"
"sort"
"strings"
)

type pipelinesDisplayData struct {
buildsForPipeline map[string][]Build
orderedPipelines []string
}

func (data *pipelinesDisplayData) Content() string {
maxPipelineLength := getLongestLength(data.orderedPipelines)

str := ""
for _, pipeline := range data.orderedPipelines {
str += fmt.Sprintf("[white]%s", padRight(pipeline, maxPipelineLength))
for _, build := range data.buildsForPipeline[pipeline] {
str += fmt.Sprintf(" [%s]%s[white]", buildColor(build.State), build.Branch)
}
str += "\n"
}

return str
}

func NewPipelinesDisplayData(builds []Build) pipelinesDisplayData {
grouped := make(map[string][]Build)

for _, build := range builds {
if _, ok := grouped[build.Pipeline.Slug]; ok {
grouped[build.Pipeline.Slug] = append(grouped[build.Pipeline.Slug], build)
} else {
grouped[build.Pipeline.Slug] = []Build{}
grouped[build.Pipeline.Slug] = append(grouped[build.Pipeline.Slug], build)
}
}

orderedPipelines := make([]string, len(grouped))
i := 0
for pipeline := range grouped {
orderedPipelines[i] = pipeline
i++
}
sort.Strings(orderedPipelines)

name := func(b1, b2 *Build) bool {
return b1.Branch < b2.Branch
}
for _, builds := range grouped {
ByBuild(name).Sort(builds)
}

return pipelinesDisplayData{
buildsForPipeline: grouped,
orderedPipelines: orderedPipelines,
}
}

type ByBuild func(b1, b2 *Build) bool

func (by ByBuild) Sort(builds []Build) {
sorter := &buildSorter{
builds: builds,
by: by,
}
sort.Sort(sorter)
}

type buildSorter struct {
builds []Build
by func(b1, b2 *Build) bool
}

func (bs *buildSorter) Len() int {
return len(bs.builds)
}

func (bs *buildSorter) Swap(i, j int) {
bs.builds[i], bs.builds[j] = bs.builds[j], bs.builds[i]
}

func (bs *buildSorter) Less(i, j int) bool {
return bs.by(&bs.builds[i], &bs.builds[j])
}

func getLongestLength(strs []string) int {
longest := 0

for _, str := range strs {
if len(str) > longest {
longest = len(str)
}
}

return longest
}

func padRight(text string, length int) string {
padLength := length - len(text)

if padLength <= 0 {
return text[:length]
}

return text + strings.Repeat(" ", padLength)
}

func buildColor(state string) string {
switch state {
case "passed":
return "green"
case "failed":
return "red"
default:
return "yellow"
}
}
63 changes: 2 additions & 61 deletions modules/buildkite/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
"strings"
)

const HelpText = `
Expand Down Expand Up @@ -65,65 +64,7 @@ func (widget *Widget) content() (string, string, bool) {
return title, widget.err.Error(), true
}

pipelineData := groupByPipeline(widget.builds)
maxPipelineLength := getLongestPipelineLength(widget.builds)
displayData := NewPipelinesDisplayData(widget.builds)

str := ""
for pipeline, builds := range pipelineData {
str += fmt.Sprintf("[white]%s", padRight(pipeline, maxPipelineLength))
for _, build := range builds {
str += fmt.Sprintf(" [%s]%s[white]", buildColor(build.State), build.Branch)
}
str += "\n"
}

return title, str, false
}

func groupByPipeline(builds []Build) map[string][]Build {
grouped := make(map[string][]Build)

for _, build := range builds {
if _, ok := grouped[build.Pipeline.Slug]; ok {
grouped[build.Pipeline.Slug] = append(grouped[build.Pipeline.Slug], build)
} else {
grouped[build.Pipeline.Slug] = []Build{}
grouped[build.Pipeline.Slug] = append(grouped[build.Pipeline.Slug], build)
}
}

return grouped
}

func getLongestPipelineLength(builds []Build) int {
maxPipelineLength := 0

for _, build := range builds {
if len(build.Pipeline.Slug) > maxPipelineLength {
maxPipelineLength = len(build.Pipeline.Slug)
}
}

return maxPipelineLength
}

func padRight(text string, length int) string {
padLength := length - len(text)

if padLength <= 0 {
return text[:length]
}

return text + strings.Repeat(" ", padLength)
}

func buildColor(state string) string {
switch state {
case "passed":
return "green"
case "failed":
return "red"
default:
return "yellow"
}
return title, displayData.Content(), false
}

0 comments on commit 61bfd97

Please sign in to comment.