Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GitHubRepo interface {
Owner() string
Repository() string
GetInfo() (*Repository, error)
AddIssueComment(issueNum int, comment string) error
AddIssueComment(issueNum int, commentID int64, comment string) error
}

func NewGitHubRepo(s *Session, f *RepositoryQueryFilter) (GitHubRepo, error) {
Expand Down
41 changes: 38 additions & 3 deletions pkg/github/issue.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
package github

import "github.com/google/go-github/v73/github"
import (
"strings"

"github.com/google/go-github/v73/github"
)

// quoteText turns each line into a Markdown quote ("> ...")
func quoteText(body string) string {
lines := strings.Split(body, "\n")
for i, line := range lines {
lines[i] = "> " + line
}
return strings.Join(lines, "\n")
}

// AddIssueComment adds a comment to an issue in the repository
// https://pkg.go.dev/github.com/google/go-github/v73/github#IssuesService.CreateComment
func (g *gitHubRepo) AddIssueComment(issueNum int, comment string) error {
_, _, err := g.session.Client.Issues.CreateComment(g.session.Context, g.owner, g.repostiory, issueNum, &github.IssueComment{Body: &comment})
func (g *gitHubRepo) AddIssueComment(issueNum int, commentID int64, comment string) error {
var newComment string
// If commentID is provided, we assume it's a reply to an existing comment
if commentID > 0 {
// Fetch the original comment
origComment, _, err := g.session.Client.Issues.GetComment(g.session.Context, g.owner, g.repostiory, commentID)
if err != nil {
return err
}

// Build the quoted comment block
quoted := quoteText(origComment.GetBody())

// Combine quoted text with the reply
newComment = quoted + "\n\n" + comment
} else {
// If no commentID is provided, just use the comment as is
newComment = comment
}

// Create a new comment on the issue
_, _, err := g.session.Client.Issues.CreateComment(g.session.Context, g.owner, g.repostiory, issueNum, &github.IssueComment{
Body: &newComment,
})
if err != nil {
return err
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/github/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@ func TestGithub_AddIssueComment(t *testing.T) {
}

comment := "This is a test comment from the GitHub API client."
err = repo.AddIssueComment(10, comment)
err = repo.AddIssueComment(10, 0, comment)
if err != nil {
t.Fatal(err)
}
}

func TestGithub_AddIssueCommentReply(t *testing.T) {
repo, err := initIssueTest()
if err != nil {
t.Fatal(err)
}

comment := "This is a test comment from the GitHub API client."
err = repo.AddIssueComment(27, 3050731768, comment)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/issue_comment_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (h *handler) ProcessIssueCommentCreatedEvent(ctx context.Context, deliveryI
}

// Process translation request
if err := svc.SendCloudEventRequest(*event.Issue.Number, *event.Comment.Body); err != nil {
if err := svc.SendCloudEventRequest(*event.Issue.Number, *event.Comment.ID, *event.Comment.Body); err != nil {
h.logger.Errorw("Failed to process translation request", "error", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/issue_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (h *handler) ProcessIssuesOpenedEvent(ctx context.Context, deliveryID strin
}

// Process translation request
if err := svc.SendCloudEventRequest(*event.Issue.Number, *event.Issue.Body); err != nil {
if err := svc.SendCloudEventRequest(*event.Issue.Number, 0, *event.Issue.Body); err != nil {
h.logger.Errorw("Failed to process translation request", "error", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/pull_request_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *handler) ProcessPullRequestsOpenedEvent(ctx context.Context, deliveryID
}

// Process translation request
if err := svc.SendCloudEventRequest(*event.PullRequest.Number, *event.PullRequest.Body); err != nil {
if err := svc.SendCloudEventRequest(*event.PullRequest.Number, 0, *event.PullRequest.Body); err != nil {
h.logger.Errorw("Failed to process translation request", "error", err)
return err
}
Expand Down
19 changes: 6 additions & 13 deletions service/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ func (s *AppService) GetRepoInfo() (*github.Repository, error) {
return result, nil
}

// AddIssueComment adds a comment to an issue in the repository
func (s *AppService) AddIssueComment(issueNumber int, comment string) error {
err := s.repo.AddIssueComment(issueNumber, comment)
if err != nil {
return errors.New("failed to add issue comment: " + err.Error())
}

return nil
}

// MultilineTranslate translates a multiline text using the translation engine
func (s *AppService) MultilineTranslate(text string) (string, error) {
if s.translateEngine == nil {
Expand All @@ -42,9 +32,12 @@ func (s *AppService) MultilineTranslate(text string) (string, error) {
}

// ProcessTranslationRequest processes a translation request for an issue comment
func (s *AppService) ProcessTranslationRequest(issueNumber int, text string) error {
func (s *AppService) ProcessTranslationRequest(issueNumber int, commentID int64, text string) error {
// Log the incoming translation request
s.logger.Infow("Processing translation request", "issueNumber", issueNumber)
s.logger.Infow("Processing translation request",
"issueNumber", issueNumber,
"commentID", commentID,
)

// Check if the text needs to be translated
langResult, err := translate.DetectLanguage(text)
Expand Down Expand Up @@ -79,7 +72,7 @@ func (s *AppService) ProcessTranslationRequest(issueNumber int, text string) err
result = "> [!NOTE]\n> The following content has been translated from its original language using an automated process powered by a proprietary API. Segments originally written in English have been preserved, while non-English portions have been machine-translated for readability. Please be aware that minor inaccuracies may exist due to the automated nature of the translation.\n\n" + result

// Add the translated text as a comment on the issue
if err := s.AddIssueComment(issueNumber, result); err != nil {
if err := s.repo.AddIssueComment(issueNumber, commentID, result); err != nil {
return errors.New("failed to add translated comment: " + err.Error())
}

Expand Down
16 changes: 13 additions & 3 deletions service/cloudevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// SendCloudEventRequest sends a translation request as a CloudEvent
func (s *AppService) SendCloudEventRequest(issueNumber int, text string) error {
func (s *AppService) SendCloudEventRequest(issueNumber int, commentID int64, text string) error {
// Log the incoming translation request
s.logger.Infof("Sending translation request for issue #%d", issueNumber)

Expand All @@ -21,6 +21,7 @@ func (s *AppService) SendCloudEventRequest(issueNumber int, text string) error {
// Construct the data to be sent in the event
data := map[string]string{
"issue_number": strconv.Itoa(issueNumber),
"comment_id": strconv.FormatInt(commentID, 10),
"text": text,
"repo_owner": s.repo.Owner(),
"repo_name": s.repo.Repository(),
Expand Down Expand Up @@ -85,11 +86,15 @@ func (s *AppService) ProcessCloudEventRequest(event cloudevent.Event) error {
// Instantiate the translation engine
s.translateEngine = translate.NewLocalAIClient()

// Extract issue number and text from the event data
// Extract issue number, issue comment number, and text from the event data
issueNumberStr, ok := data["issue_number"]
if !ok {
s.logger.Error("missing issue_number in event data")
}
commentIDStr, ok := data["comment_id"]
if !ok {
s.logger.Error("missing issue_comment_number in event data")
}
text, ok := data["text"]
if !ok {
s.logger.Error("missing text in event data")
Expand All @@ -100,9 +105,14 @@ func (s *AppService) ProcessCloudEventRequest(event cloudevent.Event) error {
if err != nil {
s.logger.Errorf("invalid issue_number format: %v", err)
}
// Convert issue comment number to integer
commentID, err := strconv.Atoi(commentIDStr)
if err != nil {
s.logger.Errorf("invalid issue_comment_number format: %v", err)
}

// Process the translation request
err = s.ProcessTranslationRequest(issueNumber, text)
err = s.ProcessTranslationRequest(issueNumber, int64(commentID), text)
if err != nil {
s.logger.Errorf("failed to process translation request: %v", err)
}
Expand Down