Skip to content

Commit 1ef7956

Browse files
authored
Scj/update/smtp validate (#973)
* add config for smtp * add function for smtp * smtp enabled check * lint * change Smtp.Test to Smtp.Enabled
1 parent ea9e501 commit 1ef7956

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

internal/validate/install/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ type Insight struct {
5252
DeleteWhenDone bool `yaml:"deleteWhenDone"`
5353
}
5454

55+
type Smtp struct {
56+
Enabled bool `yaml:"enabled"`
57+
To string `yaml:"to"`
58+
}
59+
5560
type ValidationSpec struct {
5661
// Search queries used for validation testing, e.g. "repo:^github\\.com/gorilla/mux$ Router".
5762
SearchQuery []string `yaml:"searchQuery"`
@@ -61,6 +66,9 @@ type ValidationSpec struct {
6166

6267
// Insight used for validation testing.
6368
Insight Insight `yaml:"insight"`
69+
70+
//Test SMTP configuration
71+
Smtp Smtp `yaml:"smtp"`
6472
}
6573

6674
// DefaultConfig returns a default configuration to be used for testing.
@@ -108,6 +116,10 @@ func DefaultConfig() *ValidationSpec {
108116
},
109117
DeleteWhenDone: true,
110118
},
119+
Smtp: Smtp{
120+
Enabled: false,
121+
122+
},
111123
}
112124
}
113125

internal/validate/install/install.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ func Validate(ctx context.Context, client api.Client, config *ValidationSpec) er
4747
}
4848
}
4949

50+
if config.Smtp.Enabled {
51+
log.Printf("%s validating smtp connection", validate.EmojiFingerPointRight)
52+
53+
smtpQuery := `mutation sendTestEmail($to: String!) {
54+
sendTestEmail(to: $to)
55+
}`
56+
smtpVars := map[string]interface{}{
57+
"to": config.Smtp.To,
58+
}
59+
60+
result, err := checkSmtp(ctx, client, smtpQuery, smtpVars)
61+
if err != nil {
62+
return err
63+
}
64+
log.Printf("%s '%s'", validate.SuccessEmoji, result)
65+
}
66+
5067
if config.Insight.Title != "" {
5168
log.Printf("%s validating code insight", validate.EmojiFingerPointRight)
5269

@@ -130,6 +147,27 @@ func searchMatchCount(ctx context.Context, client api.Client, searchExpr string)
130147
return result.Search.Results.MatchCount, nil
131148
}
132149

150+
func checkSmtp(ctx context.Context, client api.Client, query string, variables map[string]interface{}) (string, error) {
151+
q := clientQuery{
152+
opName: "CheckSmtpConfig",
153+
query: query,
154+
variables: variables,
155+
}
156+
157+
var result struct {
158+
SendTestEmail string `json:"sendTestEmail"`
159+
}
160+
161+
ok, err := client.NewRequest(q.query, q.variables).Do(ctx, &result)
162+
if err != nil {
163+
return "", errors.Wrap(err, "sendTestEmail failed")
164+
}
165+
if !ok {
166+
return "", errors.New("sendTestEmail failed, no data to unmarshal")
167+
}
168+
return result.SendTestEmail, nil
169+
}
170+
133171
func repoCloneTimeout(ctx context.Context, client api.Client, repo string, srv ExternalService) (bool, error) {
134172
for i := 0; i < srv.MaxRetries; i++ {
135173
repos, err := listClonedRepos(ctx, client, []string{repo})

0 commit comments

Comments
 (0)