0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-21 14:10:39 +00:00

feat: add missing metadata (#15)

This commit is contained in:
Robert Kaussow 2023-08-16 15:02:13 +02:00 committed by GitHub
parent 7c3ed98b56
commit 60acccde4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 96 additions and 60 deletions

View File

@ -36,7 +36,7 @@ linters:
- gocritic - gocritic
- gocyclo - gocyclo
- godot - godot
- godox # - godox
- goerr113 - goerr113
- gofmt - gofmt
- gofumpt - gofumpt

View File

@ -21,28 +21,35 @@ import (
type ( type (
// Commit defines runtime metadata for a commit. // Commit defines runtime metadata for a commit.
Commit struct { Commit struct {
Sha string `json:"sha,omitempty"` URL string
Ref string `json:"ref,omitempty"` SHA string
Refspec string `json:"refspec,omitempty"` Ref string
PullRequest string `json:"pull_request,omitempty"` Refspec string
SourceBranch string `json:"source_branch,omitempty"` PullRequest string
TargetBranch string `json:"target_branch,omitempty"` SourceBranch string
Branch string `json:"branch,omitempty"` TargetBranch string
Tag string `json:"tag,omitempty"` Branch string
Message string `json:"message,omitempty"` Tag string
Author Author `json:"author,omitempty"` Message string
Author Author
} }
// Author defines runtime metadata for a commit author. // Author defines runtime metadata for a commit author.
Author struct { Author struct {
Name string `json:"name,omitempty"` Name string
Email string `json:"email,omitempty"` Email string
Avatar string `json:"avatar,omitempty"` Avatar string
} }
) )
func currFlags(category string) []cli.Flag { func currFlags(category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{
Name: "commit.url",
Usage: "commit URL",
EnvVars: []string{"CI_COMMIT_URL"},
Category: category,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.sha", Name: "commit.sha",
Usage: "commit SHA", Usage: "commit SHA",
@ -120,7 +127,8 @@ func currFlags(category string) []cli.Flag {
func currFromContext(c *cli.Context) Commit { func currFromContext(c *cli.Context) Commit {
return Commit{ return Commit{
Sha: c.String("commit.sha"), URL: c.String("commit.url"),
SHA: c.String("commit.sha"),
Ref: c.String("commit.ref"), Ref: c.String("commit.ref"),
Refspec: c.String("commit.refspec"), Refspec: c.String("commit.refspec"),
PullRequest: c.String("commit.pull-request"), PullRequest: c.String("commit.pull-request"),
@ -139,6 +147,12 @@ func currFromContext(c *cli.Context) Commit {
func prevFlags(category string) []cli.Flag { func prevFlags(category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{
Name: "prev.commit.url",
Usage: "previous commit URL",
EnvVars: []string{"CI_PREV_COMMIT_URL"},
Category: category,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "prev.commit.sha", Name: "prev.commit.sha",
Usage: "previous commit SHA", Usage: "previous commit SHA",
@ -192,7 +206,8 @@ func prevFlags(category string) []cli.Flag {
func prevFromContext(c *cli.Context) Commit { func prevFromContext(c *cli.Context) Commit {
return Commit{ return Commit{
Sha: c.String("prev.commit.sha"), URL: c.String("prev.commit.url"),
SHA: c.String("prev.commit.sha"),
Ref: c.String("prev.commit.ref"), Ref: c.String("prev.commit.ref"),
Refspec: c.String("prev.commit.refspec"), Refspec: c.String("prev.commit.refspec"),
Branch: c.String("prev.commit.branch"), Branch: c.String("prev.commit.branch"),

View File

@ -20,12 +20,12 @@ import (
// Metadata defines runtime metadata. // Metadata defines runtime metadata.
type Metadata struct { type Metadata struct {
Repository Repository `json:"repo,omitempty"` Repository Repository
Pipeline Pipeline `json:"curr,omitempty"` Pipeline Pipeline
Curr Commit `json:"commit,omitempty"` Curr Commit
Prev Commit `json:"prev,omitempty"` Prev Commit
Step Step `json:"step,omitempty"` Step Step
System System `json:"sys,omitempty"` System System
} }
// MetadataFromContext creates a Metadata from the cli.Context. // MetadataFromContext creates a Metadata from the cli.Context.

View File

@ -22,15 +22,15 @@ import (
// Pipeline defines runtime metadata for a pipeline. // Pipeline defines runtime metadata for a pipeline.
type Pipeline struct { type Pipeline struct {
Number int64 `json:"number,omitempty"` Number int64
Status string `json:"status,omitempty"` Status string
Event string `json:"event,omitempty"` Event string
Link string `json:"link,omitempty"` URL string
DeployTarget string `json:"target,omitempty"` DeployTarget string
Created time.Time `json:"created,omitempty"` Created time.Time
Started time.Time `json:"started,omitempty"` Started time.Time
Finished time.Time `json:"finished,omitempty"` Finished time.Time
Parent int64 `json:"parent,omitempty"` Parent int64
} }
func pipelineFlags(category string) []cli.Flag { func pipelineFlags(category string) []cli.Flag {
@ -54,9 +54,10 @@ func pipelineFlags(category string) []cli.Flag {
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "pipeline.link", Name: "pipeline.url",
Usage: "pipeline link", Usage: "pipeline url",
EnvVars: []string{"CI_PIPELINE_URL"}, // TODO: Revert after https://github.com/woodpecker-ci/woodpecker/issues/2219
// EnvVars: []string{"CI_PIPELINE_URL"},
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
@ -97,7 +98,7 @@ 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"),
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),

View File

@ -17,7 +17,9 @@ package plugin
import ( import (
"context" "context"
"fmt" "fmt"
"net/url"
"os" "os"
"strconv"
"strings" "strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@ -89,6 +91,19 @@ func (p *Plugin) action(ctx *cli.Context) error {
p.Metadata = MetadataFromContext(ctx) p.Metadata = MetadataFromContext(ctx)
p.Network = NetworkFromContext(ctx) p.Network = NetworkFromContext(ctx)
if p.Metadata.Pipeline.URL == "" {
url, err := url.JoinPath(
p.Metadata.System.URL,
"repos",
p.Metadata.Repository.Slug,
"pipeline",
strconv.FormatInt(p.Metadata.Pipeline.Number, 10),
)
if err == nil {
p.Metadata.Pipeline.URL = url
}
}
if p.execute == nil { if p.execute == nil {
panic("plugin execute function is not set") panic("plugin execute function is not set")
} }

View File

@ -20,13 +20,14 @@ import (
// Repository defines runtime metadata for a repository. // Repository defines runtime metadata for a repository.
type Repository struct { type Repository struct {
Slug string `json:"slug,omitempty"` Slug string
Name string `json:"name,omitempty"` Name string
Owner string `json:"owner,omitempty"` Owner string
Link string `json:"link,omitempty"` URL string
CloneURL string `json:"clone_url,omitempty"` CloneURL string
Private bool `json:"private,omitempty"` Private bool
Branch string `json:"default_branch,omitempty"` Branch string
RemoteID int64
} }
func repositoryFlags(category string) []cli.Flag { func repositoryFlags(category string) []cli.Flag {
@ -50,8 +51,8 @@ func repositoryFlags(category string) []cli.Flag {
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "repo.link", Name: "repo.url",
Usage: "repo link", Usage: "repo url",
EnvVars: []string{"CI_REPO_URL"}, EnvVars: []string{"CI_REPO_URL"},
Category: category, Category: category,
}, },
@ -73,6 +74,12 @@ func repositoryFlags(category string) []cli.Flag {
EnvVars: []string{"CI_REPO_DEFAULT_BRANCH"}, EnvVars: []string{"CI_REPO_DEFAULT_BRANCH"},
Category: category, Category: category,
}, },
&cli.Int64Flag{
Name: "repo.remote-id",
Usage: "repo remote id",
EnvVars: []string{"CI_REPO_REMOTE_ID"},
Category: category,
},
} }
} }
@ -81,9 +88,10 @@ func repositoryFromContext(c *cli.Context) Repository {
Slug: c.String("repo.slug"), Slug: c.String("repo.slug"),
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"),
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"),
RemoteID: c.Int64("repo.remote-id"),
} }
} }

View File

@ -22,9 +22,9 @@ import (
// Step defines runtime metadata for a step. // Step defines runtime metadata for a step.
type Step struct { type Step struct {
Number int `json:"number,omitempty"` Number int
Started time.Time `json:"started,omitempty"` Started time.Time
Finished time.Time `json:"finished,omitempty"` Finished time.Time
} }
func stepFlags(category string) []cli.Flag { func stepFlags(category string) []cli.Flag {

View File

@ -20,11 +20,11 @@ import (
// System defines runtime metadata for a ci/cd system. // System defines runtime metadata for a ci/cd system.
type System struct { type System struct {
Name string `json:"name,omitempty"` Name string
Host string `json:"host,omitempty"` Host string
Link string `json:"link,omitempty"` URL string
Platform string `json:"arch,omitempty"` Platform string
Version string `json:"version,omitempty"` Version string
} }
func systemFlags(category string) []cli.Flag { func systemFlags(category string) []cli.Flag {
@ -42,8 +42,8 @@ func systemFlags(category string) []cli.Flag {
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "system.link", Name: "system.url",
Usage: "system link", Usage: "system url",
EnvVars: []string{"CI_SYSTEM_URL"}, EnvVars: []string{"CI_SYSTEM_URL"},
Category: category, Category: category,
}, },
@ -63,13 +63,10 @@ func systemFlags(category string) []cli.Flag {
} }
func systemFromContext(ctx *cli.Context) System { func systemFromContext(ctx *cli.Context) System {
link := ctx.String("system.link")
host := ctx.String("system.host")
return System{ return System{
Name: ctx.String("system.name"), Name: ctx.String("system.name"),
Host: host, Host: ctx.String("system.host"),
Link: link, URL: ctx.String("system.url"),
Platform: ctx.String("system.arch"), Platform: ctx.String("system.arch"),
Version: ctx.String("system.version"), Version: ctx.String("system.version"),
} }