-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Outdated packages have been updated. Refactoring for .NET 8 has been …
…completed. Improvements were made to enhance code readability. Models were consolidated into a single file.
- Loading branch information
Showing
5 changed files
with
136 additions
and
91 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,29 @@ | ||
namespace Summarize.PR.Models; | ||
|
||
public record Review(string body); | ||
|
||
public record CommitChanges | ||
{ | ||
public required string CommitSHA { get; set; } | ||
public required string RepositoryName { get; set; } | ||
public required string RepositoryAccount { get; set; } | ||
} | ||
|
||
public record CommitComment | ||
{ | ||
public required string Comment { get; set; } | ||
public required string PullRequestId { get; set; } | ||
public required string RepositoryName { get; set; } | ||
public required string RepositoryAccount { get; set; } | ||
} | ||
|
||
public record Settings | ||
{ | ||
public required string PAT { get; set; } | ||
public required string APIKey { get; set; } | ||
public required string ModelId { get; set; } | ||
public required string CommitSHA { get; set; } | ||
public required string PullRequestId { get; set; } | ||
public required string RepositoryName { get; set; } | ||
public required string RepositoryAccount { 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
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 |
---|---|---|
@@ -1,43 +1,57 @@ | ||
namespace Summarize.PR.Repository.GitHub; | ||
|
||
using Summarize.PR.Models; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
|
||
public class GitHubRepository | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public GitHubRepository(string token) | ||
{ | ||
_client = new HttpClient(); | ||
//User-Agent should be defined or actions worker does not allow for a request | ||
_client.DefaultRequestHeaders.Add("User-Agent", "SummarizePRAction"); | ||
_client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.diff"); | ||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Trim()); | ||
} | ||
|
||
public async Task<string> GetCommitChanges(string repositoryAccount, string repositoryName, string commitSHA) | ||
{ | ||
var response = await _client.GetAsync($"https://api.github.com/repos/{repositoryAccount}/{repositoryName}/commits/{commitSHA}"); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var result = await response.Content.ReadAsStringAsync(); | ||
|
||
return result; | ||
} | ||
|
||
public async Task PostComment(string comment, string repositoryAccount, string repositoryName, string pullRequestId) | ||
{ | ||
|
||
var result = await _client.PostAsJsonAsync<Review>($"https://api.github.com/repos/{repositoryAccount}/{repositoryName}/issues/{pullRequestId}/comments", new Review | ||
{ | ||
Body = comment | ||
}); | ||
|
||
result.EnsureSuccessStatusCode(); | ||
} | ||
} | ||
|
||
file class Review | ||
{ | ||
public string Body { get; set; } | ||
private readonly HttpClient _client; | ||
|
||
// The HttpClient is injected through the constructor (HttpClientFactory pattern), | ||
// and the GitHub token is used to set up authorization headers. | ||
public GitHubRepository(string token) | ||
{ | ||
_client = new HttpClient(); | ||
|
||
// Set User-Agent and Accept headers. | ||
// These headers are necessary for the GitHub API to recognize the request. | ||
_client.DefaultRequestHeaders.Add("User-Agent", "SummarizePRAction"); | ||
_client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.diff"); | ||
|
||
// Authorization header with the Bearer token for authentication. | ||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Trim()); | ||
} | ||
|
||
/// <summary> | ||
/// Fetches the commit changes from the GitHub API using the commit SHA. | ||
/// </summary> | ||
/// <param name="commitChanges">Commit changes model.</param> | ||
public async Task<string> GetCommitChangesAsync(CommitChanges commitChanges) | ||
{ | ||
// Build the request URL using string interpolation and send GET request to GitHub API. | ||
using var response = await _client.GetAsync($"repos/{commitChanges.RepositoryAccount}/{commitChanges.RepositoryName}/commits/{commitChanges.CommitSHA}"); | ||
|
||
// Ensure the response is successful (throws an exception if the status code is not 2xx). | ||
response.EnsureSuccessStatusCode(); | ||
|
||
// Read and return the response content as a string (the diff of the commit). | ||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Posts a comment on a GitHub pull request. | ||
/// </summary> | ||
/// <param name="commitComment">Commit comment model</param> | ||
public async Task PostCommentAsync(CommitComment commitComment) | ||
{ | ||
// Create a new review object with the comment body. | ||
Review review = new(commitComment.Comment); | ||
|
||
// Send a POST request to GitHub API with the review object in JSON format. | ||
using var response = await _client.PostAsJsonAsync($"repos/{commitComment.RepositoryAccount}/{commitComment.RepositoryName}/issues/{commitComment.PullRequestId}/comments", review); | ||
|
||
// Ensure the response is successful. | ||
response.EnsureSuccessStatusCode(); | ||
} | ||
} |
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