diff --git a/app.go b/app.go index 6c856ea..40a1e9e 100644 --- a/app.go +++ b/app.go @@ -8,7 +8,7 @@ func app() *cli.App { app.Usage = "Woodpecker plugin to deploy to Netlify" app.Flags = []cli.Flag{ &cli.StringFlag{ - Name: "netlify-side-id", + Name: "netlify-site-id", Usage: "Netlify Site ID", EnvVars: []string{"NETLIFY_SITE_ID", "PLUGIN_NETLIFY_SITE_ID"}, }, @@ -38,6 +38,11 @@ func app() *cli.App { Value: defaultComment, EnvVars: []string{"ISSUE_COMMENT", "PLUGIN_ISSUE_COMMENT"}, }, + &cli.BoolFlag{ + Name: "prod", + Usage: "Prod Deploy", + EnvVars: []string{"PROD", "PLUGIN_PROD"}, + }, } return app } diff --git a/go.mod b/go.mod index 7462e88..1ed7091 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/go-openapi/runtime v0.19.24 github.com/go-openapi/strfmt v0.19.11 github.com/gosimple/slug v1.11.2 - github.com/netlify/open-api v1.4.0 + github.com/netlify/open-api/v2 v2.6.0 github.com/rs/zerolog v1.26.0 github.com/urfave/cli/v2 v2.3.1-0.20211205195634-e8d81738896c go.jolheiser.com/woodpecker-env v0.0.0-20211211171841-abc1ed471fa1 @@ -40,6 +40,7 @@ require ( github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect github.com/mailru/easyjson v0.7.6 // indirect github.com/mitchellh/mapstructure v1.4.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/rsc/goversion v1.2.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sirupsen/logrus v1.6.0 // indirect diff --git a/go.sum b/go.sum index 74551a2..7f91669 100644 --- a/go.sum +++ b/go.sum @@ -276,8 +276,8 @@ github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJ github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/myitcv/gobin v0.0.14/go.mod h1:GvHEiYCWroKI2KrMT+xQkHC3FC551wigVWeR4Sgg5P4= -github.com/netlify/open-api v1.4.0 h1:/P+6sG53/EILEeQkMIi29gjqk8AJZZGtvBMMerWefK4= -github.com/netlify/open-api v1.4.0/go.mod h1:/fPwbOj5ZSaMiTZPzuxf3g4gV/29x344DZyualypq04= +github.com/netlify/open-api/v2 v2.6.0 h1:1yQnmsVY0qD7ISTzEvMeNNxelNFcGTUD1zN0g0I1zJc= +github.com/netlify/open-api/v2 v2.6.0/go.mod h1:NbccOGa/b1S22KG8yBKhkuUfhrlQC31nCWzUApIbjiQ= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -288,6 +288,7 @@ github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -343,8 +344,9 @@ github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= diff --git a/main.go b/main.go index 674ca9d..eddcc6f 100644 --- a/main.go +++ b/main.go @@ -18,13 +18,17 @@ func main() { } func doMain(ctx *cli.Context) error { - prIndex, err := strconv.Atoi(env.CI_PULL_REQUEST) - if err != nil { - return err + var prIndex int + if env.CI_PULL_REQUEST != "" { + var err error + prIndex, err = strconv.Atoi(env.CI_PULL_REQUEST) + if err != nil { + return err + } } // Netlify deploy - deploy, err := netlifyDeploy(ctx.String("netlify-auth-token"), ctx.String("netlify-site-id"), ctx.String("build-dir"), prIndex) + deploy, err := netlifyDeploy(ctx.String("netlify-auth-token"), ctx.String("netlify-site-id"), ctx.String("build-dir"), prIndex, ctx.Bool("prod")) if err != nil { return err } @@ -33,10 +37,12 @@ func doMain(ctx *cli.Context) error { } // Gitea comment - c, err := giteaComment(ctx.String("gitea-url"), ctx.String("gitea-token"), ctx.String("issue-comment"), prIndex) - if err != nil { - return err + if prIndex > 0 { + c, err := giteaComment(ctx.String("gitea-url"), ctx.String("gitea-token"), ctx.String("issue-comment"), prIndex) + if err != nil { + return err + } + log.Info().Msgf("Issue Comment created at %s", c.HTMLURL) } - log.Info().Msgf("Issue Comment created at %s", c.HTMLURL) return nil } diff --git a/netlify.go b/netlify.go index 37cf598..8964df5 100644 --- a/netlify.go +++ b/netlify.go @@ -8,21 +8,24 @@ import ( "github.com/go-openapi/runtime" "github.com/go-openapi/strfmt" "github.com/gosimple/slug" - "github.com/netlify/open-api/go/models" - "github.com/netlify/open-api/go/porcelain" - ncontext "github.com/netlify/open-api/go/porcelain/context" + "github.com/netlify/open-api/v2/go/models" + "github.com/netlify/open-api/v2/go/porcelain" + ncontext "github.com/netlify/open-api/v2/go/porcelain/context" ) -func netlifyDeploy(netlifyAuthToken, netlifySiteID, buildDir string, prIndex int) (*models.Deploy, error) { +func netlifyDeploy(netlifyAuthToken, netlifySiteID, buildDir string, prIndex int, prod bool) (*models.Deploy, error) { ctx := ncontext.WithAuthInfo(context.Background(), runtime.ClientAuthInfoWriterFunc(func(req runtime.ClientRequest, reg strfmt.Registry) error { return req.SetHeaderParam("Authorization", fmt.Sprintf("Bearer %s", netlifyAuthToken)) })) netlifyClient := porcelain.Default alias := fmt.Sprintf("%d-%s", prIndex, slug.Make(os.Getenv("DRONE_SOURCE_BRANCH"))) + if prod { + alias = "" + } return netlifyClient.DeploySite(ctx, porcelain.DeployOptions{ SiteID: netlifySiteID, Dir: buildDir, - IsDraft: true, + IsDraft: !prod, Branch: alias, }) } diff --git a/testdata/index.html b/testdata/index.html new file mode 100644 index 0000000..724c266 --- /dev/null +++ b/testdata/index.html @@ -0,0 +1,10 @@ + + + + + Woodpecker Netlify + + + THE BODY + + \ No newline at end of file