feat(expression): add evaluate setting

This commit is contained in:
Simon Vieille 2023-01-04 13:12:43 +01:00
parent 6e9918492f
commit a3aab36b52
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 76 additions and 0 deletions

View file

@ -108,6 +108,11 @@ func main() {
Usage: "attachment filename(s)",
EnvVar: "PLUGIN_ATTACHMENTS",
},
cli.StringFlag{
Name: "evaluate",
Usage: "evaluation expression",
EnvVar: "PLUGIN_EVALUATE",
},
cli.StringFlag{
Name: "clienthostname",
Value: DefaultClientHostname,
@ -347,6 +352,7 @@ func run(c *cli.Context) error {
}
plugin := Plugin{
Context: c,
Repo: Repo{
FullName: c.String("repo.fullName"),
Owner: c.String("repo.owner"),
@ -421,6 +427,7 @@ func run(c *cli.Context) error {
Attachment: c.String("attachment"),
Attachments: c.StringSlice("attachments"),
ClientHostname: c.String("clienthostname"),
Evaluation: c.String("evaluate"),
},
}

View file

@ -3,12 +3,15 @@ package main
import (
"bufio"
"crypto/tls"
"fmt"
"os"
log "github.com/Sirupsen/logrus"
"github.com/antonmedv/expr"
"github.com/aymerick/douceur/inliner"
"github.com/drone/drone-go/template"
"github.com/jaytaylor/html2text"
"github.com/urfave/cli"
gomail "gopkg.in/mail.v2"
)
@ -97,9 +100,11 @@ type (
Attachment string
Attachments []string
ClientHostname string
Evaluation string
}
Plugin struct {
Context *cli.Context
Repo Repo
Remote Remote
Commit Commit
@ -116,6 +121,28 @@ type (
// Exec will send emails over SMTP
func (p Plugin) Exec() error {
if p.Config.Evaluation != "" {
env := p.Environ()
fmt.Printf("%+v\n", expr.Env(env))
out, err := expr.Compile(p.Config.Evaluation, expr.Env(env), expr.AsBool())
if err != nil {
return err
}
result, err := expr.Run(out, env)
if err != nil {
return err
}
if result.(bool) == false {
return nil
}
}
var dialer *gomail.Dialer
if !p.Config.RecipientsOnly {
@ -244,6 +271,48 @@ func (p Plugin) Exec() error {
return nil
}
func (p Plugin) Environ() map[string]string {
return map[string]string{
"CI_REPO_OWNER": p.Context.String("repo.owner"),
"CI_REPO_NAME": p.Context.String("repo.name"),
"CI_REPO_SCM": p.Context.String("repo.scm"),
"CI_REPO_LINK": p.Context.String("repo.link"),
"DRONE_REPO_AVATAR": p.Context.String("repo.avatar"),
"CI_REPO_DEFAULT_BRANCH": p.Context.String("repo.branch"),
"CI_REPO_PRIVATE": p.Context.String("repo.private"),
"DRONE_REPO_TRUSTED": p.Context.String("repo.trusted"),
"CI_REPO_CLONE_URL": p.Context.String("remote.url"),
"CI_COMMIT_SHA": p.Context.String("commit.sha"),
"CI_COMMIT_REF": p.Context.String("commit.ref"),
"CI_COMMIT_BRANCH": p.Context.String("commit.branch"),
"CI_COMMIT_LINK": p.Context.String("commit.link"),
"CI_COMMIT_MESSAGE": p.Context.String("commit.message"),
"CI_COMMIT_AUTHOR": p.Context.String("commit.author.name"),
"CI_COMMIT_AUTHOR_EMAIL": p.Context.String("commit.author.email"),
"CI_COMMIT_AUTHOR_AVATAR": p.Context.String("commit.author.avatar"),
"CI_BUILD_NUMBER": p.Context.String("build.number"),
"CI_BUILD_EVENT": p.Context.String("build.event"),
"CI_PIPELINE_STATUS": p.Context.String("build.status"),
"CI_PIPELINE_LINK": p.Context.String("build.link"),
"CI_PIPELINE_CREATED": p.Context.String("build.created"),
"CI_PIPELINE_STARTED": p.Context.String("build.started"),
"CI_PIPELINE_FINISHED": p.Context.String("build.finished"),
"CI_PREV_PIPELINE_STATUS": p.Context.String("prev.build.status"),
"CI_PREV_PIPELINE_NUMBER": p.Context.String("prev.build.number"),
"CI_PREV_COMMIT_SHA": p.Context.String("prev.commit.sha"),
"CI_STEP_NUMBER": p.Context.String("job.number"),
"CI_STEP_STATUS": p.Context.String("job.status"),
"DRONE_JOB_EXIT_CODE": p.Context.String("job.exitCode"),
"CI_STEP_STARTED": p.Context.String("job.started"),
"CI_STEP_FINISHED": p.Context.String("job.finished"),
"DRONE_YAML_SIGNED": p.Context.String("yaml.signed"),
"DRONE_YAML_VERIFIED": p.Context.String("yaml.verified"),
"CI_COMMIT_TAG": p.Context.String("tag"),
"CI_COMMIT_PULL_REQUEST": p.Context.String("pullRequest"),
"CI_PIPELINE_DEPLOY_TARGET": p.Context.String("deployTo"),
}
}
func attach(message *gomail.Message, attachment string) {
if _, err := os.Stat(attachment); err == nil {
message.Attach(attachment)