@@ -2,12 +2,15 @@ package workflow
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "fmt"
5
7
"strconv"
6
8
"time"
7
9
8
10
"github.com/go-gorp/gorp"
9
11
10
12
"github.com/ovh/cds/engine/api/database/gorpmapping"
13
+ "github.com/ovh/cds/engine/api/integration/artifact_manager"
11
14
"github.com/ovh/cds/engine/cache"
12
15
"github.com/ovh/cds/engine/gorpmapper"
13
16
"github.com/ovh/cds/sdk"
@@ -118,7 +121,7 @@ func CanUploadRunResult(ctx context.Context, db *gorp.DbMap, store cache.Store,
118
121
return true , nil
119
122
}
120
123
121
- func AddResult (db * gorp.DbMap , store cache.Store , runResult * sdk.WorkflowRunResult ) error {
124
+ func AddResult (ctx context. Context , db * gorp.DbMap , store cache.Store , wr * sdk. WorkflowRun , runResult * sdk.WorkflowRunResult ) error {
122
125
var cacheKey string
123
126
switch runResult .Type {
124
127
case sdk .WorkflowRunResultTypeArtifact :
@@ -135,7 +138,7 @@ func AddResult(db *gorp.DbMap, store cache.Store, runResult *sdk.WorkflowRunResu
135
138
}
136
139
case sdk .WorkflowRunResultTypeArtifactManager :
137
140
var err error
138
- cacheKey , err = verifyAddResultArtifactManager (store , runResult )
141
+ cacheKey , err = verifyAddResultArtifactManager (ctx , db , store , wr , runResult )
139
142
if err != nil {
140
143
return err
141
144
}
@@ -158,14 +161,56 @@ func AddResult(db *gorp.DbMap, store cache.Store, runResult *sdk.WorkflowRunResu
158
161
return sdk .WithStack (store .Delete (cacheKey ))
159
162
}
160
163
161
- func verifyAddResultArtifactManager (store cache.Store , runResult * sdk.WorkflowRunResult ) (string , error ) {
164
+ // Check validity of the request + complete runResuklt with md5,size,type
165
+ func verifyAddResultArtifactManager (ctx context.Context , db gorp.SqlExecutor , store cache.Store , wr * sdk.WorkflowRun , runResult * sdk.WorkflowRunResult ) (string , error ) {
162
166
artResult , err := runResult .GetArtifactManager ()
163
167
if err != nil {
164
168
return "" , err
165
169
}
170
+
171
+ // Check file in integration
172
+ var artiInteg * sdk.WorkflowProjectIntegration
173
+ for i := range wr .Workflow .Integrations {
174
+ if ! wr .Workflow .Integrations [i ].ProjectIntegration .Model .ArtifactManager {
175
+ continue
176
+ }
177
+ artiInteg = & wr .Workflow .Integrations [i ]
178
+ }
179
+ if artiInteg == nil {
180
+ return "" , sdk .NewErrorFrom (sdk .ErrInvalidData , "you cannot add a artifact manager run result without an integration" )
181
+ }
182
+ secrets , err := loadRunSecretWithDecryption (ctx , db , wr .ID , []string {fmt .Sprintf (SecretProjIntegrationContext , artiInteg .ProjectIntegrationID )})
183
+ if err != nil {
184
+ return "" , err
185
+ }
186
+ var artifactManagerToken string
187
+ for _ , s := range secrets {
188
+ if s .Name == fmt .Sprintf ("cds.integration.artifact_manager.%s" , sdk .ArtifactManagerConfigToken ) {
189
+ artifactManagerToken = s .Value
190
+ break
191
+ }
192
+ }
193
+ if artifactManagerToken == "" {
194
+ return "" , sdk .NewErrorFrom (sdk .ErrNotFound , "unable to find artifact manager token" )
195
+ }
196
+ artifactClient , err := artifact_manager .NewClient (artiInteg .ProjectIntegration .Config [sdk .ArtifactManagerConfigPlatform ].Value , artiInteg .ProjectIntegration .Config [sdk .ArtifactManagerConfigURL ].Value , artifactManagerToken )
197
+ if err != nil {
198
+ return "" , err
199
+ }
200
+ fileInfo , err := artifactClient .GetFileInfo (artResult .RepoName , artResult .Path )
201
+ if err != nil {
202
+ return "" , err
203
+ }
204
+ artResult .Size = fileInfo .Size
205
+ artResult .MD5 = fileInfo .Md5
206
+ artResult .RepoType = fileInfo .Type
207
+
166
208
if err := artResult .IsValid (); err != nil {
167
209
return "" , err
168
210
}
211
+ dataUpdated , _ := json .Marshal (artResult )
212
+ runResult .DataRaw = dataUpdated
213
+
169
214
cacheKey := GetRunResultKey (runResult .WorkflowRunID , runResult .Type , artResult .Name )
170
215
b , err := store .Exist (cacheKey )
171
216
if err != nil {
0 commit comments