Skip to content

Commit

Permalink
feat: integrate github api usage and PR detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrstv committed Feb 16, 2025
1 parent 7089950 commit bc441ad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
46 changes: 35 additions & 11 deletions Kodify/AutoDoc/Services/Documentation/ChangelogGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using LibGit2Sharp;
using System;
using System.Threading.Tasks;
using Kodify.AutoDoc.Models;
using Kodify.AutoDoc.Services.Repository;
using Kodify.AI.Services;
using Kodify.AutoDoc.Services;
using Kodify.AutoDoc.Services.Documentation;
using Kodify.AutoDoc.Services.Repository;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Text;
using System.Reflection;

namespace Kodify.AutoDoc.Services.Documentation
{
public class ChangelogGenerator
{
private readonly GitRepositoryService _gitService;
private string _githubRepoUrl; // Normalized URL of the GitHub repo (if available)

public ChangelogGenerator() : this(new GitRepositoryService())
{
Expand All @@ -28,25 +27,38 @@ public ChangelogGenerator(GitRepositoryService gitService)
_gitService = gitService;
}

// Parameterless GenerateChangelog method that automatically detects the project path.
// Generate changelog by automatically detecting the project path.
public void GenerateChangelog()
{
// Automatically detect the project path using the GitRepositoryService.
var projectPath = _gitService.DetectProjectRoot();
GenerateChangelog(projectPath);
}

// Existing GenerateChangelog method that accepts a projectPath.
// Generate changelog by providing an input path
public void GenerateChangelog(string projectPath)
{
// Determine root and prepare the output file.
var rootPath = _gitService.DetectProjectRoot(projectPath);
var outputPath = Path.Combine(rootPath, "CHANGELOG.md");
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

// Determine if the repository is hosted on GitHub.
var (hasGit, remoteUrl) = _gitService.CheckForGitRepository(projectPath);
if (hasGit && !string.IsNullOrEmpty(remoteUrl) && remoteUrl.Contains("github.com"))
{
// Store the normalized GitHub URL for linking issue references.
_githubRepoUrl = remoteUrl;
}
else
{
_githubRepoUrl = null;
}

using (var writer = new StreamWriter(outputPath))
{
writer.WriteLine("# Changelog");
writer.WriteLine();
writer.WriteLine("This changelog aims to highlight user-impactful changes with references to issues/PRs where available.");
writer.WriteLine();

var repoPath = LibGit2Sharp.Repository.Discover(projectPath);
if (repoPath == null)
Expand Down Expand Up @@ -121,8 +133,8 @@ public void GenerateChangelog(string projectPath)
private void WriteCommitDetails(StreamWriter writer, Commit commit)
{
var messageParts = commit.Message.Split(new[] { '\n' }, 2);
var subject = messageParts[0].Trim();
var body = messageParts.Length > 1 ? messageParts[1].Trim() : null;
var subject = ReplaceIssueReferences(messageParts[0].Trim());
var body = messageParts.Length > 1 ? ReplaceIssueReferences(messageParts[1].Trim()) : null;

writer.WriteLine($"- **{subject}**");
writer.WriteLine($" *Commit: {commit.Sha[..7]} | Date: {commit.Committer.When:yyyy-MM-dd} | Author: {commit.Author.Name}*");
Expand All @@ -139,5 +151,17 @@ private void WriteCommitDetails(StreamWriter writer, Commit commit)
}
writer.WriteLine();
}

private string ReplaceIssueReferences(string text)
{
if (string.IsNullOrEmpty(_githubRepoUrl))
return text;

return Regex.Replace(text, "#(\\d+)", m =>
{
var issueNumber = m.Groups[1].Value;
return $"[#{issueNumber}]({_githubRepoUrl}/issues/{issueNumber})";
});
}
}
}
33 changes: 33 additions & 0 deletions Kodify/AutoDoc/Services/Repository/GitHubApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace Kodify.AutoDoc.Services.Repository
{
public class GitHubApiService
{
private readonly HttpClient _httpClient;

public GitHubApiService(string token = null)

Check warning on line 13 in Kodify/AutoDoc/Services/Repository/GitHubApiService.cs

View workflow job for this annotation

GitHub Actions / Build, Test & Package

Cannot convert null literal to non-nullable reference type.
{
_httpClient = new HttpClient();
if (!string.IsNullOrEmpty(token))
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", token);
}
}

public async Task<JObject> GetRepositoryInfoAsync(string owner, string repo)
{
var url = $"https://api.github.com/repos/{owner}/{repo}";
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("KodifyAutoDoc", "1.0"));
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
return JObject.Parse(content);
}
}
}
5 changes: 5 additions & 0 deletions Kodify/AutoDoc/Services/Repository/GitRepositoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public string NormalizeGitUrl(string gitUrl)
.Replace("[email protected]:", "https://gitlab.com/")
.Replace(".git", "");
}

if (gitUrl.EndsWith(".git"))
{
gitUrl = gitUrl.Substring(0, gitUrl.Length - 4);
}
return gitUrl;
}
}
Expand Down

0 comments on commit bc441ad

Please sign in to comment.