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

fix github adapter tool method #22

Merged
merged 1 commit into from
Feb 5, 2025
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
11 changes: 11 additions & 0 deletions pkg/source/github/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 17 additions & 9 deletions pkg/source/github/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down