Skip to content
Open
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
22 changes: 13 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"

"github.com/3-shake/alert-menta/internal/ai"
Expand Down Expand Up @@ -99,6 +98,8 @@ func validateCommand(command string, cfg *utils.Config) error {

// Construct user prompt from issue
func constructUserPrompt(ghToken string, issue *github.GitHubIssue, cfg *utils.Config, logger *log.Logger) (string, []ai.Image, error) {
var images []ai.Image

title, err := issue.GetTitle()
if err != nil {
return "", nil, fmt.Errorf("Error getting Title: %w", err)
Expand All @@ -112,15 +113,20 @@ func constructUserPrompt(ghToken string, issue *github.GitHubIssue, cfg *utils.C
var userPrompt strings.Builder
userPrompt.WriteString("Title:" + *title + "\n")
userPrompt.WriteString("Body:" + *body + "\n")
urls := utils.ExtractImageURLs(*body)
for _, url := range urls {
imgData, ext, err := utils.DownloadImage(url, ghToken)
if err != nil {
return "", nil, fmt.Errorf("Error downloading image: %w", err)
}
images = append(images, ai.Image{Data: imgData, Extension: ext})
}

comments, err := issue.GetComments()
if err != nil {
return "", nil, fmt.Errorf("Error getting comments: %w", err)
}

var images []ai.Image
imageRegex := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`)

for _, v := range comments {
if *v.User.Login == "github-actions[bot]" {
continue
Expand All @@ -130,14 +136,12 @@ func constructUserPrompt(ghToken string, issue *github.GitHubIssue, cfg *utils.C
}
userPrompt.WriteString(*v.User.Login + ":" + *v.Body + "\n")

matches := imageRegex.FindAllStringSubmatch(*v.Body, -1)
for _, match := range matches {
logger.Println("Image URL:", match[2]) // Log the URL of the image
imgData, ext, err := utils.DownloadImage(match[2], ghToken)
urls := utils.ExtractImageURLs(*body)
for _, url := range urls {
imgData, ext, err := utils.DownloadImage(url, ghToken)
if err != nil {
return "", nil, fmt.Errorf("Error downloading image: %w", err)
}

images = append(images, ai.Image{Data: imgData, Extension: ext})
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,13 @@ func ImageToBase64(data []byte, ext string) string {
base64img := base64.StdEncoding.EncodeToString(data)
return "data:image/" + ext + ";base64," + base64img
}

func ExtractImageURLs(body string) []string {
imageRegex := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`)
matches := imageRegex.FindAllStringSubmatch(body, -1)
var urls []string
for _, match := range matches {
urls = append(urls, match[2])
}
return urls
}