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

104 lines
2.6 KiB
Go
Raw Normal View History

2020-09-19 22:00:34 +00:00
package plugin
import (
"context"
"crypto/sha256"
"errors"
2020-09-19 22:00:34 +00:00
"fmt"
"net/url"
"strings"
"github.com/google/go-github/v59/github"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/file"
2020-09-19 22:00:34 +00:00
"golang.org/x/oauth2"
)
var ErrPluginEventNotSupported = errors.New("event not supported")
//nolint:revive
2023-08-21 09:35:57 +00:00
func (p *Plugin) run(ctx context.Context) error {
if err := p.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
//nolint:contextcheck
if err := p.Execute(); err != nil {
return fmt.Errorf("execution failed: %w", err)
}
return nil
}
2020-09-19 22:00:34 +00:00
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
var err error
if p.Metadata.Pipeline.Event != "pull_request" {
return fmt.Errorf("%w: %s", ErrPluginEventNotSupported, p.Metadata.Pipeline.Event)
2020-09-19 22:00:34 +00:00
}
if p.Settings.Message != "" {
if p.Settings.Message, p.Settings.IsFile, err = file.ReadStringOrFile(p.Settings.Message); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Message, err)
2020-09-19 22:00:34 +00:00
}
}
if !strings.HasSuffix(p.Settings.BaseURL, "/") {
p.Settings.BaseURL += "/"
2020-09-19 22:00:34 +00:00
}
p.Settings.baseURL, err = url.Parse(p.Settings.BaseURL)
2020-09-19 22:00:34 +00:00
if err != nil {
return fmt.Errorf("failed to parse base url: %w", err)
}
if p.Settings.Key == "" {
key := fmt.Sprintf("%s/%s/%d", p.Metadata.Repository.Owner, p.Metadata.Repository.Name, p.Settings.IssueNum)
2020-09-19 22:00:34 +00:00
hash := sha256.Sum256([]byte(key))
p.Settings.Key = fmt.Sprintf("%x", hash)
2020-09-19 22:00:34 +00:00
}
if p.Settings.Key, _, err = file.ReadStringOrFile(p.Settings.Key); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Key, err)
2020-09-19 22:00:34 +00:00
}
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: p.Settings.APIKey})
2020-09-19 22:00:34 +00:00
tc := oauth2.NewClient(
context.WithValue(p.Network.Context, oauth2.HTTPClient, p.Network.Client),
2020-09-19 22:00:34 +00:00
ts,
)
client := github.NewClient(tc)
client.BaseURL = p.Settings.baseURL
2020-09-19 22:00:34 +00:00
commentClient := commentClient{
2020-09-19 22:00:34 +00:00
Client: client,
Repo: p.Metadata.Repository.Name,
Owner: p.Metadata.Repository.Owner,
Message: p.Settings.Message,
Update: p.Settings.Update,
Key: p.Settings.Key,
IssueNum: p.Metadata.Curr.PullRequest,
2020-09-19 22:00:34 +00:00
}
if p.Settings.SkipMissing && !p.Settings.IsFile {
log.Info().
Msg("comment skipped: 'message' is not a valid path or file does not exist while 'skip-missing' is enabled")
return nil
}
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
}