Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2021-12-13 22:18:51 -06:00
parent 458b2a385b
commit d931c55c3d
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 45 additions and 18 deletions

7
app.go
View File

@ -8,7 +8,7 @@ func app() *cli.App {
app.Usage = "Woodpecker plugin to deploy to Netlify" app.Usage = "Woodpecker plugin to deploy to Netlify"
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "netlify-side-id", Name: "netlify-site-id",
Usage: "Netlify Site ID", Usage: "Netlify Site ID",
EnvVars: []string{"NETLIFY_SITE_ID", "PLUGIN_NETLIFY_SITE_ID"}, EnvVars: []string{"NETLIFY_SITE_ID", "PLUGIN_NETLIFY_SITE_ID"},
}, },
@ -38,6 +38,11 @@ func app() *cli.App {
Value: defaultComment, Value: defaultComment,
EnvVars: []string{"ISSUE_COMMENT", "PLUGIN_ISSUE_COMMENT"}, EnvVars: []string{"ISSUE_COMMENT", "PLUGIN_ISSUE_COMMENT"},
}, },
&cli.BoolFlag{
Name: "prod",
Usage: "Prod Deploy",
EnvVars: []string{"PROD", "PLUGIN_PROD"},
},
} }
return app return app
} }

3
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/go-openapi/runtime v0.19.24 github.com/go-openapi/runtime v0.19.24
github.com/go-openapi/strfmt v0.19.11 github.com/go-openapi/strfmt v0.19.11
github.com/gosimple/slug v1.11.2 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/rs/zerolog v1.26.0
github.com/urfave/cli/v2 v2.3.1-0.20211205195634-e8d81738896c github.com/urfave/cli/v2 v2.3.1-0.20211205195634-e8d81738896c
go.jolheiser.com/woodpecker-env v0.0.0-20211211171841-abc1ed471fa1 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/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/mailru/easyjson v0.7.6 // indirect github.com/mailru/easyjson v0.7.6 // indirect
github.com/mitchellh/mapstructure v1.4.0 // 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/rsc/goversion v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect github.com/sirupsen/logrus v1.6.0 // indirect

8
go.sum
View File

@ -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/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/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/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/v2 v2.6.0 h1:1yQnmsVY0qD7ISTzEvMeNNxelNFcGTUD1zN0g0I1zJc=
github.com/netlify/open-api v1.4.0/go.mod h1:/fPwbOj5ZSaMiTZPzuxf3g4gV/29x344DZyualypq04= 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 h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= 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= 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/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.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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.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.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/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 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=

22
main.go
View File

@ -18,13 +18,17 @@ func main() {
} }
func doMain(ctx *cli.Context) error { func doMain(ctx *cli.Context) error {
prIndex, err := strconv.Atoi(env.CI_PULL_REQUEST) var prIndex int
if err != nil { if env.CI_PULL_REQUEST != "" {
return err var err error
prIndex, err = strconv.Atoi(env.CI_PULL_REQUEST)
if err != nil {
return err
}
} }
// Netlify deploy // 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 { if err != nil {
return err return err
} }
@ -33,10 +37,12 @@ func doMain(ctx *cli.Context) error {
} }
// Gitea comment // Gitea comment
c, err := giteaComment(ctx.String("gitea-url"), ctx.String("gitea-token"), ctx.String("issue-comment"), prIndex) if prIndex > 0 {
if err != nil { c, err := giteaComment(ctx.String("gitea-url"), ctx.String("gitea-token"), ctx.String("issue-comment"), prIndex)
return err 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 return nil
} }

View File

@ -8,21 +8,24 @@ import (
"github.com/go-openapi/runtime" "github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt" "github.com/go-openapi/strfmt"
"github.com/gosimple/slug" "github.com/gosimple/slug"
"github.com/netlify/open-api/go/models" "github.com/netlify/open-api/v2/go/models"
"github.com/netlify/open-api/go/porcelain" "github.com/netlify/open-api/v2/go/porcelain"
ncontext "github.com/netlify/open-api/go/porcelain/context" 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 { ctx := ncontext.WithAuthInfo(context.Background(), runtime.ClientAuthInfoWriterFunc(func(req runtime.ClientRequest, reg strfmt.Registry) error {
return req.SetHeaderParam("Authorization", fmt.Sprintf("Bearer %s", netlifyAuthToken)) return req.SetHeaderParam("Authorization", fmt.Sprintf("Bearer %s", netlifyAuthToken))
})) }))
netlifyClient := porcelain.Default netlifyClient := porcelain.Default
alias := fmt.Sprintf("%d-%s", prIndex, slug.Make(os.Getenv("DRONE_SOURCE_BRANCH"))) alias := fmt.Sprintf("%d-%s", prIndex, slug.Make(os.Getenv("DRONE_SOURCE_BRANCH")))
if prod {
alias = ""
}
return netlifyClient.DeploySite(ctx, porcelain.DeployOptions{ return netlifyClient.DeploySite(ctx, porcelain.DeployOptions{
SiteID: netlifySiteID, SiteID: netlifySiteID,
Dir: buildDir, Dir: buildDir,
IsDraft: true, IsDraft: !prod,
Branch: alias, Branch: alias,
}) })
} }

10
testdata/index.html vendored 100644
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Woodpecker Netlify</title>
</head>
<body>
THE BODY
</body>
</html>