2020-09-19 22:00:34 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
2023-02-08 09:13:43 +00:00
|
|
|
"errors"
|
2020-09-19 22:00:34 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2023-04-06 07:48:46 +00:00
|
|
|
"github.com/google/go-github/v51/github"
|
2021-06-13 16:21:20 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-09-19 22:00:34 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Settings for the Plugin.
|
|
|
|
type Settings struct {
|
2021-06-13 16:21:20 +00:00
|
|
|
BaseURL string
|
|
|
|
IssueNum int
|
|
|
|
Key string
|
|
|
|
Message string
|
|
|
|
Update bool
|
|
|
|
APIKey string
|
|
|
|
SkipMissing bool
|
|
|
|
IsFile bool
|
2020-09-19 22:00:34 +00:00
|
|
|
|
|
|
|
baseURL *url.URL
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:43 +00:00
|
|
|
var ErrPluginEventNotSupported = errors.New("event not supported")
|
|
|
|
|
2020-09-19 22:00:34 +00:00
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
|
|
|
var err error
|
|
|
|
|
2020-11-08 19:46:45 +00:00
|
|
|
if p.pipeline.Build.Event != "pull_request" {
|
2023-02-08 09:13:43 +00:00
|
|
|
return fmt.Errorf("%w: %s", ErrPluginEventNotSupported, p.pipeline.Build.Event)
|
2020-09-19 22:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Message != "" {
|
2021-06-13 16:21:20 +00:00
|
|
|
if p.settings.Message, p.settings.IsFile, err = readStringOrFile(p.settings.Message); err != nil {
|
2020-09-19 22:00:34 +00:00
|
|
|
return fmt.Errorf("error while reading %s: %w", p.settings.Message, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(p.settings.BaseURL, "/") {
|
2022-04-25 10:53:45 +00:00
|
|
|
p.settings.BaseURL += "/"
|
2020-09-19 22:00:34 +00:00
|
|
|
}
|
2023-02-08 09:13:43 +00:00
|
|
|
|
2020-09-19 22:00:34 +00:00
|
|
|
p.settings.baseURL, err = url.Parse(p.settings.BaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse base url: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Key == "" {
|
|
|
|
key := fmt.Sprintf("%s/%s/%d", p.pipeline.Repo.Owner, p.pipeline.Repo.Name, p.settings.IssueNum)
|
|
|
|
hash := sha256.Sum256([]byte(key))
|
|
|
|
p.settings.Key = fmt.Sprintf("%x", hash)
|
|
|
|
}
|
2023-02-08 09:13:43 +00:00
|
|
|
|
2021-06-13 16:21:20 +00:00
|
|
|
if p.settings.Key, _, err = readStringOrFile(p.settings.Key); err != nil {
|
2020-09-19 22:00:34 +00:00
|
|
|
return fmt.Errorf("error while reading %s: %w", p.settings.Key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
|
|
|
func (p *Plugin) Execute() error {
|
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: p.settings.APIKey})
|
|
|
|
tc := oauth2.NewClient(
|
|
|
|
context.WithValue(context.Background(), oauth2.HTTPClient, p.network.Client),
|
|
|
|
ts,
|
|
|
|
)
|
|
|
|
|
|
|
|
client := github.NewClient(tc)
|
|
|
|
client.BaseURL = p.settings.baseURL
|
|
|
|
|
2023-02-08 09:13:43 +00:00
|
|
|
commentClient := commentClient{
|
2020-09-19 22:00:34 +00:00
|
|
|
Client: client,
|
|
|
|
Repo: p.pipeline.Repo.Name,
|
|
|
|
Owner: p.pipeline.Repo.Owner,
|
|
|
|
Message: p.settings.Message,
|
|
|
|
Update: p.settings.Update,
|
|
|
|
Key: p.settings.Key,
|
|
|
|
IssueNum: p.pipeline.Build.PullRequest,
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:21:20 +00:00
|
|
|
if p.settings.SkipMissing && !p.settings.IsFile {
|
2022-06-02 12:28:13 +00:00
|
|
|
logrus.Infof("comment skipped: 'message' is not a valid path or file does not exist while 'skip-missing' is enabled")
|
2023-02-08 09:13:43 +00:00
|
|
|
|
2021-06-13 16:21:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:43 +00:00
|
|
|
err := commentClient.issueComment(p.network.Context)
|
2020-09-19 22:00:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create or update comment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|