Skip to content

Commit

Permalink
diff ingestor: split ingesting a single file into a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrozek committed Sep 22, 2023
1 parent 8455abb commit 3d17288
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions internal/engine/ingester/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,11 @@ func (di *Diff) Ingest(
}

for _, file := range prFiles {
eco := di.getEcosystemForFile(*file.Filename)
if eco == DepEcosystemNone {
logger.Debug().
Str("filename", *file.Filename).
Msg("No ecosystem found, skipping")
continue
}

logger.Debug().
Str("filename", *file.Filename).
Str("package-ecosystem", string(eco)).
Msg("No ecosystem found, skipping")

parser := newEcosystemParser(eco)
if parser == nil {
return nil, fmt.Errorf("no parser found for ecosystem %s", eco)
}

depBatch, err := parser(*file.Patch)
fileDiffs, err := di.ingestFile(file.GetFilename(), file.GetPatch(), file.GetRawURL(), logger)
if err != nil {
return nil, fmt.Errorf("error parsing file %s: %w", *file.Filename, err)
}

for i := range depBatch {
dep := depBatch[i]
allDiffs = append(allDiffs, &pb.PrDependencies_ContextualDependency{
Dep: dep,
File: &pb.PrDependencies_ContextualDependency_FilePatch{
Name: file.GetFilename(),
PatchUrl: file.GetRawURL(),
},
})
return nil, fmt.Errorf("error ingesting file %s: %w", file.GetFilename(), err)
}
allDiffs = append(allDiffs, fileDiffs...)
}

if resp.NextPage == 0 {
Expand All @@ -133,6 +105,35 @@ func (di *Diff) Ingest(
}, nil
}

func (di *Diff) ingestFile(
filename, patchContents, patchUrl string,
logger zerolog.Logger,
) ([]*pb.PrDependencies_ContextualDependency, error) {
parser := di.getParserForFile(filename, logger)
if parser == nil {
return nil, nil
}

depBatch, err := parser(patchContents)
if err != nil {
return nil, fmt.Errorf("error parsing file %s: %w", filename, err)
}

batchCtxDeps := make([]*pb.PrDependencies_ContextualDependency, 0, len(depBatch))
for i := range depBatch {
dep := depBatch[i]
batchCtxDeps = append(batchCtxDeps, &pb.PrDependencies_ContextualDependency{
Dep: dep,
File: &pb.PrDependencies_ContextualDependency_FilePatch{
Name: filename,
PatchUrl: patchUrl,
},
})
}

return batchCtxDeps, nil
}

func (di *Diff) getEcosystemForFile(filename string) DependencyEcosystem {
lastComponent := filepath.Base(filename)

Expand All @@ -143,3 +144,20 @@ func (di *Diff) getEcosystemForFile(filename string) DependencyEcosystem {
}
return DepEcosystemNone
}

func (di *Diff) getParserForFile(filename string, logger zerolog.Logger) ecosystemParser {
eco := di.getEcosystemForFile(filename)
if eco == DepEcosystemNone {
logger.Debug().
Str("filename", filename).
Msg("No ecosystem found, skipping")
return nil
}

logger.Debug().
Str("filename", filename).
Str("package-ecosystem", string(eco)).
Msg("matched ecosystem")

return newEcosystemParser(eco)
}

0 comments on commit 3d17288

Please sign in to comment.