Get the latest commit and show it on the home page

This commit is contained in:
Robert Marshall 2020-04-05 07:42:22 +01:00
parent ffb3e791ab
commit 5d67e92245
23 changed files with 233 additions and 60 deletions

View file

@ -2,22 +2,29 @@
using Microsoft.AspNetCore.Mvc;
using Website.Data;
using Website.ViewModels;
using System.Linq;
namespace Website.Controllers
{
public class HomeController : Controller
{
namespace Website.Controllers {
public class HomeController : Controller {
private readonly BlogRepository _blogRepo;
private readonly GitServerApi _api;
public HomeController(BlogRepository blogRepo)
{
public HomeController(BlogRepository blogRepo, GitServerApi api) {
_api = api;
_blogRepo = blogRepo;
}
public async Task<IActionResult> Index()
{
public async Task<IActionResult> Index() {
var post = await _blogRepo.GetLatestPostAsync();
var model = new BlogPostViewModel(post, false);
var repo = (await _api.GetRepositories()).First();
var branch = (await _api.GetBranches(repo.Name)).First();
var commit = await _api.GetCommit(repo.Name, branch.Commit.Id);
var model = new HomeViewModel {
BlogPost = new BlogPostViewModel(post, false),
GitCommit = new GitCommitViewModel(repo, branch, commit)
};
return View(model);
}
}

View file

@ -3,8 +3,8 @@ using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Website.Data.States;
using Website.Models;
using Website.Data.States.Git;
using Website.Models.Git;
namespace Website.Data {
public class GitServerApi {
@ -18,10 +18,22 @@ namespace Website.Data {
_token = token;
}
public async Task<IEnumerable<GitRepository>> GetRepositories() {
public async Task<IEnumerable<Repository>> GetRepositories() {
var response = await _client.GetStringAsync($"https://{_domain}/api/v1/users/rob/repos?token={_token}");
var states = JsonConvert.DeserializeObject<IEnumerable<GitRepositoryState>>(response);
return states.Select(state => new GitRepository(state));
var states = JsonConvert.DeserializeObject<IEnumerable<RepositoryState>>(response);
return states.Select(state => new Repository(state));
}
public async Task<IEnumerable<Branch>> GetBranches(string repository) {
var response = await _client.GetStringAsync($"https://{_domain}/api/v1/repos/rob/{repository}/branches?token={_token}");
var states = JsonConvert.DeserializeObject<IEnumerable<BranchState>>(response);
return states.Select(state => new Branch(state));
}
public async Task<Commit> GetCommit(string repository, string hash) {
var response = await _client.GetStringAsync($"https://{_domain}/api/v1/repos/rob/{repository}/commits/{hash}?token={_token}");
var state = JsonConvert.DeserializeObject<CommitDetailsState>(response);
return new Commit(state);
}
}
}

View file

@ -0,0 +1,10 @@
using System;
namespace Website.Data.States.Git {
public class AuthorState {
public string name { get; set; }
public string email { get; set; }
public string username { get; set; }
public DateTimeOffset date { get; set; }
}
}

View file

@ -0,0 +1,7 @@
namespace Website.Data.States.Git {
public class BranchState
{
public string name { get; set; }
public CommitState commit { get; set; }
}
}

View file

@ -0,0 +1,11 @@
namespace Website.Data.States.Git {
public class CommitDetailsState {
public string url { get; set; }
public string sha { get; set; }
public string html_url { get; set; }
public CommitState commit { get; set; }
public UserState author { get; set; }
public UserState committer { get; set; }
public CommitPointerState[] parents { get; set; }
}
}

View file

@ -0,0 +1,6 @@
namespace Website.Data.States.Git {
public partial class CommitPointerState {
public string url { get; set; }
public string sha { get; set; }
}
}

View file

@ -0,0 +1,16 @@
using System;
namespace Website.Data.States.Git {
public class CommitState {
public string id { get; set; }
public string message { get; set; }
public string url { get; set; }
public AuthorState author { get; set; }
public AuthorState committer { get; set; }
public object added { get; set; }
public object removed { get; set; }
public object modified { get; set; }
public DateTimeOffset timestamp { get; set; }
public CommitPointerState tree { get; set; }
}
}

View file

@ -1,5 +1,5 @@
namespace Website.Data.States {
public class GitRepositoryOwnerState {
namespace Website.Data.States.Git {
public class RepositoryOwnerState {
public int id { get; set; }
public string username { get; set; }
public string login { get; set; }

View file

@ -1,5 +1,5 @@
namespace Website.Data.States {
public class GitRepositoryPermissionsState {
namespace Website.Data.States.Git {
public class RepositoryPermissionsState {
public bool admin { get; set; }
public bool push { get; set; }
public bool pull { get; set; }

View file

@ -1,9 +1,9 @@
using System;
namespace Website.Data.States {
public class GitRepositoryState {
namespace Website.Data.States.Git {
public class RepositoryState {
public int id { get; set; }
public GitRepositoryOwnerState owner { get; set; }
public RepositoryOwnerState owner { get; set; }
public string name { get; set; }
public string full_name { get; set; }
public string description { get; set; }
@ -24,7 +24,7 @@ namespace Website.Data.States {
public string default_branch { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
public GitRepositoryPermissionsState permissions { get; set; }
public RepositoryPermissionsState permissions { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace Website.Data.States.Git {
public partial class UserState {
public long id { get; set; }
public string username { get; set; }
public string login { get; set; }
public string full_name { get; set; }
public string email { get; set; }
public string avatar_url { get; set; }
}
}

View file

@ -0,0 +1,25 @@
using System;
namespace Website.Extensions {
public static class DateTimeExtensions {
private static string GetDaySuffix(int day) {
switch (day) {
case 1:
case 21:
case 31:
return @"\s\t";
case 2:
case 22:
return @"\n\d";
case 3:
case 23:
return @"\r\d";
default:
return @"\t\h";
}
}
public static string GetDaySuffix(this DateTime dateTime) => GetDaySuffix(dateTime.Day);
public static string GetDaySuffix(this DateTimeOffset dateTimeOffset) => GetDaySuffix(dateTimeOffset.Day);
}
}

View file

@ -0,0 +1,14 @@
using Website.Data.States.Git;
namespace Website.Models.Git {
public class Branch {
public Branch(BranchState state) {
Name = state.name;
Commit = new Commit(state.commit);
}
public string Name { get; }
public Commit Commit { get; private set; }
}
}

View file

@ -0,0 +1,24 @@
using System;
using Website.Data.States.Git;
namespace Website.Models.Git {
public class Commit {
public Commit(CommitState commit) {
Id = commit.id;
Message = commit.message;
Timestamp = commit.timestamp;
}
public Commit(CommitDetailsState commit) {
Id = commit.sha;
Message = commit.commit.message;
Timestamp = commit.commit.author.date;
Url = commit.html_url.Replace("/commits/", "/commit/");
}
public string Id { get; }
public string Message { get; }
public DateTimeOffset Timestamp { get; }
public string Url { get; }
}
}

View file

@ -0,0 +1,13 @@
using Website.Data.States.Git;
namespace Website.Models.Git {
public class Repository {
public Repository(RepositoryState state) {
Name = state.name;
Url = state.html_url;
}
public string Name { get; set; }
public string Url { get; set; }
}
}

View file

@ -1,13 +0,0 @@
using Website.Data.States;
namespace Website.Models {
public class GitRepository {
public GitRepository(GitRepositoryState state) {
Name = state.name;
Url = state.html_url;
}
public string Name { get; set; }
public string Url { get; set; }
}
}

View file

@ -1,6 +1,7 @@
using System;
using Markdig;
using Pek.Markdig.HighlightJs;
using Website.Extensions;
using Website.Models;
namespace Website.ViewModels
@ -15,27 +16,7 @@ namespace Website.ViewModels
Content = preview ? blogPost.Draft : blogPost.Content;
}
private static string GetDaySuffix(int day) {
switch (day) {
case 1:
case 21:
case 31:
return @"\s\t";
case 2:
case 22:
return @"\n\d";
case 3:
case 23:
return @"\r\d";
default:
return @"\t\h";
}
}
private static string FormatTimestamp(DateTime timestamp) {
var suffix = GetDaySuffix(timestamp.Day);
return timestamp.ToString($@"dddd \t\h\e d{suffix} \o\f MMMM yyyy");
}
private static string FormatTimestamp(DateTime timestamp) => timestamp.ToString($@"dddd \t\h\e d{timestamp.GetDaySuffix()} \o\f MMMM yyyy");
private static MarkdownPipeline GetPipeline() => new MarkdownPipelineBuilder()
//.UseAdvancedExtensions()

View file

@ -0,0 +1,27 @@
using System;
using Website.Extensions;
using Website.Models.Git;
namespace Website.ViewModels {
public class GitCommitViewModel {
public GitCommitViewModel(Repository repo, Branch branch, Commit commit) {
RepositoryName = repo.Name;
RepositoryUrl = repo.Url;
BranchName = branch.Name;
Hash = commit.Id;
CommitUrl = commit.Url;
CommitMessage = commit.Message;
Timestamp = FormatTimestamp(commit.Timestamp);
}
private static string FormatTimestamp(DateTimeOffset timestamp) => timestamp.ToString($@"HH:mm:ss on dddd \t\h\e d{timestamp.GetDaySuffix()} \o\f MMMM yyyy");
public string RepositoryName { get; }
public string RepositoryUrl { get; }
public string BranchName { get; }
public string Hash { get; }
public string CommitUrl { get; }
public string CommitMessage { get; }
public string Timestamp { get; }
}
}

View file

@ -0,0 +1,6 @@
namespace Website.ViewModels {
public class HomeViewModel {
public BlogPostViewModel BlogPost { get; set; }
public GitCommitViewModel GitCommit { get; internal set; }
}
}

View file

@ -1,4 +1,5 @@
@model BlogPostViewModel
@using Website.Extensions
@model HomeViewModel
@{
ViewData["Title"] = "Welcome";
@ -7,7 +8,17 @@
<p>Hello, I'm Rob. I'm a senior software engineer at <a href="https://www.codecomputerlove.com/">Code Computerlove</a> where I focus on back end development primarily using C#. In my spare time I spend my time riding bikes or making stuff, typically involving an Arduino.</p>
<p>This website is primarily an outlet for me to write about things which have been technically challenging, either in a professional or personal capacity, though not limited to that.</p>
<p>If you wish to get in contact, then get in touch via my <a href="http://uk.linkedin.com/in/robertmarshall/"><img alt="LinkedIn profile" src="https://static.licdn.com/scds/common/u/img/webpromo/btn_liprofile_blue_80x15.png" title=""></a>.</p>
<h2>Latest code commit</h2>
<table class="commit-table">
<tr><td>Repository</td><td><a href="@Model.GitCommit.RepositoryUrl">@Model.GitCommit.RepositoryName</a></td></tr>
<tr><td>Branch</td><td>@Model.GitCommit.BranchName</td></tr>
<tr><td>SHA</td><td><a href="@Model.GitCommit.CommitUrl">@Model.GitCommit.Hash</a></td></tr>
<tr><td>Message</td><td>@Model.GitCommit.CommitMessage</td></tr>
<tr><td>Timestamp</td><td>@Model.GitCommit.Timestamp</td></tr>
</table>
<h2>Latest Blog Post</h2>
<h3><a href="/blog/view/@Model.Url">@Model.Title</a></h3>
<partial name="~/Views/Blog/Entry.cshtml" , model="Model" />
<h3><a href="/blog/view/@Model.BlogPost.Url">@Model.BlogPost.Title</a></h3>
<partial name="~/Views/Blog/Entry.cshtml" , model="Model.BlogPost" />
<p><a href="/blog">View more</a></p>

View file

@ -1,4 +1,4 @@
@model IEnumerable<Website.Models.GitRepository>
@model IEnumerable<Website.Models.Git.Repository>
<h5>Projects:</h5>
<ul class="nav-list">

View file

@ -0,0 +1,5 @@
.commit-table {
td:first-child(){
font-weight: bold;
}
}

View file

@ -214,6 +214,7 @@ form {
}
@import "blog.scss";
@import "home.scss";
@media screen and (max-width: 992px) {