@@ -51,6 +51,7 @@ type TaskUsecase struct {
5151 vmLifecycle * lifecycle.Manager [string , lifecycle.VMState , lifecycle.VMMetadata ]
5252 girepo domain.GitIdentityRepo
5353 tokenProvider * gituc.TokenProvider
54+ projectRepo domain.ProjectRepo
5455}
5556
5657// NewTaskUsecase 创建任务业务逻辑实例
@@ -68,6 +69,7 @@ func NewTaskUsecase(i *do.Injector) (domain.TaskUsecase, error) {
6869 vmLifecycle : do.MustInvoke [* lifecycle.Manager [string , lifecycle.VMState , lifecycle.VMMetadata ]](i ),
6970 girepo : do.MustInvoke [domain.GitIdentityRepo ](i ),
7071 tokenProvider : do.MustInvoke [* gituc.TokenProvider ](i ),
72+ projectRepo : do.MustInvoke [domain.ProjectRepo ](i ),
7173 }
7274
7375 // 可选注入 TaskHook
@@ -276,8 +278,35 @@ func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.
276278 req .Now = time .Now ()
277279 var token string
278280
281+ git := taskflow.Git {
282+ Token : token ,
283+ Branch : req .RepoReq .Branch ,
284+ }
285+
286+ imageName := ""
287+ env := make ([]string , 0 )
288+ if req .Extra .ProjectID != uuid .Nil {
289+ project , err := a .projectRepo .Get (ctx , user .ID , req .Extra .ProjectID )
290+ if err != nil {
291+ return nil , fmt .Errorf ("failed to get project: %w" , err )
292+ }
293+
294+ git .URL = project .RepoURL
295+ if project .EnvVariables != nil {
296+ for k , v := range project .EnvVariables {
297+ env = append (env , fmt .Sprintf ("%s=%v" , k , v ))
298+ }
299+ }
300+ if project .Edges .Image != nil {
301+ imageName = project .Edges .Image .Name
302+ }
303+
304+ if gi := project .Edges .GitIdentity ; gi != nil {
305+ req .GitIdentityID = gi .ID
306+ }
307+ }
308+
279309 // 根据 GitIdentityID 解析 git token / username / email
280- var gitUsername , gitEmail string
281310 if req .GitIdentityID != uuid .Nil {
282311 identity , err := a .girepo .Get (ctx , req .GitIdentityID )
283312 if err != nil {
@@ -288,9 +317,9 @@ func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.
288317 return nil , fmt .Errorf ("get git token: %w" , err )
289318 }
290319
291- token = t
292- gitUsername = identity .Username
293- gitEmail = identity .Email
320+ git . Token = t
321+ git . Username = identity .Username
322+ git . Email = identity .Email
294323 }
295324
296325 limit := 1
@@ -316,6 +345,9 @@ func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.
316345 if t == nil {
317346 return nil , fmt .Errorf ("task edge is nil" )
318347 }
348+ if git .URL == "" {
349+ git .URL = pt .RepoURL
350+ }
319351
320352 if keys := m .Edges .Apikeys ; len (keys ) > 0 {
321353 m .APIKey = keys [0 ].APIKey
@@ -327,21 +359,13 @@ func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.
327359 return nil , err
328360 }
329361
330- git := taskflow.Git {
331- URL : pt .RepoURL ,
332- Token : token ,
333- Branch : pt .Branch ,
334- Username : gitUsername ,
335- Email : gitEmail ,
336- }
337-
338362 vm , err := a .taskflow .VirtualMachiner ().Create (ctx , & taskflow.CreateVirtualMachineReq {
339363 UserID : user .ID .String (),
340364 HostID : req .HostID ,
341365 HostName : t .ID .String (),
342366 Git : git ,
343367 ZipUrl : req .RepoReq .ZipURL ,
344- ImageURL : i .Name ,
368+ ImageURL : cmp . Or ( imageName , i .Name ) ,
345369 ProxyURL : "" ,
346370 TaskID : t .ID ,
347371 LLM : taskflow.LLMProviderReq {
@@ -352,6 +376,7 @@ func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.
352376 },
353377 Cores : fmt .Sprintf ("%d" , req .Resource .Core ),
354378 Memory : req .Resource .Memory ,
379+ Envs : env ,
355380 })
356381 if err != nil {
357382 return nil , err
0 commit comments