From 5ad80736e992124e3b834b42e65f91532343abeb Mon Sep 17 00:00:00 2001 From: Vivek Kumar Sahu Date: Thu, 6 Feb 2025 02:47:37 +0530 Subject: [PATCH] fix github adapter tool method Signed-off-by: Vivek Kumar Sahu --- pkg/source/github/adapter.go | 11 +++++++++++ pkg/source/github/iterator.go | 26 +++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/pkg/source/github/adapter.go b/pkg/source/github/adapter.go index 23032fb..7af06cf 100644 --- a/pkg/source/github/adapter.go +++ b/pkg/source/github/adapter.go @@ -20,6 +20,7 @@ import ( "net/http" "github.com/interlynk-io/sbommv/pkg/iterator" + "github.com/interlynk-io/sbommv/pkg/logger" "github.com/interlynk-io/sbommv/pkg/types" "github.com/interlynk-io/sbommv/pkg/utils" "github.com/spf13/cobra" @@ -78,6 +79,16 @@ func (g *GitHubAdapter) ParseAndValidateParams(cmd *cobra.Command) error { return fmt.Errorf("missing or invalid flag: in-github-method") } + if method == "tool" { + binaryPath, err := utils.GetBinaryPath() + if err != nil { + return fmt.Errorf("failed to get Syft binary: %w", err) + } + fmt.Println("Binary Path: ", binaryPath) + g.BinaryPath = binaryPath + logger.LogDebug(context.Background(), "Binary Path", "value", g.BinaryPath) + } + token := viper.GetString("GITHUB_TOKEN") repoURL, version, err := utils.ParseRepoVersion(url) diff --git a/pkg/source/github/iterator.go b/pkg/source/github/iterator.go index c1ba830..5ff22b4 100644 --- a/pkg/source/github/iterator.go +++ b/pkg/source/github/iterator.go @@ -28,10 +28,11 @@ import ( // GitHubIterator iterates over SBOMs fetched from GitHub (API, Release, Tool) type GitHubIterator struct { - ctx context.Context - client *Client - sboms []*iterator.SBOM // Stores all fetched SBOMs - position int // Tracks iteration position + ctx context.Context + client *Client + sboms []*iterator.SBOM // Stores all fetched SBOMs + position int // Tracks iteration position + binaryPath string } // NewGitHubIterator initializes the iterator based on the GitHub method @@ -40,9 +41,10 @@ func NewGitHubIterator(ctx context.Context, g *GitHubAdapter) (*GitHubIterator, client := NewClient(g.URL, g.Version, string(g.Method)) iterator := &GitHubIterator{ - ctx: ctx, - client: client, - sboms: []*iterator.SBOM{}, + ctx: ctx, + client: client, + sboms: []*iterator.SBOM{}, + binaryPath: g.BinaryPath, } // Fetch SBOMs based on method @@ -151,13 +153,19 @@ func (it *GitHubIterator) fetchSBOMFromTool() error { } // Generate SBOM - sbomFile, err := GenerateSBOM(it.ctx, repoDir, "/path/to/syft") + sbomFile, err := GenerateSBOM(it.ctx, repoDir, it.binaryPath) if err != nil { return fmt.Errorf("failed to generate SBOM: %w", err) } + // Ensure the "sboms" directory exists + sbomDir := "sboms" + if err := os.MkdirAll(sbomDir, 0o755); err != nil { + return fmt.Errorf("failed to create SBOM output directory: %w", err) + } + // Move SBOM to final location - sbomFilePath := fmt.Sprintf("sboms/github_tool_sbom_%s.json", sanitizeRepoName(it.client.RepoURL)) + sbomFilePath := fmt.Sprintf("%s/github_tool_sbom_%s.json", sbomDir, sanitizeRepoName(it.client.RepoURL)) if err := os.Rename(sbomFile, sbomFilePath); err != nil { return fmt.Errorf("failed to move SBOM file: %w", err) }