Skip to content

Commit

Permalink
Implement GitHub module and apply several changes
Browse files Browse the repository at this point in the history
  • Loading branch information
monambike committed Feb 17, 2024
1 parent fb27d9b commit 3cdc3ed
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 47 deletions.
16 changes: 16 additions & 0 deletions src/Monambike.Core/Data/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Monambike.Core.Data
{
/// <summary>
/// Provides access to configuration settings.
/// </summary>
internal static class Config
{
/// <summary>
/// Gets the GitHub token from the environment variables.
/// </summary>
/// <remarks>
/// The GitHub token is retrieved from the environment variable "GITHUB_REPOSITORIES_TOKEN".
/// </remarks>
internal static string GitHubToken => Environment.GetEnvironmentVariable("GITHUB_REPOSITORIES_TOKEN")!;
}
}
17 changes: 17 additions & 0 deletions src/Monambike.Core/Entities/Certificate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Monambike.Core.Entities
{
internal class Certificate
{
internal required string Name { get; init; }

internal required string Organization { get; init; }

internal DateOnly IssueDate { get; init; }

internal DateOnly ExpirationDate { get; init; }

internal ulong CredentialId { get; init; }

internal string CredentialUrl { get; init; } = null!;
}
}
45 changes: 45 additions & 0 deletions src/Monambike.Core/Entities/GithubRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Drawing;

namespace Monambike.Core.Entities
{
/// <summary>
/// Represents a GitHub repository.
/// </summary>
public class GithubRepository
{
/// <summary>
/// Gets or sets the name of the GitHub repository.
/// </summary>
public required string Name { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the GitHub repository is private.
/// </summary>
public required bool Private { get; set; }

/// <summary>
/// Gets or sets the URL of the GitHub repository.
/// </summary>
public required string Url { get; set; }

/// <summary>
/// Gets or sets the programming language used in the GitHub repository. Can be null.
/// </summary>
public string? Language { get; set; } = null;

/// <summary>
/// Gets or sets the color associated with the programming language of the GitHub repository. Can be null.
/// </summary>
public Color? LanguageColor { get; set; } = null;

/// <summary>
/// Gets or sets the date and time when the GitHub repository was created.
/// </summary>
public required DateTimeOffset CreateDate { get; set; }

/// <summary>
/// Gets or sets the date and time of the last push to the GitHub repository. Can be null.
/// </summary>
public required DateTimeOffset? PushDate { get; set; }
}
}
65 changes: 65 additions & 0 deletions src/Monambike.Core/Models/Github.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Monambike.Core.Data;
using Monambike.Core.Entities;
using Octokit;

namespace Monambike.Core.Models
{
/// <summary>
/// Provides methods for interacting with GitHub repositories.
/// </summary>
public static class Github
{
/// <summary>
/// Gets the product header value for API requests.
/// </summary>
private static ProductHeaderValue ProductHeaderValue => new("monambike-core");

/// <summary>
/// Gets the GitHub client instance for making API requests.
/// </summary>
private static GitHubClient GitHub => new(ProductHeaderValue) { Credentials = new Credentials(Config.GitHubToken) };

/// <summary>
/// Asynchronously retrieves a list of repositories for the current user.
/// </summary>
/// <returns>A task representing the asynchronous operation, containing a list of repositories.</returns>
private static async Task<IReadOnlyList<Repository>> GetRepositories() => await GitHub.Repository.GetAllForCurrent();

public static List<GithubRepository> GetGithubRepositories()
{
// Fetch repositories from GitHub.
var repositories = GetRepositories().Result;

// Fetch language colors from GitHub by a third-party API.
var languageColors = GithubLanguage.GetLanguageColors().Result;

// Initialize a list to store GitHub repositories.
var githubRepositories = new List<GithubRepository>();

// Iterate through each repository to map language colors.
foreach (var repository in repositories)
{
// Retrieve the language color corresponding to the repository's language.
var languageColor = languageColors.SingleOrDefault(language => language.Name == repository.Language);

// Create a GithubRepository object and populate its properties.
var test = new GithubRepository
{
Name = repository.Name,
CreateDate = repository.CreatedAt,
PushDate = repository.PushedAt,
Private = repository.Private,
Url = repository.Url,
Language = languageColor?.Name,
LanguageColor = languageColor?.RgbColor
};

// Add the GithubRepository object to the list.
githubRepositories.Add(test);
}

// Return the GithubRepositories object list.
return githubRepositories;
}
}
}
78 changes: 78 additions & 0 deletions src/Monambike.Core/Models/GithubLanguage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Monambike.Core.Services;
using Newtonsoft.Json;
using System.Drawing;

namespace Monambike.Core.Models
{
/// <summary>
/// Provides functionality to retrieve language colors from GitHub using a third-party.
/// </summary>
internal static class GithubLanguage
{
/// <summary>
/// Gets the HTTP service instance for making requests to third-party API.
/// </summary>
private static HttpService TestHttpService => new("https://raw.githubusercontent.com/ozh/github-colors/master/");

/// <summary>
/// Asynchronously retrieves language colors like from GitHub.
/// </summary>
/// <returns>A list of LanguageColor objects representing language names and their corresponding colors.</returns>
internal static async Task<List<LanguageColor>> GetLanguageColors()
{
// Retrieve JSON data containing language colors from GitHub.
var jsonColors = await TestHttpService.GetAsync<Dictionary<string, JsonColor>>("colors.json");

// Throw ArgumentNullException if the retrieved JSON colors are null.
ArgumentNullException.ThrowIfNull(jsonColors);

// Create the list for holding the LanguageColor objects.
var languageColors = new List<LanguageColor>();

// Convert JSON colors to LanguageColor objects.
foreach (var jsonColor in jsonColors)
languageColors.Add(new LanguageColor
{
Name = jsonColor.Key,
HexColor = jsonColor.Value.Color
});

// Return the list of LanguageColor objects.
return languageColors;
}

/// <summary>
/// Represents the JSON structure for language colors retrieved from GitHub.
/// </summary>
public class JsonColor
{
/// <summary>
/// Gets or initializes the hexadecimal color code.
/// </summary>

[JsonProperty("color")]
public required string Color { get; init; }
}

/// <summary>
/// Represents a programming language with its corresponding color.
/// </summary>
public class LanguageColor
{
/// <summary>
/// Gets or initializes the name of the programming language.
/// </summary>
public required string Name { get; init; }

/// <summary>
/// Gets or initializes the hexadecimal color code.
/// </summary>
public required string HexColor { get; init; }

/// <summary>
/// Gets the RGB color equivalent of the hexadecimal color code.
/// </summary>
public Color RgbColor => ColorTranslator.FromHtml(HexColor);
}
}
}
40 changes: 0 additions & 40 deletions src/Monambike.Core/Models/GithubProjects.cs

This file was deleted.

2 changes: 2 additions & 0 deletions src/Monambike.Core/Monambike.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/monambike/monambike-core</PackageProjectUrl>
<RepositoryUrl></RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Octokit" Version="9.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
45 changes: 45 additions & 0 deletions src/Monambike.Core/Services/HttpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Newtonsoft.Json;

namespace Monambike.Core.Services
{
/// <summary>
/// A simple HTTP service for making GET requests and retrieving the response content.
/// </summary>
public class HttpService(string httpClientBaseAddress)
{
/// <summary>
/// Base address for making api requests.
/// </summary>
public Uri Address => new(httpClientBaseAddress);

/// <summary>
/// HttpClient for making api requests.
/// </summary>
public HttpClient HttpClient => new() { BaseAddress = Address };

/// <summary>
/// Sends an asynchronous GET request to the specified URI and returns the response content as a string.
/// </summary>
/// <param name="httpClient">The HttpClient instance to use for the request.</param>
/// <param name="strRequest">The URI of the resource to request.</param>
/// <returns>A Task representing the asynchronous operation. The result contains the response content as a string.</returns>
public async Task<T?> GetAsync<T>(string strRequest)
{
// Tries to make the request with the base address and the request
// for URI validation.
Uri.TryCreate($"{HttpClient.BaseAddress}{strRequest}", UriKind.Absolute, out Uri? request);

// If current URI is valid, makes the requisition and returns the response.
using HttpResponseMessage response = await HttpClient.GetAsync(request);

// Assigning assynchronously the content response as string.
var content = await response.Content.ReadAsStringAsync();

// Deserializing object into the provided class.
var deserializedObject = JsonConvert.DeserializeObject<T>(content);

// Returning deserialized object content response into the provided class.
return deserializedObject;
}
}
}
5 changes: 1 addition & 4 deletions src/Monambike.Test/MainUnitTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Monambike.Core.Data;
using System.Diagnostics;

namespace Monambike.Test
{
[TestClass]
Expand All @@ -9,7 +6,7 @@ public class MainUnitTest
[TestMethod]
public void Main()
{
Debug.WriteLine(Links.WebsiteLearn);
var test = Core.Models.Github.GetGithubRepositories();
}
}
}
6 changes: 3 additions & 3 deletions src/Monambike.Test/Monambike.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 3cdc3ed

Please sign in to comment.