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;
|
|
|
|
|
import sonia.scm.repository.Changeset;
|
|
|
|
|
import sonia.scm.repository.Person;
|
|
|
|
|
import sonia.scm.repository.client.api.ClientCommand;
|
|
|
|
|
import sonia.scm.repository.client.api.RepositoryClient;
|
|
|
|
|
import sonia.scm.repository.client.api.RepositoryClientFactory;
|
|
|
|
|
import sonia.scm.web.VndMediaType;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
import static sonia.scm.it.RestUtil.given;
|
|
|
|
|
|
|
|
|
|
public class RepositoryUtil {
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static RepositoryClient createRepositoryClient(String repositoryType, File folder, String username, String password) throws IOException {
|
|
|
|
|
String httpProtocolUrl = given(VndMediaType.REPOSITORY, username, password)
|
2018-08-15 11:44:20 +02:00
|
|
|
|
|
|
|
|
.when()
|
|
|
|
|
.get(TestData.getDefaultRepositoryUrl(repositoryType))
|
|
|
|
|
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_OK)
|
|
|
|
|
.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-22 09:18:17 +02:00
|
|
|
}
|
2018-08-22 10:59:46 +02:00
|
|
|
|
|
|
|
|
static void createAndCommitFile(RepositoryClient repositoryClient, String username, String fileName, String content) throws IOException {
|
|
|
|
|
Files.write(content, new File(repositoryClient.getWorkingCopy(), fileName), Charsets.UTF_8);
|
2018-08-15 11:44:20 +02:00
|
|
|
repositoryClient.getAddCommand().add(fileName);
|
2018-08-22 09:18:17 +02:00
|
|
|
commit(repositoryClient, username, "added " + fileName);
|
2018-08-15 11:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 09:18:17 +02:00
|
|
|
static Changeset commit(RepositoryClient repositoryClient, String username, String message) throws IOException {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|