Skip to content

Commit

Permalink
Merge pull request #33 from interlynk-io/feat/support_branch
Browse files Browse the repository at this point in the history
support generating sbom for particular branch using tool method
  • Loading branch information
viveksahu26 authored Feb 10, 2025
2 parents 882d023 + e86c9b8 commit 6d67883
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/iterator/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type SBOM struct {
Data []byte // SBOM data stored in memory (nil if using Path)
Repo string // Repository URL (helps track multi-repo processing)
Version string // Version of the SBOM (e.g., "latest" or "v1.2.3")
Branch string // github repo main, master, or any specific branch
}

// SBOMIterator provides a way to lazily fetch SBOMs one by one
Expand Down
10 changes: 8 additions & 2 deletions pkg/source/github/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
func (g *GitHubAdapter) AddCommandParams(cmd *cobra.Command) {
cmd.Flags().String("in-github-url", "", "GitHub repository URL")
cmd.Flags().String("in-github-method", "api", "GitHub method: release, api, or tool")
cmd.Flags().String("in-github-branch", "", "Github repository branch")

// Updated to StringSlice to support multiple values (comma-separated)
cmd.Flags().StringSlice("in-github-include-repos", nil, "Include only these repositories e.g sbomqs,sbomasm")
Expand All @@ -83,13 +84,14 @@ func (g *GitHubAdapter) AddCommandParams(cmd *cobra.Command) {

// ParseAndValidateParams validates the GitHub adapter params
func (g *GitHubAdapter) ParseAndValidateParams(cmd *cobra.Command) error {
var urlFlag, methodFlag, includeFlag, excludeFlag string
var urlFlag, methodFlag, includeFlag, excludeFlag, githubBranchFlag string

if g.Role == types.InputAdapter {
urlFlag = "in-github-url"
methodFlag = "in-github-method"
includeFlag = "in-github-include-repos"
excludeFlag = "in-github-exclude-repos"
githubBranchFlag = "in-github-branch"
}

// Extract GitHub URL
Expand All @@ -103,6 +105,8 @@ func (g *GitHubAdapter) ParseAndValidateParams(cmd *cobra.Command) error {
return fmt.Errorf("missing or invalid flag: %s", methodFlag)
}

branch, _ := cmd.Flags().GetString(githubBranchFlag)

includeRepos, _ := cmd.Flags().GetStringSlice(includeFlag)
excludeRepos, _ := cmd.Flags().GetStringSlice(excludeFlag)

Expand Down Expand Up @@ -139,7 +143,7 @@ func (g *GitHubAdapter) ParseAndValidateParams(cmd *cobra.Command) error {
return fmt.Errorf("version flag is not supported for GitHub API method")
}

//Assign extracted values to struct
// Assign extracted values to struct
if version == "" {
version = "latest"
g.URL = githubURL
Expand All @@ -149,6 +153,7 @@ func (g *GitHubAdapter) ParseAndValidateParams(cmd *cobra.Command) error {

g.Owner = owner
g.Repo = repo
g.Branch = branch
g.Version = version
g.Method = method
g.GithubToken = token
Expand All @@ -160,6 +165,7 @@ func (g *GitHubAdapter) ParseAndValidateParams(cmd *cobra.Command) error {
logger.LogDebug(cmd.Context(), "Parsed GitHub parameters",
"url", g.URL,
"owner", g.Owner,
"branch", g.Branch,
"repo", g.Repo,
"version", g.Version,
"include_repos", g.IncludeRepos,
Expand Down
2 changes: 2 additions & 0 deletions pkg/source/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Client struct {
Repo string
Version string
Method string
Branch string
token string
}

Expand All @@ -85,6 +86,7 @@ func NewClient(g *GitHubAdapter) *Client {
Method: g.Method,
Owner: g.Owner,
Repo: g.Repo,
Branch: g.Branch,
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/source/github/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (it *GitHubIterator) fetchSBOMFromTool(ctx *tcontext.TransferMetadata) erro
repoDir := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s", it.client.Repo, it.client.Version))
defer os.RemoveAll(repoDir)

if err := CloneRepoWithGit(ctx, it.client.RepoURL, repoDir); err != nil {
if err := CloneRepoWithGit(ctx, it.client.RepoURL, it.client.Branch, repoDir); err != nil {
return fmt.Errorf("failed to clone the repository: %w", err)
}

Expand All @@ -149,12 +149,12 @@ func (it *GitHubIterator) fetchSBOMFromTool(ctx *tcontext.TransferMetadata) erro
return fmt.Errorf("generate SBOM with zero file data: %w", err)
}

// store data
it.sboms = append(it.sboms, &iterator.SBOM{
Path: "",
Data: sbomBytes,
Repo: fmt.Sprintf("%s/%s", it.client.Owner, it.client.Repo),
Version: it.client.Version,
Branch: it.client.Branch,
})
logger.LogDebug(ctx.Context, "SBOM successfully stored in memory", "repository", it.client.RepoURL)
return nil
Expand Down
17 changes: 14 additions & 3 deletions pkg/source/github/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func GenerateSBOM(ctx *tcontext.TransferMetadata, repoDir, binaryPath string) (s
}

// CloneRepoWithGit clones a GitHub repository using the Git command-line tool.
func CloneRepoWithGit(ctx *tcontext.TransferMetadata, repoURL, targetDir string) error {
func CloneRepoWithGit(ctx *tcontext.TransferMetadata, repoURL, branch, targetDir string) error {
// Ensure Git is installed
if _, err := exec.LookPath("git"); err != nil {
return fmt.Errorf("git is not installed, install Git or use --method=api")
Expand All @@ -85,14 +85,25 @@ func CloneRepoWithGit(ctx *tcontext.TransferMetadata, repoURL, targetDir string)
fmt.Println("🚀 Cloning repository using Git:", repoURL)

// Run `git clone --depth=1` for faster shallow cloning
cmd := exec.CommandContext(ctx.Context, "git", "clone", "--depth=1", repoURL, targetDir)
var cmd *exec.Cmd

if branch == "" {
// clones the default branch
logger.LogDebug(ctx.Context, "Repository to be cloned for", "branch", "default")
cmd = exec.CommandContext(ctx.Context, "git", "clone", "--depth=1", repoURL, targetDir)
} else {
logger.LogDebug(ctx.Context, "Repository to be cloned for", "branch", branch)
// clones the specific branch
cmd = exec.CommandContext(ctx.Context, "git", "clone", "--depth=1", "--branch", branch, repoURL, targetDir)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return fmt.Errorf("git clone failed: %w", err)
}

fmt.Println("✅ Repository successfully cloned using Git.")
logger.LogDebug(ctx.Context, "Repository successfully cloned", "repo", repoURL, "branch", branch)
return nil
}
5 changes: 4 additions & 1 deletion pkg/utils/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"os/exec"
"path/filepath"
"strings"

"github.com/interlynk-io/sbommv/pkg/logger"
)

func GetBinaryPath() (string, error) {
Expand Down Expand Up @@ -87,7 +89,8 @@ func CloneRepoWithGit(ctx context.Context, repoURL, targetDir string) error {
return fmt.Errorf("git clone failed: %w", err)
}

fmt.Println("✅ Repository successfully cloned using Git.")
logger.LogDebug(ctx, "Repository successfully cloned using git", "repo", repoURL)

return nil
}

Expand Down

0 comments on commit 6d67883

Please sign in to comment.