4
4
"fmt"
5
5
"github.com/diggerhq/digger/cli/pkg/digger"
6
6
"github.com/diggerhq/digger/cli/pkg/usage"
7
+ backend2 "github.com/diggerhq/digger/libs/backendapi"
7
8
comment_summary "github.com/diggerhq/digger/libs/comment_utils/summary"
8
9
"github.com/diggerhq/digger/libs/digger_config"
9
10
"github.com/diggerhq/digger/libs/scheduler"
@@ -15,6 +16,13 @@ import (
15
16
"time"
16
17
)
17
18
19
+ func reporterError (spec spec.Spec , backendApi backend2.Api , err error ) {
20
+ _ , reportingError := backendApi .ReportProjectJobStatus (spec .VCS .RepoName , spec .Job .ProjectName , spec .JobId , "failed" , time .Now (), nil , "" , "" )
21
+ if reportingError != nil {
22
+ usage .ReportErrorAndExit (spec .VCS .RepoOwner , fmt .Sprintf ("Failed run commands. %s" , err ), 5 )
23
+ }
24
+ }
25
+
18
26
func RunSpecNext (
19
27
spec spec.Spec ,
20
28
vcsProvider spec.VCSProvider ,
@@ -28,78 +36,101 @@ func RunSpecNext(
28
36
commentUpdaterProvider comment_summary.CommentUpdaterProvider ,
29
37
) error {
30
38
39
+ backendApi , err := backedProvider .GetBackendApi (spec .Backend )
40
+ if err != nil {
41
+ log .Printf ("error getting backend api: %v" , err )
42
+ usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get backend api: %v" , err ), 1 )
43
+ }
44
+
31
45
// checking out to the commit ID
32
46
log .Printf ("checking out to commit ID %v" , spec .Job .Commit )
33
47
cmd := exec .Command ("git" , "checkout" , spec .Job .Commit )
34
48
cmd .Stdout = os .Stdout
35
49
cmd .Stderr = os .Stderr
36
- err : = cmd .Run ()
50
+ err = cmd .Run ()
37
51
if err != nil {
38
52
log .Printf ("error while checking out to commit SHA: %v" , err )
53
+ reporterError (spec , backendApi , err )
39
54
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("error while checking out to commit sha: %v" , err ), 1 )
40
55
}
41
56
42
57
job , err := jobProvider .GetJob (spec .Job )
43
58
if err != nil {
59
+ log .Printf ("error getting job: %v" , err )
60
+ reporterError (spec , backendApi , err )
44
61
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get job: %v" , err ), 1 )
45
62
}
46
63
47
64
// get variables from the variables spec
48
65
variablesMap , err := VariablesProvider .GetVariables (spec .Variables )
49
66
if err != nil {
50
67
log .Printf ("could not get variables from provider: %v" , err )
68
+ reporterError (spec , backendApi , err )
51
69
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get variables from provider: %v" , err ), 1 )
52
70
}
53
71
job .StateEnvVars = lo .Assign (job .StateEnvVars , variablesMap )
54
72
job .CommandEnvVars = lo .Assign (job .CommandEnvVars , variablesMap )
55
73
56
74
lock , err := lockProvider .GetLock (spec .Lock )
57
75
if err != nil {
58
- usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get job: %v" , err ), 1 )
59
-
76
+ log .Printf ("error getting lock: %v" , err )
77
+ reporterError (spec , backendApi , err )
78
+ usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get lock: %v" , err ), 1 )
60
79
}
61
80
62
81
prService , err := vcsProvider .GetPrService (spec .VCS )
63
82
if err != nil {
83
+ log .Printf ("error getting prservice: %v" , err )
84
+ reporterError (spec , backendApi , err )
64
85
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get prservice: %v" , err ), 1 )
65
86
}
66
87
67
88
orgService , err := vcsProvider .GetOrgService (spec .VCS )
68
89
if err != nil {
90
+ log .Printf ("error getting orgservice: %v" , err )
91
+ reporterError (spec , backendApi , err )
69
92
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get orgservice: %v" , err ), 1 )
70
93
}
71
94
reporter , err := reporterProvider .GetReporter (fmt .Sprintf ("%v for %v" , spec .Job .JobType , job .ProjectName ), spec .Reporter , prService , * spec .Job .PullRequestNumber )
72
95
if err != nil {
96
+ log .Printf ("error getting reporter: %v" , err )
97
+ reporterError (spec , backendApi , err )
73
98
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get reporter: %v" , err ), 1 )
74
99
}
75
100
76
- backendApi , err := backedProvider .GetBackendApi (spec .Backend )
77
- if err != nil {
78
- usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get backend api: %v" , err ), 1 )
79
- }
80
-
81
101
policyChecker , err := policyProvider .GetPolicyProvider (spec .Policy , spec .Backend .BackendHostname , spec .Backend .BackendOrganisationName , spec .Backend .BackendJobToken )
82
102
if err != nil {
103
+ log .Printf ("error getting policy provider: %v" , err )
104
+ reporterError (spec , backendApi , err )
83
105
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get policy provider: %v" , err ), 1 )
84
106
}
85
107
86
108
changedFiles , err := prService .GetChangedFiles (* spec .Job .PullRequestNumber )
87
109
if err != nil {
110
+ log .Printf ("error getting changed files: %v" , err )
111
+ reporterError (spec , backendApi , err )
88
112
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get changed files: %v" , err ), 1 )
89
113
}
114
+
90
115
diggerConfig , _ , _ , err := digger_config .LoadDiggerConfig ("./" , false , changedFiles )
91
116
if err != nil {
117
+ log .Printf ("error getting digger config: %v" , err )
118
+ reporterError (spec , backendApi , err )
92
119
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("Failed to read Digger digger_config. %s" , err ), 4 )
93
120
}
94
121
log .Printf ("Digger digger_config read successfully\n " )
95
122
96
123
commentUpdater , err := commentUpdaterProvider .Get (* diggerConfig )
97
124
if err != nil {
125
+ log .Printf ("error getting comment updater: %v" , err )
126
+ reporterError (spec , backendApi , err )
98
127
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get comment updater: %v" , err ), 8 )
99
128
}
100
129
101
130
planStorage , err := PlanStorageProvider .GetPlanStorage (spec .VCS .RepoOwner , spec .VCS .RepoName , * spec .Job .PullRequestNumber )
102
131
if err != nil {
132
+ log .Printf ("error getting plan storage: %v" , err )
133
+ reporterError (spec , backendApi , err )
103
134
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get plan storage: %v" , err ), 8 )
104
135
}
105
136
@@ -108,16 +139,17 @@ func RunSpecNext(
108
139
fullRepoName := fmt .Sprintf ("%v-%v" , spec .VCS .RepoOwner , spec .VCS .RepoName )
109
140
_ , err = backendApi .ReportProjectJobStatus (fullRepoName , spec .Job .ProjectName , spec .JobId , "started" , time .Now (), nil , "" , "" )
110
141
if err != nil {
142
+ log .Printf ("error getting project status: %v" , err )
143
+ reporterError (spec , backendApi , err )
111
144
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("Failed to report jobSpec status to backend. Exiting. %v" , err ), 4 )
112
145
}
113
146
114
147
commentId := spec .CommentId
115
- if err != nil {
116
- usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("failed to get comment ID: %v" , err ), 4 )
117
- }
118
148
119
149
currentDir , err := os .Getwd ()
120
150
if err != nil {
151
+ log .Printf ("error getting current directory: %v" , err )
152
+ reporterError (spec , backendApi , err )
121
153
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("Failed to get current dir. %s" , err ), 4 )
122
154
}
123
155
0 commit comments