mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-02-13 21:00:00 +00:00
Fix BB email (#6102)
This commit is contained in:
@@ -102,7 +102,21 @@ func (c *config) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mod
|
||||
if err != nil {
|
||||
return nil, redirectURL, err
|
||||
}
|
||||
return convertUser(curr, token), redirectURL, nil
|
||||
|
||||
emails, err := client.ListEmail()
|
||||
if err != nil {
|
||||
return nil, redirectURL, err
|
||||
}
|
||||
|
||||
primaryEmail := ""
|
||||
for _, e := range emails.Values {
|
||||
if e.IsPrimary {
|
||||
primaryEmail = e.Email
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return convertUser(curr, token, primaryEmail), redirectURL, nil
|
||||
}
|
||||
|
||||
// Auth uses the Bitbucket oauth2 access token and refresh token to authenticate
|
||||
|
||||
@@ -126,7 +126,7 @@ func sshCloneLink(repo *internal.Repo) string {
|
||||
|
||||
// convertUser is a helper function used to convert a Bitbucket user account
|
||||
// structure to the Woodpecker User structure.
|
||||
func convertUser(from *internal.Account, token *oauth2.Token) *model.User {
|
||||
func convertUser(from *internal.Account, token *oauth2.Token, email string) *model.User {
|
||||
return &model.User{
|
||||
Login: from.Login,
|
||||
AccessToken: token.AccessToken,
|
||||
@@ -134,6 +134,7 @@ func convertUser(from *internal.Account, token *oauth2.Token) *model.User {
|
||||
Expiry: token.Expiry.UTC().Unix(),
|
||||
Avatar: from.Links.Avatar.Href,
|
||||
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.UUID)),
|
||||
Email: email,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,9 +86,10 @@ func Test_convertUser(t *testing.T) {
|
||||
user := &internal.Account{Login: "octocat"}
|
||||
user.Links.Avatar.Href = "http://..."
|
||||
|
||||
result := convertUser(user, token)
|
||||
result := convertUser(user, token, "test@example.com")
|
||||
assert.Equal(t, user.Links.Avatar.Href, result.Avatar)
|
||||
assert.Equal(t, user.Login, result.Login)
|
||||
assert.Equal(t, "test@example.com", result.Email)
|
||||
assert.Equal(t, token.AccessToken, result.AccessToken)
|
||||
assert.Equal(t, token.RefreshToken, result.RefreshToken)
|
||||
assert.Equal(t, token.Expiry.UTC().Unix(), result.Expiry)
|
||||
|
||||
@@ -37,6 +37,7 @@ func Handler() http.Handler {
|
||||
e.POST("/2.0/repositories/:owner/:name/commit/:commit/statuses/build", createRepoStatus)
|
||||
e.GET("/2.0/repositories/:owner", getUserRepos)
|
||||
e.GET("/2.0/user/", getUser)
|
||||
e.GET("/2.0/user/emails", getEmails)
|
||||
e.GET("/2.0/user/permissions/repositories", getPermissions)
|
||||
e.GET("/2.0/repositories/:owner/:name/commits/:commit", getBranchHead)
|
||||
e.GET("/2.0/repositories/:owner/:name/pullrequests", getPullRequests)
|
||||
@@ -179,6 +180,15 @@ func getUser(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func getEmails(c *gin.Context) {
|
||||
switch c.Request.Header.Get("Authorization") {
|
||||
case "Bearer user_not_found", "Bearer a87ff679":
|
||||
c.String(http.StatusNotFound, "")
|
||||
default:
|
||||
c.String(http.StatusOK, emailsPayload)
|
||||
}
|
||||
}
|
||||
|
||||
func getUserRepos(c *gin.Context) {
|
||||
switch c.Request.Header.Get("Authorization") {
|
||||
case "Bearer repos_not_found", "Bearer 70efdf2e":
|
||||
@@ -489,6 +499,21 @@ const userRepoPayload = `
|
||||
}
|
||||
`
|
||||
|
||||
const emailsPayload = `
|
||||
{
|
||||
"pagelen": 10,
|
||||
"values": [
|
||||
{
|
||||
"email": "test@example.com",
|
||||
"is_confirmed": true,
|
||||
"is_primary": true
|
||||
}
|
||||
],
|
||||
"page": 1,
|
||||
"size": 1
|
||||
}
|
||||
`
|
||||
|
||||
const workspacesPayload = `
|
||||
{
|
||||
"page": 1,
|
||||
|
||||
Reference in New Issue
Block a user