feat: add skip-missing to skip comment if message is not a valid file (#48)

This commit is contained in:
Robert Kaussow 2021-06-13 18:21:20 +02:00 committed by GitHub
parent b78e90c4b5
commit e95a593ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 15 deletions

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"github.com/urfave/cli/v2"
"github.com/thegeeklab/drone-github-comment/plugin" "github.com/thegeeklab/drone-github-comment/plugin"
"github.com/urfave/cli/v2"
) )
// settingsFlags has the cli.Flags for the plugin.Settings. // 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"}, EnvVars: []string{"PLUGIN_UPDATE", "GITHUB_COMMENT_UPDATE"},
Destination: &settings.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,
},
} }
} }

View File

@ -8,17 +8,20 @@ import (
"strings" "strings"
"github.com/google/go-github/v35/github" "github.com/google/go-github/v35/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
// Settings for the Plugin. // Settings for the Plugin.
type Settings struct { type Settings struct {
BaseURL string BaseURL string
IssueNum int IssueNum int
Key string Key string
Message string Message string
Update bool Update bool
APIKey string APIKey string
SkipMissing bool
IsFile bool
baseURL *url.URL baseURL *url.URL
} }
@ -39,7 +42,7 @@ func (p *Plugin) Validate() error {
return fmt.Errorf("no message provides") return fmt.Errorf("no message provides")
} }
if p.settings.Message != "" { 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) 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)) hash := sha256.Sum256([]byte(key))
p.settings.Key = fmt.Sprintf("%x", hash) 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) 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, 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() err := cc.issueComment()
if err != nil { if err != nil {

View File

@ -5,20 +5,20 @@ import (
"os" "os"
) )
func readStringOrFile(input string) (string, error) { func readStringOrFile(input string) (string, bool, error) {
if len(input) > 255 { if len(input) > 255 {
return input, nil return input, false, nil
} }
// Check if input is a file path // Check if input is a file path
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) { if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
// No file found => use input as result // No file found => use input as result
return input, nil return input, false, nil
} else if err != nil { } else if err != nil {
return "", err return "", false, err
} }
result, err := ioutil.ReadFile(input) result, err := ioutil.ReadFile(input)
if err != nil { if err != nil {
return "", err return "", true, err
} }
return string(result), nil return string(result), true, nil
} }