Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avimanyum committed Feb 7, 2024
1 parent 50df365 commit bbb42f8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ private Constants() {

public static final String CHECK_FOR_RENOVATE = "checkforrenovate";

public static final String RENOVATE_CONFIG_FILENAME = "renovate.json";
public static final String RENOVATE_CONFIG_FILEPATH = "renovate.json";
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ public void prepareToCreate(final Namespace ns,
pathToDockerfilesInParentRepo.get(currUserRepo).stream().findFirst();
if (forkWithContentPaths.isPresent()) {
try {
//If the repository has been onboarded to renovate enterprise, skip sending the DFIU PR
if(ns.getBoolean(Constants.CHECK_FOR_RENOVATE)
&& (isRenovateEnabled(Constants.RENOVATE_CONFIG_FILENAME, forkWithContentPaths.get()))) {
log.info("Found file with name %s in the repo %s. Skip sending DFIU PRs.", Constants.RENOVATE_CONFIG_FILENAME, forkWithContentPaths.get().getParent().getFullName());
&& (isRenovateEnabled(Constants.RENOVATE_CONFIG_FILEPATH, forkWithContentPaths.get()))) {
log.info("Found file with name %s in the repo %s. Skip sending DFIU PRs to this repository.", Constants.RENOVATE_CONFIG_FILEPATH, forkWithContentPaths.get().getParent().getFullName());
} else {
dockerfileGitHubUtil.changeDockerfiles(ns,
pathToDockerfilesInParentRepo,
forkWithContentPaths.get(), skippedRepos,
gitForkBranch, rateLimiter);
}


} catch (IOException | InterruptedException e) {
log.error(String.format("Error changing Dockerfile for %s", forkWithContentPaths.get().getParent().getFullName()), e);
exceptions.add((IOException) e);
Expand All @@ -50,22 +49,27 @@ public void prepareToCreate(final Namespace ns,
ResultsProcessor.processResults(skippedRepos, exceptions, log);
}

private boolean isRenovateEnabled(String fileName, GitHubContentToProcess fork) throws IOException {
/**
* Check if the repository is onboarded to Renovate. The signal we are looking for are
* (1) The presence of a file named renovate.json in the root of the repository
* (2) Ensuring that the file does not have the key "enabled" set to "false"
* @param filePath the name of the file that we are searching for in the repo
* @param fork A GitHubContentToProcess object that contains the fork repository that is under process
* @return true if the file is found in the path specified and is not disabled, false otherwise
*/
private boolean isRenovateEnabled(String filePath, GitHubContentToProcess fork) throws IOException {
try {
GHContent fileContent = fork.getParent().getFileContent(fileName);
GHContent fileContent = fork.getParent().getFileContent(filePath);
InputStream is = fileContent.read();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
JSONObject json = new JSONObject(sb.toString());
if (json.has("enabled") && json.get("enabled") == "false") {
JSONTokener tokener = new JSONTokener(bufferedReader);
JSONObject json = new JSONObject(tokener);
//If the renovate.json file has the key 'enabled' set to false, it indicates that while the repo has been onboarded to renovate, it has been disabled for some reason
if (json.has("enabled") && json.get("enabled").toString() == "false") {
return false;
}
} catch (FileNotFoundException e) {
log.debug("The file with name %s not found in the repository.");
log.debug("The file with name %s not found in the repository.", filePath);
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.*;
import java.util.*;
import java.util.Optional;

import static org.mockito.Mockito.*;
import static org.testng.Assert.assertThrows;
Expand Down Expand Up @@ -110,4 +111,51 @@ public void testPullRequestsPrepareToCreateWhenNoDockerfileFound() throws Except
eq(pathToDockerfilesInParentRepo),
eq(gitHubContentToProcess), anyList(), eq(gitForkBranch),eq(rateLimiter));
}

@Test
public void testPullRequestsPrepareSkipsSendingPRIfRepoOnboardedToRenovate() throws Exception {
Map<String, Object> nsMap = ImmutableMap.of(
Constants.IMG, "image",
Constants.TAG, "tag",
Constants.STORE,"store",
Constants.SKIP_PR_CREATION,false,
Constants.CHECK_FOR_RENOVATE, true);


Namespace ns = new Namespace(nsMap);
PullRequests pullRequests = new PullRequests();
GitHubPullRequestSender pullRequestSender = mock(GitHubPullRequestSender.class);
PagedSearchIterable<GHContent> contentsFoundWithImage = mock(PagedSearchIterable.class);
GitForkBranch gitForkBranch = mock(GitForkBranch.class);
DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class);
RateLimiter rateLimiter = Mockito.spy(new RateLimiter());
Multimap<String, GitHubContentToProcess> pathToDockerfilesInParentRepo = ArrayListMultimap.create();
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
pathToDockerfilesInParentRepo.put("repo1", gitHubContentToProcess);
pathToDockerfilesInParentRepo.put("repo2", gitHubContentToProcess);
pathToDockerfilesInParentRepo.put("repo3", gitHubContentToProcess);
GHContent content = mock(GHContent.class);
InputStream inputStream1 = new ByteArrayInputStream("{someKey:someValue}".getBytes());
InputStream inputStream2 = new ByteArrayInputStream("{enabled:false}".getBytes());
GHRepository ghRepository = mock(GHRepository.class);

when(pullRequestSender.forkRepositoriesFoundAndGetPathToDockerfiles(contentsFoundWithImage, gitForkBranch)).thenReturn(pathToDockerfilesInParentRepo);
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
//Fetch the content of the renovate.json file for the 3 repos.
// The first one returns a file with regular json content.
// The second one returns a file with the key 'enabled' set to 'false' to replicate a repo that has been onboarded to renovate but has it disabled
// The third repo does not have the renovate.json file
when(ghRepository.getFileContent(anyString())).thenReturn(content).thenReturn(content).thenThrow(new FileNotFoundException());
when(ghRepository.getFullName()).thenReturn("org/repo");
when(content.read()).thenReturn(inputStream1).thenReturn(inputStream2);

pullRequests.prepareToCreate(ns, pullRequestSender, contentsFoundWithImage,
gitForkBranch, dockerfileGitHubUtil, rateLimiter);

//Verify that the DFIU PR is skipped for the first repo, but is sent to the other two repos
verify(dockerfileGitHubUtil, times(2)).changeDockerfiles(eq(ns),
eq(pathToDockerfilesInParentRepo),
eq(gitHubContentToProcess), anyList(), eq(gitForkBranch),
eq(rateLimiter));
}
}

0 comments on commit bbb42f8

Please sign in to comment.