forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate.go
More file actions
110 lines (93 loc) · 2.48 KB
/
aggregate.go
File metadata and controls
110 lines (93 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package checks
import (
"fmt"
"sort"
"time"
"github.com/cli/cli/v2/api"
)
type check struct {
Name string `json:"name"`
State string `json:"state"`
StartedAt time.Time `json:"startedAt"`
CompletedAt time.Time `json:"completedAt"`
Link string `json:"link"`
Bucket string `json:"bucket"`
}
type checkCounts struct {
Failed int
Passed int
Pending int
Skipping int
}
func aggregateChecks(pr *api.PullRequest) ([]check, checkCounts, error) {
checks := []check{}
counts := checkCounts{}
if len(pr.StatusCheckRollup.Nodes) == 0 {
return checks, counts, fmt.Errorf("no commit found on the pull request")
}
rollup := pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes
if len(rollup) == 0 {
return checks, counts, fmt.Errorf("no checks reported on the '%s' branch", pr.HeadRefName)
}
checkContexts := pr.StatusCheckRollup.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes
for _, c := range eliminateDuplicates(checkContexts) {
state := c.State
if state == "" {
if c.Status == "COMPLETED" {
state = c.Conclusion
} else {
state = c.Status
}
}
link := c.DetailsURL
if link == "" {
link = c.TargetURL
}
name := c.Name
if name == "" {
name = c.Context
}
item := check{
Name: name,
State: state,
StartedAt: c.StartedAt,
CompletedAt: c.CompletedAt,
Link: link,
}
switch state {
case "SUCCESS":
item.Bucket = "pass"
counts.Passed++
case "SKIPPED", "NEUTRAL":
item.Bucket = "skipping"
counts.Skipping++
case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED":
item.Bucket = "fail"
counts.Failed++
default: // "EXPECTED", "REQUESTED", "WAITING", "QUEUED", "PENDING", "IN_PROGRESS", "STALE"
item.Bucket = "pending"
counts.Pending++
}
checks = append(checks, item)
}
return checks, counts, nil
}
func eliminateDuplicates(checkContexts []api.CheckContext) []api.CheckContext {
// To return the most recent check, sort in descending order by StartedAt.
sort.Slice(checkContexts, func(i, j int) bool { return checkContexts[i].StartedAt.After(checkContexts[j].StartedAt) })
m := make(map[string]struct{})
unique := make([]api.CheckContext, 0, len(checkContexts))
// Eliminate duplicates using Name or Context.
for _, ctx := range checkContexts {
key := ctx.Name
if key == "" {
key = ctx.Context
}
if _, ok := m[key]; ok {
continue
}
unique = append(unique, ctx)
m[key] = struct{}{}
}
return unique
}