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,112 @@ 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
46
+ log .Printf ("fetching commit ID %v" , spec .Job .Commit )
47
+ fetchCmd := exec .Command ("git" , "fetch" , "origin" , spec .Job .Commit )
48
+ fetchCmd .Stdout = os .Stdout
49
+ fetchCmd .Stderr = os .Stderr
50
+ err = fetchCmd .Run ()
51
+ if err != nil {
52
+ log .Printf ("error while fetching commit SHA: %v" , err )
53
+ reporterError (spec , backendApi , err )
54
+ usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("error while checking out to commit sha: %v" , err ), 1 )
55
+ }
56
+
32
57
log .Printf ("checking out to commit ID %v" , spec .Job .Commit )
33
58
cmd := exec .Command ("git" , "checkout" , spec .Job .Commit )
34
59
cmd .Stdout = os .Stdout
35
60
cmd .Stderr = os .Stderr
36
- err : = cmd .Run ()
61
+ err = cmd .Run ()
37
62
if err != nil {
38
63
log .Printf ("error while checking out to commit SHA: %v" , err )
64
+ reporterError (spec , backendApi , err )
39
65
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("error while checking out to commit sha: %v" , err ), 1 )
40
66
}
41
67
42
68
job , err := jobProvider .GetJob (spec .Job )
43
69
if err != nil {
70
+ log .Printf ("error getting job: %v" , err )
71
+ reporterError (spec , backendApi , err )
44
72
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get job: %v" , err ), 1 )
45
73
}
46
74
47
75
// get variables from the variables spec
48
76
variablesMap , err := VariablesProvider .GetVariables (spec .Variables )
49
77
if err != nil {
50
78
log .Printf ("could not get variables from provider: %v" , err )
79
+ reporterError (spec , backendApi , err )
51
80
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get variables from provider: %v" , err ), 1 )
52
81
}
53
82
job .StateEnvVars = lo .Assign (job .StateEnvVars , variablesMap )
54
83
job .CommandEnvVars = lo .Assign (job .CommandEnvVars , variablesMap )
55
84
56
85
lock , err := lockProvider .GetLock (spec .Lock )
57
86
if err != nil {
58
- usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get job: %v" , err ), 1 )
59
-
87
+ log .Printf ("error getting lock: %v" , err )
88
+ reporterError (spec , backendApi , err )
89
+ usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get lock: %v" , err ), 1 )
60
90
}
61
91
62
92
prService , err := vcsProvider .GetPrService (spec .VCS )
63
93
if err != nil {
94
+ log .Printf ("error getting prservice: %v" , err )
95
+ reporterError (spec , backendApi , err )
64
96
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get prservice: %v" , err ), 1 )
65
97
}
66
98
67
99
orgService , err := vcsProvider .GetOrgService (spec .VCS )
68
100
if err != nil {
101
+ log .Printf ("error getting orgservice: %v" , err )
102
+ reporterError (spec , backendApi , err )
69
103
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get orgservice: %v" , err ), 1 )
70
104
}
71
105
reporter , err := reporterProvider .GetReporter (fmt .Sprintf ("%v for %v" , spec .Job .JobType , job .ProjectName ), spec .Reporter , prService , * spec .Job .PullRequestNumber )
72
106
if err != nil {
107
+ log .Printf ("error getting reporter: %v" , err )
108
+ reporterError (spec , backendApi , err )
73
109
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get reporter: %v" , err ), 1 )
74
110
}
75
111
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
112
policyChecker , err := policyProvider .GetPolicyProvider (spec .Policy , spec .Backend .BackendHostname , spec .Backend .BackendOrganisationName , spec .Backend .BackendJobToken )
82
113
if err != nil {
114
+ log .Printf ("error getting policy provider: %v" , err )
115
+ reporterError (spec , backendApi , err )
83
116
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get policy provider: %v" , err ), 1 )
84
117
}
85
118
86
119
changedFiles , err := prService .GetChangedFiles (* spec .Job .PullRequestNumber )
87
120
if err != nil {
121
+ log .Printf ("error getting changed files: %v" , err )
122
+ reporterError (spec , backendApi , err )
88
123
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get changed files: %v" , err ), 1 )
89
124
}
125
+
90
126
diggerConfig , _ , _ , err := digger_config .LoadDiggerConfig ("./" , false , changedFiles )
91
127
if err != nil {
128
+ log .Printf ("error getting digger config: %v" , err )
129
+ reporterError (spec , backendApi , err )
92
130
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("Failed to read Digger digger_config. %s" , err ), 4 )
93
131
}
94
132
log .Printf ("Digger digger_config read successfully\n " )
95
133
96
134
commentUpdater , err := commentUpdaterProvider .Get (* diggerConfig )
97
135
if err != nil {
136
+ log .Printf ("error getting comment updater: %v" , err )
137
+ reporterError (spec , backendApi , err )
98
138
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get comment updater: %v" , err ), 8 )
99
139
}
100
140
101
141
planStorage , err := PlanStorageProvider .GetPlanStorage (spec .VCS .RepoOwner , spec .VCS .RepoName , * spec .Job .PullRequestNumber )
102
142
if err != nil {
143
+ log .Printf ("error getting plan storage: %v" , err )
144
+ reporterError (spec , backendApi , err )
103
145
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("could not get plan storage: %v" , err ), 8 )
104
146
}
105
147
@@ -108,16 +150,17 @@ func RunSpecNext(
108
150
fullRepoName := fmt .Sprintf ("%v-%v" , spec .VCS .RepoOwner , spec .VCS .RepoName )
109
151
_ , err = backendApi .ReportProjectJobStatus (fullRepoName , spec .Job .ProjectName , spec .JobId , "started" , time .Now (), nil , "" , "" )
110
152
if err != nil {
153
+ log .Printf ("error getting project status: %v" , err )
154
+ reporterError (spec , backendApi , err )
111
155
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("Failed to report jobSpec status to backend. Exiting. %v" , err ), 4 )
112
156
}
113
157
114
158
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
159
119
160
currentDir , err := os .Getwd ()
120
161
if err != nil {
162
+ log .Printf ("error getting current directory: %v" , err )
163
+ reporterError (spec , backendApi , err )
121
164
usage .ReportErrorAndExit (spec .VCS .Actor , fmt .Sprintf ("Failed to get current dir. %s" , err ), 4 )
122
165
}
123
166
0 commit comments