@@ -2,6 +2,8 @@ package sdk
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
6
+ "sort"
5
7
"time"
6
8
)
7
9
@@ -15,6 +17,28 @@ const (
15
17
type WorkflowRunResultType string
16
18
type WorkflowRunResultDataKey string
17
19
20
+ type WorkflowRunResults []WorkflowRunResult
21
+
22
+ // Unique returns the last version of each results
23
+ func (w WorkflowRunResults ) Unique () (WorkflowRunResults , error ) {
24
+ m := make (map [string ]WorkflowRunResult , len (w ))
25
+ for i := range w {
26
+ key , err := w [i ].ComputeUniqueKey ()
27
+ if err != nil {
28
+ return nil , err
29
+ }
30
+ if v , ok := m [key ]; ! ok || v .SubNum < w [i ].SubNum {
31
+ m [key ] = w [i ]
32
+ }
33
+ }
34
+ filtered := make (WorkflowRunResults , 0 , len (m ))
35
+ for _ , v := range m {
36
+ filtered = append (filtered , v )
37
+ }
38
+ sort .Slice (filtered , func (i , j int ) bool { return filtered [i ].Created .Before (filtered [j ].Created ) })
39
+ return filtered , nil
40
+ }
41
+
18
42
type WorkflowRunResult struct {
19
43
ID string `json:"id" db:"id"`
20
44
Created time.Time `json:"created" db:"created"`
@@ -26,12 +50,47 @@ type WorkflowRunResult struct {
26
50
DataRaw json.RawMessage `json:"data" db:"data"`
27
51
}
28
52
53
+ func (r WorkflowRunResult ) ComputeUniqueKey () (string , error ) {
54
+ key := fmt .Sprintf ("%d-%s" , r .WorkflowRunID , r .Type )
55
+ switch r .Type {
56
+ case WorkflowRunResultTypeArtifactManager :
57
+ var data WorkflowRunResultArtifactManager
58
+ if err := json .Unmarshal (r .DataRaw , & data ); err != nil {
59
+ return "" , WithStack (err )
60
+ }
61
+ key = key + "-" + data .Name + "-" + data .RepoType
62
+ default :
63
+ var data WorkflowRunResultArtifactCommon
64
+ if err := json .Unmarshal (r .DataRaw , & data ); err != nil {
65
+ return "" , WithStack (err )
66
+ }
67
+ key = key + "-" + data .Name
68
+ }
69
+ return key , nil
70
+ }
71
+
72
+ func (r WorkflowRunResult ) ComputeName () (string , error ) {
73
+ switch r .Type {
74
+ case WorkflowRunResultTypeArtifactManager :
75
+ var data WorkflowRunResultArtifactManager
76
+ if err := json .Unmarshal (r .DataRaw , & data ); err != nil {
77
+ return "" , WithStack (err )
78
+ }
79
+ return fmt .Sprintf ("%s (%s: %s)" , data .Name , r .Type , data .RepoType ), nil
80
+ default :
81
+ var data WorkflowRunResultArtifactCommon
82
+ if err := json .Unmarshal (r .DataRaw , & data ); err != nil {
83
+ return "" , WithStack (err )
84
+ }
85
+ return fmt .Sprintf ("%s (%s)" , data .Name , r .Type ), nil
86
+ }
87
+ }
88
+
29
89
func (r * WorkflowRunResult ) GetArtifact () (WorkflowRunResultArtifact , error ) {
30
90
var data WorkflowRunResultArtifact
31
91
if err := JSONUnmarshal (r .DataRaw , & data ); err != nil {
32
92
return data , WithStack (err )
33
93
}
34
-
35
94
return data , nil
36
95
}
37
96
@@ -70,8 +129,12 @@ type WorkflowRunResultCheck struct {
70
129
ResultType WorkflowRunResultType `json:"result_type"`
71
130
}
72
131
132
+ type WorkflowRunResultArtifactCommon struct {
133
+ Name string `json:"name"`
134
+ }
135
+
73
136
type WorkflowRunResultArtifactManager struct {
74
- Name string `json:"name"`
137
+ WorkflowRunResultArtifactCommon
75
138
Size int64 `json:"size"`
76
139
MD5 string `json:"md5"`
77
140
Path string `json:"path"`
@@ -98,7 +161,7 @@ func (a *WorkflowRunResultArtifactManager) IsValid() error {
98
161
}
99
162
100
163
type WorkflowRunResultStaticFile struct {
101
- Name string `json:"name"`
164
+ WorkflowRunResultArtifactCommon
102
165
RemoteURL string `json:"remote_url"`
103
166
}
104
167
@@ -113,7 +176,7 @@ func (a *WorkflowRunResultStaticFile) IsValid() error {
113
176
}
114
177
115
178
type WorkflowRunResultArtifact struct {
116
- Name string `json:"name"`
179
+ WorkflowRunResultArtifactCommon
117
180
Size int64 `json:"size"`
118
181
MD5 string `json:"md5"`
119
182
CDNRefHash string `json:"cdn_hash"`
@@ -138,7 +201,7 @@ func (a *WorkflowRunResultArtifact) IsValid() error {
138
201
}
139
202
140
203
type WorkflowRunResultCoverage struct {
141
- Name string `json:"name"`
204
+ WorkflowRunResultArtifactCommon
142
205
Size int64 `json:"size"`
143
206
MD5 string `json:"md5"`
144
207
CDNRefHash string `json:"cdn_hash"`
0 commit comments