Skip to content

Commit

Permalink
refactor, Move the logic that checks if a repository only has package…
Browse files Browse the repository at this point in the history
…s into a helper, for reusability

Part of #263
  • Loading branch information
ruiAzevedo19 authored and Munsio committed Aug 28, 2024
1 parent d945b3b commit 1fe71a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
22 changes: 2 additions & 20 deletions evaluate/task/task-code-repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,9 @@ func (t *TaskCodeRepair) unpackCodeRepairPackage(ctx evaltask.Context, fileLogge
func validateCodeRepairRepository(logger *log.Logger, repositoryPath string, language language.Language) (err error) {
logger.Printf("validating repository %q", repositoryPath)

files, err := os.ReadDir(repositoryPath)
packagePaths, err := repositoryOnlyHasPackages(repositoryPath)
if err != nil {
return pkgerrors.WithStack(err)
}

var packagePaths []string
var otherFiles []string
for _, file := range files {
if file.Name() == "repository.json" {
continue
} else if file.Name() == ".git" || file.Name() == "target" { // Do not validate Git or Maven directories.
continue
} else if file.IsDir() {
packagePaths = append(packagePaths, filepath.Join(repositoryPath, file.Name()))
} else {
otherFiles = append(otherFiles, file.Name())
}
}

if len(otherFiles) > 0 {
return pkgerrors.Errorf("the code repair repository %q must contain only packages, but found %+v", repositoryPath, otherFiles)
return err
}

for _, packagePath := range packagePaths {
Expand Down
28 changes: 28 additions & 0 deletions evaluate/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -101,3 +102,30 @@ func packageSourceFile(log *log.Logger, packagePath string, language language.La

return "", pkgerrors.WithStack(pkgerrors.Errorf("could not find any %s source file in package %q", language.Name(), packagePath))
}

// repositoryOnlyHasPackages checks if a repository only has packages and returns all package paths.
func repositoryOnlyHasPackages(repositoryPath string) (packagePaths []string, err error) {
files, err := os.ReadDir(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

var otherFiles []string
for _, file := range files {
if file.Name() == "repository.json" {
continue
} else if file.Name() == ".git" || file.Name() == "target" { // Do not validate Git or Maven directories.
continue
} else if file.IsDir() {
packagePaths = append(packagePaths, filepath.Join(repositoryPath, file.Name()))
} else {
otherFiles = append(otherFiles, file.Name())
}
}

if len(otherFiles) > 0 {
return nil, pkgerrors.Errorf("the code repair repository %q must contain only packages, but found %+v", repositoryPath, otherFiles)
}

return packagePaths, nil
}

0 comments on commit 1fe71a0

Please sign in to comment.