Skip to content

Commit af597c3

Browse files
authored
opsgenie: skip unimported members (#66)
When a team has a member which references a user that isn't imported, we shouldn't hard fail. Additionally, stop OpsGenie SDK from logging to console unless an error is encountered, since most of the warnings are irrelevant to end-user.
1 parent fb824bb commit af597c3

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

pager/opsgenie.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,25 @@ type Opsgenie struct {
2828
}
2929

3030
func NewOpsgenie(apiKey string) *Opsgenie {
31-
conf := &client.Config{ApiKey: apiKey}
31+
conf := &client.Config{
32+
ApiKey: apiKey,
33+
34+
// This corresponds to logrus.ErrorLevel but avoids importing logrus,
35+
// since we don't use it but the SDK imports it.
36+
LogLevel: 2,
37+
}
3238
return NewOpsgenieWithConfig(conf)
3339
}
3440

3541
func NewOpsgenieWithURL(apiKey, url string) *Opsgenie {
36-
conf := &client.Config{ApiKey: apiKey, OpsGenieAPIURL: client.ApiUrl(url)}
42+
conf := &client.Config{
43+
ApiKey: apiKey,
44+
OpsGenieAPIURL: client.ApiUrl(url),
45+
46+
// This corresponds to logrus.ErrorLevel but avoids importing logrus,
47+
// since we don't use it but the SDK imports it.
48+
LogLevel: 2,
49+
}
3750
return NewOpsgenieWithConfig(conf)
3851
}
3952

@@ -150,7 +163,11 @@ func (o *Opsgenie) LoadTeamMembers(ctx context.Context) error {
150163
TeamID: t.ID,
151164
UserID: m.User.ID,
152165
}); err != nil {
153-
return fmt.Errorf("saving team member to db: %w", err)
166+
if sqlErr, ok := store.AsSQLError(err); ok && sqlErr.IsForeignKeyConstraint() {
167+
console.Warnf("User %q (%s) isn't imported. Skipping...\n", m.User.Username, m.User.ID)
168+
return nil
169+
}
170+
return fmt.Errorf("saving user %q (%s) as member of %q (%s) to db: %w", m.User.Username, m.User.ID, t.Name, t.ID, err)
154171
}
155172
}
156173
}

pager/testdata/TestOpsgenie/apiserver/v2-teams-b7acbc33-9853-4150-8a4b-10156d9408c8-identifiertype-id.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
"username": "[email protected]"
1111
},
1212
"role": "admin"
13+
},
14+
{
15+
"user": {
16+
"id": "e5b92115-bfe7-43eb-8c2a-e467f2e5ddc4",
17+
"username": "[email protected]"
18+
},
19+
"role": "admin"
1320
}
1421
],
1522
"links": {
@@ -19,4 +26,4 @@
1926
},
2027
"took": 0.029,
2128
"requestId": "5a53826f-7864-4bf2-ada3-2979784d1e98"
22-
}
29+
}

pager/testdata/TestOpsgenie/apiserver/v2-teams.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
],
1313
"took": 0.009,
1414
"requestId": "0fd19d50-632a-4f13-a357-b26d0065adc5"
15-
}
15+
}

0 commit comments

Comments
 (0)