diff --git a/README.md b/README.md index 9135e80..801a107 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -# woodpecker-plugin-lib +# [WIP] woodpecker-plugin-lib -My attempt on replicating the drone-plugin-lib for woodpecker \ No newline at end of file +My attempt on replicating the drone-plugin-lib for woodpecker + +Currently only build is implemented (should suffice for plugins), this plugin-base will need updating once woodpecker reaches 1.x as build is going to be replaced with pipeline. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8fea4ab --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module git.kle.li/gapodo/woodpecker-plugin-lib + +go 1.19 + +require ( + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/urfave/cli/v2 v2.20.3 // indirect + github.com/woodpecker-ci/woodpecker v0.15.5 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7dafa03 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +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/urfave/cli/v2 v2.20.3 h1:lOgGidH/N5loaigd9HjFsOIhXSTrzl7tBpHswZ428w4= +github.com/urfave/cli/v2 v2.20.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= +github.com/woodpecker-ci/woodpecker v0.15.5 h1:8l6p1qhwUpjNXQNCkEpH6lQf0npfips4I8xYX9dBC4k= +github.com/woodpecker-ci/woodpecker v0.15.5/go.mod h1:Po9JhNe1H/AInF5eeqOAfAsXMfdmfR8E7ghUxM0sDEY= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/urfave/build.go b/urfave/build.go new file mode 100644 index 0000000..e9bd113 --- /dev/null +++ b/urfave/build.go @@ -0,0 +1,268 @@ +// Copyright 2021 Woodpecker Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package urfave + +import ( + "github.com/urfave/cli/v2" + "github.com/woodpecker-ci/woodpecker/pipeline/frontend" + "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/matrix" +) + +func BuildMetadataFromContext(c *cli.Context, axis matrix.Axis) frontend.Metadata { + return frontend.Metadata{ + Repo: frontend.Repo{ + Name: c.String("repo-name"), + Link: c.String("repo-link"), + Remote: c.String("repo-remote-url"), + Private: c.Bool("repo-private"), + }, + Curr: frontend.Build{ + Number: c.Int64("build-number"), + Parent: c.Int64("parent-build-number"), + Created: c.Int64("build-created"), + Started: c.Int64("build-started"), + Finished: c.Int64("build-finished"), + Status: c.String("build-status"), + Event: c.String("build-event"), + Link: c.String("build-link"), + Target: c.String("build-target"), + Commit: frontend.Commit{ + Sha: c.String("commit-sha"), + Ref: c.String("commit-ref"), + Refspec: c.String("commit-refspec"), + Branch: c.String("commit-branch"), + Message: c.String("commit-message"), + Author: frontend.Author{ + Name: c.String("commit-author-name"), + Email: c.String("commit-author-email"), + Avatar: c.String("commit-author-avatar"), + }, + }, + }, + Prev: frontend.Build{ + Number: c.Int64("prev-build-number"), + Created: c.Int64("prev-build-created"), + Started: c.Int64("prev-build-started"), + Finished: c.Int64("prev-build-finished"), + Status: c.String("prev-build-status"), + Event: c.String("prev-build-event"), + Link: c.String("prev-build-link"), + Commit: frontend.Commit{ + Sha: c.String("prev-commit-sha"), + Ref: c.String("prev-commit-ref"), + Refspec: c.String("prev-commit-refspec"), + Branch: c.String("prev-commit-branch"), + Message: c.String("prev-commit-message"), + Author: frontend.Author{ + Name: c.String("prev-commit-author-name"), + Email: c.String("prev-commit-author-email"), + Avatar: c.String("prev-commit-author-avatar"), + }, + }, + }, + Job: frontend.Job{ + Number: c.Int("job-number"), + Matrix: axis, + }, + Sys: frontend.System{ + Name: c.String("system-name"), + Link: c.String("system-link"), + Arch: c.String("system-arch"), + }, + } +} + +func BuildFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + EnvVars: []string{"CI_WORKSPACE_BASE"}, + Name: "workspace-base", + Value: "/woodpecker", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_WORKSPACE_PATH"}, + Name: "workspace-path", + Value: "src", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_WORKSPACE"}, + Name: "workspace", + Value: "/woodpecker/src", + }, + // + // metadata parameters + // + &cli.StringFlag{ + EnvVars: []string{"CI_SYSTEM_NAME"}, + Name: "system-name", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_SYSTEM_LINK"}, + Name: "system-link", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_REPO_NAME"}, + Name: "repo-name", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_REPO_LINK"}, + Name: "repo-link", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_REPO_REMOTE"}, + Name: "repo-remote-url", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_REPO_PRIVATE"}, + Name: "repo-private", + }, + &cli.IntFlag{ + EnvVars: []string{"CI_BUILD_NUMBER"}, + Name: "build-number", + }, + &cli.IntFlag{ + EnvVars: []string{"CI_PARENT_BUILD_NUMBER"}, + Name: "parent-build-number", + }, + &cli.Int64Flag{ + EnvVars: []string{"CI_BUILD_CREATED"}, + Name: "build-created", + }, + &cli.Int64Flag{ + EnvVars: []string{"CI_BUILD_STARTED"}, + Name: "build-started", + }, + &cli.Int64Flag{ + EnvVars: []string{"CI_BUILD_FINISHED"}, + Name: "build-finished", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_BUILD_STATUS"}, + Name: "build-status", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_BUILD_EVENT"}, + Name: "build-event", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_BUILD_LINK"}, + Name: "build-link", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_BUILD_TARGET"}, + Name: "build-target", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_SHA"}, + Name: "commit-sha", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_REF"}, + Name: "commit-ref", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_REFSPEC"}, + Name: "commit-refspec", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_BRANCH"}, + Name: "commit-branch", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_MESSAGE"}, + Name: "commit-message", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_AUTHOR_NAME"}, + Name: "commit-author-name", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_AUTHOR_AVATAR"}, + Name: "commit-author-avatar", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_COMMIT_AUTHOR_EMAIL"}, + Name: "commit-author-email", + }, + &cli.IntFlag{ + EnvVars: []string{"CI_PREV_BUILD_NUMBER"}, + Name: "prev-build-number", + }, + &cli.Int64Flag{ + EnvVars: []string{"CI_PREV_BUILD_CREATED"}, + Name: "prev-build-created", + }, + &cli.Int64Flag{ + EnvVars: []string{"CI_PREV_BUILD_STARTED"}, + Name: "prev-build-started", + }, + &cli.Int64Flag{ + EnvVars: []string{"CI_PREV_BUILD_FINISHED"}, + Name: "prev-build-finished", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_BUILD_STATUS"}, + Name: "prev-build-status", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_BUILD_EVENT"}, + Name: "prev-build-event", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_BUILD_LINK"}, + Name: "prev-build-link", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_SHA"}, + Name: "prev-commit-sha", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_REF"}, + Name: "prev-commit-ref", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_REFSPEC"}, + Name: "prev-commit-refspec", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_BRANCH"}, + Name: "prev-commit-branch", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_MESSAGE"}, + Name: "prev-commit-message", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_NAME"}, + Name: "prev-commit-author-name", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_AVATAR"}, + Name: "prev-commit-author-avatar", + }, + &cli.StringFlag{ + EnvVars: []string{"CI_PREV_COMMIT_AUTHOR_EMAIL"}, + Name: "prev-commit-author-email", + }, + &cli.IntFlag{ + EnvVars: []string{"CI_JOB_NUMBER"}, + Name: "job-number", + }, + &cli.StringSliceFlag{ + EnvVars: []string{"CI_ENV"}, + Name: "env", + }, + } +} diff --git a/urfave/urfave.go b/urfave/urfave.go new file mode 100644 index 0000000..62dea15 --- /dev/null +++ b/urfave/urfave.go @@ -0,0 +1,13 @@ +package urfave + +import ( + "github.com/urfave/cli/v2" +) + +func Flags() []cli.Flag { + flags := []cli.Flag{} + + flags = append(flags, BuildFlags()...) + + return flags +}