-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstainlessutils.go
More file actions
265 lines (237 loc) · 6.3 KB
/
stainlessutils.go
File metadata and controls
265 lines (237 loc) · 6.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package stainlessutils
import (
"fmt"
"slices"
"github.com/stainless-api/stainless-api-go"
"github.com/tidwall/gjson"
)
// Build wraps stainless.Build to provide convenience methods
type Build struct {
stainless.Build
}
var GOOD_COMMIT_CONCLUSIONS = []string{"error", "warning", "note", "success"}
func IsGoodCommitConclusion(conclusion string) bool {
return slices.Contains(GOOD_COMMIT_CONCLUSIONS, conclusion)
}
// NewBuild creates a new Build wrapper
func NewBuild(build stainless.Build) *Build {
return &Build{Build: build}
}
// BuildTarget returns the build target wrapper for a given target type, replacing getBuildTarget
func (b *Build) BuildTarget(target stainless.Target) *BuildTarget {
switch target {
case "node":
if b.Targets.JSON.Node.Valid() {
return NewBuildTarget(&b.Targets.Node, target)
}
case "typescript":
if b.Targets.JSON.Typescript.Valid() {
return NewBuildTarget(&b.Targets.Typescript, target)
}
case "python":
if b.Targets.JSON.Python.Valid() {
return NewBuildTarget(&b.Targets.Python, target)
}
case "go":
if b.Targets.JSON.Go.Valid() {
return NewBuildTarget(&b.Targets.Go, target)
}
case "java":
if b.Targets.JSON.Java.Valid() {
return NewBuildTarget(&b.Targets.Java, target)
}
case "kotlin":
if b.Targets.JSON.Kotlin.Valid() {
return NewBuildTarget(&b.Targets.Kotlin, target)
}
case "ruby":
if b.Targets.JSON.Ruby.Valid() {
return NewBuildTarget(&b.Targets.Ruby, target)
}
case "terraform":
if b.Targets.JSON.Terraform.Valid() {
return NewBuildTarget(&b.Targets.Terraform, target)
}
case "cli":
if b.Targets.JSON.Cli.Valid() {
return NewBuildTarget(&b.Targets.Cli, target)
}
case "php":
if b.Targets.JSON.Php.Valid() {
return NewBuildTarget(&b.Targets.Php, target)
}
case "csharp":
if b.Targets.JSON.Csharp.Valid() {
return NewBuildTarget(&b.Targets.Csharp, target)
}
}
return nil
}
// Languages returns all available build languages/targets for this build
func (b *Build) Languages() []stainless.Target {
var languages []stainless.Target
targets := b.Targets
if targets.JSON.Node.Valid() {
languages = append(languages, "node")
}
if targets.JSON.Typescript.Valid() {
languages = append(languages, "typescript")
}
if targets.JSON.Python.Valid() {
languages = append(languages, "python")
}
if targets.JSON.Go.Valid() {
languages = append(languages, "go")
}
if targets.JSON.Java.Valid() {
languages = append(languages, "java")
}
if targets.JSON.Kotlin.Valid() {
languages = append(languages, "kotlin")
}
if targets.JSON.Ruby.Valid() {
languages = append(languages, "ruby")
}
if targets.JSON.Terraform.Valid() {
languages = append(languages, "terraform")
}
if targets.JSON.Cli.Valid() {
languages = append(languages, "cli")
}
if targets.JSON.Php.Valid() {
languages = append(languages, "php")
}
if targets.JSON.Csharp.Valid() {
languages = append(languages, "csharp")
}
if targets.JSON.OpenAPI.Valid() {
languages = append(languages, "openapi")
}
return languages
}
// IsCompleted checks if the entire build is completed (all targets)
func (b *Build) IsCompleted() bool {
languages := b.Languages()
for _, target := range languages {
buildTarget := b.BuildTarget(target)
if buildTarget == nil || !buildTarget.IsCompleted() {
return false
}
}
return true
}
// BuildTarget wraps stainless.BuildTarget to provide convenience methods
type BuildTarget struct {
*stainless.BuildTarget
target stainless.Target
}
// NewBuildTarget creates a new BuildTarget wrapper
func NewBuildTarget(buildTarget *stainless.BuildTarget, target stainless.Target) *BuildTarget {
if buildTarget == nil {
return nil
}
return &BuildTarget{
BuildTarget: buildTarget,
target: target,
}
}
// Target returns the target type (node, python, etc.)
func (bt *BuildTarget) Target() stainless.Target {
return bt.target
}
// StepUnion returns the step union for a given step name
func (bt *BuildTarget) StepUnion(step string) any {
if bt.BuildTarget == nil {
return nil
}
switch step {
case "commit":
if bt.JSON.Commit.Valid() {
return bt.Commit
}
case "lint":
if bt.JSON.Lint.Valid() {
return bt.Lint
}
case "build":
if bt.JSON.Build.Valid() {
return bt.Build
}
case "test":
if bt.JSON.Test.Valid() {
return bt.Test
}
}
return nil
}
// StepInfo extracts status, url, and conclusion from a step union
func (bt *BuildTarget) StepInfo(step string) (status, url, conclusion string) {
stepUnion := bt.StepUnion(step)
if stepUnion == nil {
return "", "", ""
}
if u, ok := stepUnion.(stainless.BuildTargetCommitUnion); ok {
status = u.Status
if u.Status == "completed" {
conclusion = u.Conclusion
// Use merge conflict PR URL if available, otherwise use commit URL
if u.JSON.MergeConflictPr.Valid() {
url = fmt.Sprintf("https://github.com/%s/%s/pull/%.0f",
u.MergeConflictPr.Repo.Owner,
u.MergeConflictPr.Repo.Name,
u.MergeConflictPr.Number)
} else if u.JSON.Commit.Valid() {
url = fmt.Sprintf("https://github.com/%s/%s/commit/%s",
u.Commit.Repo.Owner,
u.Commit.Repo.Name,
u.Commit.Sha)
}
}
}
if u, ok := stepUnion.(stainless.CheckStepUnion); ok {
status = u.Status
url = u.URL
if u.Status == "completed" {
conclusion = u.Conclusion
}
}
return
}
// Steps returns all available steps for this build target
func (bt *BuildTarget) Steps() []string {
if bt.BuildTarget == nil {
return []string{}
}
var steps []string
if gjson.Get(bt.RawJSON(), "commit").Exists() {
steps = append(steps, "commit")
}
if gjson.Get(bt.RawJSON(), "lint").Exists() {
steps = append(steps, "lint")
}
if gjson.Get(bt.RawJSON(), "build").Exists() {
steps = append(steps, "build")
}
if gjson.Get(bt.RawJSON(), "test").Exists() {
steps = append(steps, "test")
}
return steps
}
func (bt *BuildTarget) IsCompleted() bool {
return bt.Status == "completed"
}
func (bt *BuildTarget) IsInProgress() bool {
return bt.Status != "completed"
}
func (bt *BuildTarget) IsCommitCompleted() bool {
status, _, _ := bt.StepInfo("commit")
return status == "completed"
}
func (bt *BuildTarget) IsGoodCommitConclusion() bool {
_, _, conclusion := bt.StepInfo("commit")
return IsGoodCommitConclusion(conclusion)
}
func (bt *BuildTarget) IsCommitFailed() bool {
status, _, conclusion := bt.StepInfo("commit")
return status == "completed" && conclusion == "fatal"
}