Skip to content

Commit 6e9168d

Browse files
authored
fix(api): same run result with different type (#6158)
1 parent ec18898 commit 6e9168d

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

engine/api/workflow/workflow_run_results.go

+72-25
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,33 @@ func CanUploadRunResult(ctx context.Context, db *gorp.DbMap, store cache.Store,
6565
return false, sdk.WrapError(sdk.ErrInvalidData, "unable to upload artifact on a terminated job")
6666
}
6767

68+
// We don't check duplicate filename duplicates for artifact manager
69+
if runResultCheck.ResultType == sdk.WorkflowRunResultTypeArtifactManager {
70+
return true, nil
71+
}
72+
6873
// Check File Name
6974
runResults, err := LoadRunResultsByRunIDAndType(ctx, db, runResultCheck.RunID, runResultCheck.ResultType)
7075
if err != nil {
7176
return false, sdk.WrapError(err, "unable to load run results for run %d", runResultCheck.RunID)
7277
}
73-
for _, result := range runResults {
78+
for _, runResult := range runResults {
7479
var fileName string
7580
switch runResultCheck.ResultType {
7681
case sdk.WorkflowRunResultTypeArtifact:
77-
refArt, err := result.GetArtifact()
82+
refArt, err := runResult.GetArtifact()
7883
if err != nil {
7984
return false, err
8085
}
8186
fileName = refArt.Name
8287
case sdk.WorkflowRunResultTypeCoverage:
83-
refCov, err := result.GetCoverage()
88+
refCov, err := runResult.GetCoverage()
8489
if err != nil {
8590
return false, err
8691
}
8792
fileName = refCov.Name
88-
case sdk.WorkflowRunResultTypeArtifactManager:
89-
refArt, err := result.GetArtifactManager()
90-
if err != nil {
91-
return false, err
92-
}
93-
fileName = refArt.Name
9493
case sdk.WorkflowRunResultTypeStaticFile:
95-
refArt, err := result.GetStaticFile()
94+
refArt, err := runResult.GetStaticFile()
9695
if err != nil {
9796
return false, err
9897
}
@@ -106,7 +105,7 @@ func CanUploadRunResult(ctx context.Context, db *gorp.DbMap, store cache.Store,
106105
// If we find a run result with same check, check subnumber
107106
var previousNodeRunUpload *sdk.WorkflowNodeRun
108107
for _, nr := range nrs {
109-
if nr.ID != result.WorkflowNodeRunID {
108+
if nr.ID != runResult.WorkflowNodeRunID {
110109
continue
111110
}
112111
previousNodeRunUpload = &nr
@@ -117,10 +116,10 @@ func CanUploadRunResult(ctx context.Context, db *gorp.DbMap, store cache.Store,
117116
}
118117

119118
// Check Sub num
120-
if result.SubNum == nrs[0].SubNumber {
119+
if runResult.SubNum == nrs[0].SubNumber {
121120
return false, sdk.WrapError(sdk.ErrConflictData, "artifact %s has already been uploaded", runResultCheck.Name)
122121
}
123-
if result.SubNum > nrs[0].SubNumber {
122+
if runResult.SubNum > nrs[0].SubNumber {
124123
return false, sdk.WrapError(sdk.ErrConflictData, "artifact %s cannot be uploaded into a previous run", runResultCheck.Name)
125124
}
126125
}
@@ -174,8 +173,8 @@ func AddResult(ctx context.Context, db *gorp.DbMap, store cache.Store, wr *sdk.W
174173
}
175174

176175
// Check validity of the request + complete runResuklt with md5,size,type
177-
func verifyAddResultArtifactManager(ctx context.Context, db gorp.SqlExecutor, store cache.Store, wr *sdk.WorkflowRun, runResult *sdk.WorkflowRunResult) (string, error) {
178-
artResult, err := runResult.GetArtifactManager()
176+
func verifyAddResultArtifactManager(ctx context.Context, db gorp.SqlExecutor, store cache.Store, wr *sdk.WorkflowRun, newRunResult *sdk.WorkflowRunResult) (string, error) {
177+
artNewResult, err := newRunResult.GetArtifactManager()
179178
if err != nil {
180179
return "", err
181180
}
@@ -209,24 +208,72 @@ func verifyAddResultArtifactManager(ctx context.Context, db gorp.SqlExecutor, st
209208
if err != nil {
210209
return "", err
211210
}
212-
fileInfo, err := artifactClient.GetFileInfo(artResult.RepoName, artResult.Path)
211+
fileInfo, err := artifactClient.GetFileInfo(artNewResult.RepoName, artNewResult.Path)
213212
if err != nil {
214213
return "", err
215214
}
216-
artResult.Size = fileInfo.Size
217-
artResult.MD5 = fileInfo.Md5
218-
artResult.RepoType = fileInfo.Type
219-
if artResult.FileType == "" {
220-
artResult.FileType = artResult.RepoType
215+
artNewResult.Size = fileInfo.Size
216+
artNewResult.MD5 = fileInfo.Md5
217+
artNewResult.RepoType = fileInfo.Type
218+
if artNewResult.FileType == "" {
219+
artNewResult.FileType = artNewResult.RepoType
221220
}
222221

223-
if err := artResult.IsValid(); err != nil {
222+
if err := artNewResult.IsValid(); err != nil {
224223
return "", err
225224
}
226-
dataUpdated, _ := json.Marshal(artResult)
227-
runResult.DataRaw = dataUpdated
225+
dataUpdated, _ := json.Marshal(artNewResult)
226+
newRunResult.DataRaw = dataUpdated
227+
228+
// Check existing run-result duplicates
229+
var nrs []sdk.WorkflowNodeRun
230+
for _, nodeRuns := range wr.WorkflowNodeRuns {
231+
if len(nodeRuns) < 1 {
232+
continue
233+
}
234+
// Get last noderun
235+
nodeRun := nodeRuns[0]
236+
if nodeRun.ID != newRunResult.WorkflowNodeRunID {
237+
continue
238+
}
239+
nrs = nodeRuns
240+
}
241+
runResults, err := LoadRunResultsByRunIDAndType(ctx, db, wr.ID, newRunResult.Type)
242+
if err != nil {
243+
return "", sdk.WrapError(err, "unable to load run results for run %d", wr.ID)
244+
}
245+
for _, runResult := range runResults {
246+
artRunResult, _ := runResult.GetArtifactManager()
247+
// if name is different: no problem
248+
if artRunResult.Name != artNewResult.Name {
249+
continue
250+
}
251+
// if name is the same but type is different: no problem
252+
if artRunResult.RepoType != artNewResult.RepoType {
253+
continue
254+
}
255+
// It can also be a new run
256+
var previousNodeRunUpload *sdk.WorkflowNodeRun
257+
for _, nr := range nrs {
258+
if nr.ID != runResult.WorkflowNodeRunID {
259+
continue
260+
}
261+
previousNodeRunUpload = &nr
262+
break
263+
}
264+
if previousNodeRunUpload == nil {
265+
return "", sdk.NewErrorFrom(sdk.ErrConflictData, "run-result %s has already been created from another pipeline", artNewResult.Name)
266+
}
267+
// Check Sub num
268+
if runResult.SubNum == nrs[0].SubNumber {
269+
return "", sdk.NewErrorFrom(sdk.ErrConflictData, "run-result %s has already been created", artNewResult.Name)
270+
}
271+
if runResult.SubNum > nrs[0].SubNumber {
272+
return "", sdk.NewErrorFrom(sdk.ErrConflictData, "run-result %s cannot be created into a previous run", artNewResult.Name)
273+
}
274+
}
228275

229-
cacheKey := GetRunResultKey(runResult.WorkflowRunID, runResult.Type, artResult.Name)
276+
cacheKey := GetRunResultKey(newRunResult.WorkflowRunID, newRunResult.Type, artNewResult.Name)
230277
b, err := store.Exist(cacheKey)
231278
if err != nil {
232279
return cacheKey, err

0 commit comments

Comments
 (0)