2018-08-15 11:44:20 +02:00
|
|
|
package sonia.scm.it;
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Charsets;
|
|
|
|
|
import com.google.common.io.Files;
|
|
|
|
|
import org.apache.http.HttpStatus;
|
2018-09-05 10:44:03 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-08-15 11:44:20 +02:00
|
|
|
import sonia.scm.repository.Changeset;
|
|
|
|
|
import sonia.scm.repository.Person;
|
2018-09-04 17:38:28 +02:00
|
|
|
import sonia.scm.repository.Tag;
|
2018-08-15 11:44:20 +02:00
|
|
|
import sonia.scm.repository.client.api.ClientCommand;
|
|
|
|
|
import sonia.scm.repository.client.api.RepositoryClient;
|
|
|
|
|
import sonia.scm.repository.client.api.RepositoryClientFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
2018-08-22 09:18:17 +02:00
|
|
|
import java.util.UUID;
|
2018-08-15 11:44:20 +02:00
|
|
|
|
|
|
|
|
public class RepositoryUtil {
|
|
|
|
|
|
2018-09-05 10:44:03 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(RepositoryUtil.class);
|
|
|
|
|
|
2018-08-15 11:44:20 +02:00
|
|
|
private static final RepositoryClientFactory REPOSITORY_CLIENT_FACTORY = new RepositoryClientFactory();
|
|
|
|
|
|
|
|
|
|
static RepositoryClient createRepositoryClient(String repositoryType, File folder) throws IOException {
|
2018-08-22 09:18:17 +02:00
|
|
|
return createRepositoryClient(repositoryType, folder, "scmadmin", "scmadmin");
|
|
|
|
|
}
|
2018-08-15 11:44:20 +02:00
|
|
|
|
2018-08-22 09:18:17 +02:00
|
|
|
static RepositoryClient createRepositoryClient(String repositoryType, File folder, String username, String password) throws IOException {
|
2018-08-22 11:19:19 +02:00
|
|
|
String httpProtocolUrl = TestData.callRepository(username, password, repositoryType, HttpStatus.SC_OK)
|
2018-08-15 11:44:20 +02:00
|
|
|
.extract()
|
|
|
|
|
.path("_links.httpProtocol.href");
|
|
|
|
|
|
2018-08-22 09:18:17 +02:00
|
|
|
return REPOSITORY_CLIENT_FACTORY.create(repositoryType, httpProtocolUrl, username, password, folder);
|
2018-08-15 11:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 10:59:46 +02:00
|
|
|
static String addAndCommitRandomFile(RepositoryClient client, String username) throws IOException {
|
|
|
|
|
String uuid = UUID.randomUUID().toString();
|
|
|
|
|
String name = "file-" + uuid + ".uuid";
|
|
|
|
|
createAndCommitFile(client, username, name, uuid);
|
|
|
|
|
return name;
|
2018-08-15 11:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-10 09:51:59 +02:00
|
|
|
static Changeset createAndCommitFile(RepositoryClient repositoryClient, String username, String fileName, String content) throws IOException {
|
2018-08-28 10:02:16 +02:00
|
|
|
File file = new File(repositoryClient.getWorkingCopy(), fileName);
|
2018-09-10 11:13:36 +02:00
|
|
|
Files.createParentDirs(file);
|
2018-08-23 15:52:02 +02:00
|
|
|
Files.write(content, file, Charsets.UTF_8);
|
2018-08-28 10:02:16 +02:00
|
|
|
addWithParentDirectories(repositoryClient, file);
|
2018-09-10 09:51:59 +02:00
|
|
|
return commit(repositoryClient, username, "added " + fileName);
|
2018-08-15 11:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-28 10:02:16 +02:00
|
|
|
private static String addWithParentDirectories(RepositoryClient repositoryClient, File file) throws IOException {
|
2018-08-23 15:52:02 +02:00
|
|
|
File parent = file.getParentFile();
|
|
|
|
|
String thisName = file.getName();
|
|
|
|
|
String path;
|
2018-08-28 10:02:16 +02:00
|
|
|
if (!repositoryClient.getWorkingCopy().equals(parent)) {
|
|
|
|
|
path = addWithParentDirectories(repositoryClient, parent) + File.separator + thisName;
|
2018-08-23 15:52:02 +02:00
|
|
|
} else {
|
|
|
|
|
path = thisName;
|
|
|
|
|
}
|
|
|
|
|
repositoryClient.getAddCommand().add(path);
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 09:18:17 +02:00
|
|
|
static Changeset commit(RepositoryClient repositoryClient, String username, String message) throws IOException {
|
2018-09-05 10:44:03 +02:00
|
|
|
LOG.info("user: {} try to commit with message: {}", username, message);
|
2018-08-22 09:18:17 +02:00
|
|
|
Changeset changeset = repositoryClient.getCommitCommand().commit(new Person(username, username + "@scm-manager.org"), message);
|
2018-08-15 11:44:20 +02:00
|
|
|
if (repositoryClient.isCommandSupported(ClientCommand.PUSH)) {
|
|
|
|
|
repositoryClient.getPushCommand().push();
|
|
|
|
|
}
|
|
|
|
|
return changeset;
|
|
|
|
|
}
|
2018-09-04 17:38:28 +02:00
|
|
|
|
|
|
|
|
static Tag addTag(RepositoryClient repositoryClient, String revision, String tagName) throws IOException {
|
|
|
|
|
if (repositoryClient.isCommandSupported(ClientCommand.TAG)) {
|
|
|
|
|
Tag tag = repositoryClient.getTagCommand().setRevision(revision).tag(tagName, TestData.USER_SCM_ADMIN);
|
|
|
|
|
if (repositoryClient.isCommandSupported(ClientCommand.PUSH)) {
|
|
|
|
|
repositoryClient.getPushCommand().pushTags();
|
|
|
|
|
}
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-08-15 11:44:20 +02:00
|
|
|
}
|