Skip to content

Commit

Permalink
gitlab: Sort list of returned jobs
Browse files Browse the repository at this point in the history
CIJobs is expected to return jobs in the order they were created,
but jobs returned by ListPipelineJobs are actually ordered by ID
in descending order - at least in recent gitlab versions, I suspect
this changed at one point.

Restore the expected order by sorting the list before returning it.
  • Loading branch information
fmuellner committed Dec 11, 2020
1 parent e8b6600 commit f055edf
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -824,6 +825,14 @@ func ProjectList(opts gitlab.ListProjectsOptions, n int) ([]*gitlab.Project, err
return list, nil
}

type JobSorter struct{ Jobs []*gitlab.Job }

func (s JobSorter) Len() int { return len(s.Jobs) }
func (s JobSorter) Swap(i, j int) { s.Jobs[i], s.Jobs[j] = s.Jobs[j], s.Jobs[i] }
func (s JobSorter) Less(i, j int) bool {
return time.Time(*s.Jobs[i].CreatedAt).Before(time.Time(*s.Jobs[j].CreatedAt))
}

// CIJobs returns a list of jobs in a pipeline for a given sha. The jobs are
// returned sorted by their CreatedAt time
func CIJobs(pid interface{}, sha string) ([]*gitlab.Job, error) {
Expand Down Expand Up @@ -851,6 +860,11 @@ func CIJobs(pid interface{}, sha string) ([]*gitlab.Job, error) {
break
}
}

// ListPipelineJobs returns jobs sorted by ID in descending order,
// while we want them to be ordered chronologically
sort.Sort(JobSorter{list})

return list, nil
}

Expand Down

0 comments on commit f055edf

Please sign in to comment.