|
| 1 | +package com.sap.oss.phosphor.fosstars.data.github; |
| 2 | + |
| 3 | +import com.sap.oss.phosphor.fosstars.model.subject.oss.GitHubProject; |
| 4 | +import java.io.IOException; |
| 5 | +import java.time.Duration; |
| 6 | +import java.time.Instant; |
| 7 | +import java.util.Date; |
| 8 | +import java.util.Optional; |
| 9 | +import org.kohsuke.github.GHIssueState; |
| 10 | +import org.kohsuke.github.GHPullRequest; |
| 11 | +import org.kohsuke.github.GHUser; |
| 12 | + |
| 13 | +/** |
| 14 | + * This is a base class for dependency checker data providers such as Dependabot and Snyk. |
| 15 | + */ |
| 16 | +public abstract class AbstractDependencyScanDataProvider extends GitHubCachingDataProvider { |
| 17 | + |
| 18 | + /** |
| 19 | + * Period of time to be checked. |
| 20 | + */ |
| 21 | + private static final Duration ONE_YEAR = Duration.ofDays(365); |
| 22 | + |
| 23 | + /** |
| 24 | + * A minimal number of characters in a config for dependency checker. |
| 25 | + */ |
| 26 | + private static final int ACCEPTABLE_CONFIG_SIZE = 10; |
| 27 | + |
| 28 | + protected abstract String getDependencyCheckerPattern(); |
| 29 | + |
| 30 | + /** |
| 31 | + * Initializes a data provider. |
| 32 | + * |
| 33 | + * @param fetcher An interface to GitHub. |
| 34 | + */ |
| 35 | + public AbstractDependencyScanDataProvider( |
| 36 | + GitHubDataFetcher fetcher) { |
| 37 | + super(fetcher); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks if a repository contains commits from dependency checker in the commit history. |
| 42 | + * |
| 43 | + * @param repository The repository. |
| 44 | + * @return True if at least one commit from dependency checker was found, false otherwise. |
| 45 | + */ |
| 46 | + public boolean hasDependencyCheckerCommits(LocalRepository repository) { |
| 47 | + Date date = Date.from(Instant.now().minus(ONE_YEAR)); |
| 48 | + |
| 49 | + try { |
| 50 | + for (Commit commit : repository.commitsAfter(date)) { |
| 51 | + if (isDependencyChecker(commit)) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + } catch (IOException e) { |
| 56 | + logger.warn("Something went wrong!", e); |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks if a repository has a configuration file for dependency checker. |
| 64 | + * |
| 65 | + * @param repository The repository |
| 66 | + * @param configs The config files path as String array |
| 67 | + * @return True if a config was found, false otherwise. |
| 68 | + * @throws IOException If something went wrong. |
| 69 | + */ |
| 70 | + public boolean hasDependencyCheckerConfig(LocalRepository repository, String[] configs) |
| 71 | + throws IOException { |
| 72 | + for (String config : configs) { |
| 73 | + Optional<String> content = repository.file(config); |
| 74 | + if (content.isPresent() && content.get().length() >= ACCEPTABLE_CONFIG_SIZE) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Checks whether a project has open pull requests from dependency checker. |
| 84 | + * |
| 85 | + * @param project The project. |
| 86 | + * @return True if the project has open pull requests form dependency checker. |
| 87 | + * @throws IOException If something went wrong. |
| 88 | + */ |
| 89 | + public boolean hasOpenPullRequestFromDependencyChecker(GitHubProject project) throws IOException { |
| 90 | + return fetcher.repositoryFor(project).getPullRequests(GHIssueState.OPEN).stream() |
| 91 | + .anyMatch(this::createdByDependencyChecker); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Checks if a pull request was created by dependency checker. |
| 96 | + * |
| 97 | + * @param pullRequest The pull request. |
| 98 | + * @return True if the user looks like dependency checker, false otherwise. |
| 99 | + */ |
| 100 | + private boolean createdByDependencyChecker(GHPullRequest pullRequest) { |
| 101 | + try { |
| 102 | + GHUser user = pullRequest.getUser(); |
| 103 | + return isDependencyChecker(user.getName()) || isDependencyChecker(user.getLogin()); |
| 104 | + } catch (IOException e) { |
| 105 | + logger.warn("Oops! Could not fetch name or login!", e); |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Checks if a commit was done by dependency checker. |
| 112 | + * |
| 113 | + * @param commit The commit to be checked. |
| 114 | + * @return True if the commit was done by dependency checker, false otherwise. |
| 115 | + */ |
| 116 | + private boolean isDependencyChecker(Commit commit) { |
| 117 | + if (isDependencyChecker(commit.authorName()) || isDependencyChecker(commit.committerName())) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + for (String line : commit.message()) { |
| 122 | + if ((line.startsWith("Signed-off-by:") || line.startsWith("Co-authored-by:")) |
| 123 | + && line.contains(getDependencyCheckerPattern())) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Checks whether a name looks like dependency checker. |
| 133 | + * |
| 134 | + * @param name The name. |
| 135 | + * @return True if the name looks like dependency checker, false otherwise. |
| 136 | + */ |
| 137 | + private boolean isDependencyChecker(String name) { |
| 138 | + return name != null && name.toLowerCase().contains(getDependencyCheckerPattern()); |
| 139 | + } |
| 140 | +} |
0 commit comments