From b6ce1f8b297e6136b3ec05096794e0c52274167f Mon Sep 17 00:00:00 2001 From: SnskArora Date: Sat, 22 Mar 2025 18:38:03 +0530 Subject: [PATCH] testing with splitted issues --- approval.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/approval.go b/approval.go index 94301ae..89823ea 100644 --- a/approval.go +++ b/approval.go @@ -85,9 +85,9 @@ func (a *approvalEnvironment) createApprovalIssue(ctx context.Context) error { formatAcceptedWords(deniedWords), ) - if a.issueBody != "" { - issueBody = fmt.Sprintf(">%s\n>\n%s", a.issueBody, issueBody) - } + // if a.issueBody != "" { + // issueBody = fmt.Sprintf(">%s\n>\n%s", a.issueBody, issueBody) + // } issueBody = fmt.Sprintf(">[!NOTE]\n%s", issueBody) var err error @@ -109,13 +109,14 @@ func (a *approvalEnvironment) createApprovalIssue(ctx context.Context) error { } a.approvalIssueNumber = a.approvalIssue.GetNumber() - // Now add a comment to the same issue - commentBody := "This is a comment on the issue." - _, _, err = a.client.Issues.CreateComment(ctx, a.targetRepoOwner, a.targetRepoName, *a.approvalIssue.Number, &github.IssueComment{ - Body: &commentBody, - }) - if err != nil { - return fmt.Errorf("failed to add comment to issue: %w", err) + bodyChunks := splitLongString(a.issueBody, 65) + for _, chunk := range bodyChunks { + _, _, err = a.client.Issues.CreateComment(ctx, a.targetRepoOwner, a.targetRepoName, *a.approvalIssue.Number, &github.IssueComment{ + Body: &chunk, + }) + if err != nil { + return fmt.Errorf("failed to add comment chunk to issue: %w", err) + } } fmt.Printf("Issue created: %s\n", a.approvalIssue.GetHTMLURL()) @@ -253,3 +254,75 @@ func formatAcceptedWords(words []string) string { return strings.Join(quotedWords, ", ") } + +func splitLongLine(line string, maxL int) ([]string, bool) { + if len(line) <= maxL { + return []string{line}, false + } + + words := strings.Fields(line) + var result []string + var currentLine string + + for _, word := range words { + if len(currentLine)+len(word)+1 > maxL { + result = append(result, currentLine) + currentLine = word + } else { + if currentLine != "" { + currentLine += " " + } + currentLine += word + } + } + if currentLine != "" { + result = append(result, currentLine) + } + return result, true +} + +func splitLongString(input string, maxLength int) []string { + var result []string + + lines := strings.Split(input, "\n") + currentChunk := strings.Builder{} + currentLength := 0 + + for i, line := range lines { + if i < len(lines)-1 { + lineLength++ + } + + lineLength := len(line) + if currentLength+lineLength > maxLength { + if currentChunk.Len() > 0 { + result = append(result, currentChunk.String()) + currentChunk.Reset() + currentLength = 0 + } + } + + lineSplit, isLongLine := splitLongLine(line, maxLength) + if isLongLine { + if currentChunk.Len() > 0 { + result = append(result, currentChunk.String()) + currentChunk.Reset() + } + result = append(result, lineSplit[:len(lineSplit)-1]...) + currentChunk.WriteString(lineSplit[len(lineSplit)-1]) + currentLength = len(lineSplit[len(lineSplit)-1]) + } else { + currentChunk.WriteString(line) + currentLength += lineLength + } + + if i < len(lines)-1 { + currentChunk.WriteString("\n") + } + } + if currentChunk.Len() > 0 { + result = append(result, currentChunk.String()) + } + return result +} +