Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡GitHub: make head commit computed #4302

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion providers/github/resources/github.lr
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@ private github.branch @defaults("name") {
// Whether branch protection is enabled
isProtected bool
// Repository branch HEAD commit
headCommit github.commit
headCommit() github.commit
// Repository branch HEAD commit SHA sum
headCommitSha string
// Repository branch protection rules
protectionRules() github.branchprotection
// Repository branch repository name
Expand Down
26 changes: 25 additions & 1 deletion providers/github/resources/github.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions providers/github/resources/github.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ resources:
github.branch:
fields:
headCommit: {}
headCommitSha:
min_mondoo_version: 9.0.0
isDefault:
min_mondoo_version: 6.8.0
isProtected:
Expand Down
33 changes: 22 additions & 11 deletions providers/github/resources/github_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,19 @@ func (g *mqlGithubRepository) branches() ([]interface{}, error) {
res := []interface{}{}
for i := range allBranches {
branch := allBranches[i]
rc := branch.Commit
mqlCommit, err := newMqlGithubCommit(g.MqlRuntime, rc, ownerLogin, repoName)
if err != nil {
return nil, err
}

defaultBranch := false
if repoDefaultBranchName == *branch.Name {
defaultBranch = true
}

mqlBranch, err := CreateResource(g.MqlRuntime, "github.branch", map[string]*llx.RawData{
"name": llx.StringData(branch.GetName()),
"isProtected": llx.BoolData(branch.GetProtected()),
"headCommit": llx.AnyData(mqlCommit),
"repoName": llx.StringData(repoName),
"owner": llx.ResourceData(owner, owner.MqlName()),
"isDefault": llx.BoolData(defaultBranch),
"name": llx.StringData(branch.GetName()),
"isProtected": llx.BoolData(branch.GetProtected()),
"headCommitSha": llx.StringData(branch.GetCommit().GetSHA()),
"repoName": llx.StringData(repoName),
"owner": llx.ResourceData(owner, owner.MqlName()),
"isDefault": llx.BoolData(defaultBranch),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -577,6 +572,22 @@ func (g *mqlGithubBranch) protectionRules() (*mqlGithubBranchprotection, error)
return res.(*mqlGithubBranchprotection), nil
}

func (g *mqlGithubBranch) headCommit() (*mqlGithubCommit, error) {
ownerName := g.Owner.Data
if ownerName.Login.Error != nil {
return nil, ownerName.Login.Error
}
ownerLogin := ownerName.Login.Data

commit, err := newMqlGithubCommit(g.MqlRuntime, &github.RepositoryCommit{
SHA: &g.HeadCommitSha.Data,
}, ownerLogin, g.RepoName.Data)
if err != nil {
return nil, err
}
return commit.(*mqlGithubCommit), nil
}

func newMqlGithubCommit(runtime *plugin.Runtime, rc *github.RepositoryCommit, owner string, repo string) (interface{}, error) {
var githubAuthor interface{}
var err error
Expand Down
Loading