Added webhook support

This commit is contained in:
Shubham Agrawal 2021-10-18 00:02:56 +05:30
parent 45c5927f7f
commit 5fe9ccae32
6 changed files with 102 additions and 16 deletions

@ -6,6 +6,7 @@ import (
plugin "github.com/drone-plugins/drone-github-actions" plugin "github.com/drone-plugins/drone-github-actions"
"github.com/drone-plugins/drone-github-actions/daemon" "github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/pkg/encoder"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -75,6 +76,11 @@ func main() {
Value: "node:12-buster-slim", Value: "node:12-buster-slim",
EnvVar: "PLUGIN_ACTION_IMAGE", EnvVar: "PLUGIN_ACTION_IMAGE",
}, },
cli.StringFlag{
Name: "event-payload",
Usage: "Webhook event payload",
EnvVar: "PLUGIN_EVENT_PAYLOAD",
},
// daemon flags // daemon flags
cli.StringFlag{ cli.StringFlag{
@ -172,6 +178,7 @@ func run(c *cli.Context) error {
Env: actionEnv, Env: actionEnv,
Verbose: c.Bool("action-verbose"), Verbose: c.Bool("action-verbose"),
Image: c.String("action-image"), Image: c.String("action-image"),
EventPayload: c.String("event-payload"),
}, },
Daemon: daemon.Daemon{ Daemon: daemon.Daemon{
Registry: c.String("docker.registry"), Registry: c.String("docker.registry"),
@ -199,7 +206,14 @@ func strToMap(s string) (map[string]string, error) {
} }
if err := json.Unmarshal([]byte(s), &m); err != nil { if err := json.Unmarshal([]byte(s), &m); err != nil {
return nil, err m1 := make(map[string]interface{})
if e := json.Unmarshal([]byte(s), &m1); e != nil {
return nil, e
}
for k, v := range m1 {
m[k] = encoder.Encode(v)
}
} }
return m, nil return m, nil
} }

@ -12,7 +12,7 @@ type Daemon struct {
Insecure bool // Docker daemon enable insecure registries Insecure bool // Docker daemon enable insecure registries
StorageDriver string // Docker daemon storage driver StorageDriver string // Docker daemon storage driver
StoragePath string // Docker daemon storage path StoragePath string // Docker daemon storage path
Disabled bool // DOcker daemon is disabled (already running) Disabled bool // Docker daemon is disabled (already running)
Debug bool // Docker daemon started in debug mode Debug bool // Docker daemon started in debug mode
Bip string // Docker daemon network bridge IP address Bip string // Docker daemon network bridge IP address
DNS []string // Docker daemon dns server DNS []string // Docker daemon dns server
@ -23,7 +23,9 @@ type Daemon struct {
} }
func StartDaemon(d Daemon) error { func StartDaemon(d Daemon) error {
if !d.Disabled {
startDaemon(d) startDaemon(d)
}
return waitForDaemon() return waitForDaemon()
} }

2
go.mod

@ -3,6 +3,8 @@ module github.com/drone-plugins/drone-github-actions
go 1.16 go 1.16
require ( require (
github.com/buildkite/yaml v2.1.0+incompatible
github.com/ghodss/yaml v1.0.0
github.com/joho/godotenv v1.3.0 github.com/joho/godotenv v1.3.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1

4
go.sum

@ -1,7 +1,11 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/buildkite/yaml v2.1.0+incompatible h1:xirI+ql5GzfikVNDmt+yeiXpf/v1Gt03qXTtT5WXdr8=
github.com/buildkite/yaml v2.1.0+incompatible/go.mod h1:UoU8vbcwu1+vjZq01+KrpSeLBgQQIjL/H7Y6KwikUrI=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

52
pkg/encoder/encoder.go Normal file

@ -0,0 +1,52 @@
package encoder
import (
"encoding/base64"
"strconv"
"strings"
"github.com/buildkite/yaml"
json "github.com/ghodss/yaml"
)
// Encode encodes an interface value as a string. This function
// assumes all types were unmarshaled by the yaml.v2 library.
// The yaml.v2 package only supports a subset of primitive types.
func Encode(v interface{}) string {
switch v := v.(type) {
case string:
return v
case bool:
return strconv.FormatBool(v)
case int:
return strconv.Itoa(v)
case float64:
return strconv.FormatFloat(v, 'g', -1, 64)
case []byte:
return base64.StdEncoding.EncodeToString(v)
case []interface{}:
return encodeSlice(v)
default:
return encodeMap(v)
}
}
// helper function encodes a parameter in map format.
func encodeMap(v interface{}) string {
yml, _ := yaml.Marshal(v)
out, _ := json.YAMLToJSON(yml)
return string(out)
}
// helper function encodes a parameter in slice format.
func encodeSlice(v interface{}) string {
out, _ := yaml.Marshal(v)
in := []string{}
err := yaml.Unmarshal(out, &in)
if err == nil {
return strings.Join(in, ",")
}
out, _ = json.YAMLToJSON(out)
return string(out)
}

@ -2,18 +2,21 @@ package plugin
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"github.com/drone-plugins/drone-github-actions/daemon" "github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/utils" "github.com/drone-plugins/drone-github-actions/utils"
"github.com/pkg/errors"
) )
const ( const (
envFile = "/tmp/action.env" envFile = "/tmp/action.env"
secretFile = "/tmp/action.secrets" secretFile = "/tmp/action.secrets"
workflowFile = "/tmp/workflow.yml" workflowFile = "/tmp/workflow.yml"
eventPayloadFile = "/tmp/event.json"
) )
var ( var (
@ -26,6 +29,7 @@ type (
With map[string]string With map[string]string
Env map[string]string Env map[string]string
Image string Image string
EventPayload string // Webhook event payload
Verbose bool Verbose bool
} }
@ -63,6 +67,14 @@ func (p Plugin) Exec() error {
"--detect-event", "--detect-event",
} }
if p.Action.EventPayload != "" {
if err := ioutil.WriteFile(eventPayloadFile, []byte(p.Action.EventPayload), 0644); err != nil {
return errors.Wrap(err, "failed to write event payload to file")
}
cmdArgs = append(cmdArgs, "--eventpath", eventPayloadFile)
}
if p.Action.Verbose { if p.Action.Verbose {
cmdArgs = append(cmdArgs, "-v") cmdArgs = append(cmdArgs, "-v")
} }