Skip to content

Commit

Permalink
Merge pull request #8 from ardacetinkaya/ai-comment-revision
Browse files Browse the repository at this point in the history
TODO messages
  • Loading branch information
ardacetinkaya authored Oct 13, 2024
2 parents d75a4a4 + 9a01207 commit 9ac5753
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/summarizePR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
exit 0
fi
- name: Summarize PR action
uses: ardacetinkaya/summarize-pull-request-action/@main
uses: ardacetinkaya/summarize-pull-request-action/@ai-comment-revision
env:
PAT: ${{ secrets.ACTION_TOKEN }}
APIKey: ${{ secrets.AI_API_KEY }}
Expand Down
Binary file added Example02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

A simple GitHub action that can create a brief information about commits in a pull request(PR). So, within brief, initial description of commit changes helps reviewing the PR.

![Image](/Example01.png)
And also if there are some TODOs comment in the code, an issue is generated automatically to follow-up the TODO and not missed. Within TODO issue, some brief code is also suggested so that for whom will take care of the issue will have some starting point.


| PR Description |
| ------------------------- |
| ![Image](/Example01.png) |

| Auto-generated issue for TODOs |
| ------------------------------ |
| ![alt text](Example02.png) |


## How

Expand Down
9 changes: 3 additions & 6 deletions src/Summarize.PR/Models/GithubRepositoryModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ public record CommitComment
public required string RepositoryAccount { get; set; }
}

public record Settings
public record Issue
{
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 Title { get; set; }
public required string Detail { get; set; }
public required string RepositoryName { get; set; }
public required string RepositoryAccount { get; set; }
}
5 changes: 5 additions & 0 deletions src/Summarize.PR/Models/PRDescriptionAnswer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Summarize.PR.Models;

public record PRDescriptionAnswer(string Comment, IReadOnlyList<Todo> Todos);

public record Todo(string Title, string Code);
11 changes: 11 additions & 0 deletions src/Summarize.PR/Models/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Summarize.PR.Models;
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; }
}
59 changes: 53 additions & 6 deletions src/Summarize.PR/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Summarize.PR.Models;
using Summarize.PR.Repository;
using System.Net.Http.Headers;
using System.Text.Json;

var builder = Host.CreateApplicationBuilder(args);

Expand All @@ -18,9 +19,8 @@

builder.Services.Configure<Settings>(config);

builder.Services.AddTransient<IGitHubRepository, GitHubRepository>();

builder.Services.AddHttpClient<GitHubRepository>((sp, client) =>
builder.Services.AddHttpClient<GitHubRepository>("GitHub", (sp, client) =>
{
var settings = sp.GetRequiredService<IOptions<Settings>>().Value;
Expand All @@ -33,6 +33,7 @@
// Authorization header with the Bearer token for authentication.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", settings.PAT.Trim());
});
builder.Services.AddTransient<IGitHubRepository, GitHubRepository>();

builder.Services.AddSingleton<IChatClient>(sp =>
{
Expand Down Expand Up @@ -90,15 +91,35 @@ Your descriptions are simple and clear so that they help developers to understan
{
Role = Microsoft.Extensions.AI.ChatRole.User,
Text = $$"""
Describe the following commit and group descriptions per file.
Describe the following commit and group descriptions per file.
If there are some TODO comments in the commit also add them into your response.
And also suggest some brief code for the TODO comments.
When suggesting the code also explain it in code description and also underline that it is just a suggestion and pseudo-code

<code>
{{diff}}
</code>

Response the description in this JSON format

{
"Comment": "___DESCRIPTION___",
"Todos": [
{
"Title":"___TODO_MESSAGE___",
"Description": "___SUGGESTED_CODE_DESCRIPTION___",
"Code":"___CODE_IN_MARKDOWN___"
},
]
}
""",
});

var result = await client.CompleteAsync(messages);
var result = await client.CompleteAsync(messages, new ChatOptions
{
ResponseFormat = ChatResponseFormat.Json,
Temperature = 0
});

if (string.IsNullOrEmpty(result.Message.Text))
{
Expand All @@ -107,14 +128,40 @@ Describe the following commit and group descriptions per file.
return;
}

var answer = JsonSerializer.Deserialize<PRDescriptionAnswer>(result.Message.Text);
if (answer == null)
{
Console.WriteLine("Invalid answer, summarization is skipped.");
return;
}

var commitComment = new CommitComment
{
Comment = result.Message.Text,
Comment = answer.Comment,
PullRequestId = settings.PullRequestId,
RepositoryName = settings.RepositoryName,
RepositoryAccount = settings.RepositoryAccount,
};

await repository.PostCommentAsync(commitComment);

Console.WriteLine("Commit changes are summarized.");
Console.WriteLine("Commit changes are summarized.");

if (answer.Todos != null && answer.Todos.Count != 0)
{
foreach (var todo in answer.Todos)
{
Console.WriteLine(todo.Title);
Console.WriteLine(todo.Code);
await repository.AddIssueAsync(new Issue
{
Title = todo.Title,
Detail = @$"This is an auto-generated issue due to [PR#{commitComment.PullRequestId}](https://github.com/{settings.RepositoryAccount}/{settings.RepositoryName}/pull/{commitComment.PullRequestId})
{todo.Code}",
RepositoryName = settings.RepositoryName,
RepositoryAccount = settings.RepositoryAccount,
});
Console.WriteLine("There is some TODO(s) in commit, an issue is created to follow it");
}
}
17 changes: 14 additions & 3 deletions src/Summarize.PR/Repository/GitHubRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using Summarize.PR.Models;
using System.Net.Http.Json;

public class GitHubRepository(HttpClient client) : IGitHubRepository
public class GitHubRepository(IHttpClientFactory clientFactory) : IGitHubRepository
{
private readonly HttpClient _client = client;

private readonly HttpClient _client = clientFactory.CreateClient("GitHub");
/// <summary>
/// Fetches the commit changes from the GitHub API using the commit SHA.
/// </summary>
Expand Down Expand Up @@ -43,4 +42,16 @@ public async Task PostCommentAsync(CommitComment commitComment)
// Ensure the response is successful.
response.EnsureSuccessStatusCode();
}

public async Task AddIssueAsync(Issue issue)
{
using var response = await _client.PostAsJsonAsync(
$"repos/{issue.RepositoryAccount}/{issue.RepositoryName}/issues",
new
{
title = issue.Title,
body = issue.Detail
}
);
}
}
2 changes: 2 additions & 0 deletions src/Summarize.PR/Repository/IGitHubRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface IGitHubRepository
Task PostCommentAsync(CommitComment commitComment);

Task<string> GetCommitChangesAsync(CommitChanges commitChanges);

Task AddIssueAsync(Issue issue);
}

0 comments on commit 9ac5753

Please sign in to comment.