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

⭐️ expose github default branch to make the access easier #4490

Merged
merged 1 commit into from
Aug 6, 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
2 changes: 2 additions & 0 deletions providers/github/resources/github.lr
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ private github.repository @defaults("fullName") {
branches() []github.branch
// Default branch name for the repository
defaultBranchName string
// Default branch
defaultBranch() github.branch
// List of commits for the repository
commits() []github.commit
// List of contributors for the repository
Expand Down
24 changes: 24 additions & 0 deletions providers/github/resources/github.lr.go

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

12 changes: 7 additions & 5 deletions providers/github/resources/github.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ resources:
updatedAt: {}
url: {}
is_private: true
min_mondoo_version: latest
min_mondoo_version: 9.0.0
github.license:
fields:
key: {}
name: {}
spdxId: {}
url: {}
is_private: true
min_mondoo_version: latest
min_mondoo_version: 9.0.0
github.mergeRequest:
fields:
assignees: {}
Expand Down Expand Up @@ -309,9 +309,9 @@ resources:
github.repository:
fields:
MergeRequests:
min_mondoo_version: latest
min_mondoo_version: 9.0.0
allMergeRequests:
min_mondoo_version: latest
min_mondoo_version: 9.0.0
allowAutoMerge:
min_mondoo_version: 6.4.0
allowForking:
Expand All @@ -330,14 +330,16 @@ resources:
closedIssues:
min_mondoo_version: 7.14.0
closedMergeRequests:
min_mondoo_version: latest
min_mondoo_version: 9.0.0
collaborators:
min_mondoo_version: 6.11.0
commits:
min_mondoo_version: 6.4.0
contributors:
min_mondoo_version: 6.4.0
createdAt: {}
defaultBranch:
min_mondoo_version: 9.0.0
defaultBranchName:
min_mondoo_version: 6.4.0
description: {}
Expand Down
56 changes: 48 additions & 8 deletions providers/github/resources/github_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,7 @@ func (g *mqlGithubRepository) branches() ([]interface{}, error) {
defaultBranch = true
}

mqlBranch, err := CreateResource(g.MqlRuntime, "github.branch", map[string]*llx.RawData{
"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),
})
mqlBranch, err := newMqlBranch(g.MqlRuntime, branch, owner, repoName, defaultBranch)
if err != nil {
return nil, err
}
Expand All @@ -439,6 +432,53 @@ func (g *mqlGithubRepository) branches() ([]interface{}, error) {
return res, nil
}

func newMqlBranch(runtime *plugin.Runtime, branch *github.Branch, owner *mqlGithubUser, repoName string, defaultBranch bool) (*mqlGithubBranch, error) {
mqlBranch, err := CreateResource(runtime, "github.branch", map[string]*llx.RawData{
"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
}
return mqlBranch.(*mqlGithubBranch), nil
}

func (g *mqlGithubRepository) defaultBranch() (*mqlGithubBranch, error) {
conn := g.MqlRuntime.Connection.(*connection.GithubConnection)
if g.Name.Error != nil {
return nil, g.Name.Error
}
repoName := g.Name.Data
if g.Owner.Error != nil {
return nil, g.Owner.Error
}
owner := g.Owner.Data

if owner.Login.Error != nil {
return nil, owner.Login.Error
}
ownerLogin := owner.Login.Data

if g.DefaultBranchName.Error != nil {
return nil, g.DefaultBranchName.Error
}
repoDefaultBranchName := g.DefaultBranchName.Data

branch, _, err := conn.Client().Repositories.GetBranch(conn.Context(), ownerLogin, repoName, repoDefaultBranchName, 3)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, nil
}
return nil, err
}

return newMqlBranch(g.MqlRuntime, branch, owner, repoName, true)
}

type githubDismissalRestrictions struct {
Users []string `json:"users"`
Teams []string `json:"teams"`
Expand Down
Loading