github.com/drone/runner-go@v1.12.0/pipeline/reporter/history/entry.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package history
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/drone/drone-go/drone"
    11  )
    12  
    13  // Entry represents a history entry.
    14  type Entry struct {
    15  	Stage   *drone.Stage `json:"stage"`
    16  	Build   *drone.Build `json:"build"`
    17  	Repo    *drone.Repo  `json:"repo"`
    18  	Created time.Time    `json:"created"`
    19  	Updated time.Time    `json:"updated"`
    20  }
    21  
    22  // ByTimestamp sorts a list of entries by timestamp
    23  type ByTimestamp []*Entry
    24  
    25  func (a ByTimestamp) Len() int      { return len(a) }
    26  func (a ByTimestamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    27  
    28  func (a ByTimestamp) Less(i, j int) bool {
    29  	return a[i].Stage.ID > a[j].Stage.ID
    30  }
    31  
    32  // ByStatus sorts a list of entries by status
    33  type ByStatus []*Entry
    34  
    35  func (a ByStatus) Len() int      { return len(a) }
    36  func (a ByStatus) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    37  
    38  func (a ByStatus) Less(i, j int) bool {
    39  	return order(a[i].Stage) < order(a[j].Stage)
    40  }
    41  
    42  func order(stage *drone.Stage) int64 {
    43  	switch stage.Status {
    44  	case drone.StatusPending:
    45  		return 0
    46  	case drone.StatusRunning:
    47  		return 1
    48  	default:
    49  		return 2
    50  	}
    51  }