using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace TCDependencyManager { public class GitHubAPI { private const string BaseUrl = "https://api.github.com/"; private readonly string _oauthToken; public GitHubAPI(string oauthToken) { _oauthToken = oauthToken; } public List GetRepos() { using (var client = GetClient()) { var response = client.GetAsync("orgs/aspnet/repos?page=1&per_page=100").Result; return response.EnsureSuccessStatusCode() .Content .ReadAsAsync>().Result; } } public List GetProjects(Repository repo) { IEnumerable projectNames = null; using (var client = GetClient()) { string path = string.Format("/repos/aspnet/{0}/contents/src?ref=dev", repo.Name); var response = client.GetAsync(path).Result; if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsAsync().Result; projectNames = result.Select(r => r["name"].Value()); } else { projectNames = Enumerable.Empty(); } } return projectNames .AsParallel() .Select(p => new Project { Repo = repo, ProjectName = p, Dependencies = ReadDependencies(repo, p) }) .Where(p => p.Dependencies != null) .ToList(); } private List ReadDependencies(Repository repo, string project) { using (var client = GetClient()) { string path = string.Format("/repos/aspnet/{0}/contents/src/{1}/project.json?ref=dev", repo.Name, project); var response = client.GetAsync(path).Result; if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsAsync().Result; var content = JsonConvert.DeserializeObject( Encoding.UTF8.GetString( Convert.FromBase64String(result["content"].Value()))); // Ignore shared repos since they can have the same names if (content["shared"] != null) { return null; } var dependencies = (JObject)content["dependencies"]; if (dependencies != null) { return dependencies.Cast() .Where(prop => !String.IsNullOrEmpty(prop.Value.Value())) .Select(prop => prop.Name) .ToList(); } return new List(0); } } // Ignore directories that do not have a project.json return null; } private HttpClient GetClient() { var client = new HttpClient { BaseAddress = new Uri(BaseUrl) }; client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("AspNet-CI", "1.0")); client.DefaultRequestHeaders.Add("Authorization", "token " + _oauthToken); return client; } } }