Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributors Page that auto-fills using GitHub API #26

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions ReplayBrowser/Helpers/GitHubApiHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Net.Http;
using System.Net.Mime;
using System.Text.Json; // Add this for JSON deserialization
using Serilog;

namespace ReplayBrowser.Helpers;

public class GitHubApiHelper
{
public List<GitHubAccount> Contributors;
private readonly string? _apiToken;

public GitHubApiHelper(IConfiguration configuration)
{
Contributors = new List<GitHubAccount>();

try
{
_apiToken = configuration["GitHubAPIToken"];
}
catch (Exception e)
{
Log.Error("GitHubAPIToken not set, contributors cannot be fetched.");
}
}

public async Task<List<GitHubAccount>> GetContributors()
{
try
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/Simyon264/ReplayBrowser/contributors"))
{
// lesson learned: don't forget the user agent
request.Headers.TryAddWithoutValidation("Accept", "application/vnd.github+json");
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {_apiToken}");
request.Headers.TryAddWithoutValidation("X-GitHub-Api-Version", "2022-11-28");
request.Headers.TryAddWithoutValidation("User-Agent", "YourAppNameHere");

var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync();

// Deserialize JSON into GitHubAccount list
var responseJson = JsonSerializer.Deserialize<List<JsonElement>>(responseBody);

foreach (var obj in responseJson)
{

// Initialize variables with default values
// Doing this to 100% make sure that respinse errors / null responses don't crash anything
string accountName = null;
string accountImageURL = null;
string accountLink = null;

// Safely retrieve properties
if (obj.TryGetProperty("login", out var loginProperty))
{
accountName = loginProperty.GetString();
}

if (obj.TryGetProperty("avatar_url", out var avatarUrlProperty))
{
accountImageURL = avatarUrlProperty.GetString();
}

if (obj.TryGetProperty("html_url", out var htmlUrlProperty))
{
accountLink = htmlUrlProperty.GetString();
}

// Check for nulls before adding to the list
if (accountName != null && accountImageURL != null && accountLink != null)
{
var account = new GitHubAccount
{
AccountName = accountName,
AccountImageURL = accountImageURL,
AccountLink = accountLink
};

Contributors.Add(account);
}
else
{
Log.Warning("A required property was missing in the JSON response while attempting to fetch project contributors.");
}
}

Log.Information("Successfully fetched project contributor list.");
return Contributors;
bhenrich marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
catch (Exception e)
{
Log.Error($"Exception when querying GitHub: {e.Message} - {e.StackTrace.ToString()}");

// return empty list
// TODO - Proper handling
return new List<GitHubAccount>();
}
}
}

public struct GitHubAccount
{
public string AccountName { get; set; }
public string AccountImageURL { get; set; }
public string AccountLink { get; set; }
bhenrich marked this conversation as resolved.
Show resolved Hide resolved
}
34 changes: 34 additions & 0 deletions ReplayBrowser/Pages/Contributors.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@page "/contributors"

@inject GitHubApiHelper GitHubApiHelper

@using Microsoft.AspNetCore.Components.Web
@using ReplayBrowser.Pages.Shared
@using ReplayBrowser.Helpers

<PageTitle>Contributors</PageTitle>

<h3>Contributors</h3>

<div style="display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; align-items: center; padding: 20px;">
@if (_contributors.Count == 0)
{
<h4>Error fetching contributors from the GitHub API</h4>
bhenrich marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
@foreach (var contrib in _contributors)
{
<ContributorCard ContributorName="@contrib.AccountName" ContributorImage="@contrib.AccountImageURL" ContributorLink="@contrib.AccountLink" />
}
}
</div>

@code {
private List<GitHubAccount>? _contributors = new List<GitHubAccount>();
bhenrich marked this conversation as resolved.
Show resolved Hide resolved

protected async override Task OnInitializedAsync()
{
_contributors = await GitHubApiHelper.GetContributors();
}
}
32 changes: 32 additions & 0 deletions ReplayBrowser/Pages/Shared/ContributorCard.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="card"
style="display: flex; flex-direction: column; align-items: center; justify-content: center; width: 200px;">
<div class="card-title">
<br /><h4>@ContributorName</h4>
</div>

<div>
<div>
<img src="@ContributorImage"
style="width: 50px; height: 50px; border-radius: 100px; transform: translateX(calc(25.8px / 2))"
alt="Profile Picture of the Contributor"
/>
</div>

<br />

<a href="@ContributorLink" class="btn btn-primary"
style="margin-bottom: 25px">
GitHub
</a>
</div>
</div>


@code {
[Parameter] public required string ContributorName { get; set; }
[Parameter] public required string ContributorImage { get; set; }
[Parameter] public required string ContributorLink { get; set; }



bhenrich marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion ReplayBrowser/Pages/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

<footer class="footer text-muted text-center">
<div class="container">
<p>Replay Browser is a project by Simyon, YuNii and Saphire. Source code available on <a href="https://github.com/Simyon264/ReplayBrowser">GitHub</a>.</p>
<p>Replay Browser is a project by Simyon and <a href="/contributors">contributors</a>. Source code available on <a href="https://github.com/Simyon264/ReplayBrowser">GitHub</a>.</p>
</div>

<div class="container">
Expand Down
1 change: 1 addition & 0 deletions ReplayBrowser/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<ReplayParserService>();
services.AddSingleton<AnalyticsService>();
services.AddSingleton<NoticeHelper>();
services.AddSingleton<GitHubApiHelper>();

services.AddHostedService<BackgroundServiceStarter<ReplayParserService>>();
services.AddHostedService<BackgroundServiceStarter<AccountService>>();
Expand Down
34 changes: 17 additions & 17 deletions ReplayBrowser/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,135 +9,135 @@
"ReplayUrls": [
{
"url": "https://cdn.networkgamez.com/replays/replays/goobstation/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "goobstation",
"fallBackServerId": "goobstation",
"replayRegex": "(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "http://cdn.harmony14.com:27690/replays/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "harmony",
"fallBackServerId": "harmony",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.rouny-ss14.com/replays/alamo/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "alamo",
"fallBackServerId": "rmc14",
"replayRegex": "(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "https://replays.rouny-ss14.com/replays/normandy/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "rouny_2",
"fallBackServerId": "rmc14",
"replayRegex": "(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "https://moon.spacestation14.com/replays/rain_one_one_gateway/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "rain_one_one_gateway",
"fallBackServerId": "wizards_rain",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/rain_one_three_core/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "rain_one_three_core",
"fallBackServerId": "wizards_rain",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/rain_two_one_tide/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "rain_two_one_tide",
"fallBackServerId": "wizards_rain",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://sector-umbra.net/replays/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "Sector Umbra",
"fallBackServerId": "sector-umbra",
"replayRegex": "(\\d{4}-\\d{2}-\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "https://axolotl.yuniiworks.de/replays/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "axolotl",
"fallBackServerId": "axolotl",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "(^[a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/leviathan/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "leviathan",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/lizard/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "lizard",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/miros/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "miros",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/salamander/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "salamander",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/vulture/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "vulture",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.delta-v.org/apoapsis/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "Server 1",
"fallBackServerId": "deltav",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "(^[a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.delta-v.org/periapsis/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "Server 2",
"fallBackServerId": "deltav",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "(^[a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.delta-v.org/horizon/",
"provider": "nginx",
"provider": "dummy",
bhenrich marked this conversation as resolved.
Show resolved Hide resolved
"fallBackServerName": "Server 3",
"fallBackServerId": "deltav",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
Expand Down
Loading