0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-21 04:00:40 +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
- gocyclo
- godot
- godox
# - godox
- goerr113
- gofmt
- gofumpt

View File

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

View File

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

View File

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

View File

@ -17,7 +17,9 @@ package plugin
import (
"context"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
@ -89,6 +91,19 @@ func (p *Plugin) action(ctx *cli.Context) error {
p.Metadata = MetadataFromContext(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 {
panic("plugin execute function is not set")
}

View File

@ -20,13 +20,14 @@ import (
// Repository defines runtime metadata for a repository.
type Repository struct {
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Owner string `json:"owner,omitempty"`
Link string `json:"link,omitempty"`
CloneURL string `json:"clone_url,omitempty"`
Private bool `json:"private,omitempty"`
Branch string `json:"default_branch,omitempty"`
Slug string
Name string
Owner string
URL string
CloneURL string
Private bool
Branch string
RemoteID int64
}
func repositoryFlags(category string) []cli.Flag {
@ -50,8 +51,8 @@ func repositoryFlags(category string) []cli.Flag {
Category: category,
},
&cli.StringFlag{
Name: "repo.link",
Usage: "repo link",
Name: "repo.url",
Usage: "repo url",
EnvVars: []string{"CI_REPO_URL"},
Category: category,
},
@ -73,6 +74,12 @@ func repositoryFlags(category string) []cli.Flag {
EnvVars: []string{"CI_REPO_DEFAULT_BRANCH"},
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"),
Name: c.String("repo.name"),
Owner: c.String("repo.owner"),
Link: c.String("repo.link"),
URL: c.String("repo.url"),
CloneURL: c.String("repo.clone-url"),
Private: c.Bool("repo.private"),
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.
type Step struct {
Number int `json:"number,omitempty"`
Started time.Time `json:"started,omitempty"`
Finished time.Time `json:"finished,omitempty"`
Number int
Started time.Time
Finished time.Time
}
func stepFlags(category string) []cli.Flag {

View File

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