forked from opengovern/og-describer-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
4,740 additions
and
3,267 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.