Skip to content

Commit

Permalink
Scj/update/smtp validate (#973)
Browse files Browse the repository at this point in the history
* add config for smtp

* add function for smtp

* smtp enabled check

* lint

* change Smtp.Test to Smtp.Enabled
  • Loading branch information
scjohns authored Apr 24, 2023
1 parent ea9e501 commit 1ef7956
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/validate/install/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.
Expand Down Expand Up @@ -108,6 +116,10 @@ func DefaultConfig() *ValidationSpec {
},
DeleteWhenDone: true,
},
Smtp: Smtp{
Enabled: false,
To: "[email protected]",
},
}
}

Expand Down
38 changes: 38 additions & 0 deletions internal/validate/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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})
Expand Down

0 comments on commit 1ef7956

Please sign in to comment.