diff --git a/internal/validate/install/config.go b/internal/validate/install/config.go index 39695dbb25..7578522f13 100644 --- a/internal/validate/install/config.go +++ b/internal/validate/install/config.go @@ -52,6 +52,11 @@ type Insight struct { DeleteWhenDone bool `yaml:"deleteWhenDone"` } +type Smtp struct { + Enabled bool `yaml:"enabled"` + To string `yaml:"to"` +} + type ValidationSpec struct { // Search queries used for validation testing, e.g. "repo:^github\\.com/gorilla/mux$ Router". SearchQuery []string `yaml:"searchQuery"` @@ -61,6 +66,9 @@ type ValidationSpec struct { // Insight used for validation testing. Insight Insight `yaml:"insight"` + + //Test SMTP configuration + Smtp Smtp `yaml:"smtp"` } // DefaultConfig returns a default configuration to be used for testing. @@ -108,6 +116,10 @@ func DefaultConfig() *ValidationSpec { }, DeleteWhenDone: true, }, + Smtp: Smtp{ + Enabled: false, + To: "example@domain.com", + }, } } diff --git a/internal/validate/install/install.go b/internal/validate/install/install.go index d9154b1d64..a9d905a696 100644 --- a/internal/validate/install/install.go +++ b/internal/validate/install/install.go @@ -47,6 +47,23 @@ func Validate(ctx context.Context, client api.Client, config *ValidationSpec) er } } + if config.Smtp.Enabled { + log.Printf("%s validating smtp connection", validate.EmojiFingerPointRight) + + smtpQuery := `mutation sendTestEmail($to: String!) { + sendTestEmail(to: $to) + }` + smtpVars := map[string]interface{}{ + "to": config.Smtp.To, + } + + result, err := checkSmtp(ctx, client, smtpQuery, smtpVars) + if err != nil { + return err + } + log.Printf("%s '%s'", validate.SuccessEmoji, result) + } + if config.Insight.Title != "" { log.Printf("%s validating code insight", validate.EmojiFingerPointRight) @@ -130,6 +147,27 @@ func searchMatchCount(ctx context.Context, client api.Client, searchExpr string) return result.Search.Results.MatchCount, nil } +func checkSmtp(ctx context.Context, client api.Client, query string, variables map[string]interface{}) (string, error) { + q := clientQuery{ + opName: "CheckSmtpConfig", + query: query, + variables: variables, + } + + var result struct { + SendTestEmail string `json:"sendTestEmail"` + } + + ok, err := client.NewRequest(q.query, q.variables).Do(ctx, &result) + if err != nil { + return "", errors.Wrap(err, "sendTestEmail failed") + } + if !ok { + return "", errors.New("sendTestEmail failed, no data to unmarshal") + } + return result.SendTestEmail, nil +} + func repoCloneTimeout(ctx context.Context, client api.Client, repo string, srv ExternalService) (bool, error) { for i := 0; i < srv.MaxRetries; i++ { repos, err := listClonedRepos(ctx, client, []string{repo})