Skip to content

Commit c98d468

Browse files
authored
fixed error of bitbucket in struct
1 parent 5dc244d commit c98d468

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

providers/bitbucket/bitbucket.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ const (
1919
endpointEmail string = "https://api.bitbucket.org/2.0/user/emails"
2020
)
2121

22+
type EmailAddress struct {
23+
Type string `json:"type"`
24+
Links Links `json:"links"`
25+
Email string `json:"email"`
26+
IsPrimary bool `json:"is_primary"`
27+
IsConfirmed bool `json:"is_confirmed"`
28+
}
29+
30+
type Links struct {
31+
Self Self `json:"self"`
32+
}
33+
34+
type Self struct {
35+
Href string `json:"href"`
36+
}
37+
38+
type MailList struct {
39+
Values []EmailAddress `json:"values"`
40+
Pagelen int `json:"pagelen"`
41+
Size int `json:"size"`
42+
Page int `json:"page"`
43+
}
44+
2245
// New creates a new Bitbucket provider, and sets up important connection details.
2346
// You should always call `bitbucket.New` to get a new Provider. Never try to create
2447
// one manually.
@@ -162,21 +185,19 @@ func (p *Provider) getEmail(user *goth.User, sess *Session) error {
162185
return fmt.Errorf("%s responded with a %d trying to fetch email addresses", p.providerName, response.StatusCode)
163186
}
164187

165-
var mailList []struct {
166-
Email string `json:"email"`
167-
Primary bool `json:"is_primary"`
168-
Confirmed bool `json:"is_confirmed"`
169-
}
188+
var mailList MailList
170189
err = json.NewDecoder(response.Body).Decode(&mailList)
171190
if err != nil {
172191
return err
173192
}
174-
for _, v := range mailList {
175-
if v.Primary && v.Confirmed {
176-
user.Email = v.Email
193+
194+
for _, emailAddress := range mailList.Values {
195+
if emailAddress.IsPrimary && emailAddress.IsConfirmed {
196+
user.Email = emailAddress.Email
177197
return nil
178198
}
179199
}
200+
180201
return fmt.Errorf("%s did not return any confirmed, primary email address", p.providerName)
181202
}
182203

0 commit comments

Comments
 (0)