|
| 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/connectorbuilder" |
| 10 | + "github.com/conductorone/baton-sdk/pkg/pagination" |
| 11 | + "github.com/conductorone/baton-sdk/pkg/types/resource" |
| 12 | + "github.com/google/go-github/v69/github" |
| 13 | +) |
| 14 | + |
| 15 | +func invitationToUserResource(invitation *github.Invitation) (*v2.Resource, error) { |
| 16 | + login := invitation.GetLogin() |
| 17 | + if login == "" { |
| 18 | + login = invitation.GetEmail() |
| 19 | + } |
| 20 | + |
| 21 | + ret, err := resource.NewUserResource( |
| 22 | + login, |
| 23 | + resourceTypeInvitation, |
| 24 | + invitation.GetID(), |
| 25 | + []resource.UserTraitOption{ |
| 26 | + resource.WithEmail(invitation.GetEmail(), true), |
| 27 | + resource.WithUserProfile(map[string]interface{}{ |
| 28 | + "login": login, |
| 29 | + "inviter": invitation.GetInviter().GetLogin(), |
| 30 | + }), |
| 31 | + resource.WithStatus(v2.UserTrait_Status_STATUS_UNSPECIFIED), |
| 32 | + resource.WithUserLogin(login), |
| 33 | + }, |
| 34 | + ) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + return ret, nil |
| 39 | +} |
| 40 | + |
| 41 | +type invitationResourceType struct { |
| 42 | + client *github.Client |
| 43 | + orgCache *orgNameCache |
| 44 | +} |
| 45 | + |
| 46 | +func (i *invitationResourceType) ResourceType(_ context.Context) *v2.ResourceType { |
| 47 | + return resourceTypeInvitation |
| 48 | +} |
| 49 | + |
| 50 | +func (i *invitationResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) { |
| 51 | + var annotations annotations.Annotations |
| 52 | + if parentID == nil { |
| 53 | + return nil, "", nil, nil |
| 54 | + } |
| 55 | + |
| 56 | + bag, page, err := parsePageToken(pt.Token, &v2.ResourceId{ResourceType: resourceTypeUser.Id}) |
| 57 | + if err != nil { |
| 58 | + return nil, "", nil, err |
| 59 | + } |
| 60 | + |
| 61 | + orgName, err := i.orgCache.GetOrgName(ctx, parentID) |
| 62 | + if err != nil { |
| 63 | + return nil, "", nil, err |
| 64 | + } |
| 65 | + invitations, resp, err := i.client.Organizations.ListPendingOrgInvitations(ctx, orgName, &github.ListOptions{ |
| 66 | + Page: page, |
| 67 | + PerPage: pt.Size, |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return nil, "", nil, fmt.Errorf("github-connector: ListPendingOrgInvitatioins failed: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + restApiRateLimit, err := extractRateLimitData(resp) |
| 74 | + if err != nil { |
| 75 | + return nil, "", nil, err |
| 76 | + } |
| 77 | + |
| 78 | + nextPage, _, err := parseResp(resp) |
| 79 | + if err != nil { |
| 80 | + return nil, "", nil, err |
| 81 | + } |
| 82 | + |
| 83 | + pageToken, err := bag.NextToken(nextPage) |
| 84 | + if err != nil { |
| 85 | + return nil, "", nil, err |
| 86 | + } |
| 87 | + |
| 88 | + invitationResources := make([]*v2.Resource, 0, len(invitations)) |
| 89 | + for _, invitation := range invitations { |
| 90 | + ir, err := invitationToUserResource(invitation) |
| 91 | + if err != nil { |
| 92 | + return nil, "", nil, err |
| 93 | + } |
| 94 | + invitationResources = append(invitationResources, ir) |
| 95 | + } |
| 96 | + annotations.WithRateLimiting(restApiRateLimit) |
| 97 | + return invitationResources, pageToken, nil, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (i *invitationResourceType) Entitlements(_ context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) { |
| 101 | + return nil, "", nil, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (i *invitationResourceType) Grants(ctx context.Context, resource *v2.Resource, pToken *pagination.Token) ([]*v2.Grant, string, annotations.Annotations, error) { |
| 105 | + return nil, "", nil, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (i *invitationResourceType) CreateAccountCapabilityDetails(ctx context.Context) (*v2.CredentialDetailsAccountProvisioning, annotations.Annotations, error) { |
| 109 | + return &v2.CredentialDetailsAccountProvisioning{ |
| 110 | + SupportedCredentialOptions: []v2.CapabilityDetailCredentialOption{ |
| 111 | + v2.CapabilityDetailCredentialOption_CAPABILITY_DETAIL_CREDENTIAL_OPTION_NO_PASSWORD, |
| 112 | + }, |
| 113 | + PreferredCredentialOption: v2.CapabilityDetailCredentialOption_CAPABILITY_DETAIL_CREDENTIAL_OPTION_NO_PASSWORD, |
| 114 | + }, nil, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (i *invitationResourceType) CreateAccount( |
| 118 | + ctx context.Context, |
| 119 | + accountInfo *v2.AccountInfo, |
| 120 | + credentialOptions *v2.CredentialOptions, |
| 121 | +) ( |
| 122 | + connectorbuilder.CreateAccountResponse, |
| 123 | + []*v2.PlaintextData, |
| 124 | + annotations.Annotations, |
| 125 | + error, |
| 126 | +) { |
| 127 | + params, err := getCreateUserParams(accountInfo) |
| 128 | + if err != nil { |
| 129 | + return nil, nil, nil, fmt.Errorf("github-connectorv2: failed to get CreateUserParams: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + invitation, resp, err := i.client.Organizations.CreateOrgInvitation(ctx, params.org, &github.CreateOrgInvitationOptions{ |
| 133 | + Email: params.email, |
| 134 | + }) |
| 135 | + if err != nil { |
| 136 | + return nil, nil, nil, fmt.Errorf("github-connectorv2: failed to invite user to org: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + restApiRateLimit, err := extractRateLimitData(resp) |
| 140 | + if err != nil { |
| 141 | + return nil, nil, nil, err |
| 142 | + } |
| 143 | + |
| 144 | + var annotations annotations.Annotations |
| 145 | + annotations.WithRateLimiting(restApiRateLimit) |
| 146 | + |
| 147 | + r, err := invitationToUserResource(invitation) |
| 148 | + if err != nil { |
| 149 | + return nil, nil, nil, fmt.Errorf("github-connectorv2: cannot create user resource: %w", err) |
| 150 | + } |
| 151 | + return &v2.CreateAccountResponse_SuccessResult{ |
| 152 | + Resource: r, |
| 153 | + }, nil, nil, nil |
| 154 | +} |
| 155 | + |
| 156 | +type createUserParams struct { |
| 157 | + org string |
| 158 | + email *string |
| 159 | +} |
| 160 | + |
| 161 | +func getCreateUserParams(accountInfo *v2.AccountInfo) (*createUserParams, error) { |
| 162 | + pMap := accountInfo.Profile.AsMap() |
| 163 | + org, ok := pMap["org"].(string) |
| 164 | + if !ok || org == "" { |
| 165 | + return nil, fmt.Errorf("org is required") |
| 166 | + } |
| 167 | + |
| 168 | + e, emailExisted := pMap["email"].(string) |
| 169 | + if !emailExisted || e == "" { |
| 170 | + return nil, fmt.Errorf("email is required") |
| 171 | + } |
| 172 | + |
| 173 | + return &createUserParams{ |
| 174 | + org: org, |
| 175 | + email: &e, |
| 176 | + }, nil |
| 177 | +} |
| 178 | + |
| 179 | +type invitationBuilderParams struct { |
| 180 | + client *github.Client |
| 181 | + orgCache *orgNameCache |
| 182 | +} |
| 183 | + |
| 184 | +func invitationBuilder(p invitationBuilderParams) *invitationResourceType { |
| 185 | + return &invitationResourceType{ |
| 186 | + client: p.client, |
| 187 | + orgCache: p.orgCache, |
| 188 | + } |
| 189 | +} |
0 commit comments