diff --git a/OurUmbraco.Site/Views/Partials/Home/GitHubContributors.cshtml b/OurUmbraco.Site/Views/Partials/Home/GitHubContributors.cshtml index 046f20d8..ba3a951e 100644 --- a/OurUmbraco.Site/Views/Partials/Home/GitHubContributors.cshtml +++ b/OurUmbraco.Site/Views/Partials/Home/GitHubContributors.cshtml @@ -4,12 +4,10 @@ { foreach (var contributor in Model.Contributors) { - var author = contributor.Author; - if (author != null) - { - +
- @author.Login + @contributor.AuthorLogin @contributor.TotalCommits @@ -22,7 +20,6 @@
- } }
Load more diff --git a/OurUmbraco/Community/Controllers/GitHubContributorController.cs b/OurUmbraco/Community/Controllers/GitHubContributorController.cs index 4f6d86ea..70673779 100644 --- a/OurUmbraco/Community/Controllers/GitHubContributorController.cs +++ b/OurUmbraco/Community/Controllers/GitHubContributorController.cs @@ -9,6 +9,9 @@ using RestSharp; using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Web.Mvc; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using System.Text; namespace OurUmbraco.Community.Controllers { @@ -27,68 +30,128 @@ namespace OurUmbraco.Community.Controllers "Umbraco.Deploy.ValueConnectors" }; + protected string JsonPath + { + get { return Server.MapPath("~/App_Data/TEMP/GithubContributors.json"); } + } + + public List GetCachedContributors() + { + + if (!System.IO.File.Exists(JsonPath)) throw new Exception("The JSON file doesn't exist on disk"); + + + List temp = new List(); + + // Parse the JSON file from disk + foreach (JToken token in Skybrud.Essentials.Json.JsonUtils.LoadJsonArray(JsonPath)) + { + + temp.Add(token.ToObject()); + + } + + return temp; + + } + /// /// Gets data for all GitHub contributors for all listed Umbraco repositories, /// excluding the GitHub IDs of the HQ contributors from the text file list /// /// - public ActionResult GitHubGetContributorsResult() + public ActionResult GitHubGetContributorsResult(bool force = false) { var model = new GitHubContributorsModel(); + model.Contributors = new List(); + try { - string configPath = Server.MapPath("~/config/githubhq.txt"); - if (!System.IO.File.Exists(configPath)) + + // If the file exists on disk and hasn't expired (AKA not older than one day), we should just load that + if (force == false && System.IO.File.Exists(JsonPath) && System.IO.File.GetLastWriteTimeUtc(JsonPath) >= DateTime.UtcNow.AddDays(-1)) { - LogHelper.Debug("Config file was not found: " + configPath); + model.Contributors = GetCachedContributors(); return PartialView("~/Views/Partials/Home/GitHubContributors.cshtml", model); } - string[] login = System.IO.File.ReadAllLines(configPath).Where(x => x.Trim() != "").Distinct().ToArray(); - var contributors = ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem>("UmbracoGitHubContributors", - () => - { - var githubController = new GitHubController(); - var gitHubContributors = new List(); - foreach (var repo in Repositories) - { - var response = githubController.GetAllRepoContributors(repo); - if (response.StatusCode == HttpStatusCode.OK && - response.ResponseStatus == ResponseStatus.Completed) - { - gitHubContributors.AddRange(response.Data); - } - else - { - LogHelper.Warn(string.Format("Invalid HTTP response for repository {0}", repo)); - } - } + try + { + // Load the contributors via the GitHub API and map to the cached model + var contributors = GetContributors().Select(x => new GitHubCachedGlobalContributorModel(x)); - var filteredContributors = gitHubContributors - .Where(g => !login.Contains(g.Author.Login)) - .GroupBy(g => g.Author.Id) - .OrderByDescending(c => c.Sum(g => g.Total)); + // Serialize the contributors to raw JSON + string rawJson = JsonConvert.SerializeObject(contributors, Formatting.Indented); - List temp = new List(); + // Save the JSON to disk + System.IO.File.WriteAllText(JsonPath, rawJson, Encoding.UTF8); - foreach (var group in filteredContributors) - { - temp.Add(new GitHubGlobalContributorModel(group)); - } + model.Contributors = contributors.ToList(); + } catch (Exception ex) + { + // Log the error so we can debug it later + LogHelper.Error("Unable to load GitHub contributors from the GitHub API", ex); - return temp; + // Load the contributors from the cache (if we have any) + model.Contributors = GetCachedContributors(); + } - }, TimeSpan.FromDays(1)); - - - model.Contributors = contributors; - } - catch (Exception ex) + } catch (Exception ex) { - LogHelper.Error("Could not get GitHub Contributors", ex); + // Log the error so we can debug it later + LogHelper.Error("Unable to load GitHub contributors from the GitHub API", ex); + } return PartialView("~/Views/Partials/Home/GitHubContributors.cshtml", model); } + + public List GetContributors() + { + + + string configPath = Server.MapPath("~/config/githubhq.txt"); + if (!System.IO.File.Exists(configPath)) + { + LogHelper.Debug("Config file was not found: " + configPath); + throw new Exception("Config file was not found: " + configPath); + } + + string[] login = System.IO.File.ReadAllLines(configPath).Where(x => x.Trim() != "").Distinct().ToArray(); + + var githubController = new GitHubController(); + var gitHubContributors = new List(); + foreach (var repo in Repositories) + { + var response = githubController.GetAllRepoContributors(repo); + if (response.StatusCode == HttpStatusCode.OK && + response.ResponseStatus == ResponseStatus.Completed) + { + gitHubContributors.AddRange(response.Data); + } + else + { + LogHelper.Warn(string.Format("Invalid HTTP response for repository {0}", repo)); + throw new Exception(string.Format("Invalid HTTP response for repository {0}", repo)); + } + } + + var filteredContributors = gitHubContributors + .Where(g => !login.Contains(g.Author.Login)) + .GroupBy(g => g.Author.Id) + .OrderByDescending(c => c.Sum(g => g.Total)); + + List temp = new List(); + + foreach (var group in filteredContributors) + { + temp.Add(new GitHubGlobalContributorModel(group)); + } + + return temp; + + + } + } } diff --git a/OurUmbraco/Community/Controllers/GitHubGlobalContributorModel.cs b/OurUmbraco/Community/Controllers/GitHubGlobalContributorModel.cs index a5766ed3..9277bdc0 100644 --- a/OurUmbraco/Community/Controllers/GitHubGlobalContributorModel.cs +++ b/OurUmbraco/Community/Controllers/GitHubGlobalContributorModel.cs @@ -4,6 +4,39 @@ using OurUmbraco.Community.Models; namespace OurUmbraco.Community.Controllers { + + public class GitHubCachedGlobalContributorModel + { + + public int AuthorId { get; set; } + + public string AuthorLogin { get; set; } + + public string AuthorUrl { get; set; } + + public string AuthorAvatarUrl { get; set; } + + public int TotalCommits { get; set; } + + public int TotalAdditions { get; set; } + + public int TotalDeletions { get; set; } + + public GitHubCachedGlobalContributorModel() { } + + public GitHubCachedGlobalContributorModel(GitHubGlobalContributorModel contributor) + { + AuthorId = contributor.Author.Id; + AuthorLogin = contributor.Author.Login; + AuthorUrl = contributor.Author.HtmlUrl; + AuthorAvatarUrl = contributor.Author.AvatarUrl; + TotalCommits = contributor.TotalCommits; + TotalAdditions = contributor.TotalAdditions; + TotalDeletions = contributor.TotalDeletions; + } + + } + public class GitHubGlobalContributorModel { public List Items { get; set; } diff --git a/OurUmbraco/Community/Models/GitHubContributorsModel.cs b/OurUmbraco/Community/Models/GitHubContributorsModel.cs index 592a5524..c8d2405a 100644 --- a/OurUmbraco/Community/Models/GitHubContributorsModel.cs +++ b/OurUmbraco/Community/Models/GitHubContributorsModel.cs @@ -5,6 +5,6 @@ namespace OurUmbraco.Community.Models { public class GitHubContributorsModel : IGitHubContributorsModel { - public List Contributors { get; set; } + public List Contributors { get; set; } } } \ No newline at end of file