package com.cadrlife.devsearch.agent.service.stash;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import com.cadrlife.devsearch.agent.service.stash.model.StashJsonParser;
import com.cadrlife.devsearch.agent.service.stash.model.StashProject;
import com.cadrlife.devsearch.domain.Project;
import org.elasticsearch.common.Preconditions;
import org.elasticsearch.common.base.Function;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.collect.Iterables;
public class Stash {
private final String ALL = "limit=" + Short.MAX_VALUE;
private final StashJsonParser parser = new StashJsonParser();
private final StashRestClient stashRestClient;
private final String baseUrl;
@Inject
public Stash(StashRestClient src, @Named("repo.stash.api.base.url") String baseUrl){
this.stashRestClient = src;
this.baseUrl = baseUrl;
}
// public List<StashProject> getStashProjects() {
// String url = baseUrl + "/projects" + "?" + ALL;
// String response = stashRestClient.performGetRequest(url);
// List<StashProject> projects = parser.extractStashProjects(response);
// for (StashProject project : projects){
// project.setRepositories(getRepositories(getRepoName(), project.getKey()));
// }
// return projects;
// }
public List<Project> getRepositories(String repoName, String requestedProjectKey) {
List<Project> repositories = new ArrayList<>();
Preconditions.checkArgument(repoName.endsWith(requestedProjectKey), "repoName " + repoName + " should end with stash key " + requestedProjectKey);
String repoPrefix = repoName.substring(0, repoName.length() - requestedProjectKey.length());
for (String projectKey : resolveProjectKeys(requestedProjectKey)) {
String url = baseUrl + "/projects/" + projectKey + "/repos?" + ALL;
String response = stashRestClient.performGetRequest(url);
repositories.addAll(parser.extractCodeProjects(repoPrefix + projectKey, response));
}
return repositories;
}
private Iterable<String> resolveProjectKeys(String projectKey) {
if ("*".equals(projectKey)) {
return getAllStashProjectKeys();
}
return ImmutableList.of(projectKey);
}
private Iterable<String> getAllStashProjectKeys() {
String url = baseUrl + "/projects" + "?" + ALL;
String response = stashRestClient.performGetRequest(url);
List<StashProject> projects = parser.extractStashProjects(response);
return Iterables.transform(projects, new Function<StashProject, String>() {
@Override
public String apply(StashProject stashProject) {
return stashProject.getKey();
}
});
}
public List<Project> getRepositoriesByName(String repoName, String projectKey, String name){
List<Project> projects = new ArrayList<>();
for (Project stashRepo : getRepositories(repoName, projectKey)){
if(stashRepo.getName().equals(name) ) {
projects.add(stashRepo);
}
}
if (projects.isEmpty()){
throw new RuntimeException("Could not find repo " + name + " in stash project " + projectKey);
}
return projects;
}
// public Date getLastUpdate(String projectKey, String name) {
// String slug = getSlug(projectKey,name);
//
// if(slug.isEmpty()) {
// return null;
// }
//
// String url = baseUrl + "/projects/" + projectKey + "/repos/" + slug + "/commits";
// String response = stashRestClient.performGetRequest(url);
// return parser.extractAuthorTimestamp(response);
//
// }
public String createCloneUri(String projectKey, String slug) {
return String.format("ssh://git@stash:7999/%s/%s.git", projectKey.toLowerCase(), slug);
}
}