-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathprocess.go
142 lines (126 loc) · 4.39 KB
/
process.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
133
134
135
136
137
138
139
140
141
142
package workflow
import (
"context"
"fmt"
"time"
"github.com/rockbears/log"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/luascript"
)
func setValuesGitInBuildParameters(run *sdk.WorkflowNodeRun, vcsInfos vcsInfos) {
if run.ApplicationID != 0 {
run.VCSRepository = vcsInfos.Repository
if vcsInfos.Tag == "" {
run.VCSBranch = vcsInfos.Branch
}
run.VCSTag = vcsInfos.Tag
run.VCSHash = vcsInfos.Hash
run.VCSServer = vcsInfos.Server
}
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitRepository, sdk.StringParameter, vcsInfos.Repository)
if vcsInfos.Tag == "" {
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitBranch, sdk.StringParameter, vcsInfos.Branch)
}
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitTag, sdk.StringParameter, vcsInfos.Tag)
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitHash, sdk.StringParameter, vcsInfos.Hash)
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitHashShort, sdk.StringParameter, sdk.StringFirstN(run.VCSHash, 7))
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitAuthor, sdk.StringParameter, vcsInfos.Author)
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitMessage, sdk.StringParameter, vcsInfos.Message)
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitURL, sdk.StringParameter, vcsInfos.URL)
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitHTTPURL, sdk.StringParameter, vcsInfos.HTTPUrl)
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitServer, sdk.StringParameter, vcsInfos.Server)
}
func checkCondition(ctx context.Context, wr *sdk.WorkflowRun, conditions sdk.WorkflowNodeConditions, params []sdk.Parameter) bool {
var conditionsOK bool
var errc error
if conditions.LuaScript == "" {
conditionsOK, errc = sdk.WorkflowCheckConditions(conditions.PlainConditions, params)
} else {
luacheck, err := luascript.NewCheck()
if err != nil {
log.Warn(ctx, "processWorkflowNodeRun> WorkflowCheckConditions error: %s", err)
AddWorkflowRunInfo(wr, sdk.SpawnMsgNew(*sdk.MsgWorkflowError, fmt.Sprintf("Error init LUA System: %v", err)))
}
luacheck.SetVariables(sdk.ParametersToMap(params))
errc = luacheck.Perform(conditions.LuaScript)
conditionsOK = luacheck.Result
}
if errc != nil {
log.Warn(ctx, "processWorkflowNodeRun> WorkflowCheckConditions error: %s", errc)
AddWorkflowRunInfo(wr, sdk.SpawnMsgNew(*sdk.MsgWorkflowError, fmt.Sprintf("Error on LUA Condition: %v", errc)))
return false
}
return conditionsOK
}
// AddWorkflowRunInfo add WorkflowRunInfo on a WorkflowRun
func AddWorkflowRunInfo(run *sdk.WorkflowRun, infos ...sdk.SpawnMsg) {
for _, i := range infos {
run.Infos = append(run.Infos, sdk.WorkflowRunInfo{
APITime: time.Now(),
Message: i,
Type: i.Type,
SubNumber: run.LastSubNumber,
UserMessage: i.DefaultUserMessage(),
})
}
}
// computeRunStatus is useful to compute number of runs in success, building and fail
type statusCounter struct {
success, building, failed, stoppped, skipped, disabled int
}
// getRunStatus return the status depending on number of runs in success, building, stopped and fail
func getRunStatus(counter statusCounter) string {
switch {
case counter.building > 0:
return sdk.StatusBuilding
case counter.failed > 0:
return sdk.StatusFail
case counter.stoppped > 0:
return sdk.StatusStopped
case counter.success > 0:
return sdk.StatusSuccess
case counter.skipped > 0:
return sdk.StatusSkipped
case counter.disabled > 0:
return sdk.StatusDisabled
default:
return sdk.StatusNeverBuilt
}
}
func computeRunStatus(status string, counter *statusCounter) {
switch status {
case sdk.StatusSuccess:
counter.success++
case sdk.StatusBuilding, sdk.StatusWaiting:
counter.building++
case sdk.StatusFail:
counter.failed++
case sdk.StatusStopped:
counter.stoppped++
case sdk.StatusSkipped:
counter.skipped++
case sdk.StatusDisabled:
counter.disabled++
}
}
// MaxSubNumber returns the MaxSubNumber of workflowNodeRuns
func MaxSubNumber(workflowNodeRuns map[int64][]sdk.WorkflowNodeRun) int64 {
var maxsn int64
for _, wNodeRuns := range workflowNodeRuns {
for _, wNodeRun := range wNodeRuns {
if maxsn < wNodeRun.SubNumber {
maxsn = wNodeRun.SubNumber
}
}
}
return maxsn
}
func lastSubNumber(workflowNodeRuns []sdk.WorkflowNodeRun) int64 {
var lastSn int64
for _, wNodeRun := range workflowNodeRuns {
if lastSn < wNodeRun.SubNumber {
lastSn = wNodeRun.SubNumber
}
}
return lastSn
}