2022-06-15 19:33:29 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-09-30 11:33:16 +00:00
|
|
|
"maps"
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2024-12-22 09:44:34 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/pipeline/rpc"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/pipeline/stepbuilder"
|
2022-06-15 19:33:29 +00:00
|
|
|
)
|
|
|
|
|
2024-01-11 21:15:15 +00:00
|
|
|
func queuePipeline(ctx context.Context, repo *model.Repo, pipelineItems []*stepbuilder.Item) error {
|
2023-03-20 03:50:56 +00:00
|
|
|
var tasks []*model.Task
|
2022-10-18 01:24:12 +00:00
|
|
|
for _, item := range pipelineItems {
|
2023-04-08 11:15:28 +00:00
|
|
|
if item.Workflow.State == model.StatusSkipped {
|
2022-06-15 19:33:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-09-30 11:33:16 +00:00
|
|
|
task := &model.Task{
|
|
|
|
ID: fmt.Sprint(item.Workflow.ID),
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
}
|
|
|
|
maps.Copy(task.Labels, item.Labels)
|
|
|
|
err := task.ApplyLabelsFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
2024-02-08 21:49:07 +00:00
|
|
|
task.Dependencies = taskIDs(item.DependsOn, pipelineItems)
|
2022-06-15 19:33:29 +00:00
|
|
|
task.RunOn = item.RunsOn
|
2023-03-20 03:50:56 +00:00
|
|
|
task.DepStatus = make(map[string]model.StatusValue)
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2024-01-09 20:35:37 +00:00
|
|
|
task.Data, err = json.Marshal(rpc.Workflow{
|
2023-04-08 11:15:28 +00:00
|
|
|
ID: fmt.Sprint(item.Workflow.ID),
|
2022-06-15 19:33:29 +00:00
|
|
|
Config: item.Config,
|
|
|
|
Timeout: repo.Timeout,
|
|
|
|
})
|
2024-01-09 20:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-15 19:33:29 +00:00
|
|
|
|
|
|
|
tasks = append(tasks, task)
|
|
|
|
}
|
2024-01-11 21:15:15 +00:00
|
|
|
return server.Config.Services.Queue.PushAtOnce(ctx, tasks)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 21:49:07 +00:00
|
|
|
func taskIDs(dependsOn []string, pipelineItems []*stepbuilder.Item) (taskIDs []string) {
|
2022-06-15 19:33:29 +00:00
|
|
|
for _, dep := range dependsOn {
|
2022-10-18 01:24:12 +00:00
|
|
|
for _, pipelineItem := range pipelineItems {
|
2023-04-08 11:15:28 +00:00
|
|
|
if pipelineItem.Workflow.Name == dep {
|
2024-02-08 21:49:07 +00:00
|
|
|
taskIDs = append(taskIDs, fmt.Sprint(pipelineItem.Workflow.ID))
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|