Skip to content

Commit

Permalink
fix: add new tables
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Nov 19, 2024
1 parent b02f0a8 commit 4a1fbd1
Show file tree
Hide file tree
Showing 39 changed files with 4,740 additions and 3,267 deletions.
2,891 changes: 2,612 additions & 279 deletions pkg/sdk/es/resources_clients.go

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions provider/describer/code_owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package describer

import (
"fmt"
"github.com/google/go-github/v55/github"
"github.com/opengovern/og-describer-github/pkg/sdk/models"
"github.com/opengovern/og-describer-github/provider/model"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"golang.org/x/net/context"
"strings"
)

func ListCodeOwners(ctx context.Context, githubClient GitHubClient, organizationName string, stream *models.StreamSender) ([]models.Resource, error) {
var fileContent *github.RepositoryContent
var err error

opt := &github.RepositoryContentGetOptions{}

repositories, err := getRepositories(ctx, githubClient.RestClient, organizationName)
if err != nil {
return nil, nil
}

var values []models.Resource
for _, repo := range repositories {
var paths = []string{".github/CODEOWNERS", "CODEOWNERS", "docs/CODEOWNERS"}
for _, path := range paths {
fileContent, _, _, err = githubClient.RestClient.Repositories.GetContents(ctx, organizationName, repo.GetName(), path, opt)
// Stop on the first CODEOWNERS file found
if err == nil {
break
}
// HTTP 404 is the only tolerated HTTP error code, if it's different, it
// means something is wrong with your rights or repository
if err.(*github.ErrorResponse).Response.StatusCode != 404 {
plugin.Logger(ctx).Error("github_code_owner.tableGitHubCodeOwnerList", "api_error", err, "path", path)
return nil, err
}
}

if fileContent != nil {
decodedContent, err := fileContent.GetContent()
if err != nil {
plugin.Logger(ctx).Error("github_code_owner.tableGitHubCodeOwnerList", "decode_error", err)
return nil, err
}
for _, c := range decodeCodeOwnerFileContent(repo.GetName(), decodedContent) {
values = append(values, c)
if stream != nil {
if err := (*stream)(c); err != nil {
return nil, err
}
} else {
values = append(values, c)
}
}
}

}
return values, nil
}

func decodeCodeOwnerFileContent(repoName, content string) []models.Resource {
var codeOwnerRules []models.Resource

var comments []string
for i, line := range strings.Split(content, "\n") {
lineNumber := i + 1
// if line is empty, consider the codeblock end
if len(line) == 0 {
comments = []string{}
continue
}
// code block with comments
if strings.HasPrefix(line, "#") {
comments = append(comments, line)
continue
}
// code owners rule
// if line is empty, consider the codeblock end
rule := strings.SplitN(line, " ", 2)
if len(rule) < 2 {
comments = []string{}
continue
}

var pattern, lineComment string
pattern = rule[0]

// line comment
ownersAndComment := strings.SplitN(rule[1], "#", 2)
if len(ownersAndComment) == 2 && len(ownersAndComment[1]) > 0 {
lineComment = ownersAndComment[1]
} else {
ownersAndComment = []string{rule[1]}
}

// owners computing
var users, teams []string
for _, owner := range strings.Split(strings.TrimSpace(ownersAndComment[0]), " ") {
if strings.Index(owner, "/") > 0 {
teams = append(teams, owner)
} else {
users = append(users, owner)
}
}
codeOwnerRules = append(codeOwnerRules, models.Resource{
ID: fmt.Sprintf("%s/%s", repoName, line),
Type: "Github/CodeOwner",
Description: model.CodeOwnerDescription{
RepositoryFullName: repoName,
LineNumber: int64(lineNumber),
Pattern: pattern,
Users: users,
Teams: teams,
PreComments: comments,
LineComment: lineComment,
},
})
}
return codeOwnerRules
}
1 change: 1 addition & 0 deletions provider/describer/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func GetIssueList(ctx context.Context, githubClient GitHubClient, organizationNa
Name: issue.Title,
Description: JSONAllFieldsMarshaller{
Value: model.IssueDescription{
RepositoryFullName: r.GetFullName(),
Id: issue.Id,
NodeId: issue.NodeId,
Number: issue.Number,
Expand Down
54 changes: 54 additions & 0 deletions provider/describer/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package describer

import (
"github.com/google/go-github/v55/github"
"github.com/opengovern/og-describer-github/pkg/sdk/models"
"github.com/opengovern/og-describer-github/provider/model"
"golang.org/x/net/context"
"strconv"
)

func GetReleaseList(ctx context.Context, githubClient GitHubClient, organizationName string, stream *models.StreamSender) ([]models.Resource, error) {
repositories, err := getRepositories(ctx, githubClient.RestClient, organizationName)
if err != nil {
return nil, err
}

var values []models.Resource
opts := &github.ListOptions{PerPage: 100}
for _, r := range repositories {
for {
releases, resp, err := githubClient.RestClient.Repositories.ListReleases(ctx, organizationName, r.GetName(), opts)
if err != nil {
return nil, err
}
for _, release := range releases {
if release == nil {
continue
}
value := models.Resource{
ID: strconv.FormatInt(release.GetID(), 10),
Name: release.GetName(),
Description: model.ReleaseDescription{
RepositoryRelease: *release,
RepositoryFullName: r.GetFullName(),
},
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
} else {
values = append(values, value)
}
}

if resp.NextPage == 0 {
break
}

opts.Page = resp.NextPage
}
}
return values, nil
}
16 changes: 16 additions & 0 deletions provider/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type GitIgnoreDescription struct {
}

type IssueDescription struct {
RepositoryFullName string
Id int
NodeId string
Number int
Expand Down Expand Up @@ -451,6 +452,11 @@ type RepositoryDescription struct {
NetworkCount int
}

type ReleaseDescription struct {
github.RepositoryRelease
RepositoryFullName string
}

type RepoCollaboratorsDescription struct {
Affiliation string
RepoFullName string
Expand Down Expand Up @@ -683,3 +689,13 @@ type WorkflowDescription struct {
WorkFlowFileContentJson *github.RepositoryContent
Pipeline *goPipeline.Pipeline
}

type CodeOwnerDescription struct {
RepositoryFullName string
LineNumber int64
Pattern string
Users []string
Teams []string
PreComments []string
LineComment string
}
84 changes: 84 additions & 0 deletions provider/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ var ResourceTypes = map[string]model.ResourceType{
GetDescriber: nil,
},

"Github/Issue": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Issue",
Tags: map[string][]string{
"category": {"Issue"},
},
Labels: map[string]string{
},
Annotations: map[string]string{
},
ListDescriber: DescribeByGithub(describer.GetIssueList),
GetDescriber: nil,
},

"Github/Issue/Comment": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Issue/Comment",
Expand Down Expand Up @@ -174,6 +188,20 @@ var ResourceTypes = map[string]model.ResourceType{
GetDescriber: nil,
},

"Github/Organization": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Organization",
Tags: map[string][]string{
"category": {"Organization"},
},
Labels: map[string]string{
},
Annotations: map[string]string{
},
ListDescriber: DescribeByGithub(describer.GetOrganizationList),
GetDescriber: nil,
},

"Github/Organization/Collaborator": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Organization/Collaborator",
Expand Down Expand Up @@ -244,6 +272,34 @@ var ResourceTypes = map[string]model.ResourceType{
GetDescriber: nil,
},

"Github/Release": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Release",
Tags: map[string][]string{
"category": {"Release"},
},
Labels: map[string]string{
},
Annotations: map[string]string{
},
ListDescriber: DescribeByGithub(describer.GetReleaseList),
GetDescriber: nil,
},

"Github/Repository": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Repository",
Tags: map[string][]string{
"category": {"Repository"},
},
Labels: map[string]string{
},
Annotations: map[string]string{
},
ListDescriber: DescribeByGithub(describer.GetRepositoryList),
GetDescriber: nil,
},

"Github/Repository/Collaborator": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Repository/Collaborator",
Expand Down Expand Up @@ -370,6 +426,20 @@ var ResourceTypes = map[string]model.ResourceType{
GetDescriber: nil,
},

"Github/Team": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Team",
Tags: map[string][]string{
"category": {"Team"},
},
Labels: map[string]string{
},
Annotations: map[string]string{
},
ListDescriber: DescribeByGithub(describer.GetTeamList),
GetDescriber: nil,
},

"Github/Team/Member": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/Team/Member",
Expand Down Expand Up @@ -467,4 +537,18 @@ var ResourceTypes = map[string]model.ResourceType{
ListDescriber: DescribeByGithub(describer.GetAllWorkflows),
GetDescriber: nil,
},

"Github/CodeOwner": {
IntegrationType: configs.IntegrationName,
ResourceName: "Github/CodeOwner",
Tags: map[string][]string{
"category": {"code_owner"},
},
Labels: map[string]string{
},
Annotations: map[string]string{
},
ListDescriber: DescribeByGithub(describer.ListCodeOwners),
GetDescriber: nil,
},
}
Loading

0 comments on commit 4a1fbd1

Please sign in to comment.