Skip to content

Commit

Permalink
Add more logging statements to github_code_owner table and fix return…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
cbruno10 committed Oct 3, 2022
1 parent 09afbe3 commit 150a7e5
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions github/table_github_code_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package github

import (
"context"
"fmt"
"strings"

"github.com/google/go-github/v45/github"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin/transform"
"strings"

"github.com/turbot/steampipe-plugin-sdk/v4/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin"
Expand Down Expand Up @@ -66,42 +66,47 @@ func tableGitHubCodeOwnerList(ctx context.Context, d *plugin.QueryData, h *plugi
PreComments []string
LineComment string
}

getCodeOwners := func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var fileContent *github.RepositoryContent
var err error

client := connect(ctx, d)
opt := &github.RepositoryContentGetOptions{}
// stop on the first found CODEOWNERS file.
// NOTE : a repository can have multiple CODEOWNERS files, even if it's invalid
// In that case, GitHub uses precedence over these files in the following order : .github/CODEOWNERS, CODEOWNERS, docs/CODEOWNERS
// If a repository has multiple CODEOWNERS files, GitHub uses the following
// precedence: .github/CODEOWNERS, CODEOWNERS, docs/CODEOWNERS
var paths = []string{".github/CODEOWNERS", "CODEOWNERS", "docs/CODEOWNERS"}
for _, path := range paths {
fileContent, _, _, err = client.Repositories.GetContents(ctx, owner, repoName, 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 your repository)
// 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 {
return nil, fmt.Errorf("Downloading file \"%s\" : %s", path, err.(*github.ErrorResponse).Response.Status)
plugin.Logger(ctx).Error("github_code_owner.tableGitHubCodeOwnerList", "api_error", err, "path", path)
return nil, err
}
}

// no CODEOWNERS file
if err != nil {
return []CodeOwnerRuleResponse{}, err
// No CODEOWNERS file
if fileContent == nil {
return []*CodeOwnerRule{}, nil
}

decodedContent, err := fileContent.GetContent()
if err != nil {
return []CodeOwnerRuleResponse{}, err
plugin.Logger(ctx).Error("github_code_owner.tableGitHubCodeOwnerList", "decode_error", err)
return []*CodeOwnerRule{}, err
}

return decodeCodeOwnerFileContent(decodedContent), err
}

codeOwnersElements, err := retryHydrate(ctx, d, h, getCodeOwners)
if err != nil {
plugin.Logger(ctx).Error("github_code_owner.tableGitHubCodeOwnerList", "retry_hydrate_error", err)
return nil, err
}

Expand Down

0 comments on commit 150a7e5

Please sign in to comment.