Skip to content

Commit 4ab22f5

Browse files
committed
fix: fix repository collaborators table
1 parent 8aa6038 commit 4ab22f5

File tree

5 files changed

+70
-34
lines changed

5 files changed

+70
-34
lines changed

cloudql/github/table_github_repository_collaborator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ import (
1212

1313
func gitHubRepositoryCollaboratorColumns() []*plugin.Column {
1414
return []*plugin.Column{
15+
{Name: "repository_name", Type: proto.ColumnType_STRING, Description: "The full name of the repository, including the owner and repo name.",
16+
Transform: transform.FromField("Description.RepositoryName")},
1517
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Description: "The full name of the repository, including the owner and repo name.",
1618
Transform: transform.FromField("Description.RepoFullName")},
17-
{Name: "affiliation", Type: proto.ColumnType_STRING, Description: "Affiliation filter - valid values 'ALL' (default), 'OUTSIDE', 'DIRECT'.",
18-
Transform: transform.FromField("Description.Affiliation")},
19+
{Name: "collaborator_id", Type: proto.ColumnType_STRING, Description: "The full name of the repository, including the owner and repo name.",
20+
Transform: transform.FromField("Description.CollaboratorID")},
21+
{Name: "collaborator_type", Type: proto.ColumnType_STRING, Description: "The full name of the repository, including the owner and repo name.",
22+
Transform: transform.FromField("Description.CollaboratorType")},
1923
{Name: "permission", Type: proto.ColumnType_STRING, Description: "The permission the collaborator has on the repository.",
2024
Transform: transform.FromField("Description.Permission")},
21-
{Name: "user_login", Type: proto.ColumnType_STRING, Description: "The login of the collaborator",
22-
Transform: transform.FromField("Description.UserLogin")},
2325
{
24-
Name: "repository_name",
26+
Name: "organization_id",
2527
Type: proto.ColumnType_STRING,
26-
Transform: transform.FromField("Description.RepositoryName"),
28+
Transform: transform.FromField("Description.OrganizationID"),
2729
Description: "repository name",
2830
},
2931
}

discovery/describers/organization.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func GetOrganizationList(ctx context.Context, githubClient model.GitHubClient, o
3535
if err != nil {
3636
return nil, err
3737
}
38-
additionalOrgInfo, err := GetOrganizationAdditionalData(ctx, githubClient.RestClient, org)
38+
additionalOrgInfo, err := GetOrganizationAdditionalData(ctx, githubClient.RestClient, org.Login)
3939
if err != nil {
4040
return nil, err
4141
}
@@ -151,9 +151,8 @@ func GetOrganizationHooks(ctx context.Context, client *github.Client, org steamp
151151
return orgHooks, nil
152152
}
153153

154-
func GetOrganizationAdditionalData(ctx context.Context, client *github.Client, org steampipemodels.OrganizationWithCounts) (*github.Organization, error) {
155-
login := org.Login
156-
organization, _, err := client.Organizations.Get(ctx, login)
154+
func GetOrganizationAdditionalData(ctx context.Context, client *github.Client, orgName string) (*github.Organization, error) {
155+
organization, _, err := client.Organizations.Get(ctx, orgName)
157156
if err != nil {
158157
return nil, err
159158
}

discovery/describers/repository_collaborators.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ func GetAllRepositoriesCollaborators(ctx context.Context, githubClient model.Git
1717
repositoryName = value.(string)
1818
}
1919

20+
teamsRepositories, err := GetAllTeamsRepositories(ctx, githubClient, organizationName, stream)
21+
if err != nil {
22+
return nil, err
23+
}
24+
2025
if repositoryName != "" {
21-
repoValues, err := GetRepositoryCollaborators(ctx, githubClient, stream, organizationName, repositoryName)
26+
org, err := GetOrganizationAdditionalData(ctx, githubClient.RestClient, organizationName)
27+
var orgId int64
28+
if org != nil && org.ID != nil {
29+
orgId = *org.ID
30+
}
31+
repoValues, err := GetRepositoryCollaborators(ctx, githubClient, stream, organizationName, orgId, repositoryName)
2232
if err != nil {
2333
return nil, err
2434
}
35+
for _, t := range teamsRepositories {
36+
if t.Description.(model.RepoCollaboratorsDescription).RepositoryName == repositoryName {
37+
repoValues = append(repoValues, t)
38+
}
39+
}
2540
return repoValues, nil
2641
}
2742
repositories, err := getRepositories(ctx, client, organizationName)
@@ -30,16 +45,22 @@ func GetAllRepositoriesCollaborators(ctx context.Context, githubClient model.Git
3045
}
3146
var values []models.Resource
3247
for _, repo := range repositories {
33-
repoValues, err := GetRepositoryCollaborators(ctx, githubClient, stream, organizationName, repo.GetName())
48+
org, err := GetOrganizationAdditionalData(ctx, githubClient.RestClient, organizationName)
49+
var orgId int64
50+
if org != nil && org.ID != nil {
51+
orgId = *org.ID
52+
}
53+
repoValues, err := GetRepositoryCollaborators(ctx, githubClient, stream, organizationName, orgId, repo.GetName())
3454
if err != nil {
3555
return nil, err
3656
}
3757
values = append(values, repoValues...)
3858
}
59+
values = append(values, teamsRepositories...)
3960
return values, nil
4061
}
4162

42-
func GetRepositoryCollaborators(ctx context.Context, githubClient model.GitHubClient, stream *models.StreamSender, owner, repo string) ([]models.Resource, error) {
63+
func GetRepositoryCollaborators(ctx context.Context, githubClient model.GitHubClient, stream *models.StreamSender, owner string, orgId int64, repo string) ([]models.Resource, error) {
4364
client := githubClient.GraphQLClient
4465
affiliation := githubv4.CollaboratorAffiliationAll
4566
var query struct {
@@ -76,12 +97,13 @@ func GetRepositoryCollaborators(ctx context.Context, githubClient model.GitHubCl
7697
ID: id,
7798
Name: collaborator.Node.Name,
7899
Description: model.RepoCollaboratorsDescription{
79-
Affiliation: "ALL",
80-
RepoFullName: repoFullName,
81-
Permission: collaborator.Permission,
82-
UserLogin: collaborator.Node.Login,
83-
Organization: owner,
84-
RepositoryName: repo,
100+
RepositoryName: repo,
101+
RepoFullName: repoFullName,
102+
CollaboratorID: collaborator.Node.Login,
103+
CollaboratorType: "User",
104+
Permission: collaborator.Permission,
105+
Organization: owner,
106+
OrganizationID: orgId,
85107
},
86108
}
87109
if stream != nil {

discovery/describers/team_repository.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package describers
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/opengovern/og-describer-github/discovery/pkg/models"
67
model "github.com/opengovern/og-describer-github/discovery/provider"
78
"github.com/shurcooL/githubv4"
@@ -18,7 +19,12 @@ func GetAllTeamsRepositories(ctx context.Context, githubClient model.GitHubClien
1819
}
1920
var values []models.Resource
2021
for _, team := range teams {
21-
teamValues, err := GetTeamRepositories(ctx, githubClient, organizationName, stream, team.GetOrganization().GetLogin(), team.GetSlug(), team.GetID())
22+
org, err := GetOrganizationAdditionalData(ctx, githubClient.RestClient, organizationName)
23+
var orgId int64
24+
if org != nil && org.ID != nil {
25+
orgId = *org.ID
26+
}
27+
teamValues, err := GetTeamRepositories(ctx, githubClient, organizationName, stream, team.GetOrganization().GetLogin(), orgId, team.GetSlug(), team.GetID())
2228
if err != nil {
2329
return nil, err
2430
}
@@ -27,11 +33,13 @@ func GetAllTeamsRepositories(ctx context.Context, githubClient model.GitHubClien
2733
return values, nil
2834
}
2935

30-
func GetTeamRepositories(ctx context.Context, githubClient model.GitHubClient, organizationName string, stream *models.StreamSender, org, slug string, teamID int64) ([]models.Resource, error) {
36+
func GetTeamRepositories(ctx context.Context, githubClient model.GitHubClient, organizationName string, stream *models.StreamSender, org string, orgId int64, slug string, teamID int64) ([]models.Resource, error) {
3137
client := githubClient.GraphQLClient
3238
var query struct {
3339
RateLimit steampipemodels.RateLimit
3440
Organization struct {
41+
ID string `graphql:"id"`
42+
Name string `graphql:"name"`
3543
Team struct {
3644
Repositories struct {
3745
TotalCount int
@@ -58,15 +66,19 @@ func GetTeamRepositories(ctx context.Context, githubClient model.GitHubClient, o
5866
return nil, err
5967
}
6068
for _, repo := range query.Organization.Team.Repositories.Edges {
69+
id := fmt.Sprintf("%s/%d/%s", repo.Node.NameWithOwner, teamID, string(repo.Permission))
70+
6171
value := models.Resource{
62-
ID: strconv.Itoa(repo.Node.Id),
72+
ID: id,
6373
Name: repo.Node.Name,
64-
Description: model.TeamRepositoryDescription{
65-
TeamID: int(teamID),
66-
RepositoryFullName: repo.Node.NameWithOwner,
67-
Permission: string(repo.Permission),
68-
CreatedAt: repo.Node.CreatedAt.Time,
69-
UpdatedAt: repo.Node.UpdatedAt.Time,
74+
Description: model.RepoCollaboratorsDescription{
75+
RepositoryName: repo.Node.Name,
76+
RepoFullName: repo.Node.NameWithOwner,
77+
CollaboratorID: strconv.FormatInt(teamID, 10),
78+
CollaboratorType: "Team",
79+
Permission: repo.Permission,
80+
Organization: query.Organization.Name,
81+
OrganizationID: orgId,
7082
},
7183
}
7284
if stream != nil {

discovery/provider/model.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,12 +1279,13 @@ type ReleaseDescription struct {
12791279
}
12801280

12811281
type RepoCollaboratorsDescription struct {
1282-
Affiliation string
1283-
RepoFullName string
1284-
Permission githubv4.RepositoryPermission
1285-
UserLogin string
1286-
Organization string
1287-
RepositoryName string
1282+
RepositoryName string
1283+
RepoFullName string
1284+
CollaboratorID string
1285+
CollaboratorType string
1286+
Permission githubv4.RepositoryPermission
1287+
Organization string
1288+
OrganizationID int64
12881289
}
12891290

12901291
type RepoAlertDependabotDescription struct {

0 commit comments

Comments
 (0)