Skip to content

Commit c9dfff3

Browse files
authored
Continue if users.GetByID() fails. (#31)
This uses an undocumented API that can return 404 in some cases. We can still use the partially-populated user object returned by Organizations.ListMembers(). It lacks fields like the email address, but it's better than nothing.
1 parent 1415dd6 commit c9dfff3

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pkg/connector/user.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package connector
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"net/mail"
78
"strconv"
89
"strings"
@@ -12,7 +13,9 @@ import (
1213
"github.com/conductorone/baton-sdk/pkg/pagination"
1314
"github.com/conductorone/baton-sdk/pkg/types/resource"
1415
"github.com/google/go-github/v41/github"
16+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1517
"github.com/shurcooL/githubv4"
18+
"go.uber.org/zap"
1619
"google.golang.org/protobuf/types/known/timestamppb"
1720
)
1821

@@ -82,6 +85,7 @@ func (o *userResourceType) ResourceType(_ context.Context) *v2.ResourceType {
8285
}
8386

8487
func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) {
88+
l := ctxzap.Extract(ctx)
8589
var annotations annotations.Annotations
8690
if parentID == nil {
8791
return nil, "", nil, nil
@@ -130,9 +134,14 @@ func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
130134
q := listUsersQuery{}
131135
rv := make([]*v2.Resource, 0, len(users))
132136
for _, user := range users {
133-
u, _, err := o.client.Users.GetByID(ctx, user.GetID())
137+
u, res, err := o.client.Users.GetByID(ctx, user.GetID())
134138
if err != nil {
135-
return nil, "", nil, err
139+
// This undocumented API can return 404 for some users. If this fails it means we won't get some of their details like email
140+
if res.StatusCode != http.StatusNotFound {
141+
return nil, "", nil, err
142+
}
143+
l.Error("error fetching user by id", zap.Error(err), zap.Int64("user_id", user.GetID()))
144+
u = user
136145
}
137146
userEmail := u.GetEmail()
138147
var extraEmails []string

0 commit comments

Comments
 (0)