|
| 1 | +package connector |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" |
| 8 | + "github.com/conductorone/baton-sdk/pkg/annotations" |
| 9 | + "github.com/conductorone/baton-sdk/pkg/pagination" |
| 10 | + "github.com/google/go-github/v69/github" |
| 11 | +) |
| 12 | + |
| 13 | +type invitationResourceType struct { |
| 14 | + client *github.Client |
| 15 | + orgCache *orgNameCache |
| 16 | +} |
| 17 | + |
| 18 | +func (i *invitationResourceType) ResourceType(_ context.Context) *v2.ResourceType { |
| 19 | + return resourceTypeInvitation |
| 20 | +} |
| 21 | + |
| 22 | +func (i *invitationResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) { |
| 23 | + var annotations annotations.Annotations |
| 24 | + if parentID == nil { |
| 25 | + return nil, "", nil, nil |
| 26 | + } |
| 27 | + |
| 28 | + bag, page, err := parsePageToken(pt.Token, &v2.ResourceId{ResourceType: resourceTypeUser.Id}) |
| 29 | + if err != nil { |
| 30 | + return nil, "", nil, err |
| 31 | + } |
| 32 | + |
| 33 | + orgName, err := i.orgCache.GetOrgName(ctx, parentID) |
| 34 | + if err != nil { |
| 35 | + return nil, "", nil, err |
| 36 | + } |
| 37 | + invitations, resp, err := i.client.Organizations.ListPendingOrgInvitations(ctx, orgName, &github.ListOptions{ |
| 38 | + Page: page, |
| 39 | + PerPage: pt.Size, |
| 40 | + }) |
| 41 | + if err != nil { |
| 42 | + return nil, "", nil, fmt.Errorf("github-connector: ListPendingOrgInvitatioins failed: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + restApiRateLimit, err := extractRateLimitData(resp) |
| 46 | + if err != nil { |
| 47 | + return nil, "", nil, err |
| 48 | + } |
| 49 | + |
| 50 | + nextPage, _, err := parseResp(resp) |
| 51 | + if err != nil { |
| 52 | + return nil, "", nil, err |
| 53 | + } |
| 54 | + |
| 55 | + pageToken, err := bag.NextToken(nextPage) |
| 56 | + if err != nil { |
| 57 | + return nil, "", nil, err |
| 58 | + } |
| 59 | + |
| 60 | + invitationResources := make([]*v2.Resource, 0, len(invitations)) |
| 61 | + for _, invitation := range invitations { |
| 62 | + ir, err := invitationToUserResource(invitation) |
| 63 | + if err != nil { |
| 64 | + return nil, "", nil, err |
| 65 | + } |
| 66 | + invitationResources = append(invitationResources, ir) |
| 67 | + } |
| 68 | + annotations.WithRateLimiting(restApiRateLimit) |
| 69 | + return invitationResources, pageToken, nil, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (i *invitationResourceType) Entitlements(_ context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) { |
| 73 | + return nil, "", nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (i *invitationResourceType) Grants(ctx context.Context, resource *v2.Resource, pToken *pagination.Token) ([]*v2.Grant, string, annotations.Annotations, error) { |
| 77 | + return nil, "", nil, nil |
| 78 | +} |
| 79 | + |
| 80 | +type invitationBuilderParams struct { |
| 81 | + client *github.Client |
| 82 | + orgCache *orgNameCache |
| 83 | +} |
| 84 | + |
| 85 | +func invitationBuilder(p invitationBuilderParams) *invitationResourceType { |
| 86 | + return &invitationResourceType{ |
| 87 | + client: p.client, |
| 88 | + orgCache: p.orgCache, |
| 89 | + } |
| 90 | +} |
0 commit comments