Skip to content

Commit

Permalink
Add auxiliary test github user repository (#9308)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-ndp committed Apr 2, 2018
1 parent b961c97 commit a42e873
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** @author Dmytro Nochevnov */
/**
* This is facade and helper for {@link GHRepository}.
*
* @author Dmytro Nochevnov
*/
public class TestGitHubRepository {

private static final int GITHUB_OPERATION_TIMEOUT_SEC = 1;
Expand All @@ -41,6 +45,15 @@ public class TestGitHubRepository {
private final GHRepository ghRepo;
private final GitHub gitHub;

/**
* Creates repository with semi-random name on GitHub for certain {@code gitHubUsername}. Waits
* until repository is really created.
*
* @param gitHubUsername default github user name
* @param gitHubPassword default github user password
* @throws IOException
* @throws InterruptedException
*/
@Inject
public TestGitHubRepository(
@Named("github.username") String gitHubUsername,
Expand All @@ -50,38 +63,6 @@ public TestGitHubRepository(
ghRepo = create();
}

private GHRepository create() throws IOException, InterruptedException {
GHRepository repo = gitHub.createRepository(repoName).create();
ensureRepositoryCreated(repo, System.currentTimeMillis());

LOG.info("GitHub repo {} has been created", repo.getHtmlUrl());
return repo;
}

private void ensureRepositoryCreated(GHRepository repo, long startCreationTimeInMillisec)
throws IOException {
Throwable lastIOException = null;
for (int i = 0; i < REPO_CREATION_ATTEMPTS; i++) {
try {
gitHub.getRepository(repo.getFullName());
return;
} catch (IOException e) {
lastIOException = e;
LOG.info("Waiting for {} to be created", repo.getHtmlUrl());
sleepQuietly(GITHUB_OPERATION_TIMEOUT_SEC); // sleep one second
}
}

long durationOfRepoCreationInSec =
(System.currentTimeMillis() - startCreationTimeInMillisec) / 1000;

throw new IOException(
format(
"GitHub repo %s hasn't been created in %s seconds",
repo.getHtmlUrl(), durationOfRepoCreationInSec),
lastIOException);
}

public String getName() {
return repoName;
}
Expand All @@ -90,13 +71,21 @@ public String getName() {
* Creates reference to branch, tag, ... from master branch.
*
* @param refName is a name of branch, tag, etc
* @return reference to the new branch
* @throws IOException
*/
public GHRef addRefFromMaster(String refName) throws IOException {
public GHRef createBranchFromMaster(String refName) throws IOException {
GHRef master = ghRepo.getRef("heads/master");
return ghRepo.createRef("refs/heads/" + refName, master.getObject().getSha());
}

/**
* Copies content of directory {@code pathToRootContentDirectory} to the GitHub repository. It
* tries to recreate the file ones again in case of FileNotFoundException occurs.
*
* @param pathToRootContentDirectory path to the directory with content
* @throws IOException
*/
public void addContent(Path pathToRootContentDirectory) throws IOException {
Files.walk(pathToRootContentDirectory)
.filter(Files::isRegularFile)
Expand All @@ -110,23 +99,6 @@ public void addContent(Path pathToRootContentDirectory) throws IOException {
});
}

private void createFile(Path pathToRootContentDirectory, Path pathToFile) throws IOException {
byte[] contentBytes = Files.readAllBytes(pathToFile);
String relativePath = pathToRootContentDirectory.relativize(pathToFile).toString();
String commitMessage = String.format("Add file %s", relativePath);

try {
ghRepo.createContent(contentBytes, commitMessage, relativePath);
} catch (GHFileNotFoundException e) {
// try to create content once again
LOG.warn(
"Error of creation of {} occurred. Is trying to create it once again...",
ghRepo.getHtmlUrl() + "/" + relativePath);
sleepQuietly(GITHUB_OPERATION_TIMEOUT_SEC);
ghRepo.createContent(contentBytes, commitMessage, relativePath);
}
}

/**
* Changes content of the file
*
Expand Down Expand Up @@ -155,6 +127,13 @@ public void deleteFile(String pathToFile) throws IOException {
ghRepo.getFileContent(pathToFile).delete("Delete file " + pathToFile);
}

/**
* Delete folder with content inside the repository on GitHub.
*
* @param folder folder to delete
* @param deleteCommitMessage commit message which is used to delete the message
* @throws IOException
*/
public void deleteFolder(Path folder, String deleteCommitMessage) throws IOException {
for (GHContent ghContent : ghRepo.getDirectoryContent(folder.toString())) {
ghContent.delete(deleteCommitMessage);
Expand All @@ -174,4 +153,60 @@ public String getHtmlUrl() {
public String getSshUrl() {
return ghRepo.getSshUrl();
}

private GHRepository create() throws IOException, InterruptedException {
GHRepository repo = gitHub.createRepository(repoName).create();
ensureRepositoryCreated(repo, System.currentTimeMillis());

LOG.info("GitHub repo {} has been created", repo.getHtmlUrl());
return repo;
}

private void ensureRepositoryCreated(GHRepository repo, long startCreationTimeInMillisec)
throws IOException {
Throwable lastIOException = null;
for (int i = 0; i < REPO_CREATION_ATTEMPTS; i++) {
try {
gitHub.getRepository(repo.getFullName());
return;
} catch (IOException e) {
lastIOException = e;
LOG.info("Waiting for {} to be created", repo.getHtmlUrl());
sleepQuietly(GITHUB_OPERATION_TIMEOUT_SEC); // sleep one second
}
}

long durationOfRepoCreationInSec =
(System.currentTimeMillis() - startCreationTimeInMillisec) / 1000;

throw new IOException(
format(
"GitHub repo %s hasn't been created in %s seconds",
repo.getHtmlUrl(), durationOfRepoCreationInSec),
lastIOException);
}

/**
* Creates file in GitHub repository.
*
* @param pathToRootContentDirectory path to the root directory of file locally
* @param pathToFile path to file locally
* @throws IOException
*/
private void createFile(Path pathToRootContentDirectory, Path pathToFile) throws IOException {
byte[] contentBytes = Files.readAllBytes(pathToFile);
String relativePath = pathToRootContentDirectory.relativize(pathToFile).toString();
String commitMessage = String.format("Add file %s", relativePath);

try {
ghRepo.createContent(contentBytes, commitMessage, relativePath);
} catch (GHFileNotFoundException e) {
// try to create content once again
LOG.warn(
"Error of creation of {} occurred. Is trying to create it once again...",
ghRepo.getHtmlUrl() + "/" + relativePath);
sleepQuietly(GITHUB_OPERATION_TIMEOUT_SEC);
ghRepo.createContent(contentBytes, commitMessage, relativePath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import javax.inject.Named;
import java.io.IOException;
import org.eclipse.che.api.core.rest.HttpJsonRequestFactory;
import org.eclipse.che.selenium.core.action.ActionsFactory;
import org.eclipse.che.selenium.core.action.GenericActionsFactory;
import org.eclipse.che.selenium.core.action.MacOSActionsFactory;
import org.eclipse.che.selenium.core.client.CheTestUserServiceClient;
import org.eclipse.che.selenium.core.client.TestGitHubRepository;
import org.eclipse.che.selenium.core.client.TestOrganizationServiceClient;
import org.eclipse.che.selenium.core.client.TestUserServiceClient;
import org.eclipse.che.selenium.core.client.TestUserServiceClientFactory;
Expand Down Expand Up @@ -64,6 +66,8 @@
*/
public class CheSeleniumSuiteModule extends AbstractModule {

public static final String AUXILIARY = "auxiliary";

private static final String CHE_MULTIUSER_VARIABLE = "CHE_MULTIUSER";
private static final String CHE_INFRASTRUCTURE_VARIABLE = "CHE_INFRASTRUCTURE";
private static final String DOCKER_INFRASTRUCTURE = "docker";
Expand Down Expand Up @@ -147,4 +151,13 @@ public TestOrganizationServiceClient getAdminOrganizationServiceClient(
public ActionsFactory getActionFactory() {
return isMac() ? new MacOSActionsFactory() : new GenericActionsFactory();
}

@Provides
@Named(AUXILIARY)
public TestGitHubRepository getTestGitHubRepository(
@Named("github.auxiliary.username") String gitHubAuxiliaryUsername,
@Named("github.auxiliary.password") String gitHubAuxiliaryPassword)
throws IOException, InterruptedException {
return new TestGitHubRepository(gitHubAuxiliaryUsername, gitHubAuxiliaryPassword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public void prepare() throws Exception {

ide.open(ws);

// create other branch in the test repo
testRepo.addRefFromMaster(SECOND_BRANCH);
// create another branch in the test repo
testRepo.createBranchFromMaster(SECOND_BRANCH);
}

@Test
Expand Down

0 comments on commit a42e873

Please sign in to comment.