forked from SAP/fosstars-rating-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new feature Has executable binaries in project (SAP#850)
* Implement new feature Has executable binaries in project Fixes SAP#733
- Loading branch information
1 parent
1d36d4d
commit 05c8d1c
Showing
14 changed files
with
398 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/sap/oss/phosphor/fosstars/data/github/HasExecutableBinaries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.sap.oss.phosphor.fosstars.data.github; | ||
|
||
import static com.sap.oss.phosphor.fosstars.model.feature.oss.OssFeatures.HAS_EXECUTABLE_BINARIES; | ||
|
||
import com.google.common.base.Predicate; | ||
import com.sap.oss.phosphor.fosstars.model.Feature; | ||
import com.sap.oss.phosphor.fosstars.model.Value; | ||
import com.sap.oss.phosphor.fosstars.model.subject.oss.GitHubProject; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.kohsuke.github.GitHub; | ||
import org.kohsuke.github.GitHubBuilder; | ||
|
||
/** | ||
* The data provider tries to figure out if an open-source project has executable binaries (for | ||
* example, .class, .pyc, .exe).. | ||
*/ | ||
public class HasExecutableBinaries extends CachedSingleFeatureGitHubDataProvider<Boolean> { | ||
|
||
/** | ||
* List of file extensions deemed as executable binaries. | ||
*/ | ||
static final List<String> FILE_EXTENSIONS = Arrays.asList(".crx", ".deb", ".dex", ".dey", ".elf", | ||
".o", ".so", ".iso", ".class", ".jar", ".bundle", ".dylib", ".lib", ".msi", ".acm", ".ax", | ||
".cpl", ".dll", ".drv", ".efi", ".exe", ".mui", ".ocx", ".scr", ".sys", ".tsp", ".pyc", | ||
".pyo", ".par", ".rpm", ".swf", ".torrent", ".cab", ".whl"); | ||
|
||
/** | ||
* Predicate to confirm if there is a file in open-source project with the executable binary | ||
* extension. | ||
*/ | ||
private static final Predicate<Path> FILE_EXTENSIONS_PREDICATE = path -> isExecutableBinary(path); | ||
|
||
/** | ||
* Initializes a data provider. | ||
* | ||
* @param fetcher An interface to GitHub. | ||
*/ | ||
public HasExecutableBinaries(GitHubDataFetcher fetcher) { | ||
super(fetcher); | ||
} | ||
|
||
@Override | ||
protected Feature<Boolean> supportedFeature() { | ||
return HAS_EXECUTABLE_BINARIES; | ||
} | ||
|
||
@Override | ||
protected Value<Boolean> fetchValueFor(GitHubProject project) throws IOException { | ||
logger.info("Figuring out if the project has executable binaries..."); | ||
|
||
LocalRepository localRepository = loadLocalRepository(project); | ||
List<Path> paths = localRepository.files(FILE_EXTENSIONS_PREDICATE); | ||
return HAS_EXECUTABLE_BINARIES.value(!paths.isEmpty()); | ||
} | ||
|
||
/** | ||
* Fetch the locally cloned repository. | ||
* | ||
* @param project The GitHub project. | ||
* @return {@link LocalRepository} clone repository. | ||
* @throws IOException If something went wrong. | ||
*/ | ||
LocalRepository loadLocalRepository(GitHubProject project) throws IOException { | ||
return GitHubDataFetcher.localRepositoryFor(project); | ||
} | ||
|
||
/** | ||
* Check if the file represented by the path is a executable binary file. | ||
* | ||
* @param path The file path. | ||
* @return true if the executable binary file type is found, otherwise false. | ||
*/ | ||
private static boolean isExecutableBinary(Path path) { | ||
return FILE_EXTENSIONS.stream().anyMatch(ext -> path.getFileName().toString().endsWith(ext)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/test/java/com/sap/oss/phosphor/fosstars/data/github/HasExecutableBinariesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.sap.oss.phosphor.fosstars.data.github; | ||
|
||
import static com.sap.oss.phosphor.fosstars.model.feature.oss.OssFeatures.HAS_EXECUTABLE_BINARIES; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.sap.oss.phosphor.fosstars.model.Value; | ||
import com.sap.oss.phosphor.fosstars.model.ValueSet; | ||
import com.sap.oss.phosphor.fosstars.model.subject.oss.GitHubProject; | ||
import com.sap.oss.phosphor.fosstars.model.value.ValueHashSet; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class HasExecutableBinariesTest extends TestGitHubDataFetcherHolder { | ||
|
||
private static Path BASE_DIR; | ||
|
||
private static final GitHubProject PROJECT = new GitHubProject("org", "test"); | ||
|
||
private static LocalRepository LOCAL_REPOSITORY; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
try { | ||
BASE_DIR = Files.createTempDirectory(HasExecutableBinariesTest.class.getName()); | ||
Path git = BASE_DIR.resolve(".git"); | ||
Files.createDirectory(git); | ||
Path submodule = BASE_DIR.resolve("submodule"); | ||
Files.createDirectory(submodule); | ||
Path packageJson = submodule.resolve("pom.xml"); | ||
Files.write(packageJson, StringUtils.repeat("x", 500).getBytes()); | ||
|
||
LocalRepositoryInfo localRepositoryInfo = mock(LocalRepositoryInfo.class); | ||
when(localRepositoryInfo.path()).thenReturn(BASE_DIR); | ||
Repository repository = mock(Repository.class); | ||
when(repository.getDirectory()).thenReturn(git.toFile()); | ||
|
||
LOCAL_REPOSITORY = new LocalRepository(localRepositoryInfo, repository); | ||
LOCAL_REPOSITORY = spy(LOCAL_REPOSITORY); | ||
when(LOCAL_REPOSITORY.info()).thenReturn(localRepositoryInfo); | ||
|
||
TestGitHubDataFetcher.addForTesting(PROJECT, LOCAL_REPOSITORY); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testExecutableIsPresent() throws IOException { | ||
Path exe = BASE_DIR.resolve("game.exe"); | ||
try { | ||
Files.write(exe, StringUtils.repeat("x", 1000).getBytes()); | ||
|
||
HasExecutableBinaries provider = new HasExecutableBinaries(fetcher); | ||
provider = spy(provider); | ||
when(provider.loadLocalRepository(PROJECT)).thenReturn(LOCAL_REPOSITORY); | ||
|
||
ValueSet values = new ValueHashSet(); | ||
provider.update(PROJECT, values); | ||
|
||
assertTrue(values.has(HAS_EXECUTABLE_BINARIES)); | ||
|
||
Optional<Value<Boolean>> something = values.of(HAS_EXECUTABLE_BINARIES); | ||
assertTrue(something.isPresent()); | ||
|
||
Value<Boolean> value = something.get(); | ||
assertTrue(value.get()); | ||
} finally { | ||
FileUtils.forceDeleteOnExit(exe.toFile()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testExecutableIsNotPresent() throws IOException { | ||
Path javaFile = BASE_DIR.resolve("Test.java"); | ||
try { | ||
Files.write(javaFile, StringUtils.repeat("x", 1000).getBytes()); | ||
|
||
HasExecutableBinaries provider = new HasExecutableBinaries(fetcher); | ||
provider = spy(provider); | ||
when(provider.loadLocalRepository(PROJECT)).thenReturn(LOCAL_REPOSITORY); | ||
|
||
ValueSet values = new ValueHashSet(); | ||
provider.update(PROJECT, values); | ||
|
||
assertTrue(values.has(HAS_EXECUTABLE_BINARIES)); | ||
|
||
Optional<Value<Boolean>> something = values.of(HAS_EXECUTABLE_BINARIES); | ||
assertTrue(something.isPresent()); | ||
|
||
Value<Boolean> value = something.get(); | ||
assertFalse(value.get()); | ||
} finally { | ||
FileUtils.forceDeleteOnExit(javaFile.toFile()); | ||
} | ||
|
||
} | ||
|
||
@AfterClass | ||
public static void shutdown() { | ||
try { | ||
FileUtils.forceDeleteOnExit(BASE_DIR.toFile()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.