0
0
mirror of https://github.com/thegeeklab/wp-github-comment.git synced 2024-11-22 10:10:40 +00:00

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
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,
},
}
}

View File

@ -8,6 +8,7 @@ import (
"strings"
"github.com/google/go-github/v35/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
@ -19,6 +20,8 @@ type Settings struct {
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 {

View File

@ -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
}