@@ -3,12 +3,14 @@ package reporting
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "github.com/diggerhq/digger/libs/comment_utils/utils"
6
7
"github.com/diggerhq/digger/libs/digger_config"
7
8
"github.com/diggerhq/digger/libs/orchestrator"
8
9
"github.com/diggerhq/digger/libs/orchestrator/scheduler"
9
10
"github.com/diggerhq/digger/libs/terraform_utils"
10
11
"github.com/samber/lo"
11
12
"log"
13
+ "strconv"
12
14
)
13
15
14
16
type ProjectNameSourceDetail struct {
@@ -25,24 +27,29 @@ type SourceGroupingReporter struct {
25
27
PrService orchestrator.PullRequestService
26
28
}
27
29
28
- func (r SourceGroupingReporter ) Report ( report string , reportFormatting func ( report string ) string ) ( string , string , error ) {
30
+ func (r SourceGroupingReporter ) UpdateComment ( sourceDetails [] SourceDetails , location string , terraformOutputs map [ string ] string ) error {
29
31
jobSpecs , err := scheduler .GetJobSpecs (r .Jobs )
30
32
if err != nil {
31
- return "" , "" , fmt .Errorf ("could not get job specs: %v" , err )
33
+ return fmt .Errorf ("could not get job specs: %v" , err )
32
34
}
33
35
34
- //impactedSources := jobSpecs[0].ImpactedSources
35
- // TODO: populate from batch field
36
- var impactedSources map [string ]digger_config.ProjectToSourceMapping
36
+ sourceDetaiItem , found := lo .Find (sourceDetails , func (item SourceDetails ) bool {
37
+ return item .SourceLocation == location
38
+ })
39
+
40
+ if ! found {
41
+ log .Printf ("location not found in sourcedetails list" )
42
+ return fmt .Errorf ("location not found in sourcedetails list" )
43
+ }
37
44
38
45
projectNameToJobMap , err := scheduler .JobsToProjectMap (r .Jobs )
39
46
if err != nil {
40
- return "" , "" , fmt .Errorf ("could not convert jobs to map: %v" , err )
47
+ return fmt .Errorf ("could not convert jobs to map: %v" , err )
41
48
}
42
49
43
50
projectNameToJobSpecMap , err := orchestrator .JobsSpecsToProjectMap (jobSpecs )
44
51
if err != nil {
45
- return "" , "" , fmt .Errorf ("could not convert jobs to map: %v" , err )
52
+ return fmt .Errorf ("could not convert jobs to map: %v" , err )
46
53
}
47
54
48
55
projectNameToFootPrintMap := make (map [string ]terraform_utils.TerraformPlanFootprint )
@@ -52,51 +59,50 @@ func (r SourceGroupingReporter) Report(report string, reportFormatting func(repo
52
59
err := json .Unmarshal (job .PlanFootprint , & footprint )
53
60
if err != nil {
54
61
log .Printf ("could not unmarshal footprint: %v" , err )
55
- return "" , "" , fmt .Errorf ("could not unmarshal footprint: %v" , err )
62
+ return fmt .Errorf ("could not unmarshal footprint: %v" , err )
56
63
}
57
64
} else {
58
65
footprint = terraform_utils.TerraformPlanFootprint {}
59
66
}
60
67
projectNameToFootPrintMap [job .ProjectName ] = footprint
61
68
}
62
69
63
- groupsToProjectMap := ImpactedSourcesMapToGroupMapping (impactedSources , projectNameToJobMap , projectNameToJobSpecMap , projectNameToFootPrintMap )
64
-
65
- message := ":construction_worker: Jobs status:\n \n "
66
- for sourceLocation , projectSourceDetailList := range groupsToProjectMap {
67
- footprints := lo .Map (projectSourceDetailList , func (detail ProjectNameSourceDetail , i int ) terraform_utils.TerraformPlanFootprint {
68
- return detail .PlanFootPrint
69
- })
70
- allSimilarInGroup , err := terraform_utils .SimilarityCheck (footprints )
71
- if err != nil {
72
- return "" , "" , fmt .Errorf ("error performing similar check: %v" , err )
70
+ footprints := lo .FilterMap (sourceDetaiItem .Projects , func (project string , i int ) (terraform_utils.TerraformPlanFootprint , bool ) {
71
+ if projectNameToJobMap [project ].Status == scheduler .DiggerJobSucceeded {
72
+ return projectNameToFootPrintMap [project ], true
73
73
}
74
+ return terraform_utils.TerraformPlanFootprint {}, false
75
+ })
76
+ allSimilarInGroup , err := terraform_utils .SimilarityCheck (footprints )
77
+ if err != nil {
78
+ return fmt .Errorf ("error performing similar check: %v" , err )
79
+ }
74
80
75
- message = message + fmt .Sprintf ("# Group: %v (similar: %v)" , sourceLocation , allSimilarInGroup )
76
- for _ , projectSourceDetail := range projectSourceDetailList {
77
- job := projectSourceDetail .Job
78
- jobSpec := projectSourceDetail .JobSpec
79
- isPlan := jobSpec .IsPlan ()
80
- message = message + fmt .Sprintf ("<!-- PROJECTHOLDER %v -->\n " , job .ProjectName )
81
- message = message + fmt .Sprintf ("%v **%v** <a href='%v'>%v</a>%v\n " , job .Status .ToEmoji (), job .ProjectName , * job .WorkflowRunUrl , job .Status .ToString (), job .ResourcesSummaryString (isPlan ))
82
- message = message + fmt .Sprintf ("<!-- PROJECTHOLDEREND %v -->\n " , job .ProjectName )
81
+ message := ""
82
+ message = message + fmt .Sprintf ("# Group: %v (similar: %v)\n " , location , allSimilarInGroup )
83
+ for i , project := range sourceDetaiItem .Projects {
84
+ job := projectNameToJobMap [project ]
85
+ if job .Status != scheduler .DiggerJobSucceeded {
86
+ continue
83
87
}
84
-
88
+ jobSpec := projectNameToJobSpecMap [project ]
89
+ isPlan := jobSpec .JobType == orchestrator .DiggerCommandPlan
90
+ expanded := i == 0 || ! allSimilarInGroup
91
+ var commenter func (terraformOutput string ) string
92
+ if isPlan {
93
+ commenter = utils .GetTerraformOutputAsCollapsibleComment (fmt .Sprintf ("Plan for %v" , project ), expanded )
94
+ } else {
95
+ commenter = utils .GetTerraformOutputAsCollapsibleComment (fmt .Sprintf ("Apply for %v" , project ), false )
96
+ }
97
+ message = message + commenter (terraformOutputs [project ]) + "\n "
85
98
}
86
99
87
- r .PrService .PublishComment (r .PrNumber , message )
88
- return "" , "" , nil
89
- }
90
-
91
- func (reporter SourceGroupingReporter ) Flush () (string , string , error ) {
92
- return "" , "" , nil
93
- }
94
-
95
- func (reporter SourceGroupingReporter ) SupportsMarkdown () bool {
96
- return false
97
- }
98
-
99
- func (reporter SourceGroupingReporter ) Suppress () error {
100
+ CommentId , err := strconv .ParseInt (sourceDetaiItem .CommentId , 10 , 64 )
101
+ if err != nil {
102
+ log .Printf ("Could not convert commentId to int64: %v" , err )
103
+ return fmt .Errorf ("could not convert commentId to int64: %v" , err )
104
+ }
105
+ r .PrService .EditComment (r .PrNumber , CommentId , message )
100
106
return nil
101
107
}
102
108
0 commit comments