2023-06-04 22:15:07 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
2022-09-14 07:30:37 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-12-29 20:19:42 +00:00
|
|
|
package stepbuilder
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2017-03-12 08:46:59 +00:00
|
|
|
import (
|
2023-06-04 22:15:07 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2017-03-12 08:46:59 +00:00
|
|
|
"strings"
|
2021-11-25 19:43:31 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/version"
|
2022-11-15 18:47:27 +00:00
|
|
|
)
|
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
// MetadataFromStruct return the metadata from a pipeline will run with.
|
2023-11-14 16:12:12 +00:00
|
|
|
func MetadataFromStruct(forge metadata.ServerForge, repo *model.Repo, pipeline, last *model.Pipeline, workflow *model.Workflow, sysURL string) metadata.Metadata {
|
|
|
|
host := sysURL
|
|
|
|
uri, err := url.Parse(sysURL)
|
2023-06-04 22:15:07 +00:00
|
|
|
if err == nil {
|
|
|
|
host = uri.Host
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
fForge := metadata.Forge{}
|
|
|
|
if forge != nil {
|
|
|
|
fForge = metadata.Forge{
|
|
|
|
Type: forge.Name(),
|
|
|
|
URL: forge.URL(),
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
fRepo := metadata.Repo{}
|
|
|
|
if repo != nil {
|
|
|
|
fRepo = metadata.Repo{
|
2023-11-13 08:38:02 +00:00
|
|
|
ID: repo.ID,
|
2023-08-12 15:39:13 +00:00
|
|
|
Name: repo.Name,
|
|
|
|
Owner: repo.Owner,
|
|
|
|
RemoteID: fmt.Sprint(repo.ForgeRemoteID),
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: repo.ForgeURL,
|
2023-08-12 15:39:13 +00:00
|
|
|
CloneURL: repo.Clone,
|
|
|
|
CloneSSHURL: repo.CloneSSH,
|
|
|
|
Private: repo.IsSCMPrivate,
|
|
|
|
Branch: repo.Branch,
|
|
|
|
Trusted: repo.IsTrusted,
|
2023-06-04 22:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if idx := strings.LastIndex(repo.FullName, "/"); idx != -1 {
|
|
|
|
if fRepo.Name == "" && repo.FullName != "" {
|
|
|
|
fRepo.Name = repo.FullName[idx+1:]
|
|
|
|
}
|
|
|
|
if fRepo.Owner == "" && repo.FullName != "" {
|
|
|
|
fRepo.Owner = repo.FullName[:idx]
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
fWorkflow := metadata.Workflow{}
|
|
|
|
if workflow != nil {
|
|
|
|
fWorkflow = metadata.Workflow{
|
|
|
|
Name: workflow.Name,
|
|
|
|
Number: workflow.PID,
|
|
|
|
Matrix: workflow.Environ,
|
|
|
|
}
|
2023-04-08 11:15:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
return metadata.Metadata{
|
|
|
|
Repo: fRepo,
|
|
|
|
Curr: metadataPipelineFromModelPipeline(pipeline, true),
|
|
|
|
Prev: metadataPipelineFromModelPipeline(last, false),
|
|
|
|
Workflow: fWorkflow,
|
|
|
|
Step: metadata.Step{},
|
|
|
|
Sys: metadata.System{
|
|
|
|
Name: "woodpecker",
|
2023-11-14 16:12:12 +00:00
|
|
|
URL: sysURL,
|
2023-06-04 22:15:07 +00:00
|
|
|
Host: host,
|
|
|
|
Platform: "", // will be set by pipeline platform option or by agent
|
|
|
|
Version: version.Version,
|
|
|
|
},
|
|
|
|
Forge: fForge,
|
2017-03-12 08:46:59 +00:00
|
|
|
}
|
2023-06-04 22:15:07 +00:00
|
|
|
}
|
2017-03-12 08:46:59 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
func metadataPipelineFromModelPipeline(pipeline *model.Pipeline, includeParent bool) metadata.Pipeline {
|
|
|
|
if pipeline == nil {
|
|
|
|
return metadata.Pipeline{}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2023-05-31 16:30:41 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
cron := ""
|
|
|
|
if pipeline.Event == model.EventCron {
|
|
|
|
cron = pipeline.Sender
|
2023-05-31 16:30:41 +00:00
|
|
|
}
|
2021-11-25 19:43:31 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
parent := int64(0)
|
|
|
|
if includeParent {
|
|
|
|
parent = pipeline.Parent
|
2017-04-21 15:32:33 +00:00
|
|
|
}
|
2019-10-24 09:39:14 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
return metadata.Pipeline{
|
2024-07-07 11:43:07 +00:00
|
|
|
Number: pipeline.Number,
|
|
|
|
Parent: parent,
|
|
|
|
Created: pipeline.Created,
|
|
|
|
Started: pipeline.Started,
|
|
|
|
Finished: pipeline.Finished,
|
|
|
|
Status: string(pipeline.Status),
|
|
|
|
Event: string(pipeline.Event),
|
|
|
|
ForgeURL: pipeline.ForgeURL,
|
|
|
|
DeployTo: pipeline.DeployTo,
|
|
|
|
DeployTask: pipeline.DeployTask,
|
2023-06-04 22:15:07 +00:00
|
|
|
Commit: metadata.Commit{
|
|
|
|
Sha: pipeline.Commit,
|
|
|
|
Ref: pipeline.Ref,
|
|
|
|
Refspec: pipeline.Refspec,
|
|
|
|
Branch: pipeline.Branch,
|
|
|
|
Message: pipeline.Message,
|
|
|
|
Author: metadata.Author{
|
|
|
|
Name: pipeline.Author,
|
|
|
|
Email: pipeline.Email,
|
|
|
|
Avatar: pipeline.Avatar,
|
|
|
|
},
|
|
|
|
ChangedFiles: pipeline.ChangedFiles,
|
|
|
|
PullRequestLabels: pipeline.PullRequestLabels,
|
2024-01-30 16:39:00 +00:00
|
|
|
IsPrerelease: pipeline.IsPrerelease,
|
2023-06-04 22:15:07 +00:00
|
|
|
},
|
|
|
|
Cron: cron,
|
2019-10-24 09:39:14 +00:00
|
|
|
}
|
2019-06-05 13:58:27 +00:00
|
|
|
}
|