Quick hack to get latest commit
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 2m54s

This commit is contained in:
Robert Marshall 2025-07-05 20:48:30 +01:00
parent fdec73d5e3
commit 1cc884bec3
8 changed files with 36 additions and 23 deletions

View file

@ -2,6 +2,7 @@
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using NSubstitute;
using System;
using Website.Controllers;
using Website.Data;
using Website.Models.Blog;
@ -17,15 +18,16 @@ namespace Website.Tests.Controllers {
var blogApi = Substitute.For<IBlogApi>();
var gitApi = Substitute.For<IGitApi>();
blogApi.GetLatestPostAsync().Returns(new BlogPost{Content = ""});
blogApi.GetLatestPostAsync().Returns(new BlogPost { Content = "" });
gitApi.GetRepositories("rob").Returns(new[] {new Repository()});
gitApi.GetBranches("rob", null).Returns(new[] {new Branch {Commit = new Commit()}});
var repo = new Repository();
gitApi.GetRepositories("rob").Returns(new[] { repo });
gitApi.GetBranches("rob", repo).Returns(new[] { new Branch { Commit = new() { Timestamp = DateTimeOffset.Now }, Repository = repo} });
gitApi.GetCommit("rob", null, null).Returns(new Commit());
var expectation = new HomeViewModel {
BlogPost = new BlogPostViewModel(new BlogPost{Content = ""}, false),
GitCommit = new GitCommitViewModel(new Repository(), new Branch{Commit = new Commit()}, new Commit())
BlogPost = new BlogPostViewModel(new BlogPost { Content = "" }, false),
GitCommit = new GitCommitViewModel(repo, new Branch { Commit = new Commit() }, new Commit())
};
var controller = new HomeController(blogApi, gitApi);

View file

@ -42,6 +42,7 @@ namespace Website.Tests.Data {
var cache = Substitute.For<IMemoryCache>();
var repo = new Repository { Name = "repo" };
var expectation = new[] {
new Branch {
Name = "master",
@ -49,12 +50,13 @@ namespace Website.Tests.Data {
Id = "0923b554309ef562fca978c7e981b3812bc4af40",
Message = "message",
Timestamp = new DateTimeOffset(2020, 04, 11, 14, 09, 29, TimeSpan.FromHours(1))
}
},
Repository = repo
}
};
var api = new GitApi(httpClient, cache, new CacheDurations());
(await api.GetBranches("test", "repo")).Should().BeEquivalentTo(expectation);
(await api.GetBranches("test", repo)).Should().BeEquivalentTo(expectation);
}
[Fact]

View file

@ -17,16 +17,21 @@ namespace Website.Controllers {
public async Task<IActionResult> Index() {
var post = await _blogApi.GetLatestPostAsync();
var repo = (await _gitApi.GetRepositories("rob")).First();
var branch = (await _gitApi.GetBranches("rob", repo.Name)).First();
var commit = await _gitApi.GetCommit("rob", repo.Name, branch.Commit.Id);
var repos = await _gitApi.GetRepositories("rob");
var branchTasks = repos.Select(repo => _gitApi.GetBranches("rob", repo));
var branchResults = await Task.WhenAll(branchTasks);
var branches = branchResults.SelectMany(branch => branch);
var branch = branches.OrderByDescending(branch => branch.Commit.Timestamp).First();
var commit = await _gitApi.GetCommit("rob", branch.Repository.Name, branch.Commit.Id);
var model = new HomeViewModel {
BlogPost = new BlogPostViewModel(post, false),
GitCommit = new GitCommitViewModel(repo, branch, commit)
GitCommit = new GitCommitViewModel(branch.Repository, branch, commit)
};
return View(model);
}
}
}
}

View file

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Robware.Lib.ApiClient;
using System;
using System.Linq;
using Website.Models.Git;
namespace Website.Data {
@ -22,16 +23,18 @@ namespace Website.Data {
}
}
public async Task<IEnumerable<Branch>> GetBranches(string user, string repository) {
public async Task<IEnumerable<Branch>> GetBranches(string user, Repository repository) {
try {
return await Get<IEnumerable<Branch>>("branches", new { user, repository });
var branches = await Get<IEnumerable<Branch>>("branches", new { user, repository = repository.Name });
return branches.Select(branch => branch with { Repository = repository });
}
catch (Exception e) {
Console.WriteLine(e);
return [
new() {
Name = "Unable to get branches",
Commit = new() { Url = "https://git.robware.uk", Id = "000000", Message = "Unable to get commit", Timestamp = DateTimeOffset.Now }
Commit = new() { Url = "https://git.robware.uk", Id = "000000", Message = "Unable to get commit", Timestamp = DateTimeOffset.Now },
Repository = repository,
}
];
}
@ -47,4 +50,4 @@ namespace Website.Data {
}
}
}
}
}

View file

@ -5,7 +5,7 @@ using Website.Models.Git;
namespace Website.Data {
public interface IGitApi {
Task<IEnumerable<Repository>> GetRepositories(string user);
Task<IEnumerable<Branch>> GetBranches(string user, string repository);
Task<IEnumerable<Branch>> GetBranches(string user, Repository repository);
Task<Commit> GetCommit(string user, string repository, string hash);
}
}

View file

@ -1,6 +1,7 @@
namespace Website.Models.Git {
public class Branch {
public record Branch {
public string Name { get; set; }
public Commit Commit { get; set; }
public Repository Repository { get; set; }
}
}
}

View file

@ -1,10 +1,10 @@
using System;
namespace Website.Models.Git {
public class Commit {
public record Commit {
public string Id { get; set; }
public string Message { get; set; }
public DateTimeOffset Timestamp { get; set; }
public string Url { get; set; }
}
}
}

View file

@ -1,6 +1,6 @@
namespace Website.Models.Git {
public class Repository {
public record Repository {
public string Name { get; set; }
public string Url { get; set; }
}
}
}