-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGithub.cs
40 lines (36 loc) · 1.15 KB
/
Github.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Net;
using System.Net.Http.Json;
namespace bot
{
public class Github : IDisposable
{
private class CommitResponse
{
public Commit Commit { get; set; } = new();
}
private class Commit
{
public string Message { get; set; } = "";
}
private readonly HttpClient _client = new();
private readonly string _owner;
private readonly string _repo;
private readonly string _branch;
private string Path => $"https://api.github.com/repos/{_owner}/{_repo}/commits?sha={_branch}&per_page=1";
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
public Github(string owner, string repo, string branch)
{
_owner = owner;
_repo = repo;
_branch = branch;
_client.DefaultRequestHeaders.Add("User-Agent", "mparchin");
}
public async Task<string> GetLatestCommitMessageAsync() =>
(await _client.GetFromJsonAsync<CommitResponse[]>(Path))?.FirstOrDefault()?
.Commit.Message ?? "";
}
}