Skip to content

Commit

Permalink
feat: support log level config for external plugin (#365)
Browse files Browse the repository at this point in the history
Close: #69
  • Loading branch information
Mini256 authored Mar 3, 2021
1 parent 472d2dc commit 1765abb
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 72 deletions.
12 changes: 5 additions & 7 deletions cmd/ticommunityautoresponder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", autoresponder.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
12 changes: 5 additions & 7 deletions cmd/ticommunityblunderbuss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", blunderbuss.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
13 changes: 5 additions & 8 deletions cmd/ticommunitylabel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
// TODO: Use global option from the prow config.
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", label.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
12 changes: 5 additions & 7 deletions cmd/ticommunitylabelblocker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", labelblocker.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
13 changes: 5 additions & 8 deletions cmd/ticommunitylgtm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
// TODO: Use global option from the prow config.
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", lgtm.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
13 changes: 5 additions & 8 deletions cmd/ticommunitymerge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
// TODO: Use global option from the prow config.
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", merge.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
13 changes: 5 additions & 8 deletions cmd/ticommunityowners/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
// TODO: Use global option from the prow config.
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", lgtm.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
Expand Down
12 changes: 5 additions & 7 deletions cmd/ticommunitytars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,18 @@ func main() {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.InfoLevel)
log := logrus.StandardLogger().WithField("plugin", tars.PluginName)

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

pa := &plugins.ConfigAgent{}
if err := pa.Start(o.pluginConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading plugin config from %q.", o.pluginConfig)
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
logrus.WithError(err).Fatal("Error starting secrets agent.")
}

epa := &tiexternalplugins.ConfigAgent{}
if err := epa.Start(o.externalPluginsConfig, false); err != nil {
log.WithError(err).Fatalf("Error loading external plugin config from %q.", o.externalPluginsConfig)
Expand Down
37 changes: 34 additions & 3 deletions internal/pkg/externalplugins/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"net/url"
"regexp"

"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
)

const (
// defaultGracePeriodDuration define the time for blunderbuss plugin to wait
// before requesting a review (default five seconds).
defaultGracePeriodDuration = 5
// defaultLogLevel defines the default log level of all ti community plugins.
defaultLogLevel = logrus.InfoLevel
)

// Allowed value of the action configuration of the label blocker plugin.
Expand All @@ -23,9 +26,19 @@ const (

// Configuration is the top-level serialization target for external plugin Configuration.
type Configuration struct {
TichiWebURL string `json:"tichi-web-url,omitempty"`
PRProcessLink string `json:"pr-process-link,omitempty"`
CommandHelpLink string `json:"command-help-link,omitempty"`
TichiWebURL string `json:"tichi_web_url,omitempty"`
PRProcessLink string `json:"pr_process_link,omitempty"`
CommandHelpLink string `json:"command_help_link,omitempty"`

// LogLevel enables dynamically updating the log level of the
// standard logger that is used by all ti community plugin.
//
// Valid values:
//
// "trace" "debug", "info", "warn", "warning", "error", "fatal", "panic"
//
// Defaults to "info".
LogLevel string `json:"log_level,omitempty"`

TiCommunityLgtm []TiCommunityLgtm `json:"ti-community-lgtm,omitempty"`
TiCommunityMerge []TiCommunityMerge `json:"ti-community-merge,omitempty"`
Expand Down Expand Up @@ -365,6 +378,10 @@ func (c *Configuration) setDefaults() {
for i := range c.TiCommunityBlunderbuss {
c.TiCommunityBlunderbuss[i].setDefaults()
}

if len(c.LogLevel) == 0 {
c.LogLevel = defaultLogLevel.String()
}
}

// Validate will return an error if there are any invalid external plugin config.
Expand All @@ -387,6 +404,10 @@ func (c *Configuration) Validate() error {
return err
}

if err := validateLogLevel(c.LogLevel); err != nil {
return err
}

if err := validateLgtm(c.TiCommunityLgtm); err != nil {
return err
}
Expand Down Expand Up @@ -414,6 +435,16 @@ func (c *Configuration) Validate() error {
return nil
}

// validateLogLevel will return an error if the value of the log level is invalid.
func validateLogLevel(logLevel string) error {
_, err := logrus.ParseLevel(logLevel)
if err != nil {
return err
}

return nil
}

// validateLgtm will return an error if the URL configured by lgtm is invalid.
func validateLgtm(lgtms []TiCommunityLgtm) error {
for _, lgtm := range lgtms {
Expand Down
49 changes: 49 additions & 0 deletions internal/pkg/externalplugins/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestValidateConfig(t *testing.T) {
tichiWebURL string
prProcessLink string
commandHelpLink string
logLevel string
lgtm *TiCommunityLgtm
merge *TiCommunityMerge
owners *TiCommunityOwners
Expand Down Expand Up @@ -708,6 +709,53 @@ func TestValidateConfig(t *testing.T) {
},
expected: fmt.Errorf("actions contain illegal value nop"),
},
{
name: "invalid log level",
tichiWebURL: "https://tichiWebURL",
commandHelpLink: "https://commandHelpLink",
prProcessLink: "https://prProcessLink",
logLevel: "nop",
lgtm: &TiCommunityLgtm{
Repos: []string{"ti-community-infra/test-dev"},
ReviewActsAsLgtm: true,
PullOwnersEndpoint: "https://bots.tidb.io/ti-community-bot",
},
merge: &TiCommunityMerge{
Repos: []string{"ti-community-infra/test-dev"},
PullOwnersEndpoint: "https://bots.tidb.io/ti-community-bot",
},
owners: &TiCommunityOwners{
Repos: []string{"ti-community-infra/test-dev"},
SigEndpoint: "https://bots.tidb.io/ti-community-bot",
},
autoresponders: &TiCommunityAutoresponder{
Repos: []string{"ti-community-infra/test-dev"},
AutoResponds: []AutoRespond{
{
Regex: `(?mi)^/merge\s*$`,
Message: "/run-all-test",
},
},
},
blunderbuss: &TiCommunityBlunderbuss{
Repos: []string{"ti-community-infra/test-dev"},
MaxReviewerCount: 2,
ExcludeReviewers: []string{},
PullOwnersEndpoint: "https://bots.tidb.io/ti-community-bot",
},
labelBlocker: &TiCommunityLabelBlocker{
Repos: []string{"ti-community-infra/test-dev"},
BlockLabels: []BlockLabel{
{
Regex: `^status/can-merge$`,
Actions: []string{"labeled", "unlabeled"},
TrustedTeams: []string{"release-team"},
TrustedUsers: []string{"ti-chi-bot"},
},
},
},
expected: fmt.Errorf("not a valid logrus Level: \"nop\""),
},
}

for _, testcase := range testcases {
Expand All @@ -717,6 +765,7 @@ func TestValidateConfig(t *testing.T) {
TichiWebURL: tc.tichiWebURL,
PRProcessLink: tc.prProcessLink,
CommandHelpLink: tc.commandHelpLink,
LogLevel: tc.logLevel,
TiCommunityLgtm: []TiCommunityLgtm{
*tc.lgtm,
},
Expand Down
11 changes: 11 additions & 0 deletions internal/pkg/externalplugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func (pa *ConfigAgent) Load(path string) error {
return err
}

// Set up a unified log configuration.
lvl, err := logrus.ParseLevel(np.LogLevel)
if err != nil {
return err
}

logrus.SetLevel(lvl)
logrus.SetFormatter(&logrus.JSONFormatter{})
// Output line number and call function information.
logrus.SetReportCaller(true)

pa.Set(np)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions test/testdata/config_combine.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tichi-web-url: https://prow-dev.tidb.io/tichi
pr-process-link: https://book.prow.tidb.io/#/en/workflows/pr
command-help-link: https://prow-dev.tidb.io/command-help
tichi_web_url: https://prow-dev.tidb.io/tichi
pr_process_link: https://book.prow.tidb.io/#/en/workflows/pr
command_help_link: https://prow-dev.tidb.io/command-help

ti-community-lgtm:
- repos:
Expand Down
6 changes: 3 additions & 3 deletions test/testdata/config_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tichi-web-url: "https://tichi"
pr-process-link: "https://pr"
command-help-link: "https://command"
tichi_web_url: "https://tichi"
pr_process_link: "https://pr"
command_help_link: "https://command"
ti-community-lgtm:
- repos:
- ti-community-infra/test-dev
Expand Down
Loading

0 comments on commit 1765abb

Please sign in to comment.