-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathdeploy.go
More file actions
255 lines (225 loc) · 7.36 KB
/
Copy pathdeploy.go
File metadata and controls
255 lines (225 loc) · 7.36 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
package command
import (
"errors"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
. "github.com/ForceCLI/force/lib"
"github.com/spf13/cobra"
)
type deployOutputOptions struct {
verbosity int
quiet bool
interactive bool
reportFormat string
ignoreCodeCoverageWarnings bool
suppressUnexpectedError bool
errorOnTestFailure bool
}
func defaultDeployOutputOptions() *deployOutputOptions {
o := deployOutputOptions{
reportFormat: "text",
quiet: false,
verbosity: 0,
ignoreCodeCoverageWarnings: false,
suppressUnexpectedError: false,
errorOnTestFailure: true,
}
return &o
}
var testFailureError = errors.New("Apex tests failed")
type deployStatus struct {
mu sync.Mutex
aborted bool
}
func (c *deployStatus) abort() {
c.mu.Lock()
c.aborted = true
c.mu.Unlock()
}
func (c *deployStatus) isAborted() bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.aborted
}
func deploy(force *Force, files ForceMetadataFiles, deployOptions *ForceDeployOptions, outputOptions *deployOutputOptions) error {
status := deployStatus{aborted: false}
return deployWith(force, &status, files, deployOptions, outputOptions)
}
func deployWith(force *Force, status *deployStatus, files ForceMetadataFiles, deployOptions *ForceDeployOptions, outputOptions *deployOutputOptions) error {
startTime := time.Now()
deployId, err := force.Metadata.StartDeploy(files, *deployOptions)
if err != nil {
return err
}
stopDeployUponSignal(force, deployId)
if outputOptions.interactive {
watchDeploy(deployId)
return nil
}
result, err := monitorDeploy(force, deployId, status)
if err != nil {
return err
}
endTime := time.Now()
duration := endTime.Sub(startTime)
junitOutput := outputOptions.reportFormat == "junit"
if outputOptions.suppressUnexpectedError {
filteredComponentFailures := result.Details.ComponentFailures[:0]
for _, f := range result.Details.ComponentFailures {
if !strings.HasPrefix(f.Problem, `An unexpected error occurred. Please include this ErrorId`) {
filteredComponentFailures = append(filteredComponentFailures, f)
}
}
result.Details.ComponentFailures = filteredComponentFailures
}
switch {
case outputOptions.quiet:
case junitOutput:
output, err := result.ToJunit(duration.Seconds())
if err != nil {
return fmt.Errorf("Failed to generate output: %w", err)
}
fmt.Println(output)
if result.HasComponentFailures() || !result.Success {
return fmt.Errorf("Deploy unsuccessful")
}
if result.HasTestFailures() {
return testFailureError
}
return nil
default:
output := result.ToString(duration.Seconds(), outputOptions.verbosity > 0)
fmt.Println(output)
codeCoverageWarnings := result.Details.RunTestResult.CodeCoverageWarnings
if !outputOptions.ignoreCodeCoverageWarnings && len(codeCoverageWarnings) > 0 {
fmt.Printf("\nCode Coverage Warnings - %d\n", len(codeCoverageWarnings))
for _, warning := range codeCoverageWarnings {
fmt.Printf("\n %s: %s\n", warning.Name, warning.Message)
}
if outputOptions.verbosity > 1 {
for _, c := range result.Details.RunTestResult.CodeCoverage {
component := c.Name
if c.Namespace != "" {
component = c.Namespace + "." + c.Name
}
for _, line := range c.LocationsNotCovered {
fmt.Printf("%s %s: Line %d not covered\n", c.Type, component, line.Line)
}
}
}
}
if result.HasComponentFailures() {
err = errors.New("Some components failed deployment")
} else if result.HasTestFailures() {
err = testFailureError
} else if !result.Success {
err = errors.New(fmt.Sprintf("Status: %s, Status Code: %s, Error Message: %s", result.Status, result.ErrorStatusCode, result.ErrorMessage))
}
if err != nil {
return fmt.Errorf("Deploy unsuccessful: %w", err)
}
}
return nil
}
func monitorDeploy(force *Force, deployId string, status *deployStatus) (ForceCheckDeploymentStatusResult, error) {
var result ForceCheckDeploymentStatusResult
var err error
retrying := false
for {
if status.isAborted() {
fmt.Fprintf(os.Stderr, "Cancelling deploy %s\n", deployId)
force.Metadata.CancelDeploy(deployId)
return result, nil
}
result, err = force.Metadata.CheckDeployStatus(deployId)
if err != nil {
if retrying {
return result, fmt.Errorf("Error getting deploy status: %w", err)
} else {
retrying = true
Log.Info(fmt.Sprintf("Received error checking deploy status: %s. Will retry once before aborting.", err.Error()))
}
} else {
retrying = false
}
result.UserName = force.GetCredentials().UserInfo.UserName
if result.Done {
break
}
if !retrying {
Log.Info(result)
}
time.Sleep(5000 * time.Millisecond)
}
return result, err
}
func stopDeployUponSignal(force *Force, deployId string) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
interuptsReceived := 0
for {
<-sigs
if interuptsReceived > 0 {
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Cancelling deploy %s\n", deployId)
force.Metadata.CancelDeploy(deployId)
interuptsReceived++
}
}()
}
func getDeploymentOutputOptions(cmd *cobra.Command) *deployOutputOptions {
outputOptions := defaultDeployOutputOptions()
if reportFormat, err := cmd.Flags().GetString("reporttype"); err == nil {
outputOptions.reportFormat = reportFormat
}
if quiet, err := cmd.Flags().GetBool("quiet"); err == nil {
outputOptions.quiet = quiet
}
if verbosity, err := cmd.Flags().GetCount("verbose"); err == nil {
outputOptions.verbosity = verbosity
}
if interactive, err := cmd.Flags().GetBool("interactive"); err == nil {
outputOptions.interactive = interactive
}
if ignoreCoverageWarnings, err := cmd.Flags().GetBool("ignorecoverage"); err == nil {
outputOptions.ignoreCodeCoverageWarnings = ignoreCoverageWarnings
}
if suppressUnexpectedError, err := cmd.Flags().GetBool("suppressunexpected"); err == nil {
outputOptions.suppressUnexpectedError = suppressUnexpectedError
}
if errorOnTestFailure, err := cmd.Flags().GetBool("erroronfailure"); err == nil {
outputOptions.errorOnTestFailure = errorOnTestFailure
}
return outputOptions
}
func getDeploymentOptions(cmd *cobra.Command) ForceDeployOptions {
var deploymentOptions ForceDeployOptions
deploymentOptions.AllowMissingFiles, _ = cmd.Flags().GetBool("allowmissingfiles")
deploymentOptions.AutoUpdatePackage, _ = cmd.Flags().GetBool("autoupdatepackage")
deploymentOptions.CheckOnly, _ = cmd.Flags().GetBool("checkonly")
deploymentOptions.IgnoreWarnings, _ = cmd.Flags().GetBool("ignorewarnings")
deploymentOptions.PurgeOnDelete, _ = cmd.Flags().GetBool("purgeondelete")
deploymentOptions.RollbackOnError, _ = cmd.Flags().GetBool("rollbackonerror")
deploymentOptions.TestLevel, _ = cmd.Flags().GetString("testlevel")
deploymentOptions.RunTests, _ = cmd.Flags().GetStringSlice("test")
deploymentOptions.SinglePackage = true
runAllTests, _ := cmd.Flags().GetBool("runalltests")
if runAllTests {
deploymentOptions.TestLevel = "RunAllTestsInOrg"
}
if cmd.Flags().Changed("test") && len(deploymentOptions.RunTests) == 0 {
// NoTestRun can't be used when deploying to production, but
// RunSpecifiedTests can be used with an empty set of tests by passing
// `--test ''`
deploymentOptions.TestLevel = "RunSpecifiedTests"
deploymentOptions.RunTests = []string{""}
}
return deploymentOptions
}