From 292d7729f29d6e6a303e8ade2b9d1ddc1fcce80a Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Fri, 3 Jan 2020 17:19:50 +0000 Subject: [PATCH] Get git projects from Gogs --- Website/Data/GitServerApi.cs | 27 ++++++++++++++++ .../Data/States/GitRepositoryOwnerState.cs | 10 ++++++ .../States/GitRepositoryPermissionsState.cs | 7 +++++ Website/Data/States/GitRepositoryState.cs | 31 +++++++++++++++++++ Website/Models/GitRepository.cs | 13 ++++++++ Website/Startup.cs | 8 +++-- .../ProjectNavigationViewComponent.cs | 19 ++++++++++++ .../ProjectNavigation/Default.cshtml | 9 ++++++ Website/Views/Shared/_Layout.cshtml | 8 +---- 9 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 Website/Data/GitServerApi.cs create mode 100644 Website/Data/States/GitRepositoryOwnerState.cs create mode 100644 Website/Data/States/GitRepositoryPermissionsState.cs create mode 100644 Website/Data/States/GitRepositoryState.cs create mode 100644 Website/Models/GitRepository.cs create mode 100644 Website/ViewComponents/ProjectNavigationViewComponent.cs create mode 100644 Website/Views/Shared/Components/ProjectNavigation/Default.cshtml diff --git a/Website/Data/GitServerApi.cs b/Website/Data/GitServerApi.cs new file mode 100644 index 0000000..06b4aa2 --- /dev/null +++ b/Website/Data/GitServerApi.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Website.Data.States; +using Website.Models; + +namespace Website.Data { + public class GitServerApi { + private readonly HttpClient _client; + private readonly string _domain; + private readonly string _token; + + public GitServerApi(HttpClient client, string domain, string token) { + _client = client; + _domain = domain; + _token = token; + } + + public async Task> GetRepositories() { + var response = await _client.GetStringAsync($"https://{_domain}/api/v1/users/rob/repos?token={_token}"); + var states = JsonConvert.DeserializeObject>(response); + return states.Select(state => new GitRepository(state)); + } + } +} \ No newline at end of file diff --git a/Website/Data/States/GitRepositoryOwnerState.cs b/Website/Data/States/GitRepositoryOwnerState.cs new file mode 100644 index 0000000..85e5b20 --- /dev/null +++ b/Website/Data/States/GitRepositoryOwnerState.cs @@ -0,0 +1,10 @@ +namespace Website.Data.States { + public class GitRepositoryOwnerState { + public int 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; } + } +} \ No newline at end of file diff --git a/Website/Data/States/GitRepositoryPermissionsState.cs b/Website/Data/States/GitRepositoryPermissionsState.cs new file mode 100644 index 0000000..0b8d492 --- /dev/null +++ b/Website/Data/States/GitRepositoryPermissionsState.cs @@ -0,0 +1,7 @@ +namespace Website.Data.States { + public class GitRepositoryPermissionsState { + public bool admin { get; set; } + public bool push { get; set; } + public bool pull { get; set; } + } +} \ No newline at end of file diff --git a/Website/Data/States/GitRepositoryState.cs b/Website/Data/States/GitRepositoryState.cs new file mode 100644 index 0000000..74ff9ee --- /dev/null +++ b/Website/Data/States/GitRepositoryState.cs @@ -0,0 +1,31 @@ +using System; + +namespace Website.Data.States { + public class GitRepositoryState { + public int id { get; set; } + public GitRepositoryOwnerState owner { get; set; } + public string name { get; set; } + public string full_name { get; set; } + public string description { get; set; } + public bool _private { get; set; } + public bool fork { get; set; } + public object parent { get; set; } + public bool empty { get; set; } + public bool mirror { get; set; } + public int size { get; set; } + public string html_url { get; set; } + public string ssh_url { get; set; } + public string clone_url { get; set; } + public string website { get; set; } + public int stars_count { get; set; } + public int forks_count { get; set; } + public int watchers_count { get; set; } + public int open_issues_count { get; set; } + public string default_branch { get; set; } + public DateTime created_at { get; set; } + public DateTime updated_at { get; set; } + public GitRepositoryPermissionsState permissions { get; set; } + } +} + + diff --git a/Website/Models/GitRepository.cs b/Website/Models/GitRepository.cs new file mode 100644 index 0000000..32d9719 --- /dev/null +++ b/Website/Models/GitRepository.cs @@ -0,0 +1,13 @@ +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; } + } +} \ No newline at end of file diff --git a/Website/Startup.cs b/Website/Startup.cs index ce3c750..dcae634 100644 --- a/Website/Startup.cs +++ b/Website/Startup.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authentication.Cookies; +using System.Net.Http; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -38,8 +39,9 @@ namespace Website private void RegisterRepositories(IServiceCollection services) => services.AddSingleton() - .AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton() + .AddSingleton(new GitServerApi(new HttpClient(), Configuration["gitDomain"], Configuration["gitToken"])); // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) diff --git a/Website/ViewComponents/ProjectNavigationViewComponent.cs b/Website/ViewComponents/ProjectNavigationViewComponent.cs new file mode 100644 index 0000000..da0beb9 --- /dev/null +++ b/Website/ViewComponents/ProjectNavigationViewComponent.cs @@ -0,0 +1,19 @@ +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Website.Data; + +namespace Website.ViewComponents { + public class ProjectNavigationViewComponent :ViewComponent { + private readonly GitServerApi _api; + + public ProjectNavigationViewComponent(GitServerApi api) { + _api = api; + } + + public async Task InvokeAsync() { + var repositories = await _api.GetRepositories(); + return View(repositories.Take(5)); + } + } +} \ No newline at end of file diff --git a/Website/Views/Shared/Components/ProjectNavigation/Default.cshtml b/Website/Views/Shared/Components/ProjectNavigation/Default.cshtml new file mode 100644 index 0000000..f07abef --- /dev/null +++ b/Website/Views/Shared/Components/ProjectNavigation/Default.cshtml @@ -0,0 +1,9 @@ +@model IEnumerable + +
Projects:
+ \ No newline at end of file diff --git a/Website/Views/Shared/_Layout.cshtml b/Website/Views/Shared/_Layout.cshtml index f5e83b1..fb9c0be 100644 --- a/Website/Views/Shared/_Layout.cshtml +++ b/Website/Views/Shared/_Layout.cshtml @@ -23,13 +23,7 @@ @RenderBody()