Add & update some env data (#24)

- Some renamings to be more descriptive
- link -> url
- deprecated all old properties

closes #22

Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com>
Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/24
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: anbraten <anbraten@noreply.codeberg.org>
Co-committed-by: anbraten <anbraten@noreply.codeberg.org>
This commit is contained in:
anbraten 2024-02-15 18:53:26 +00:00 committed by 6543
parent 6a59bd9561
commit 090b4571b4
8 changed files with 97 additions and 54 deletions

View File

@ -41,7 +41,7 @@ type (
} }
) )
func currFlags() []cli.Flag { func commitFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.sha", Name: "commit.sha",
@ -106,7 +106,7 @@ func currFlags() []cli.Flag {
} }
} }
func currFromContext(c *cli.Context) Commit { func commitFromContext(c *cli.Context) 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"),
@ -125,7 +125,7 @@ func currFromContext(c *cli.Context) Commit {
} }
} }
func prevFlags() []cli.Flag { func previousCommitFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.sha", Name: "prev.commit.sha",
@ -170,7 +170,7 @@ func prevFlags() []cli.Flag {
} }
} }
func prevFromContext(c *cli.Context) Commit { func previousCommitFromContext(c *cli.Context) 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

@ -25,8 +25,8 @@ func Flags() []cli.Flag {
// Pipeline flags // Pipeline flags
flags = append(flags, repositoryFlags()...) flags = append(flags, repositoryFlags()...)
flags = append(flags, pipelineFlags()...) flags = append(flags, pipelineFlags()...)
flags = append(flags, currFlags()...) flags = append(flags, commitFlags()...)
flags = append(flags, prevFlags()...) flags = append(flags, previousCommitFlags()...)
flags = append(flags, stepFlags()...) flags = append(flags, stepFlags()...)
flags = append(flags, systemFlags()...) flags = append(flags, systemFlags()...)
flags = append(flags, forgeFlags()...) flags = append(flags, forgeFlags()...)

View File

@ -28,6 +28,9 @@ const (
// Forge defines metadata for integration with a forge. // Forge defines metadata for integration with a forge.
type Forge struct { type Forge struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
URL string `json:"url,omitempty"`
// Deprecated: Please use URL instead.
Link string `json:"link,omitempty"` Link string `json:"link,omitempty"`
} }
@ -35,14 +38,15 @@ func forgeFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "forge.type", Name: "forge.type",
Usage: "forge type (gitea, github, gitlab, bitbucker)", Usage: "forge type (gitea, github, gitlab, bitbucket)",
EnvVars: []string{ EnvVars: []string{
"CI_FORGE_TYPE", "CI_FORGE_TYPE",
}, },
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "forge.link", Name: "forge.url",
Usage: "forge link", Aliases: []string{"forge.link"},
Usage: "forge url",
EnvVars: []string{ EnvVars: []string{
"CI_FORGE_URL", "CI_FORGE_URL",
"CI_FORGE_LINK", "CI_FORGE_LINK",
@ -54,6 +58,7 @@ func forgeFlags() []cli.Flag {
func forgeFromContext(ctx *cli.Context) Forge { func forgeFromContext(ctx *cli.Context) Forge {
return Forge{ return Forge{
Type: ctx.String("forge.type"), Type: ctx.String("forge.type"),
Link: ctx.String("forge.link"), Link: ctx.String("forge.url"),
URL: ctx.String("forge.url"),
} }
} }

View File

@ -22,22 +22,34 @@ import (
type Metadata struct { type Metadata struct {
Repository Repository `json:"repo,omitempty"` Repository Repository `json:"repo,omitempty"`
Pipeline Pipeline `json:"curr,omitempty"` Pipeline Pipeline `json:"curr,omitempty"`
Curr Commit `json:"commit,omitempty"` Commit Commit `json:"commit,omitempty"`
Prev Commit `json:"prev,omitempty"` PreviousCommit Commit `json:"previous_commit,omitempty"`
Step Step `json:"step,omitempty"` Step Step `json:"step,omitempty"`
System System `json:"sys,omitempty"` System System `json:"sys,omitempty"`
Forge Forge `json:"forge,omitempty"` Forge Forge `json:"forge,omitempty"`
// Deprecated: Please use Commit instead.
Curr Commit
// Deprecated: Please use PreviousCommit instead.
Prev Commit `json:"prev,omitempty"`
} }
// MetadataFromContext creates a Metadata from the cli.Context. // MetadataFromContext creates a Metadata from the cli.Context.
func MetadataFromContext(ctx *cli.Context) Metadata { func MetadataFromContext(ctx *cli.Context) Metadata {
commit := commitFromContext(ctx)
previousCommit := previousCommitFromContext(ctx)
return Metadata{ return Metadata{
Repository: repositoryFromContext(ctx), Repository: repositoryFromContext(ctx),
Pipeline: pipelineFromContext(ctx), Pipeline: pipelineFromContext(ctx),
Curr: currFromContext(ctx), Commit: commit,
Prev: prevFromContext(ctx), PreviousCommit: previousCommit,
Step: stepFromContext(ctx), Step: stepFromContext(ctx),
System: systemFromContext(ctx), System: systemFromContext(ctx),
Forge: forgeFromContext(ctx), Forge: forgeFromContext(ctx),
Curr: commit,
Prev: previousCommit,
} }
} }

View File

@ -85,7 +85,7 @@ func TestMetadata(t *testing.T) {
// Repository // Repository
assert.Equal(t, "woodpecker", plugin.Metadata.Repository.Name) assert.Equal(t, "woodpecker", plugin.Metadata.Repository.Name)
assert.Equal(t, "woodpecker-ci", plugin.Metadata.Repository.Owner) assert.Equal(t, "woodpecker-ci", plugin.Metadata.Repository.Owner)
assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin", plugin.Metadata.Repository.Link) assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin", plugin.Metadata.Repository.URL)
assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin.git", plugin.Metadata.Repository.CloneURL) assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin.git", plugin.Metadata.Repository.CloneURL)
assert.Equal(t, "main", plugin.Metadata.Repository.Branch) assert.Equal(t, "main", plugin.Metadata.Repository.Branch)
assert.False(t, plugin.Metadata.Repository.Private) assert.False(t, plugin.Metadata.Repository.Private)
@ -103,7 +103,7 @@ func TestMetadata(t *testing.T) {
// Pipeline // Pipeline
assert.Equal(t, int64(1), plugin.Metadata.Pipeline.Number) assert.Equal(t, int64(1), plugin.Metadata.Pipeline.Number)
assert.Equal(t, EventTypePush, plugin.Metadata.Pipeline.Event) assert.Equal(t, EventTypePush, plugin.Metadata.Pipeline.Event)
assert.Equal(t, "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", plugin.Metadata.Pipeline.Link) assert.Equal(t, "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", plugin.Metadata.Pipeline.URL)
assert.Equal(t, "running", plugin.Metadata.Pipeline.Status) assert.Equal(t, "running", plugin.Metadata.Pipeline.Status)
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Created) assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Created)
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Started) assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Started)
@ -114,11 +114,11 @@ func TestMetadata(t *testing.T) {
// System // System
assert.Equal(t, "woodpecker", plugin.Metadata.System.Name) assert.Equal(t, "woodpecker", plugin.Metadata.System.Name)
assert.Equal(t, "https://ci.woodpecker-ci.org", plugin.Metadata.System.Link) assert.Equal(t, "https://ci.woodpecker-ci.org", plugin.Metadata.System.URL)
assert.Equal(t, "1.0.0", plugin.Metadata.System.Version) assert.Equal(t, "1.0.0", plugin.Metadata.System.Version)
assert.Equal(t, "woodpecker-ci.org", plugin.Metadata.System.Host) assert.Equal(t, "woodpecker-ci.org", plugin.Metadata.System.Host)
// Forge // Forge
assert.Equal(t, ForgeTypeGitea, plugin.Metadata.Forge.Type) assert.Equal(t, ForgeTypeGitea, plugin.Metadata.Forge.Type)
assert.Equal(t, "https://codeberg.org", plugin.Metadata.Forge.Link) assert.Equal(t, "https://codeberg.org", plugin.Metadata.Forge.URL)
} }

View File

@ -34,12 +34,15 @@ type Pipeline struct {
Number int64 `json:"number,omitempty"` Number int64 `json:"number,omitempty"`
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`
Event string `json:"event,omitempty"` Event string `json:"event,omitempty"`
Link string `json:"link,omitempty"` URL string `json:"url,omitempty"`
DeployTarget string `json:"target,omitempty"` DeployTarget string `json:"target,omitempty"`
Created time.Time `json:"created,omitempty"` Created time.Time `json:"created,omitempty"`
Started time.Time `json:"started,omitempty"` Started time.Time `json:"started,omitempty"`
Finished time.Time `json:"finished,omitempty"` Finished time.Time `json:"finished,omitempty"`
Parent int64 `json:"parent,omitempty"` Parent int64 `json:"parent,omitempty"`
// Deprecated: Please use URL instead.
Link string `json:"link,omitempty"`
} }
func pipelineFlags() []cli.Flag { func pipelineFlags() []cli.Flag {
@ -69,8 +72,9 @@ func pipelineFlags() []cli.Flag {
}, },
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "pipeline.link", Name: "pipeline.url",
Usage: "pipeline link", Aliases: []string{"pipeline.link"},
Usage: "pipeline url",
EnvVars: []string{ EnvVars: []string{
"CI_PIPELINE_URL", "CI_PIPELINE_URL",
"CI_PIPELINE_LINK", "CI_PIPELINE_LINK",
@ -125,7 +129,8 @@ func pipelineFromContext(c *cli.Context) Pipeline {
Number: c.Int64("pipeline.number"), Number: c.Int64("pipeline.number"),
Status: c.String("pipeline.status"), Status: c.String("pipeline.status"),
Event: c.String("pipeline.event"), Event: c.String("pipeline.event"),
Link: c.String("pipeline.link"), URL: 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.Int64("pipeline.created"), 0),
Started: time.Unix(c.Int64("pipeline.started"), 0), Started: time.Unix(c.Int64("pipeline.started"), 0),

24
repo.go
View File

@ -20,16 +20,28 @@ import (
// Repository defines runtime metadata for a repository. // Repository defines runtime metadata for a repository.
type Repository struct { type Repository struct {
RemoteID string `json:"remote_id,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Owner string `json:"owner,omitempty"` Owner string `json:"owner,omitempty"`
Link string `json:"link,omitempty"` Link string `json:"link,omitempty"`
URL string `json:"url,omitempty"`
CloneURL string `json:"clone_url,omitempty"` CloneURL string `json:"clone_url,omitempty"`
Private bool `json:"private,omitempty"` Private bool `json:"private,omitempty"`
Branch string `json:"default_branch,omitempty"` DefaultBranch string `json:"default_branch,omitempty"`
// Deprecated: Please use DefaultBranch instead.
Branch string
} }
func repositoryFlags() []cli.Flag { func repositoryFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{
Name: "repo.remote-id",
Usage: "repo remote id",
EnvVars: []string{
"CI_REPO_REMOTE_ID",
},
},
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.name", Name: "repo.name",
Usage: "repo name", Usage: "repo name",
@ -47,8 +59,9 @@ func repositoryFlags() []cli.Flag {
}, },
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.link", Name: "repo.url",
Usage: "repo link", Aliases: []string{"repo.link"},
Usage: "repo url",
EnvVars: []string{ EnvVars: []string{
"CI_REPO_URL", "CI_REPO_URL",
"CI_REPO_LINK", "CI_REPO_LINK",
@ -84,11 +97,14 @@ func repositoryFlags() []cli.Flag {
func repositoryFromContext(c *cli.Context) Repository { func repositoryFromContext(c *cli.Context) Repository {
return Repository{ return Repository{
RemoteID: c.String("repo.remote-id"),
Name: c.String("repo.name"), Name: c.String("repo.name"),
Owner: c.String("repo.owner"), Owner: c.String("repo.owner"),
Link: c.String("repo.link"), URL: c.String("repo.url"),
Link: c.String("repo.url"),
CloneURL: c.String("repo.clone-url"), CloneURL: c.String("repo.clone-url"),
Private: c.Bool("repo.private"), Private: c.Bool("repo.private"),
Branch: c.String("repo.default-branch"), Branch: c.String("repo.default-branch"),
DefaultBranch: c.String("repo.default-branch"),
} }
} }

View File

@ -24,9 +24,12 @@ import (
type System struct { type System struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Link string `json:"link,omitempty"`
Platform string `json:"arch,omitempty"` Platform string `json:"arch,omitempty"`
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
URL string `json:"url,omitempty"`
// Deprecated: Please use URL instead.
Link string `json:"link,omitempty"`
} }
func systemFlags() []cli.Flag { func systemFlags() []cli.Flag {
@ -48,8 +51,9 @@ func systemFlags() []cli.Flag {
}, },
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "system.link", Name: "system.url",
Usage: "system link", Aliases: []string{"system.link"},
Usage: "system url",
EnvVars: []string{ EnvVars: []string{
"CI_SYSTEM_URL", "CI_SYSTEM_URL",
"CI_SYSTEM_LINK", "CI_SYSTEM_LINK",
@ -75,19 +79,20 @@ func systemFlags() []cli.Flag {
} }
func systemFromContext(ctx *cli.Context) System { func systemFromContext(ctx *cli.Context) System {
link := ctx.String("system.link") url := ctx.String("system.url")
host := ctx.String("system.host") host := ctx.String("system.host")
if link == "" && host != "" { if url == "" && host != "" {
// Alternative link format used by Drone. // Alternative url format used by Drone.
proto := os.Getenv("DRONE_SYSTEM_PROTO") proto := os.Getenv("DRONE_SYSTEM_PROTO")
if proto != "" { if proto != "" {
link = proto + "://" + host url = proto + "://" + host
} }
} }
return System{ return System{
Name: ctx.String("system.name"), Name: ctx.String("system.name"),
Host: host, Host: host,
Link: link, URL: url,
Link: url,
Platform: ctx.String("system.arch"), Platform: ctx.String("system.arch"),
Version: ctx.String("system.version"), Version: ctx.String("system.version"),
} }