extend plugin to publish adaptive card (#1)

* extend plugin to publish adaptive card

* fix template url

* updated template

* fixes issue with template facts not working

* adjust width

* strech column for card

* add data back in

* add sample data
This commit is contained in:
Eoin McAfee
2022-01-25 11:10:33 +00:00
committed by GitHub
parent b9f723b385
commit 23e0e1d500
7 changed files with 282 additions and 4 deletions
+41
View File
@@ -0,0 +1,41 @@
package plugin
import (
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"os"
"github.com/drone/drone-go/drone"
)
func (args Args) writeCard(dat []match) error {
result, _ := json.Marshal(CardIssues{Issues: dat})
card := drone.CardInput{
Schema: "https://drone.github.io/drone-gitleaks/card.json",
Data: result,
}
writeCard(args.CardFilePath, &card)
return nil
}
func writeCard(path string, card interface{}) {
data, _ := json.Marshal(card)
switch {
case path == "/dev/stdout":
writeCardTo(os.Stdout, data)
case path == "/dev/stderr":
writeCardTo(os.Stderr, data)
case path != "":
ioutil.WriteFile(path, data, 0644)
}
}
func writeCardTo(out io.Writer, data []byte) {
encoded := base64.StdEncoding.EncodeToString(data)
io.WriteString(out, "\u001B]1338;")
io.WriteString(out, encoded)
io.WriteString(out, "\u001B]0m")
io.WriteString(out, "\n")
}
+21 -4
View File
@@ -7,6 +7,7 @@ package plugin
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
@@ -19,15 +20,18 @@ type Args struct {
Pipeline
// Level defines the plugin log level.
Level string `envconfig:"PLUGIN_LOG_LEVEL"`
Level string `envconfig:"PLUGIN_LOG_LEVEL"`
Path string `envconfig:"PLUGIN_PATH" default:"."`
Conf string `envconfig:"PLUGIN_CONFIG"`
CardFilePath string `envconfig:"DRONE_CARD_PATH"`
}
Path string `envconfig:"PLUGIN_PATH" default:"."`
Conf string `envconfig:"PLUGIN_CONFIG"`
type CardIssues struct {
Issues []match
}
// Exec executes the plugin.
func Exec(ctx context.Context, args Args) error {
// generate a temp file to store the report
file, err := ioutil.TempFile("", "gitleaks")
if err != nil {
@@ -49,6 +53,9 @@ func Exec(ctx context.Context, args Args) error {
// read the generated report and unmarshal
dat := []match{}
out, ferr := ioutil.ReadFile(file.Name())
if len(out) == 0 {
return nil
}
if ferr != nil {
logrus.WithError(ferr).Warnln("Cannot read report")
}
@@ -56,9 +63,19 @@ func Exec(ctx context.Context, args Args) error {
logrus.WithError(jsonerr).Warnln("Cannot unmarshal report")
}
reacted := []match{}
// loop through and print each violation
for _, match := range dat {
match.Line = "*****"
match.Offender = "*****"
logrus.Errorf("%s violation in %q at line %d\n", match.Rule, match.File, match.Linenumber)
reacted = append(reacted, match)
}
if len(dat) != 0 {
if err := args.writeCard(reacted); err != nil {
fmt.Printf("Could not create adaptive card. %s\n", err)
}
}
return err