Skip to content

Commit

Permalink
Merge pull request #34 from interlynk-io/fix/fixes_minor
Browse files Browse the repository at this point in the history
add filename and discard unnecessary cloning o/p
  • Loading branch information
riteshnoronha authored Feb 10, 2025
2 parents 6d67883 + b7a481c commit 158d63a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/engine/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TransferRun(ctx context.Context, cmd *cobra.Command, config mvtypes.Config)
func dryMode(ctx context.Context, iterator iterator.SBOMIterator, outputDir string) error {
logger.LogDebug(ctx, "Dry-run mode enabled. Preparing to display SBOM details.")

processor := sbom.NewSBOMProcessor(outputDir, false) // No need for output directory in dry-run mode
processor := sbom.NewSBOMProcessor(outputDir, true) // No need for output directory in dry-run mode
sbomCount := 0

for {
Expand All @@ -111,7 +111,7 @@ func dryMode(ctx context.Context, iterator iterator.SBOMIterator, outputDir stri

logger.LogDebug(ctx, "Processing SBOM from memory", "repo", sbom.Repo, "version", sbom.Version)

doc, err := processor.ProcessSBOMs(sbom.Data, sbom.Repo)
doc, err := processor.ProcessSBOMs(sbom.Data, sbom.Repo, sbom.Path)
if err != nil {
logger.LogError(ctx, err, "Failed to process SBOM")
continue
Expand All @@ -125,7 +125,7 @@ func dryMode(ctx context.Context, iterator iterator.SBOMIterator, outputDir stri
}

sbomCount++
logger.LogDebug(ctx, fmt.Sprintf("%d. Repo: %s | Format: %s | SpecVersion: %s", sbomCount, sbom.Repo, doc.Format, doc.SpecVersion))
fmt.Printf("%d. Repo: %s | Format: %s | SpecVersion: %s | Filename: %s \n", sbomCount, sbom.Repo, doc.Format, doc.SpecVersion, doc.Filename)
}

logger.LogDebug(ctx, "Dry-run mode completed", "total_sboms_processed", sbomCount)
Expand Down
8 changes: 6 additions & 2 deletions pkg/sbom/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ func NewSBOMProcessor(outputDir string, verbose bool) *SBOMProcessor {
}

// ProcessSBOMFromBytes processes an SBOM directly from memory
func (p *SBOMProcessor) ProcessSBOMs(content []byte, repoName string) (SBOMDocument, error) {
func (p *SBOMProcessor) ProcessSBOMs(content []byte, repoName, filePath string) (SBOMDocument, error) {
if len(content) == 0 {
return SBOMDocument{}, errors.New("empty SBOM content")
}
if filePath == "" {
filePath = "N/A"
}

doc := SBOMDocument{
Filename: fmt.Sprintf("%s.sbom.json", repoName), // Use repo name as filename
// Filename: fmt.Sprintf("%s.sbom.json", repoName), // Use repo name as filename
Filename: filePath,
Content: content,
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/source/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ type SBOMAsset struct {

// VersionedSBOMs maps versions to their respective SBOMs in that version
// type VersionedSBOMs map[string][]string
type VersionedSBOMs map[string][][]byte
type VersionedSBOMs map[string][]SBOMData

type SBOMData struct {
Content []byte
Filename string
}

// Client interacts with the GitHub API
type Client struct {
Expand Down Expand Up @@ -317,9 +322,13 @@ func (c *Client) downloadSBOMs(ctx *tcontext.TransferMetadata, sboms []SBOMAsset
return
}

// Store SBOM content in memory
versionedSBOM := SBOMData{
Content: sbomData,
Filename: sbom.Name,
}

mu.Lock()
versionedSBOMs[sbom.Release] = append(versionedSBOMs[sbom.Release], sbomData)
versionedSBOMs[sbom.Release] = append(versionedSBOMs[sbom.Release], versionedSBOM)
mu.Unlock()

logger.LogDebug(ctx.Context, "SBOM fetched and stored in memory", "name", sbom.Name)
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 @@ -112,8 +112,8 @@ func (it *GitHubIterator) fetchSBOMFromReleases(ctx *tcontext.TransferMetadata)
for version, sbomDataList := range sbomFiles {
for _, sbomData := range sbomDataList { // sbomPath is a string (file path)
it.sboms = append(it.sboms, &iterator.SBOM{
Path: "", // No file path, storing in memory
Data: sbomData,
Path: sbomData.Filename,
Data: sbomData.Content,
Repo: fmt.Sprintf("%s/%s", it.client.Owner, it.client.Repo),
Version: version,
})
Expand Down
11 changes: 7 additions & 4 deletions pkg/source/github/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package github

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -81,8 +83,7 @@ func CloneRepoWithGit(ctx *tcontext.TransferMetadata, repoURL, branch, targetDir
if _, err := exec.LookPath("git"); err != nil {
return fmt.Errorf("git is not installed, install Git or use --method=api")
}

fmt.Println("🚀 Cloning repository using Git:", repoURL)
logger.LogDebug(ctx.Context, "🚀 Cloning repository using Git", "repo", repoURL)

// Run `git clone --depth=1` for faster shallow cloning
var cmd *exec.Cmd
Expand All @@ -91,14 +92,16 @@ func CloneRepoWithGit(ctx *tcontext.TransferMetadata, repoURL, branch, targetDir
// 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
var stderr bytes.Buffer
cmd.Stdout = io.Discard // Suppress standard output
cmd.Stderr = &stderr

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

0 comments on commit 158d63a

Please sign in to comment.