package com.cadrlife.devsearch.agent.service.git; import com.cadrlife.devsearch.agent.domain.RepoDescriptor; import com.cadrlife.devsearch.domain.Project; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Strings; import com.google.common.base.Throwables; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class GithubService { private static final Logger LOG = LoggerFactory.getLogger(GithubService.class); private final HttpClient httpClient; private final ObjectMapper objectMapper; private final String apiBaseUrl = "https://api.github.com"; @Inject public GithubService(HttpClient httpClient, ObjectMapper objectMapper) { this.httpClient = httpClient; this.objectMapper = objectMapper; } public List<Project> findAllProjectsForOrg(String org) { return findAllReposByUri(apiBaseUrl + "/orgs/" + org + "/repos"); } public List<Project> findAllProjectsForUser(String user) { return findAllReposByUri(apiBaseUrl + "/users/" + user + "/repos"); } private List<Project> findAllReposByUri(String uri) { HttpGet get = new HttpGet(uri); get.setHeader("User-Agent", "dev-search"); get.setHeader("Accept","application/vnd.github.v3+json"); List<Project> repos = new ArrayList<>(); LOG.info("API Url {}", uri); try { HttpResponse response = httpClient.execute(get); LOG.debug("Got from github: {}", response.getStatusLine()); if (response.getStatusLine().getStatusCode() > 299) { throw new RuntimeException("Got bad response from GitHub, uri '" + uri + " '" + response.getStatusLine()); } String json = EntityUtils.toString(response.getEntity()); LOG.debug("get response {}", json); List<GithubProject> githubResponse = objectMapper.readValue(json, new TypeReference<List<GithubProject>>() { }); for (GithubProject githubProject : githubResponse) { repos.add(new Project() .setName(githubProject.getName()) .setCloneUrl(githubProject.getCloneUrl()) .setDefaultBranch(githubProject.getDefaultBranch())); } // EntityUtils.consume(response.getEntity()); } catch (IOException e) { Throwables.propagate(e); } finally { get.abort(); } return repos; } public static class GithubProject { private String name; @JsonProperty("default_branch") private String defaultBranch; @JsonProperty("git_url") private String cloneUrl; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCloneUrl() { return cloneUrl; } public void setCloneUrl(String cloneUrl) { this.cloneUrl = cloneUrl; } public String getDefaultBranch() { return defaultBranch; } public void setDefaultBranch(String defaultBranch) { this.defaultBranch = defaultBranch; } } }