From e95a593ee4d188fb5a88553e851bca3df6857250 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 13 Jun 2021 18:21:20 +0200 Subject: [PATCH] feat: add skip-missing to skip comment if message is not a valid file (#48) --- cmd/drone-github-comment/config.go | 9 ++++++++- plugin/impl.go | 24 ++++++++++++++++-------- plugin/utils.go | 12 ++++++------ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/cmd/drone-github-comment/config.go b/cmd/drone-github-comment/config.go index 3be1e99..2d8be65 100644 --- a/cmd/drone-github-comment/config.go +++ b/cmd/drone-github-comment/config.go @@ -1,8 +1,8 @@ package main import ( - "github.com/urfave/cli/v2" "github.com/thegeeklab/drone-github-comment/plugin" + "github.com/urfave/cli/v2" ) // settingsFlags has the cli.Flags for the plugin.Settings. @@ -39,5 +39,12 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { EnvVars: []string{"PLUGIN_UPDATE", "GITHUB_COMMENT_UPDATE"}, Destination: &settings.Update, }, + &cli.BoolFlag{ + Name: "skip-missing", + Value: false, + Usage: "message need to be an existing file", + EnvVars: []string{"PLUGIN_SKIP_MISSING", "GITHUB_COMMENT_SKIP_MISSING"}, + Destination: &settings.SkipMissing, + }, } } diff --git a/plugin/impl.go b/plugin/impl.go index 9f85d56..52e68a3 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -8,17 +8,20 @@ import ( "strings" "github.com/google/go-github/v35/github" + "github.com/sirupsen/logrus" "golang.org/x/oauth2" ) // Settings for the Plugin. type Settings struct { - BaseURL string - IssueNum int - Key string - Message string - Update bool - APIKey string + BaseURL string + IssueNum int + Key string + Message string + Update bool + APIKey string + SkipMissing bool + IsFile bool baseURL *url.URL } @@ -39,7 +42,7 @@ func (p *Plugin) Validate() error { return fmt.Errorf("no message provides") } if p.settings.Message != "" { - if p.settings.Message, err = readStringOrFile(p.settings.Message); err != nil { + if p.settings.Message, p.settings.IsFile, err = readStringOrFile(p.settings.Message); err != nil { return fmt.Errorf("error while reading %s: %w", p.settings.Message, err) } } @@ -57,7 +60,7 @@ func (p *Plugin) Validate() error { hash := sha256.Sum256([]byte(key)) p.settings.Key = fmt.Sprintf("%x", hash) } - if p.settings.Key, err = readStringOrFile(p.settings.Key); err != nil { + if p.settings.Key, _, err = readStringOrFile(p.settings.Key); err != nil { return fmt.Errorf("error while reading %s: %w", p.settings.Key, err) } @@ -86,6 +89,11 @@ func (p *Plugin) Execute() error { IssueNum: p.pipeline.Build.PullRequest, } + if p.settings.SkipMissing && !p.settings.IsFile { + logrus.Printf("comment skipped: 'message' is not a valid path or file does not exist wile 'skip-missing' is enabled") + return nil + } + err := cc.issueComment() if err != nil { diff --git a/plugin/utils.go b/plugin/utils.go index f4caaf7..1d497c3 100644 --- a/plugin/utils.go +++ b/plugin/utils.go @@ -5,20 +5,20 @@ import ( "os" ) -func readStringOrFile(input string) (string, error) { +func readStringOrFile(input string) (string, bool, error) { if len(input) > 255 { - return input, nil + return input, false, nil } // Check if input is a file path if _, err := os.Stat(input); err != nil && os.IsNotExist(err) { // No file found => use input as result - return input, nil + return input, false, nil } else if err != nil { - return "", err + return "", false, err } result, err := ioutil.ReadFile(input) if err != nil { - return "", err + return "", true, err } - return string(result), nil + return string(result), true, nil }