-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GitHub module and apply several changes
- Loading branch information
Showing
10 changed files
with
272 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters