-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexecution_report.go
132 lines (111 loc) · 4.33 KB
/
execution_report.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/aweris/gale/common/model"
)
// FIXME: Report structs are modified copies of the structs in ghx/context/reports.go to workaround the dagger's
// not supporting map types. Once dagger supports map types, these structs should be moved to common package and
// used in both ghx and gale.
// TODO: Removed 'Matrix model.MatrixCombination` from report since it's not possible use map and don't know
// how to handle interface{} in dagger. Find way to handle this.
type WorkflowRunReport struct {
Ran bool // Ran indicates if the execution ran
Duration string // Duration of the execution
Name string // Name is the name of the workflow
Conclusion model.Conclusion // Conclusion is the result of a completed workflow run after continue-on-error is applied
File *File // File is the report file contains json report of the workflow
}
// NewWorkflowRunReport creates a new WorkflowRunReport from the given parameters.
func NewWorkflowRunReport(
ran bool,
runID string,
workflow *Workflow,
conclusion model.Conclusion,
duration time.Duration,
jrs []*JobRun,
) (*WorkflowRunReport, error) {
var jobs = make(map[string]model.Conclusion)
for _, jr := range jrs {
jobs[jr.Job.JobID] = jr.Report.Conclusion
}
wm := &model.WorkflowRunReport{
Ran: ran,
Duration: duration.String(),
Name: workflow.Name,
Path: workflow.Path,
RunID: runID,
Conclusion: conclusion,
Jobs: jobs,
}
data, err := json.Marshal(wm)
if err != nil {
return nil, err
}
file := dag.Directory().WithNewFile("workflow_run.json", string(data)).File("workflow_run.json")
return &WorkflowRunReport{
Ran: ran,
Duration: duration.String(),
Name: workflow.Name,
Conclusion: conclusion,
File: file,
}, nil
}
type JobRunReport struct {
Ran bool // Ran indicates if the execution ran
Duration string // Duration of the execution
Name string // Name is the name of the job
RunID string // RunID is the ID of the run
Conclusion model.Conclusion // Conclusion is the result of a completed job after continue-on-error is applied
Outcome model.Conclusion // Outcome is the result of a completed job before continue-on-error is applied
Outputs []KV // Outputs is the outputs generated by the job
Steps []StepRunSummary // Steps is the list of steps in the job
File *File // File is the report file contains original json report of the job
}
type StepRunSummary struct {
StepID string // ID is the unique identifier of the step.
Name string // Name is the name of the step
Stage model.StepStage // Stage is the stage of the step during the execution of the job. Possible values are: setup, pre, main, post, complete.
Conclusion model.Conclusion // Conclusion is the result of a completed job after continue-on-error is applied
}
// parseJobRunReport converts the report file to JobRunReport struct
func parseJobRunReport(ctx context.Context, file *File) (*JobRunReport, error) {
contents, err := file.Contents(ctx)
if err != nil {
return nil, fmt.Errorf("failed to read report file: %w", err)
}
// original report model to unmarshal the report file
var rm model.JobRunReport
if err := json.Unmarshal([]byte(contents), &rm); err != nil {
return nil, fmt.Errorf("failed to unmarshal report file: %w", err)
}
// convert model types to module types since dagger doesn't support map types and native reporting types
// slightly different from the model types
steps := make([]StepRunSummary, 0, len(rm.Steps))
for _, step := range rm.Steps {
steps = append(steps, convertStepRunSummary(step))
}
report := &JobRunReport{
Ran: rm.Ran,
Duration: rm.Duration,
Name: rm.Name,
RunID: rm.RunID,
Conclusion: rm.Conclusion,
Outcome: rm.Outcome,
Outputs: ConvertMapToKVSlice(rm.Outputs),
Steps: steps,
File: file,
}
return report, nil
}
// convertStepRunSummary converts model.StepRunSummary to StepRunSummary
func convertStepRunSummary(srs model.StepRunSummary) StepRunSummary {
return StepRunSummary{
StepID: srs.ID,
Name: srs.Name,
Stage: srs.Stage,
Conclusion: srs.Conclusion,
}
}