From 4138e06711d1d9852a59baed78b44dd33853a06e Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Tue, 7 May 2024 15:03:20 +0200 Subject: [PATCH] docs: hide system flags and refactor generator (#112) --- Makefile | 3 +- cmd/wp-github-comment/docs.go | 58 ------------- cmd/wp-github-comment/flags.go | 61 ------------- cmd/wp-github-comment/main.go | 15 +--- .../templates/docs-data.yaml.tmpl | 18 ---- docs/data/data.yaml | 14 +++ go.mod | 8 +- go.sum | 16 ++-- internal/doc/main.go | 42 +++++++++ plugin/comment.go | 3 +- plugin/impl.go | 2 +- plugin/plugin.go | 86 +++++++++++++++++-- 12 files changed, 153 insertions(+), 173 deletions(-) delete mode 100644 cmd/wp-github-comment/docs.go delete mode 100644 cmd/wp-github-comment/flags.go delete mode 100644 cmd/wp-github-comment/templates/docs-data.yaml.tmpl create mode 100644 internal/doc/main.go diff --git a/Makefile b/Makefile index f13ffee..33614b1 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,6 @@ GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(G XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest GOTESTSUM_PACKAGE ?= gotest.tools/gotestsum@latest -GENERATE ?= XGO_VERSION := go-1.22.x XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64 @@ -65,7 +64,7 @@ lint: golangci-lint .PHONY: generate generate: - $(GO) generate $(GENERATE) + $(GO) generate $(PACKAGES) .PHONY: generate-docs generate-docs: diff --git a/cmd/wp-github-comment/docs.go b/cmd/wp-github-comment/docs.go deleted file mode 100644 index f0f0ba9..0000000 --- a/cmd/wp-github-comment/docs.go +++ /dev/null @@ -1,58 +0,0 @@ -//go:build generate -// +build generate - -package main - -import ( - "bytes" - "embed" - "fmt" - "os" - "text/template" - - "github.com/thegeeklab/wp-github-comment/plugin" - "github.com/thegeeklab/wp-plugin-go/docs" - wp "github.com/thegeeklab/wp-plugin-go/plugin" - wp_template "github.com/thegeeklab/wp-plugin-go/template" - "github.com/urfave/cli/v2" -) - -//go:embed templates/docs-data.yaml.tmpl -var yamlTemplate embed.FS - -func main() { - settings := &plugin.Settings{} - app := &cli.App{ - Flags: settingsFlags(settings, wp.FlagsPluginCategory), - } - - out, err := toYAML(app) - if err != nil { - panic(err) - } - - fi, err := os.Create("../../docs/data/data-raw.yaml") - if err != nil { - panic(err) - } - defer fi.Close() - if _, err := fi.WriteString(out); err != nil { - panic(err) - } -} - -func toYAML(app *cli.App) (string, error) { - var w bytes.Buffer - - yamlTmpl, err := template.New("docs").Funcs(wp_template.LoadFuncMap()).ParseFS(yamlTemplate, "templates/docs-data.yaml.tmpl") - if err != nil { - fmt.Println(yamlTmpl) - return "", err - } - - if err := yamlTmpl.ExecuteTemplate(&w, "docs-data.yaml.tmpl", docs.GetTemplateData(app)); err != nil { - return "", err - } - - return w.String(), nil -} diff --git a/cmd/wp-github-comment/flags.go b/cmd/wp-github-comment/flags.go deleted file mode 100644 index afcaa51..0000000 --- a/cmd/wp-github-comment/flags.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "github.com/thegeeklab/wp-github-comment/plugin" - "github.com/urfave/cli/v2" -) - -// settingsFlags has the cli.Flags for the plugin.Settings. -// -//go:generate go run docs.go flags.go -func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: "api-key", - EnvVars: []string{"PLUGIN_API_KEY", "GITHUB_COMMENT_API_KEY"}, - Usage: "personal access token to access the GitHub API", - Destination: &settings.APIKey, - Category: category, - Required: true, - }, - &cli.StringFlag{ - Name: "base-url", - EnvVars: []string{"PLUGIN_BASE_URL", "GITHUB_COMMENT_BASE_URL"}, - Usage: "API URL", - Value: "https://api.github.com/", - Destination: &settings.BaseURL, - Category: category, - }, - &cli.StringFlag{ - Name: "key", - EnvVars: []string{"PLUGIN_KEY", "GITHUB_COMMENT_KEY"}, - Usage: "unique identifier to assign to a comment", - Destination: &settings.Key, - Category: category, - }, - &cli.StringFlag{ - Name: "message", - EnvVars: []string{"PLUGIN_MESSAGE", "GITHUB_COMMENT_MESSAGE"}, - Usage: "path to file or string that contains the comment text", - Destination: &settings.Message, - Category: category, - Required: true, - }, - &cli.BoolFlag{ - Name: "update", - EnvVars: []string{"PLUGIN_UPDATE", "GITHUB_COMMENT_UPDATE"}, - Usage: "enable update of an existing comment that matches the key", - Value: false, - Destination: &settings.Update, - Category: category, - }, - &cli.BoolFlag{ - Name: "skip-missing", - EnvVars: []string{"PLUGIN_SKIP_MISSING", "GITHUB_COMMENT_SKIP_MISSING"}, - Usage: "skip comment creation if the given message file does not exist", - Value: false, - Destination: &settings.SkipMissing, - Category: category, - }, - } -} diff --git a/cmd/wp-github-comment/main.go b/cmd/wp-github-comment/main.go index d9cb59d..93888ed 100644 --- a/cmd/wp-github-comment/main.go +++ b/cmd/wp-github-comment/main.go @@ -1,11 +1,7 @@ package main import ( - "fmt" - "github.com/thegeeklab/wp-github-comment/plugin" - - wp "github.com/thegeeklab/wp-plugin-go/plugin" ) //nolint:gochecknoglobals @@ -15,14 +11,5 @@ var ( ) func main() { - settings := &plugin.Settings{} - options := wp.Options{ - Name: "wp-github-comment", - Description: "Add comments to GitHub Issues and Pull Requests", - Version: BuildVersion, - VersionMetadata: fmt.Sprintf("date=%s", BuildDate), - Flags: settingsFlags(settings, wp.FlagsPluginCategory), - } - - plugin.New(options, settings).Run() + plugin.New(nil, BuildVersion, BuildDate).Run() } diff --git a/cmd/wp-github-comment/templates/docs-data.yaml.tmpl b/cmd/wp-github-comment/templates/docs-data.yaml.tmpl deleted file mode 100644 index e453a95..0000000 --- a/cmd/wp-github-comment/templates/docs-data.yaml.tmpl +++ /dev/null @@ -1,18 +0,0 @@ ---- -{{- if .GlobalArgs }} -properties: -{{- range $v := .GlobalArgs }} - - name: {{ $v.Name }} - {{- with $v.Description }} - description: | - {{ . | ToSentence }} - {{- end }} - {{- with $v.Type }} - type: {{ . }} - {{- end }} - {{- with $v.Default }} - defaultValue: {{ . }} - {{- end }} - required: {{ default false $v.Required }} -{{ end -}} -{{ end -}} diff --git a/docs/data/data.yaml b/docs/data/data.yaml index 09e47e7..76ad48b 100644 --- a/docs/data/data.yaml +++ b/docs/data/data.yaml @@ -15,6 +15,13 @@ properties: defaultValue: "https://api.github.com/" required: false + - name: insecure_skip_verify + description: | + Skip SSL verification. + type: bool + defaultValue: false + required: false + - name: key description: | Unique identifier to assign to a comment. @@ -23,6 +30,13 @@ properties: type: string required: false + - name: log_level + description: | + Plugin log level. + type: string + defaultValue: "info" + required: false + - name: message description: | Path to file or string that contains the comment text. diff --git a/go.mod b/go.mod index 36475e0..41f1e23 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 require ( github.com/google/go-github/v61 v61.0.0 github.com/rs/zerolog v1.32.0 - github.com/thegeeklab/wp-plugin-go v1.7.1 + github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 github.com/urfave/cli/v2 v2.27.2 golang.org/x/oauth2 v0.20.0 ) @@ -28,7 +28,7 @@ require ( github.com/shopspring/decimal v1.2.0 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect - golang.org/x/crypto v0.22.0 // indirect - golang.org/x/net v0.24.0 // indirect - golang.org/x/sys v0.19.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect ) diff --git a/go.sum b/go.sum index 3a7d15f..ec0763a 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 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/thegeeklab/wp-plugin-go v1.7.1 h1:zfR/rfNPuyVhXJu1fsLfp4+Mz2pTf6WwW/mIqw9750I= -github.com/thegeeklab/wp-plugin-go v1.7.1/go.mod h1:Ixi5plt9tpFGTu6yc/Inm5DcDpp3xPTeohfr86gf2EU= +github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 h1:ARwYgTPZ5iPsmOenmqcCf8TjiEe8wBOHKO7H/Xshe48= +github.com/thegeeklab/wp-plugin-go/v2 v2.3.1/go.mod h1:0t8M8txtEFiaB6RqLX8vLrxkqAo5FT5Hx7dztN592D4= github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw= @@ -63,15 +63,15 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -85,8 +85,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= diff --git a/internal/doc/main.go b/internal/doc/main.go new file mode 100644 index 0000000..a7dea5f --- /dev/null +++ b/internal/doc/main.go @@ -0,0 +1,42 @@ +//go:build generate +// +build generate + +package main + +import ( + "context" + "flag" + "net/http" + "os" + "time" + + "github.com/thegeeklab/wp-github-comment/plugin" + "github.com/thegeeklab/wp-plugin-go/v2/docs" + "github.com/thegeeklab/wp-plugin-go/v2/template" +) + +func main() { + tmpl := "https://raw.githubusercontent.com/thegeeklab/woodpecker-plugins/main/templates/docs-data.yaml.tmpl" + client := http.Client{ + Timeout: 30 * time.Second, + } + + p := plugin.New(nil) + + out, err := template.Render(context.Background(), client, tmpl, docs.GetTemplateData(p.App)) + if err != nil { + panic(err) + } + + outputFile := flag.String("output", "", "Output file path") + flag.Parse() + + if *outputFile == "" { + panic("no output file specified") + } + + err = os.WriteFile(*outputFile, []byte(out), 0o644) + if err != nil { + panic(err) + } +} diff --git a/plugin/comment.go b/plugin/comment.go index 4a67869..565c4a9 100644 --- a/plugin/comment.go +++ b/plugin/comment.go @@ -9,14 +9,13 @@ import ( ) type commentClient struct { + *github.Client Message string Update bool Key string Repo string Owner string IssueNum int - - *github.Client } func (cc *commentClient) issueComment(ctx context.Context) error { diff --git a/plugin/impl.go b/plugin/impl.go index a28476d..aa46e59 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -10,7 +10,7 @@ import ( "github.com/google/go-github/v61/github" "github.com/rs/zerolog/log" - "github.com/thegeeklab/wp-plugin-go/file" + "github.com/thegeeklab/wp-plugin-go/v2/file" "golang.org/x/oauth2" ) diff --git a/plugin/plugin.go b/plugin/plugin.go index b7f1bfe..6137cc0 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -1,11 +1,15 @@ package plugin import ( + "fmt" "net/url" - wp "github.com/thegeeklab/wp-plugin-go/plugin" + wp "github.com/thegeeklab/wp-plugin-go/v2/plugin" + "github.com/urfave/cli/v2" ) +//go:generate go run ../internal/doc/main.go -output=../docs/data/data-raw.yaml + // Plugin implements provide the plugin. type Plugin struct { *wp.Plugin @@ -26,13 +30,85 @@ type Settings struct { baseURL *url.URL } -func New(options wp.Options, settings *Settings) *Plugin { - p := &Plugin{} +func New(e wp.ExecuteFunc, build ...string) *Plugin { + p := &Plugin{ + Settings: &Settings{}, + } + + options := wp.Options{ + Name: "wp-github-comment", + Description: "Add comments to GitHub Issues and Pull Requests", + Flags: Flags(p.Settings, wp.FlagsPluginCategory), + Execute: p.run, + HideWoodpeckerFlags: true, + } + + if len(build) > 0 { + options.Version = build[0] + } - options.Execute = p.run + if len(build) > 1 { + options.VersionMetadata = fmt.Sprintf("date=%s", build[1]) + } + + if e != nil { + options.Execute = e + } p.Plugin = wp.New(options) - p.Settings = settings return p } + +// Flags returns a slice of CLI flags for the plugin. +func Flags(settings *Settings, category string) []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "api-key", + EnvVars: []string{"PLUGIN_API_KEY", "GITHUB_COMMENT_API_KEY"}, + Usage: "personal access token to access the GitHub API", + Destination: &settings.APIKey, + Category: category, + Required: true, + }, + &cli.StringFlag{ + Name: "base-url", + EnvVars: []string{"PLUGIN_BASE_URL", "GITHUB_COMMENT_BASE_URL"}, + Usage: "API URL", + Value: "https://api.github.com/", + Destination: &settings.BaseURL, + Category: category, + }, + &cli.StringFlag{ + Name: "key", + EnvVars: []string{"PLUGIN_KEY", "GITHUB_COMMENT_KEY"}, + Usage: "unique identifier to assign to a comment", + Destination: &settings.Key, + Category: category, + }, + &cli.StringFlag{ + Name: "message", + EnvVars: []string{"PLUGIN_MESSAGE", "GITHUB_COMMENT_MESSAGE"}, + Usage: "path to file or string that contains the comment text", + Destination: &settings.Message, + Category: category, + Required: true, + }, + &cli.BoolFlag{ + Name: "update", + EnvVars: []string{"PLUGIN_UPDATE", "GITHUB_COMMENT_UPDATE"}, + Usage: "enable update of an existing comment that matches the key", + Value: false, + Destination: &settings.Update, + Category: category, + }, + &cli.BoolFlag{ + Name: "skip-missing", + EnvVars: []string{"PLUGIN_SKIP_MISSING", "GITHUB_COMMENT_SKIP_MISSING"}, + Usage: "skip comment creation if the given message file does not exist", + Value: false, + Destination: &settings.SkipMissing, + Category: category, + }, + } +}