Migrate to github.com/urfave/cli/v3 (#29)

Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/29
Reviewed-by: Patrick Schratz <pat-s@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2024-07-22 23:32:14 +00:00 committed by Patrick Schratz
parent 2e397e1b02
commit 67b6cdf4a6
16 changed files with 268 additions and 207 deletions

View File

@ -25,7 +25,7 @@ import (
"codeberg.org/woodpecker-plugins/go-plugin" "codeberg.org/woodpecker-plugins/go-plugin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
type Settings struct { type Settings struct {
@ -44,7 +44,7 @@ func (p *Plugin) Flags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "sample.flag", Name: "sample.flag",
Usage: "sample flag", Usage: "sample flag",
EnvVars: []string{"PLUGIN_SAMPLE_FLAG"}, Sources: cli.EnvVars("PLUGIN_SAMPLE_FLAG"),
Destination: &p.Settings.SampleFlag, Destination: &p.Settings.SampleFlag,
}, },
} }

100
commit.go
View File

@ -15,7 +15,7 @@
package plugin package plugin
import ( import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
type ( type (
@ -46,67 +46,104 @@ func commitFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.sha", Name: "commit.sha",
Usage: "commit SHA", Usage: "commit SHA",
EnvVars: []string{"CI_COMMIT_SHA", "DRONE_COMMIT", "DRONE_COMMIT_SHA"}, Sources: cli.EnvVars(
"CI_COMMIT_SHA",
"DRONE_COMMIT",
"DRONE_COMMIT_SHA",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.ref", Name: "commit.ref",
Usage: "commit ref", Usage: "commit ref",
EnvVars: []string{"CI_COMMIT_REF", "DRONE_COMMIT_REF"}, Sources: cli.EnvVars(
"CI_COMMIT_REF",
"DRONE_COMMIT_REF",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.refspec", Name: "commit.refspec",
Usage: "commit refspec", Usage: "commit refspec",
EnvVars: []string{"CI_COMMIT_REFSPEC"}, Sources: cli.EnvVars(
"CI_COMMIT_REFSPEC",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.pull-request", Name: "commit.pull-request",
Usage: "commit pull request", Usage: "commit pull request",
EnvVars: []string{"CI_COMMIT_PULL_REQUEST", "DRONE_PULL_REQUEST"}, Sources: cli.EnvVars(
"CI_COMMIT_PULL_REQUEST",
"DRONE_PULL_REQUEST",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.source-branch", Name: "commit.source-branch",
Usage: "commit source branch", Usage: "commit source branch",
EnvVars: []string{"CI_COMMIT_SOURCE_BRANCH", "DRONE_SOURCE_BRANCH"}, Sources: cli.EnvVars(
"CI_COMMIT_SOURCE_BRANCH",
"DRONE_SOURCE_BRANCH",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.target-branch", Name: "commit.target-branch",
Usage: "commit target branch", Usage: "commit target branch",
EnvVars: []string{"CI_COMMIT_TARGET_BRANCH", "DRONE_TARGET_BRANCH"}, Sources: cli.EnvVars(
"CI_COMMIT_TARGET_BRANCH",
"DRONE_TARGET_BRANCH",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.branch", Name: "commit.branch",
Usage: "commit branch", Usage: "commit branch",
EnvVars: []string{"CI_COMMIT_BRANCH", "DRONE_BRANCH"}, Sources: cli.EnvVars(
"CI_COMMIT_BRANCH",
"DRONE_BRANCH",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.tag", Name: "commit.tag",
Usage: "commit tag", Usage: "commit tag",
EnvVars: []string{"CI_COMMIT_TAG", "DRONE_TAG"}, Sources: cli.EnvVars(
"CI_COMMIT_TAG",
"DRONE_TAG",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.message", Name: "commit.message",
Usage: "commit message", Usage: "commit message",
EnvVars: []string{"CI_COMMIT_MESSAGE", "DRONE_COMMIT_MESSAGE"}, Sources: cli.EnvVars(
"CI_COMMIT_MESSAGE",
"DRONE_COMMIT_MESSAGE",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.author.name", Name: "commit.author.name",
Usage: "commit author name", Usage: "commit author name",
EnvVars: []string{"CI_COMMIT_AUTHOR", "DRONE_COMMIT_AUTHOR", "DRONE_COMMIT_AUTHOR_NAME"}, Sources: cli.EnvVars(
"CI_COMMIT_AUTHOR",
"DRONE_COMMIT_AUTHOR",
"DRONE_COMMIT_AUTHOR_NAME",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.author.email", Name: "commit.author.email",
Usage: "commit author email", Usage: "commit author email",
EnvVars: []string{"CI_COMMIT_AUTHOR_EMAIL", "DRONE_COMMIT_AUTHOR_EMAIL"}, Sources: cli.EnvVars(
"CI_COMMIT_AUTHOR_EMAIL",
"DRONE_COMMIT_AUTHOR_EMAIL",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.author.avatar", Name: "commit.author.avatar",
Usage: "commit author avatar", Usage: "commit author avatar",
EnvVars: []string{"CI_COMMIT_AUTHOR_AVATAR", "DRONE_COMMIT_AUTHOR_AVATAR"}, Sources: cli.EnvVars(
"CI_COMMIT_AUTHOR_AVATAR",
"DRONE_COMMIT_AUTHOR_AVATAR",
),
}, },
} }
} }
func commitFromContext(c *cli.Context) Commit { func commitFromContext(c *cli.Command) Commit {
return Commit{ return Commit{
Sha: c.String("commit.sha"), Sha: c.String("commit.sha"),
Ref: c.String("commit.ref"), Ref: c.String("commit.ref"),
@ -130,47 +167,64 @@ func previousCommitFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.sha", Name: "prev.commit.sha",
Usage: "previous commit SHA", Usage: "previous commit SHA",
EnvVars: []string{"CI_PREV_COMMIT_SHA", "DRONE_COMMIT_BEFORE"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_SHA",
"DRONE_COMMIT_BEFORE",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.ref", Name: "prev.commit.ref",
Usage: "previous commit ref", Usage: "previous commit ref",
EnvVars: []string{"CI_PREV_COMMIT_REF"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_REF",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.refspec", Name: "prev.commit.refspec",
Usage: "previous commit refspec", Usage: "previous commit refspec",
EnvVars: []string{"CI_PREV_COMMIT_REFSPEC"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_REFSPEC",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.branch", Name: "prev.commit.branch",
Usage: "previous commit branch", Usage: "previous commit branch",
EnvVars: []string{"CI_PREV_COMMIT_BRANCH"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_BRANCH",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.message", Name: "prev.commit.message",
Usage: "previous commit message", Usage: "previous commit message",
EnvVars: []string{"CI_PREV_COMMIT_MESSAGE"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_MESSAGE",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.author.name", Name: "prev.commit.author.name",
Usage: "previous commit author name", Usage: "previous commit author name",
EnvVars: []string{"CI_PREV_COMMIT_AUTHOR"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_AUTHOR",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.author.email", Name: "prev.commit.author.email",
Usage: "previous commit author email", Usage: "previous commit author email",
EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_EMAIL"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_AUTHOR_EMAIL",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.author.avatar", Name: "prev.commit.author.avatar",
Usage: "previous commit author avatar", Usage: "previous commit author avatar",
EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_AVATAR"}, Sources: cli.EnvVars(
"CI_PREV_COMMIT_AUTHOR_AVATAR",
),
}, },
} }
} }
func previousCommitFromContext(c *cli.Context) Commit { func previousCommitFromContext(c *cli.Command) Commit {
return Commit{ return Commit{
Sha: c.String("prev.commit.sha"), Sha: c.String("prev.commit.sha"),
Ref: c.String("prev.commit.ref"), Ref: c.String("prev.commit.ref"),

View File

@ -15,7 +15,7 @@
package plugin package plugin
import ( import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
// Flags has the cli.Flags for the Woodpecker plugin. // Flags has the cli.Flags for the Woodpecker plugin.

View File

@ -15,7 +15,7 @@
package plugin package plugin
import ( import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
const ( const (
@ -40,26 +40,26 @@ func forgeFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "forge.type", Name: "forge.type",
Usage: "forge type (gitea, forgejo, github, gitlab, bitbucket)", Usage: "forge type (gitea, forgejo, github, gitlab, bitbucket)",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_FORGE_TYPE", "CI_FORGE_TYPE",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "forge.url", Name: "forge.url",
Aliases: []string{"forge.link"}, Aliases: []string{"forge.link"},
Usage: "forge url", Usage: "forge url",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_FORGE_URL", "CI_FORGE_URL",
"CI_FORGE_LINK", "CI_FORGE_LINK",
}, ),
}, },
} }
} }
func forgeFromContext(ctx *cli.Context) Forge { func forgeFromContext(c *cli.Command) Forge {
return Forge{ return Forge{
Type: ctx.String("forge.type"), Type: c.String("forge.type"),
Link: ctx.String("forge.url"), Link: c.String("forge.url"),
URL: ctx.String("forge.url"), URL: c.String("forge.url"),
} }
} }

5
go.mod
View File

@ -6,18 +6,15 @@ require (
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/rs/zerolog v1.33.0 github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.2 github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f
golang.org/x/net v0.27.0 golang.org/x/net v0.27.0
) )
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/sys v0.22.0 // indirect golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

10
go.sum
View File

@ -1,6 +1,4 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@ -17,14 +15,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f h1:yCJ90PBe7+45EQSF3qJXyAGW5rkE65lE8huv5pM0HY8=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f/go.mod h1:Z1ItyMma7t6I7zHG9OpbExhHQOSkFf/96n+mAZ9MtVI=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

26
http.go
View File

@ -23,7 +23,7 @@ import (
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
) )
@ -32,28 +32,34 @@ func httpClientFlags() []cli.Flag {
&cli.BoolFlag{ &cli.BoolFlag{
Name: "transport.skip-verify", Name: "transport.skip-verify",
Usage: "skip ssl verify", Usage: "skip ssl verify",
EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, Sources: cli.EnvVars(
"PLUGIN_SKIP_VERIFY",
),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "transport.socks-proxy", Name: "transport.socks-proxy",
Usage: "socks proxy address", Usage: "socks proxy address",
EnvVars: []string{"SOCKS_PROXY"}, Sources: cli.EnvVars(
"SOCKS_PROXY",
),
Hidden: true, Hidden: true,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "transport.socks-proxy-off", Name: "transport.socks-proxy-off",
Usage: "socks proxy ignored", Usage: "socks proxy ignored",
EnvVars: []string{"SOCKS_PROXY_OFF"}, Sources: cli.EnvVars(
"SOCKS_PROXY_OFF",
),
Hidden: true, Hidden: true,
}, },
} }
} }
func HTTPClientFromContext(ctx *cli.Context) *http.Client { func HTTPClientFromContext(c *cli.Command) *http.Client {
var ( var (
skip = ctx.Bool("transport.skip-verify") skip = c.Bool("transport.skip-verify")
socks = ctx.String("transport.socks-proxy") socks = c.String("transport.socks-proxy")
socksoff = ctx.Bool("transport.socks-proxy-off") socksOff = c.Bool("transport.socks-proxy-off")
) )
certs, err := x509.SystemCertPool() certs, err := x509.SystemCertPool()
@ -80,7 +86,7 @@ func HTTPClientFromContext(ctx *cli.Context) *http.Client {
DualStack: true, DualStack: true,
} }
if len(socks) != 0 && !socksoff { if len(socks) != 0 && !socksOff {
proxyDialer, err := proxy.SOCKS5("tcp", socks, nil, dialer) proxyDialer, err := proxy.SOCKS5("tcp", socks, nil, dialer)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to create socks proxy") log.Error().Err(err).Msg("failed to create socks proxy")
@ -88,7 +94,7 @@ func HTTPClientFromContext(ctx *cli.Context) *http.Client {
if contextDialer, ok := proxyDialer.(proxy.ContextDialer); ok { if contextDialer, ok := proxyDialer.(proxy.ContextDialer); ok {
transport.DialContext = contextDialer.DialContext transport.DialContext = contextDialer.DialContext
} else { } else {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { transport.DialContext = func(_ context.Context, network, addr string) (net.Conn, error) {
return proxyDialer.Dial(network, addr) return proxyDialer.Dial(network, addr)
} }
} }

View File

@ -19,7 +19,7 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
func loggingFlags() []cli.Flag { func loggingFlags() []cli.Flag {
@ -27,14 +27,16 @@ func loggingFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "log-level", Name: "log-level",
Usage: "log level", Usage: "log level",
EnvVars: []string{"PLUGIN_LOG_LEVEL"}, Sources: cli.EnvVars(
"PLUGIN_LOG_LEVEL",
),
Value: "info", Value: "info",
}, },
} }
} }
// SetupConsoleLogger sets up the console logger. // SetupConsoleLogger sets up the console logger.
func SetupConsoleLogger(c *cli.Context) error { func SetupConsoleLogger(c *cli.Command) error {
level := c.String("log-level") level := c.String("log-level")
lvl, err := zerolog.ParseLevel(level) lvl, err := zerolog.ParseLevel(level)
if err != nil { if err != nil {

View File

@ -15,7 +15,7 @@
package plugin package plugin
import ( import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
// Metadata defines runtime metadata. // Metadata defines runtime metadata.
@ -36,18 +36,18 @@ type Metadata struct {
} }
// MetadataFromContext creates a Metadata from the cli.Context. // MetadataFromContext creates a Metadata from the cli.Context.
func MetadataFromContext(ctx *cli.Context) Metadata { func MetadataFromContext(c *cli.Command) Metadata {
commit := commitFromContext(ctx) commit := commitFromContext(c)
previousCommit := previousCommitFromContext(ctx) previousCommit := previousCommitFromContext(c)
return Metadata{ return Metadata{
Repository: repositoryFromContext(ctx), Repository: repositoryFromContext(c),
Pipeline: pipelineFromContext(ctx), Pipeline: pipelineFromContext(c),
Commit: commit, Commit: commit,
PreviousCommit: previousCommit, PreviousCommit: previousCommit,
Step: stepFromContext(ctx), Step: stepFromContext(c),
System: systemFromContext(ctx), System: systemFromContext(c),
Forge: forgeFromContext(ctx), Forge: forgeFromContext(c),
Curr: commit, Curr: commit,
Prev: previousCommit, Prev: previousCommit,

View File

@ -79,7 +79,7 @@ func TestMetadata(t *testing.T) {
return nil return nil
}, },
}) })
err := plugin.app.Run([]string{"test"}) err := plugin.app.Run(plugin.ctx, []string{"test"})
require.NoError(t, err) require.NoError(t, err)
// Repository // Repository

View File

@ -17,7 +17,7 @@ package plugin
import ( import (
"time" "time"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
const ( const (
@ -47,94 +47,94 @@ type Pipeline struct {
func pipelineFlags() []cli.Flag { func pipelineFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.Int64Flag{ &cli.IntFlag{
Name: "pipeline.number", Name: "pipeline.number",
Usage: "pipeline number", Usage: "pipeline number",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_NUMBER", "CI_PIPELINE_NUMBER",
"DRONE_BUILD_NUMBER", "DRONE_BUILD_NUMBER",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "pipeline.status", Name: "pipeline.status",
Usage: "pipeline status", Usage: "pipeline status",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_STATUS", "CI_PIPELINE_STATUS",
"DRONE_BUILD_STATUS", "DRONE_BUILD_STATUS",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "pipeline.event", Name: "pipeline.event",
Usage: "pipeline event", Usage: "pipeline event",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_EVENT", "CI_PIPELINE_EVENT",
"DRONE_BUILD_EVENT", "DRONE_BUILD_EVENT",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "pipeline.url", Name: "pipeline.url",
Aliases: []string{"pipeline.link"}, Aliases: []string{"pipeline.link"},
Usage: "pipeline url", Usage: "pipeline url",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_URL", "CI_PIPELINE_URL",
"CI_PIPELINE_LINK", "CI_PIPELINE_LINK",
"DRONE_BUILD_LINK", "DRONE_BUILD_LINK",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "pipeline.deploy-target", Name: "pipeline.deploy-target",
Usage: "pipeline deployment target", Usage: "pipeline deployment target",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_DEPLOY_TARGET", "CI_PIPELINE_DEPLOY_TARGET",
"DRONE_DEPLOY_TO", "DRONE_DEPLOY_TO",
),
}, },
}, &cli.IntFlag{
&cli.Int64Flag{
Name: "pipeline.created", Name: "pipeline.created",
Usage: "pipeline creation time", Usage: "pipeline creation time",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_CREATED", "CI_PIPELINE_CREATED",
"DRONE_BUILD_CREATED", "DRONE_BUILD_CREATED",
),
}, },
}, &cli.IntFlag{
&cli.Int64Flag{
Name: "pipeline.started", Name: "pipeline.started",
Usage: "pipeline start time", Usage: "pipeline start time",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_STARTED", "CI_PIPELINE_STARTED",
"DRONE_BUILD_STARTED", "DRONE_BUILD_STARTED",
),
}, },
}, &cli.IntFlag{
&cli.Int64Flag{
Name: "pipeline.finished", Name: "pipeline.finished",
Usage: "pipeline finish time", Usage: "pipeline finish time",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_FINISHED", "CI_PIPELINE_FINISHED",
"DRONE_BUILD_FINISHED", "DRONE_BUILD_FINISHED",
),
}, },
}, &cli.IntFlag{
&cli.Int64Flag{
Name: "pipeline.parent", Name: "pipeline.parent",
Usage: "pipeline parent", Usage: "pipeline parent",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_PIPELINE_PARENT", "CI_PIPELINE_PARENT",
"DRONE_BUILD_PARENT", "DRONE_BUILD_PARENT",
}, ),
}, },
} }
} }
func pipelineFromContext(c *cli.Context) Pipeline { func pipelineFromContext(c *cli.Command) Pipeline {
return Pipeline{ return Pipeline{
Number: c.Int64("pipeline.number"), Number: c.Int("pipeline.number"),
Status: c.String("pipeline.status"), Status: c.String("pipeline.status"),
Event: c.String("pipeline.event"), Event: c.String("pipeline.event"),
URL: c.String("pipeline.url"), URL: c.String("pipeline.url"),
Link: c.String("pipeline.url"), Link: c.String("pipeline.url"),
DeployTarget: c.String("pipeline.deploy-target"), DeployTarget: c.String("pipeline.deploy-target"),
Created: time.Unix(c.Int64("pipeline.created"), 0), Created: time.Unix(c.Int("pipeline.created"), 0),
Started: time.Unix(c.Int64("pipeline.started"), 0), Started: time.Unix(c.Int("pipeline.started"), 0),
Finished: time.Unix(c.Int64("pipeline.finished"), 0), Finished: time.Unix(c.Int("pipeline.finished"), 0),
Parent: c.Int64("pipeline.parent"), Parent: c.Int("pipeline.parent"),
} }
} }

View File

@ -21,7 +21,7 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
// Options defines the options for the plugin. // Options defines the options for the plugin.
@ -36,13 +36,16 @@ type Options struct {
Flags []cli.Flag Flags []cli.Flag
// Execute function of the plugin. // Execute function of the plugin.
Execute ExecuteFunc Execute ExecuteFunc
// Context the plugin will use while executing.
Context context.Context
} }
// Plugin defines the plugin instance. // Plugin defines the plugin instance.
type Plugin struct { type Plugin struct {
app *cli.App app *cli.Command
execute ExecuteFunc execute ExecuteFunc
client *http.Client client *http.Client
ctx context.Context
// Metadata of the current pipeline. // Metadata of the current pipeline.
Metadata Metadata Metadata Metadata
} }
@ -56,7 +59,7 @@ func New(opt Options) *Plugin {
_ = godotenv.Overload("/run/woodpecker/env") _ = godotenv.Overload("/run/woodpecker/env")
} }
app := &cli.App{ app := &cli.Command{
Name: opt.Name, Name: opt.Name,
Usage: "Run the Woodpecker CI plugin", Usage: "Run the Woodpecker CI plugin",
Description: opt.Description, Description: opt.Description,
@ -67,25 +70,30 @@ func New(opt Options) *Plugin {
plugin := &Plugin{ plugin := &Plugin{
app: app, app: app,
execute: opt.Execute, execute: opt.Execute,
ctx: opt.Context,
} }
plugin.app.Action = plugin.action plugin.app.Action = plugin.action
if plugin.ctx == nil {
plugin.ctx = context.Background()
}
return plugin return plugin
} }
func (p *Plugin) action(ctx *cli.Context) error { func (p *Plugin) action(ctx context.Context, c *cli.Command) error {
if err := SetupConsoleLogger(ctx); err != nil { if err := SetupConsoleLogger(c); err != nil {
return err return err
} }
p.Metadata = MetadataFromContext(ctx) p.Metadata = MetadataFromContext(c)
p.client = HTTPClientFromContext(ctx) p.client = HTTPClientFromContext(c)
if p.execute == nil { if p.execute == nil {
panic("plugin execute function is not set") panic("plugin execute function is not set")
} }
return p.execute(ctx.Context) return p.execute(ctx)
} }
// HTTPClient returns the http.Client instance. // HTTPClient returns the http.Client instance.
@ -95,7 +103,7 @@ func (p *Plugin) HTTPClient() *http.Client {
// Run the plugin. // Run the plugin.
func (p *Plugin) Run() { func (p *Plugin) Run() {
if err := p.app.Run(os.Args); err != nil { if err := p.app.Run(p.ctx, os.Args); err != nil {
log.Error().Err(err).Msg("execution failed") log.Error().Err(err).Msg("execution failed")
os.Exit(1) os.Exit(1)
} }

View File

@ -30,7 +30,7 @@ func TestPlugin(t *testing.T) {
return nil return nil
}, },
}) })
err := p.app.Run([]string{"test"}) err := p.app.Run(p.ctx, []string{"test"})
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, executed) assert.True(t, executed)
} }

32
repo.go
View File

@ -15,7 +15,7 @@
package plugin package plugin
import ( import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
// Repository defines runtime metadata for a repository. // Repository defines runtime metadata for a repository.
@ -38,64 +38,64 @@ func repositoryFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.remote-id", Name: "repo.remote-id",
Usage: "repo remote id", Usage: "repo remote id",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_REMOTE_ID", "CI_REPO_REMOTE_ID",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.name", Name: "repo.name",
Usage: "repo name", Usage: "repo name",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_NAME", "CI_REPO_NAME",
"DRONE_REPO_NAME", "DRONE_REPO_NAME",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.owner", Name: "repo.owner",
Usage: "repo owner", Usage: "repo owner",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_OWNER", "CI_REPO_OWNER",
"DRONE_REPO_OWNER", "DRONE_REPO_OWNER",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.url", Name: "repo.url",
Aliases: []string{"repo.link"}, Aliases: []string{"repo.link"},
Usage: "repo url", Usage: "repo url",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_URL", "CI_REPO_URL",
"CI_REPO_LINK", "CI_REPO_LINK",
"DRONE_REPO_LINK", "DRONE_REPO_LINK",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.clone-url", Name: "repo.clone-url",
Usage: "repo clone url", Usage: "repo clone url",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_CLONE_URL", "CI_REPO_CLONE_URL",
"DRONE_GIT_HTTP_URL", "DRONE_GIT_HTTP_URL",
}, ),
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "repo.private", Name: "repo.private",
Usage: "repo private", Usage: "repo private",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_PRIVATE", "CI_REPO_PRIVATE",
"DRONE_REPO_PRIVATE", "DRONE_REPO_PRIVATE",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.default-branch", Name: "repo.default-branch",
Usage: "repo default branch", Usage: "repo default branch",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_REPO_DEFAULT_BRANCH", "CI_REPO_DEFAULT_BRANCH",
"DRONE_REPO_BRANCH", "DRONE_REPO_BRANCH",
}, ),
}, },
} }
} }
func repositoryFromContext(c *cli.Context) Repository { func repositoryFromContext(c *cli.Command) Repository {
return Repository{ return Repository{
RemoteID: c.String("repo.remote-id"), RemoteID: c.String("repo.remote-id"),
Name: c.String("repo.name"), Name: c.String("repo.name"),

26
step.go
View File

@ -17,7 +17,7 @@ package plugin
import ( import (
"time" "time"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
// Step defines runtime metadata for a step. // Step defines runtime metadata for a step.
@ -32,32 +32,32 @@ func stepFlags() []cli.Flag {
&cli.IntFlag{ &cli.IntFlag{
Name: "step.number", Name: "step.number",
Usage: "step number", Usage: "step number",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_STEP_NUMBER", "CI_STEP_NUMBER",
"DRONE_STEP_NUMBER", "DRONE_STEP_NUMBER",
),
}, },
}, &cli.IntFlag{
&cli.Int64Flag{
Name: "step.started", Name: "step.started",
Usage: "step start time", Usage: "step start time",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_STEP_STARTED", "CI_STEP_STARTED",
),
}, },
}, &cli.IntFlag{
&cli.Int64Flag{
Name: "step.finished", Name: "step.finished",
Usage: "step finish time", Usage: "step finish time",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_STEP_FINISHED", "CI_STEP_FINISHED",
}, ),
}, },
} }
} }
func stepFromContext(c *cli.Context) Step { func stepFromContext(c *cli.Command) Step {
return Step{ return Step{
Number: c.Int("step.number"), Number: int(c.Int("step.number")),
Started: time.Unix(c.Int64("step.started"), 0), Started: time.Unix(c.Int("step.started"), 0),
Finished: time.Unix(c.Int64("step.finished"), 0), Finished: time.Unix(c.Int("step.finished"), 0),
} }
} }

View File

@ -17,7 +17,7 @@ package plugin
import ( import (
"os" "os"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v3"
) )
// System defines runtime metadata for a ci/cd system. // System defines runtime metadata for a ci/cd system.
@ -37,50 +37,50 @@ func systemFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "system.name", Name: "system.name",
Usage: "system name", Usage: "system name",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_SYSTEM_NAME", "CI_SYSTEM_NAME",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "system.host", Name: "system.host",
Usage: "system host", Usage: "system host",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_SYSTEM_HOST", "CI_SYSTEM_HOST",
"DRONE_SYSTEM_HOST", "DRONE_SYSTEM_HOST",
"DRONE_SYSTEM_HOSTNAME", "DRONE_SYSTEM_HOSTNAME",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "system.url", Name: "system.url",
Aliases: []string{"system.link"}, Aliases: []string{"system.link"},
Usage: "system url", Usage: "system url",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_SYSTEM_URL", "CI_SYSTEM_URL",
"CI_SYSTEM_LINK", "CI_SYSTEM_LINK",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "system.arch", Name: "system.arch",
Usage: "system arch", Usage: "system arch",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_SYSTEM_PLATFORM", "CI_SYSTEM_PLATFORM",
"DRONE_STAGE_ARCH", "DRONE_STAGE_ARCH",
}, ),
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "system.version", Name: "system.version",
Usage: "system version", Usage: "system version",
EnvVars: []string{ Sources: cli.EnvVars(
"CI_SYSTEM_VERSION", "CI_SYSTEM_VERSION",
"DRONE_SYSTEM_VERSION", "DRONE_SYSTEM_VERSION",
}, ),
}, },
} }
} }
func systemFromContext(ctx *cli.Context) System { func systemFromContext(c *cli.Command) System {
url := ctx.String("system.url") url := c.String("system.url")
host := ctx.String("system.host") host := c.String("system.host")
if url == "" && host != "" { if url == "" && host != "" {
// Alternative url format used by Drone. // Alternative url format used by Drone.
proto := os.Getenv("DRONE_SYSTEM_PROTO") proto := os.Getenv("DRONE_SYSTEM_PROTO")
@ -89,11 +89,11 @@ func systemFromContext(ctx *cli.Context) System {
} }
} }
return System{ return System{
Name: ctx.String("system.name"), Name: c.String("system.name"),
Host: host, Host: host,
URL: url, URL: url,
Link: url, Link: url,
Platform: ctx.String("system.arch"), Platform: c.String("system.arch"),
Version: ctx.String("system.version"), Version: c.String("system.version"),
} }
} }