-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdescribe_affected.go
More file actions
590 lines (531 loc) · 19.2 KB
/
describe_affected.go
File metadata and controls
590 lines (531 loc) · 19.2 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package exec
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/go-git/go-git/v5/plumbing"
giturl "github.com/kubescape/go-git-url"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/internal/tui/templates/term"
"github.com/cloudposse/atmos/pkg/auth"
"github.com/cloudposse/atmos/pkg/ci"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/data"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/pager"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/pro"
"github.com/cloudposse/atmos/pkg/pro/dtos"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
u "github.com/cloudposse/atmos/pkg/utils"
)
var ErrRepoPathConflict = errors.New("if the '--repo-path' flag is specified, the '--base', '--ref', '--sha', '--ssh-key' and '--ssh-key-password' flags can't be used")
type DescribeAffectedExecCreator func(atmosConfig *schema.AtmosConfiguration) DescribeAffectedExec
type DescribeAffectedCmdArgs struct {
CLIConfig *schema.AtmosConfiguration
Base string // Unified base commit (ref or SHA). Takes precedence over Ref/SHA.
CloneTargetRef bool
Format string
IncludeDependents bool
IncludeSettings bool
IncludeSpaceliftAdminStacks bool
OutputFile string
GithubOutputFile string // Output file for $GITHUB_OUTPUT format (key=value).
Ref string
RepoPath string
SHA string
SSHKeyPath string
SSHKeyPassword string
Verbose bool
Upload bool
Stack string
Query string
ProcessTemplates bool
ProcessYamlFunctions bool
Skip []string
ExcludeLocked bool
AuthManager auth.AuthManager // Optional: Auth manager for credential management (from --identity flag).
HeadSHAOverride string // PR head SHA from CI event payload, used for upload correlation with Atmos Pro.
CIEventType string // CI event type (e.g., "pull_request", "push") for upload validation.
}
//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE
type DescribeAffectedExec interface {
Execute(*DescribeAffectedCmdArgs) error
}
type describeAffectedExec struct {
atmosConfig *schema.AtmosConfiguration
executeDescribeAffectedWithTargetRepoPath func(
atmosConfig *schema.AtmosConfiguration,
targetRefPath string,
includeSpaceliftAdminStacks bool,
includeSettings bool,
stack string,
processTemplates bool,
processYamlFunctions bool,
skip []string,
excludeLocked bool,
authManager auth.AuthManager,
) ([]schema.Affected, *plumbing.Reference, *plumbing.Reference, string, error)
executeDescribeAffectedWithTargetRefClone func(
atmosConfig *schema.AtmosConfiguration,
ref string,
sha string,
sshKeyPath string,
sshKeyPassword string,
includeSpaceliftAdminStacks bool,
includeSettings bool,
stack string,
processTemplates bool,
processYamlFunctions bool,
skip []string,
excludeLocked bool,
authManager auth.AuthManager,
) ([]schema.Affected, *plumbing.Reference, *plumbing.Reference, string, error)
executeDescribeAffectedWithTargetRefCheckout func(
atmosConfig *schema.AtmosConfiguration,
ref string,
sha string,
includeSpaceliftAdminStacks bool,
includeSettings bool,
stack string,
processTemplates bool,
processYamlFunctions bool,
skip []string,
excludeLocked bool,
authManager auth.AuthManager,
) ([]schema.Affected, *plumbing.Reference, *plumbing.Reference, string, error)
addDependentsToAffected func(
atmosConfig *schema.AtmosConfiguration,
affected *[]schema.Affected,
includeSettings bool,
processTemplates bool,
processYamlFunctions bool,
skip []string,
onlyInStack string,
authManager auth.AuthManager,
) error
printOrWriteToFile func(
atmosConfig *schema.AtmosConfiguration,
format string,
file string,
data any,
) error
IsTTYSupportForStdout func() bool
pageCreator pager.PageCreator
}
// NewDescribeAffectedExec creates a new `describe affected` executor.
func NewDescribeAffectedExec(
atmosConfig *schema.AtmosConfiguration,
) DescribeAffectedExec {
defer perf.Track(atmosConfig, "exec.NewDescribeAffectedExec")()
return &describeAffectedExec{
atmosConfig: atmosConfig,
executeDescribeAffectedWithTargetRepoPath: ExecuteDescribeAffectedWithTargetRepoPath,
executeDescribeAffectedWithTargetRefClone: ExecuteDescribeAffectedWithTargetRefClone,
executeDescribeAffectedWithTargetRefCheckout: ExecuteDescribeAffectedWithTargetRefCheckout,
addDependentsToAffected: addDependentsToAffected,
printOrWriteToFile: printOrWriteToFile,
IsTTYSupportForStdout: term.IsTTYSupportForStdout,
pageCreator: pager.New(),
}
}
// ParseDescribeAffectedCliArgs parses the command-line arguments of the `atmos describe affected` command.
func ParseDescribeAffectedCliArgs(cmd *cobra.Command, args []string) (DescribeAffectedCmdArgs, error) {
defer perf.Track(nil, "exec.ParseDescribeAffectedCliArgs")()
var atmosConfig schema.AtmosConfiguration
if info, err := ProcessCommandLineArgs("", cmd, args, nil); err != nil {
return DescribeAffectedCmdArgs{}, err
} else if atmosConfig, err = cfg.InitCliConfig(info, true); err != nil {
return DescribeAffectedCmdArgs{}, err
}
if err := ValidateStacks(&atmosConfig); err != nil {
return DescribeAffectedCmdArgs{}, err
}
// Process flags
flags := cmd.Flags()
result := DescribeAffectedCmdArgs{
CLIConfig: &atmosConfig,
}
SetDescribeAffectedFlagValueInCliArgs(flags, &result)
if result.Format != "yaml" && result.Format != "json" && result.Format != "matrix" {
return DescribeAffectedCmdArgs{}, ErrInvalidFormat
}
if result.RepoPath != "" && (result.Base != "" || result.Ref != "" || result.SHA != "" || result.SSHKeyPath != "" || result.SSHKeyPassword != "") {
return DescribeAffectedCmdArgs{}, ErrRepoPathConflict
}
return result, nil
}
// SetDescribeAffectedFlagValueInCliArgs sets the flag values in CLI arguments.
func SetDescribeAffectedFlagValueInCliArgs(flags *pflag.FlagSet, describe *DescribeAffectedCmdArgs) {
defer perf.Track(nil, "exec.SetDescribeAffectedFlagValueInCliArgs")()
flagsKeyValue := map[string]any{
"base": &describe.Base,
"ref": &describe.Ref,
"sha": &describe.SHA,
"repo-path": &describe.RepoPath,
"ssh-key": &describe.SSHKeyPath,
"ssh-key-password": &describe.SSHKeyPassword,
"include-spacelift-admin-stacks": &describe.IncludeSpaceliftAdminStacks,
"include-dependents": &describe.IncludeDependents,
"include-settings": &describe.IncludeSettings,
"upload": &describe.Upload,
"clone-target-ref": &describe.CloneTargetRef,
"process-templates": &describe.ProcessTemplates,
"process-functions": &describe.ProcessYamlFunctions,
"skip": &describe.Skip,
"pager": &describe.CLIConfig.Settings.Terminal.Pager,
"stack": &describe.Stack,
"format": &describe.Format,
"file": &describe.OutputFile,
"output-file": &describe.GithubOutputFile,
"query": &describe.Query,
"verbose": &describe.Verbose,
"exclude-locked": &describe.ExcludeLocked,
}
// By default, process templates and YAML functions
describe.ProcessTemplates = true
describe.ProcessYamlFunctions = true
var err error
for k := range flagsKeyValue {
if !flags.Changed(k) {
continue
}
switch v := flagsKeyValue[k].(type) {
case *string:
*v, err = flags.GetString(k)
case *bool:
*v, err = flags.GetBool(k)
case *[]string:
*v, err = flags.GetStringSlice(k)
default:
er := fmt.Errorf("unsupported type %T for flag %s", v, k)
errUtils.CheckErrorPrintAndExit(er, "", "")
}
errUtils.CheckErrorPrintAndExit(err, "", "")
}
// Resolve --base flag: auto-detect ref vs SHA and populate the appropriate field.
if describe.Base != "" {
if ci.IsCommitSHA(describe.Base) {
describe.SHA = describe.Base
} else {
describe.Ref = describe.Base
}
}
// Auto-detect base from CI environment when ci.enabled is true and no explicit base provided.
if describe.Ref == "" && describe.SHA == "" && describe.CLIConfig != nil && describe.CLIConfig.CI.Enabled {
resolveBaseFromCI(describe)
}
// When uploading, always include dependents and settings for all affected components.
if describe.Upload {
describe.IncludeDependents = true
describe.IncludeSettings = true
}
if describe.Format == "" {
describe.Format = "json"
}
}
// resolveBaseFromCI attempts to auto-detect the base commit from the CI provider.
func resolveBaseFromCI(describe *DescribeAffectedCmdArgs) {
defer perf.Track(nil, "exec.resolveBaseFromCI")()
p := ci.Detect()
if p == nil {
return
}
resolution, err := p.ResolveBase()
if err != nil {
log.Warn("Failed to auto-detect CI base", "provider", p.Name(), "error", err)
return
}
if resolution == nil {
return
}
describe.Ref = resolution.Ref
describe.SHA = resolution.SHA
describe.HeadSHAOverride = resolution.HeadSHA
describe.CIEventType = resolution.EventType
base := resolution.SHA
if base == "" {
base = resolution.Ref
}
log.Info("Auto-detected CI base",
"provider", p.Name(),
"event", resolution.EventType,
"base", base,
"source", resolution.Source)
}
// Execute executes `describe affected` command.
func (d *describeAffectedExec) Execute(a *DescribeAffectedCmdArgs) error {
defer perf.Track(nil, "exec.Execute")()
var affected []schema.Affected
var headHead, baseHead *plumbing.Reference
var repoUrl string
var err error
switch {
case a.RepoPath != "":
affected, headHead, baseHead, repoUrl, err = d.executeDescribeAffectedWithTargetRepoPath(
a.CLIConfig,
a.RepoPath,
a.IncludeSpaceliftAdminStacks,
a.IncludeSettings,
a.Stack,
a.ProcessTemplates,
a.ProcessYamlFunctions,
a.Skip,
a.ExcludeLocked,
a.AuthManager,
)
case a.CloneTargetRef:
affected, headHead, baseHead, repoUrl, err = d.executeDescribeAffectedWithTargetRefClone(
a.CLIConfig,
a.Ref,
a.SHA,
a.SSHKeyPath,
a.SSHKeyPassword,
a.IncludeSpaceliftAdminStacks,
a.IncludeSettings,
a.Stack,
a.ProcessTemplates,
a.ProcessYamlFunctions,
a.Skip,
a.ExcludeLocked,
a.AuthManager,
)
default:
affected, headHead, baseHead, repoUrl, err = d.executeDescribeAffectedWithTargetRefCheckout(
a.CLIConfig,
a.Ref,
a.SHA,
a.IncludeSpaceliftAdminStacks,
a.IncludeSettings,
a.Stack,
a.ProcessTemplates,
a.ProcessYamlFunctions,
a.Skip,
a.ExcludeLocked,
a.AuthManager,
)
}
if err != nil {
return err
}
// Add dependent components and stacks for each affected component.
if len(affected) > 0 && a.IncludeDependents {
err = d.addDependentsToAffected(a.CLIConfig, &affected, a.IncludeSettings, a.ProcessTemplates, a.ProcessYamlFunctions, a.Skip, a.Stack, a.AuthManager)
if err != nil {
return err
}
}
// Strip unnecessary fields when uploading to Atmos Pro to reduce payload size
// and stay within serverless function payload limits.
if a.Upload {
affected = StripAffectedForUpload(affected)
}
return d.view(a, repoUrl, headHead, baseHead, affected)
}
func (d *describeAffectedExec) view(a *DescribeAffectedCmdArgs, repoUrl string, headHead, baseHead *plumbing.Reference, affected []schema.Affected) error {
// Handle matrix format specially - it bypasses the normal view flow.
if a.Format == "matrix" {
return writeMatrixOutput(affected, a.GithubOutputFile)
}
if a.Query == "" {
if err := d.uploadableQuery(a, repoUrl, headHead, baseHead, affected); err != nil {
return err
}
} else {
res, err := u.EvaluateYqExpression(d.atmosConfig, affected, a.Query)
if err != nil {
return err
}
err = viewWithScroll(&viewWithScrollProps{d.pageCreator, term.IsTTYSupportForStdout, d.printOrWriteToFile, d.atmosConfig, "Affected components and stacks", a.Format, a.OutputFile, res})
if err != nil {
return err
}
}
return nil
}
func (d *describeAffectedExec) uploadableQuery(args *DescribeAffectedCmdArgs, repoUrl string, headHead, baseHead *plumbing.Reference, affected []schema.Affected) error {
log.Debug("Affected components and stacks:")
// When uploading, suppress the large JSON dump unless verbose mode or file output is requested.
if !args.Upload || args.Verbose || args.OutputFile != "" {
err := viewWithScroll(&viewWithScrollProps{d.pageCreator, d.IsTTYSupportForStdout, d.printOrWriteToFile, d.atmosConfig, "Affected components and stacks", args.Format, args.OutputFile, affected})
if err != nil {
return err
}
}
if !args.Upload {
return nil
}
// Validate that the CI event is a pull_request event when uploading.
// Atmos Pro only processes pull_request webhooks, so push events cannot be correlated.
if args.CIEventType != "" && args.CIEventType != "pull_request" && args.CIEventType != "pull_request_target" {
return errUtils.Build(
fmt.Errorf("%w: detected CI event %q, but Atmos Pro only supports pull_request events", errUtils.ErrUploadRequiresPullRequestEvent, args.CIEventType),
).
WithHint("Ensure your workflow triggers on pull_request events when using --upload.").
WithHint("Push events and other event types are not supported for Atmos Pro uploads.").
WithHint("See https://atmos.tools/integrations/pro for supported CI configurations.").
Err()
}
// Parse the repo URL.
gitURL, err := giturl.NewGitURL(repoUrl)
if err != nil {
return err
}
log.Debug("Creating API client")
apiClient, err := pro.NewAtmosProAPIClientFromEnv(d.atmosConfig)
if err != nil {
log.Warn("Failed to create Atmos Pro API client for upload. The describe affected result is unaffected.", "error", err)
return nil
}
// Use the PR head SHA from the CI event payload when available.
// This ensures the upload SHA matches what Atmos Pro indexed from the webhook,
// regardless of which commit the workflow has checked out (e.g., merge commit vs PR head).
headSHA := headHead.Hash().String()
if args.HeadSHAOverride != "" {
headSHA = args.HeadSHAOverride
log.Debug("Using PR head SHA for upload correlation", "headSHA", headSHA, "localHEAD", headHead.Hash().String())
}
req := dtos.UploadAffectedStacksRequest{
HeadSHA: headSHA,
BaseSHA: baseHead.Hash().String(),
RepoURL: repoUrl,
RepoName: gitURL.GetRepoName(),
RepoOwner: gitURL.GetOwnerName(),
RepoHost: gitURL.GetHostName(),
Stacks: affected,
}
log.Debug("Preparing upload affected stacks request", "req", req)
if uploadErr := apiClient.UploadAffectedStacks(&req); uploadErr != nil {
ui.Error("Failed to upload affected stacks to Atmos Pro")
return uploadErr
}
ui.Successf("Uploaded %d affected component(s) to Atmos Pro", len(affected))
return nil
}
type viewWithScrollProps struct {
pageCreator pager.PageCreator
isTTYSupportForStdout func() bool
printOrWriteToFile func(atmosConfig *schema.AtmosConfiguration, format string, file string, data any) error
atmosConfig *schema.AtmosConfiguration
displayName string
format string
file string
res any
}
func viewWithScroll(v *viewWithScrollProps) error {
if v.atmosConfig.Settings.Terminal.IsPagerEnabled() && v.file == "" {
err := viewConfig(&viewConfigProps{v.pageCreator, v.isTTYSupportForStdout, v.atmosConfig, v.displayName, v.format, v.res})
switch err.(type) {
case DescribeConfigFormatError:
return err
case nil:
return nil
default:
log.Debug("Failed to use pager")
}
}
err := v.printOrWriteToFile(v.atmosConfig, v.format, v.file, v.res)
if err != nil {
return err
}
return nil
}
type viewConfigProps struct {
pageCreator pager.PageCreator
isTTYSupportForStdout func() bool
atmosConfig *schema.AtmosConfiguration
displayName string
format string
data any
}
func viewConfig(v *viewConfigProps) error {
if !v.isTTYSupportForStdout() {
return ErrTTYNotSupported
}
var content string
var err error
switch v.format {
case "yaml":
content, err = u.GetHighlightedYAML(v.atmosConfig, v.data)
if err != nil {
return err
}
case "json":
content, err = u.GetHighlightedJSON(v.atmosConfig, v.data)
if err != nil {
return err
}
default:
return DescribeConfigFormatError{
v.format,
}
}
if err := v.pageCreator.Run(v.displayName, content); err != nil {
return err
}
return nil
}
// MatrixOutput represents the GitHub Actions matrix strategy format.
type MatrixOutput struct {
Include []MatrixEntry `json:"include"`
}
// MatrixEntry represents a single entry in the matrix include array.
type MatrixEntry struct {
Stack string `json:"stack"`
Component string `json:"component"`
ComponentPath string `json:"component_path"`
ComponentType string `json:"component_type"`
}
// convertAffectedToMatrix converts the affected list to GitHub Actions matrix format.
func convertAffectedToMatrix(affected []schema.Affected) MatrixOutput {
matrix := MatrixOutput{
Include: make([]MatrixEntry, 0, len(affected)),
}
for i := range affected {
a := &affected[i]
entry := MatrixEntry{
Stack: a.Stack,
Component: a.Component,
ComponentPath: a.ComponentPath,
ComponentType: a.ComponentType,
}
matrix.Include = append(matrix.Include, entry)
}
return matrix
}
// writeMatrixOutput writes the matrix output to stdout or a file.
// If outputFile is specified (for $GITHUB_OUTPUT), writes in key=value format.
// Otherwise, writes JSON to stdout.
func writeMatrixOutput(affected []schema.Affected, outputFile string) error {
matrix := convertAffectedToMatrix(affected)
matrixJSON, err := json.Marshal(matrix)
if err != nil {
return fmt.Errorf("failed to marshal matrix output: %w", err)
}
if outputFile != "" {
// Write to file in key=value format for $GITHUB_OUTPUT.
// Using 0644 permissions - matrix contains only stack/component names, not secrets.
f, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, defaultFilePermissions)
if err != nil {
return fmt.Errorf("failed to open output file %s: %w", outputFile, err)
}
defer f.Close()
// Write matrix=<json> format.
if _, err := fmt.Fprintf(f, "matrix=%s\n", string(matrixJSON)); err != nil {
return fmt.Errorf("failed to write to output file %s: %w", outputFile, err)
}
// Also write count for convenience.
if _, err := fmt.Fprintf(f, "affected_count=%d\n", len(affected)); err != nil {
return fmt.Errorf("failed to write count to output file %s: %w", outputFile, err)
}
log.Debug("Wrote matrix output to file", "file", outputFile, "count", len(affected))
return nil
}
// Write to stdout.
_ = data.Writeln(string(matrixJSON))
return nil
}