Encapsulate kohsuke-related code and ensure test repo creation (#9141)

Signed-off-by: Dmytro Nochevnov <dnochevnov@codenvy.com>
6.19.x
Dmytro Nochevnov 2018-03-20 18:15:48 +02:00 committed by GitHub
parent b6fef93c3c
commit f61a779dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 147 additions and 84 deletions

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.selenium.core.client;
import static java.lang.String.format;
import static org.eclipse.che.selenium.core.utils.WaitUtils.sleepQuietly;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.che.commons.lang.NameGenerator;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** @author Dmytro Nochevnov */
public class TestGitHubRepository {
private static final int REPO_CREATION_TIMEOUT_SEC = 6;
private final String repoName = NameGenerator.generate("EclipseCheTestRepo-", 5);
private static final Logger LOG = LoggerFactory.getLogger(TestGitHubRepository.class);
private final GHRepository ghRepository;
private final GitHub gitHub;
@Inject
public TestGitHubRepository(
@Named("github.username") String gitHubUsername,
@Named("github.password") String gitHubPassword)
throws IOException, InterruptedException {
gitHub = GitHub.connectUsingPassword(gitHubUsername, gitHubPassword);
ghRepository = create();
}
private GHRepository create() throws IOException, InterruptedException {
GHRepository repo = gitHub.createRepository(repoName).create();
ensureRepositoryCreated(repo);
LOG.info("GitHub repo {} has been created", repo.getHtmlUrl());
return repo;
}
private void ensureRepositoryCreated(GHRepository repo) throws IOException {
Throwable lastIOException = null;
for (int i = 0; i < REPO_CREATION_TIMEOUT_SEC; i++) {
try {
gitHub.getRepository(repo.getFullName());
return;
} catch (IOException e) {
lastIOException = e;
LOG.info("Waiting for {} to be created", repo.getHtmlUrl());
sleepQuietly(1); // sleep one second
}
}
throw new IOException(
format(
"GitHub repo %s hasn't been created in %s seconds",
repo.getHtmlUrl(), REPO_CREATION_TIMEOUT_SEC),
lastIOException);
}
public String getName() {
return repoName;
}
public void addContent(Path localRepo, String commitMessage) throws IOException {
Files.walk(localRepo)
.filter(Files::isRegularFile)
.forEach(
path -> {
try {
byte[] contentBytes = Files.readAllBytes(path);
String relativePath = localRepo.relativize(path).toString();
ghRepository.createContent(contentBytes, commitMessage, relativePath);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
public void deleteFolder(Path folder, String deleteCommitMessage) throws IOException {
for (GHContent ghContent : ghRepository.getDirectoryContent(folder.toString())) {
ghContent.delete(deleteCommitMessage);
}
}
public void delete() throws IOException {
ghRepository.delete();
LOG.info("GitHub repo {} has been removed", ghRepository.getHtmlUrl());
}
public GHContent getFileContent(String path) throws IOException {
return ghRepository.getFileContent(path);
}
public String getHtmlUrl() {
return ghRepository.getHtmlUrl().toString();
}
public String getSshUrl() {
return ghRepository.getSshUrl();
}
}

View File

@ -20,10 +20,7 @@ import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import javax.xml.bind.DatatypeConverter;
@ -32,7 +29,6 @@ import org.eclipse.che.api.core.rest.HttpJsonRequestFactory;
import org.eclipse.che.api.core.rest.HttpJsonResponse;
import org.eclipse.che.dto.server.JsonStringMapImpl;
import org.eclipse.che.plugin.github.shared.GitHubKey;
import org.kohsuke.github.GHRepository;
import org.slf4j.Logger;
/** @author Mihail Kuznyetsov. */
@ -229,20 +225,4 @@ public class TestGitHubServiceClient {
String base64 = DatatypeConverter.printBase64Binary(nameAndPass);
return "Basic " + base64;
}
public void addContentToRepository(Path repoRoot, String commitMessage, GHRepository ghRepository)
throws IOException {
Files.walk(repoRoot)
.filter(Files::isRegularFile)
.forEach(
it -> {
try {
byte[] contentBytes = Files.readAllBytes(it);
String relativePath = repoRoot.relativize(it).toString();
ghRepository.createContent(contentBytes, commitMessage, relativePath);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}

View File

@ -19,6 +19,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.che.commons.lang.NameGenerator;
import org.eclipse.che.selenium.core.TestGroup;
import org.eclipse.che.selenium.core.client.TestGitHubRepository;
import org.eclipse.che.selenium.core.client.TestGitHubServiceClient;
import org.eclipse.che.selenium.core.client.TestProjectServiceClient;
import org.eclipse.che.selenium.core.client.TestSshServiceClient;
@ -34,8 +35,6 @@ import org.eclipse.che.selenium.pageobject.Loader;
import org.eclipse.che.selenium.pageobject.Menu;
import org.eclipse.che.selenium.pageobject.ProjectExplorer;
import org.eclipse.che.selenium.pageobject.git.Git;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -43,26 +42,19 @@ import org.testng.annotations.Test;
/** @author aleksandr shmaraev */
@Test(groups = TestGroup.GITHUB)
public class FetchAndMergeRemoteBranchIntoLocalTest {
private static final String REPO_NAME = NameGenerator.generate("FetchAndMergeTest-", 3);
private static final String PROJECT_NAME = NameGenerator.generate("FetchAndMergeTest-", 4);
private static final String CHANGE_CONTENT =
String.format("//change_content-%s", String.valueOf(System.currentTimeMillis()));
private GitHub gitHub;
private GHRepository gitHubRepository;
@Inject private TestWorkspace ws;
@Inject private Ide ide;
@Inject private TestUser productUser;
@Inject private TestGitHubRepository testRepo;
@Inject(optional = true)
@Named("github.username")
private String gitHubUsername;
@Inject(optional = true)
@Named("github.password")
private String gitHubPassword;
@Inject private TestProjectServiceClient testProjectServiceClient;
@Inject private ProjectExplorer projectExplorer;
@Inject private Menu menu;
@ -77,18 +69,18 @@ public class FetchAndMergeRemoteBranchIntoLocalTest {
@BeforeClass
public void prepare() throws Exception {
gitHub = GitHub.connectUsingPassword(gitHubUsername, gitHubPassword);
gitHubRepository = gitHub.createRepository(REPO_NAME).create();
String commitMess = String.format("add-new-content-%s ", System.currentTimeMillis());
testUserPreferencesServiceClient.addGitCommitter(gitHubUsername, productUser.getEmail());
Path entryPath = Paths.get(getClass().getResource("/projects/guess-project").getPath());
gitHubClientService.addContentToRepository(entryPath, commitMess, gitHubRepository);
testRepo.addContent(entryPath, commitMess);
ide.open(ws);
}
@AfterClass
public void deleteRepo() throws IOException {
gitHubRepository.delete();
testRepo.delete();
}
@Test
@ -100,15 +92,13 @@ public class FetchAndMergeRemoteBranchIntoLocalTest {
String pathToJavaFile = "src/main/java/org/eclipse/qa/examples";
String pathToJspFile = "src/main/webapp";
String originMaster = "origin/master";
String fetchMess =
String.format("%s/%s/%s.git", "Fetched from https://github.com", gitHubUsername, REPO_NAME);
String fetchMess = String.format("Fetched from %s", testRepo.getHtmlUrl());
String mergeMess1 = "Fast-forward Merged commits:";
String mergeMess2 = "New HEAD commit: ";
String mergeMess3 = "Already up-to-date";
projectExplorer.waitProjectExplorer();
String repoUrl = String.format("https://github.com/%s/%s.git", gitHubUsername, REPO_NAME);
git.importJavaApp(repoUrl, PROJECT_NAME, MAVEN);
git.importJavaApp(testRepo.getHtmlUrl(), PROJECT_NAME, MAVEN);
// change content in the test repo on GitHub
deleteFileOnGithubSide(String.format("%s/%s", pathToJspFile, jspFile), "delete index.jsp");
@ -160,13 +150,11 @@ public class FetchAndMergeRemoteBranchIntoLocalTest {
}
private void deleteFileOnGithubSide(String pathToContent, String commitMess) throws IOException {
gitHubRepository.getFileContent(pathToContent).delete(commitMess);
testRepo.getFileContent(pathToContent).delete(commitMess);
}
private void changeContentOnGithubSide(String pathToContent, String content) throws IOException {
gitHubRepository
.getFileContent(String.format("/%s", pathToContent))
.update(content, "add " + content);
testRepo.getFileContent(String.format("/%s", pathToContent)).update(content, "add " + content);
}
private void performFetch() {

View File

@ -23,6 +23,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.che.commons.lang.NameGenerator;
import org.eclipse.che.selenium.core.TestGroup;
import org.eclipse.che.selenium.core.client.TestGitHubRepository;
import org.eclipse.che.selenium.core.client.TestGitHubServiceClient;
import org.eclipse.che.selenium.core.client.TestProjectServiceClient;
import org.eclipse.che.selenium.core.client.TestSshServiceClient;
@ -39,8 +40,6 @@ import org.eclipse.che.selenium.pageobject.Loader;
import org.eclipse.che.selenium.pageobject.Menu;
import org.eclipse.che.selenium.pageobject.ProjectExplorer;
import org.eclipse.che.selenium.pageobject.git.Git;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -48,7 +47,6 @@ import org.testng.annotations.Test;
/** @author aleksandr shmaraev */
@Test(groups = TestGroup.GITHUB)
public class GitPullConflictTest {
private static final String REPO_NAME = NameGenerator.generate("PullConflictTest-", 3);
private static final String PROJECT_NAME = NameGenerator.generate("PullConflictProject-", 4);
private static final String PATH_TO_JAVA_FILE = "src/main/java/org/eclipse/qa/examples";
private static final String COMMIT_MSG = "commit_changes";
@ -71,21 +69,15 @@ public class GitPullConflictTest {
private static final String HEAD_CONF_PREFIX_CONF_MESS =
String.format("<<<<<<< HEAD\n//second_change\n=======\n%s\n>>>>>>>", CHANGE_STRING_1);
private GitHub gitHub;
private GHRepository gitHubRepository;
@Inject private TestWorkspace ws;
@Inject private Ide ide;
@Inject private TestUser productUser;
@Inject private TestGitHubRepository testRepo;
@Inject(optional = true)
@Named("github.username")
private String gitHubUsername;
@Inject(optional = true)
@Named("github.password")
private String gitHubPassword;
@Inject private TestProjectServiceClient testProjectServiceClient;
@Inject private ProjectExplorer projectExplorer;
@Inject private Menu menu;
@ -100,19 +92,18 @@ public class GitPullConflictTest {
@BeforeClass
public void prepare() throws Exception {
gitHub = GitHub.connectUsingPassword(gitHubUsername, gitHubPassword);
gitHubRepository = gitHub.createRepository(REPO_NAME).create();
String commitMess = String.format("add-new-content %s ", System.currentTimeMillis());
testUserPreferencesServiceClient.addGitCommitter(gitHubUsername, productUser.getEmail());
Path entryPath =
Paths.get(getClass().getResource("/projects/default-spring-project").getPath());
gitHubClientService.addContentToRepository(entryPath, commitMess, gitHubRepository);
testRepo.addContent(entryPath, commitMess);
ide.open(ws);
}
@AfterClass
public void deleteRepo() throws IOException {
gitHubRepository.delete();
testRepo.delete();
}
@Test
@ -126,8 +117,7 @@ public class GitPullConflictTest {
String pathTextFile = String.format("%s/%s", PROJECT_NAME, textFileChange);
projectExplorer.waitProjectExplorer();
String repoUrl = String.format("https://github.com/%s/%s.git", gitHubUsername, REPO_NAME);
git.importJavaApp(repoUrl, PROJECT_NAME, MAVEN);
git.importJavaApp(testRepo.getHtmlUrl(), PROJECT_NAME, MAVEN);
// change files in the test repo on GitHub
changeContentOnGithubSide(
@ -161,9 +151,7 @@ public class GitPullConflictTest {
}
private void changeContentOnGithubSide(String pathToContent, String content) throws IOException {
gitHubRepository
.getFileContent(String.format("/%s", pathToContent))
.update(content, "add " + content);
testRepo.getFileContent(String.format("/%s", pathToContent)).update(content, "add " + content);
}
private void performPull() {

View File

@ -22,6 +22,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.che.commons.lang.NameGenerator;
import org.eclipse.che.selenium.core.TestGroup;
import org.eclipse.che.selenium.core.client.TestGitHubRepository;
import org.eclipse.che.selenium.core.client.TestGitHubServiceClient;
import org.eclipse.che.selenium.core.client.TestSshServiceClient;
import org.eclipse.che.selenium.core.client.TestUserPreferencesServiceClient;
@ -34,9 +35,6 @@ import org.eclipse.che.selenium.pageobject.Ide;
import org.eclipse.che.selenium.pageobject.Loader;
import org.eclipse.che.selenium.pageobject.Menu;
import org.eclipse.che.selenium.pageobject.ProjectExplorer;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -45,22 +43,16 @@ import org.testng.annotations.Test;
@Test(groups = TestGroup.GITHUB)
public class GitPullTest {
private static final String PROJECT_NAME = NameGenerator.generate("FirstProject-", 4);
private static final String REPO_NAME = NameGenerator.generate("GitPullTest", 3);
private GitHub gitHub;
private GHRepository gitHubRepository;
@Inject private TestWorkspace ws;
@Inject private Ide ide;
@Inject private TestUser productUser;
@Inject private TestGitHubRepository testRepo;
@Inject(optional = true)
@Named("github.username")
private String gitHubUsername;
@Inject(optional = true)
@Named("github.password")
private String gitHubPassword;
@Inject private ProjectExplorer projectExplorer;
@Inject private Menu menu;
@Inject private org.eclipse.che.selenium.pageobject.git.Git git;
@ -74,18 +66,18 @@ public class GitPullTest {
@BeforeClass
public void prepare() throws Exception {
gitHub = GitHub.connectUsingPassword(gitHubUsername, gitHubPassword);
gitHubRepository = gitHub.createRepository(REPO_NAME).create();
String commitMess = String.format("new_content_was_added %s ", System.currentTimeMillis());
testUserPreferencesServiceClient.addGitCommitter(gitHubUsername, productUser.getEmail());
Path entryPath = Paths.get(getClass().getResource("/projects/git-pull-test").getPath());
gitHubClientService.addContentToRepository(entryPath, commitMess, gitHubRepository);
testRepo.addContent(entryPath, commitMess);
ide.open(ws);
}
@AfterClass
public void deleteRepo() throws IOException {
gitHubRepository.delete();
testRepo.delete();
}
@Test
@ -97,12 +89,11 @@ public class GitPullTest {
String currentTimeInMillis = Long.toString(System.currentTimeMillis());
projectExplorer.waitProjectExplorer();
String repoUrl = String.format("https://github.com/%s/%s.git", gitHubUsername, REPO_NAME);
git.importJavaApp(repoUrl, PROJECT_NAME, BLANK);
git.importJavaApp(testRepo.getHtmlUrl(), PROJECT_NAME, BLANK);
prepareFilesForTest(jsFileName);
prepareFilesForTest(htmlFileName);
prepareFilesForTest("plain-files/" + readmeTxtFileName);
prepareFilesForTest(folderWithPlainFilesPath + "/" + readmeTxtFileName);
changeContentOnGithubSide(jsFileName, currentTimeInMillis);
changeContentOnGithubSide(htmlFileName, currentTimeInMillis);
@ -111,17 +102,15 @@ public class GitPullTest {
performPull();
git.waitGitStatusBarWithMess("Successfully pulled");
git.waitGitStatusBarWithMess(
String.format("from https://github.com/%s/%s.git", gitHubUsername, REPO_NAME));
String.format("Successfully pulled from %s", testRepo.getHtmlUrl()));
checkPullAfterUpdatingContent(readmeTxtFileName, currentTimeInMillis);
checkPullAfterUpdatingContent(htmlFileName, currentTimeInMillis);
checkPullAfterUpdatingContent(readmeTxtFileName, currentTimeInMillis);
for (GHContent ghContent : gitHubRepository.getDirectoryContent(folderWithPlainFilesPath)) {
ghContent.delete("remove file " + ghContent.getName());
}
testRepo.deleteFolder(Paths.get(folderWithPlainFilesPath), "remove file");
performPull();
checkPullAfterRemovingContent(
readmeTxtFileName,
@ -156,7 +145,7 @@ public class GitPullTest {
}
private void changeContentOnGithubSide(String pathToContent, String content) throws IOException {
gitHubRepository
testRepo
.getFileContent(String.format("/%s", pathToContent))
.update(content, "add " + NameGenerator.generate(content, 3));
}