mirror of
https://codeberg.org/woodpecker-plugins/go-plugin.git
synced 2024-12-27 12:57:43 +01:00
162 lines
4.1 KiB
Go
162 lines
4.1 KiB
Go
// Copyright 2023 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 plugin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
const (
|
|
EventTypePush = "push"
|
|
EventTypePullRequest = "pull_request"
|
|
EventTypeTag = "tag"
|
|
EventTypeDeployment = "deployment"
|
|
EventTypeCron = "cron"
|
|
EventTypeManual = "manual"
|
|
)
|
|
|
|
// Pipeline defines runtime metadata for a pipeline.
|
|
type Pipeline struct {
|
|
Number int64 `json:"number,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
Event string `json:"event,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
DeployTarget string `json:"target,omitempty"`
|
|
Created time.Time `json:"created,omitempty"`
|
|
Started time.Time `json:"started,omitempty"`
|
|
Finished time.Time `json:"finished,omitempty"`
|
|
Parent int64 `json:"parent,omitempty"`
|
|
ChangedFiles []string `json:"files,omitempty"`
|
|
|
|
// Deprecated: Please use URL instead.
|
|
Link string `json:"link,omitempty"`
|
|
}
|
|
|
|
func pipelineFlags() []cli.Flag {
|
|
return []cli.Flag{
|
|
&cli.IntFlag{
|
|
Name: "pipeline.number",
|
|
Usage: "pipeline number",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_NUMBER",
|
|
"DRONE_BUILD_NUMBER",
|
|
),
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "pipeline.status",
|
|
Usage: "pipeline status",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_STATUS",
|
|
"DRONE_BUILD_STATUS",
|
|
),
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "pipeline.event",
|
|
Usage: "pipeline event",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_EVENT",
|
|
"DRONE_BUILD_EVENT",
|
|
),
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "pipeline.url",
|
|
Aliases: []string{"pipeline.link"},
|
|
Usage: "pipeline url",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_URL",
|
|
"CI_PIPELINE_LINK",
|
|
"DRONE_BUILD_LINK",
|
|
),
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "pipeline.deploy-target",
|
|
Usage: "pipeline deployment target",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_DEPLOY_TARGET",
|
|
"DRONE_DEPLOY_TO",
|
|
),
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "pipeline.created",
|
|
Usage: "pipeline creation time",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_CREATED",
|
|
"DRONE_BUILD_CREATED",
|
|
),
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "pipeline.started",
|
|
Usage: "pipeline start time",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_STARTED",
|
|
"DRONE_BUILD_STARTED",
|
|
),
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "pipeline.finished",
|
|
Usage: "pipeline finish time",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_FINISHED",
|
|
"DRONE_BUILD_FINISHED",
|
|
),
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "pipeline.parent",
|
|
Usage: "pipeline parent",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_PARENT",
|
|
"DRONE_BUILD_PARENT",
|
|
),
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "pipeline.files",
|
|
Usage: "pipeline changed files (string list as json)",
|
|
Sources: cli.EnvVars(
|
|
"CI_PIPELINE_FILES",
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
func pipelineFromContext(c *cli.Command) Pipeline {
|
|
var changedFiles []string
|
|
|
|
if files := c.String("pipeline.files"); files != "" {
|
|
if err := json.Unmarshal([]byte(files), &changedFiles); err != nil {
|
|
log.Error().Err(err).Msg("parse \"CI_PIPELINE_FILES\" failed")
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
return Pipeline{
|
|
Number: c.Int("pipeline.number"),
|
|
Status: c.String("pipeline.status"),
|
|
Event: c.String("pipeline.event"),
|
|
URL: c.String("pipeline.url"),
|
|
Link: c.String("pipeline.url"),
|
|
DeployTarget: c.String("pipeline.deploy-target"),
|
|
Created: time.Unix(c.Int("pipeline.created"), 0),
|
|
Started: time.Unix(c.Int("pipeline.started"), 0),
|
|
Finished: time.Unix(c.Int("pipeline.finished"), 0),
|
|
Parent: c.Int("pipeline.parent"),
|
|
ChangedFiles: changedFiles,
|
|
}
|
|
}
|