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"
"github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/pkg/encoder"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -75,6 +76,11 @@ func main() {
Value: "node:12-buster-slim",
EnvVar: "PLUGIN_ACTION_IMAGE",
},
cli.StringFlag{
Name: "event-payload",
Usage: "Webhook event payload",
EnvVar: "PLUGIN_EVENT_PAYLOAD",
},
// daemon flags
cli.StringFlag{
@ -172,6 +178,7 @@ func run(c *cli.Context) error {
Env: actionEnv,
Verbose: c.Bool("action-verbose"),
Image: c.String("action-image"),
EventPayload: c.String("event-payload"),
},
Daemon: daemon.Daemon{
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 {
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
}

@ -12,7 +12,7 @@ type Daemon struct {
Insecure bool // Docker daemon enable insecure registries
StorageDriver string // Docker daemon storage driver
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
Bip string // Docker daemon network bridge IP address
DNS []string // Docker daemon dns server
@ -23,7 +23,9 @@ type Daemon struct {
}
func StartDaemon(d Daemon) error {
if !d.Disabled {
startDaemon(d)
}
return waitForDaemon()
}

2
go.mod

@ -3,6 +3,8 @@ module github.com/drone-plugins/drone-github-actions
go 1.16
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/pkg/errors v0.9.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/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/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/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/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
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 (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/utils"
"github.com/pkg/errors"
)
const (
envFile = "/tmp/action.env"
secretFile = "/tmp/action.secrets"
workflowFile = "/tmp/workflow.yml"
eventPayloadFile = "/tmp/event.json"
)
var (
@ -26,6 +29,7 @@ type (
With map[string]string
Env map[string]string
Image string
EventPayload string // Webhook event payload
Verbose bool
}
@ -63,6 +67,14 @@ func (p Plugin) Exec() error {
"--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 {
cmdArgs = append(cmdArgs, "-v")
}