Skip to content

Commit 611c62f

Browse files
authored
When provisioning team or repositories, don't rely on the entitlement slug (#42)
* When provisioning team or repositories, don't rely on the entitlement slug * Fix tests
1 parent afbd152 commit 611c62f

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

pkg/connector/repository.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strconv"
7+
"strings"
78

89
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
910
"github.com/conductorone/baton-sdk/pkg/annotations"
@@ -263,6 +264,12 @@ func (o *repositoryResourceType) Grant(ctx context.Context, principal *v2.Resour
263264
return nil, err
264265
}
265266

267+
enIDParts := strings.Split(en.Id, ":")
268+
if len(enIDParts) != 3 {
269+
return nil, fmt.Errorf("github-connectorv2: invalid entitlement ID: %s", en.Id)
270+
}
271+
permission := enIDParts[2]
272+
266273
switch principal.Id.ResourceType {
267274
case resourceTypeUser.Id:
268275
user, _, err := o.client.Users.GetByID(ctx, principalID)
@@ -275,8 +282,9 @@ func (o *repositoryResourceType) Grant(ctx context.Context, principal *v2.Resour
275282
repo.GetOwner().GetLogin(),
276283
repo.GetName(),
277284
user.GetLogin(),
278-
&github.RepositoryAddCollaboratorOptions{Permission: en.Slug},
285+
&github.RepositoryAddCollaboratorOptions{Permission: permission},
279286
)
287+
280288
if e != nil {
281289
return nil, fmt.Errorf("github-connectorv2: failed to add user to a repository: %w", e)
282290
}
@@ -287,7 +295,7 @@ func (o *repositoryResourceType) Grant(ctx context.Context, principal *v2.Resour
287295
}
288296

289297
_, err = o.client.Teams.AddTeamRepoBySlug(ctx, org.GetLogin(), team.GetSlug(), repo.GetOwner().GetLogin(), repo.GetName(), &github.TeamAddTeamRepoOptions{
290-
Permission: en.Slug,
298+
Permission: permission,
291299
})
292300
if err != nil {
293301
return nil, fmt.Errorf("github-connectorv2: failed to add team to a repo: %w", err)

pkg/connector/repository_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/conductorone/baton-github/test"
8-
"github.com/conductorone/baton-github/test/mocks"
97
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
108
"github.com/conductorone/baton-sdk/pkg/pagination"
9+
entitlement2 "github.com/conductorone/baton-sdk/pkg/types/entitlement"
1110
"github.com/google/go-github/v63/github"
1211
"github.com/stretchr/testify/require"
12+
13+
"github.com/conductorone/baton-github/test"
14+
"github.com/conductorone/baton-github/test/mocks"
1315
)
1416

1517
func TestRepository(t *testing.T) {
@@ -28,7 +30,10 @@ func TestRepository(t *testing.T) {
2830
repository, _ := repositoryResource(ctx, githubRepository, organization.Id)
2931
user, _ := userResource(ctx, githubUser, *githubUser.Email, nil)
3032

31-
entitlement := v2.Entitlement{Resource: repository}
33+
entitlement := v2.Entitlement{
34+
Id: entitlement2.NewEntitlementID(repository, "admin"),
35+
Resource: repository,
36+
}
3237

3338
grantAnnotations, err := client.Grant(ctx, user, &entitlement)
3439
require.Nil(t, err)

pkg/connector/team.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strconv"
7+
"strings"
78

89
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
910
"github.com/conductorone/baton-sdk/pkg/annotations"
@@ -266,13 +267,20 @@ func (o *teamResourceType) Grant(ctx context.Context, principal *v2.Resource, en
266267
return nil, fmt.Errorf("github-connectorv2: failed to get user %d, err: %w", userId, err)
267268
}
268269

270+
enIDParts := strings.Split(entitlement.Id, ":")
271+
if len(enIDParts) != 3 {
272+
return nil, fmt.Errorf("github-connectorv2: invalid entitlement ID: %s", entitlement.Id)
273+
}
274+
permission := enIDParts[2]
275+
269276
_, _, e := o.client.Teams.AddTeamMembershipByID(
270277
ctx,
271278
orgId,
272279
teamId,
273280
user.GetLogin(),
274-
&github.TeamAddTeamMembershipOptions{Role: entitlement.Slug},
281+
&github.TeamAddTeamMembershipOptions{Role: permission},
275282
)
283+
276284
if e != nil {
277285
return nil, fmt.Errorf("github-connectorv2: failed to add user to a team: %w", e)
278286
}

pkg/connector/team_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/conductorone/baton-github/test"
8-
"github.com/conductorone/baton-github/test/mocks"
97
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
108
"github.com/conductorone/baton-sdk/pkg/pagination"
9+
entitlement2 "github.com/conductorone/baton-sdk/pkg/types/entitlement"
1110
"github.com/google/go-github/v63/github"
1211
"github.com/stretchr/testify/require"
12+
13+
"github.com/conductorone/baton-github/test"
14+
"github.com/conductorone/baton-github/test/mocks"
1315
)
1416

1517
func TestTeam(t *testing.T) {
@@ -28,7 +30,10 @@ func TestTeam(t *testing.T) {
2830
team, _ := teamResource(githubTeam, organization.Id)
2931
user, _ := userResource(ctx, githubUser, *githubUser.Email, nil)
3032

31-
entitlement := v2.Entitlement{Resource: team}
33+
entitlement := v2.Entitlement{
34+
Id: entitlement2.NewEntitlementID(team, "member"),
35+
Resource: team,
36+
}
3237

3338
grantAnnotations, err := client.Grant(ctx, user, &entitlement)
3439
require.Nil(t, err)

0 commit comments

Comments
 (0)