From bfb0ba38d02a528a2c101381809dc6c3062732c1 Mon Sep 17 00:00:00 2001 From: Sunny <55059942+snskArora@users.noreply.github.com> Date: Sat, 26 Apr 2025 12:02:02 +0530 Subject: [PATCH] Adding support for issue-body with more than 65536 words, issue body to be created as an issue comment (#167) * push a built package to ghcr * adding err catch * auth check * auth update * update uname * update uname * testing with splitted issues * minor fix * revert testing changes * Update approval.go --- .github/workflows/ci.yaml | 1 + Makefile | 6 +-- approval.go | 86 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dbcfa1a..c0d4e03 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -27,6 +27,7 @@ jobs: run: make build env: VERSION: latest + - name: Test run: make test - name: Lint diff --git a/Makefile b/Makefile index 56ba8bc..54f20b9 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ tidy: .PHONY: build build: - @if [ -z "$$VERSION" ]; then \ + @if [ -z "$(VERSION)" ]; then \ echo "VERSION is required"; \ exit 1; \ fi @@ -15,11 +15,11 @@ build: .PHONY: push push: - @if [ -z "$$VERSION" ]; then \ + @if [ -z "$(VERSION)" ]; then \ echo "VERSION is required"; \ exit 1; \ fi - docker push $(IMAGE_REPO):$$VERSION + docker push $(IMAGE_REPO):$(VERSION) .PHONY: test test: diff --git a/approval.go b/approval.go index 8487e26..2b7baa1 100644 --- a/approval.go +++ b/approval.go @@ -85,9 +85,6 @@ func (a *approvalEnvironment) createApprovalIssue(ctx context.Context) error { formatAcceptedWords(deniedWords), ) - if a.issueBody != "" { - issueBody = fmt.Sprintf(">%s\n>\n%s", a.issueBody, issueBody) - } issueBody = fmt.Sprintf(">[!NOTE]\n%s", issueBody) var err error @@ -109,6 +106,16 @@ func (a *approvalEnvironment) createApprovalIssue(ctx context.Context) error { } a.approvalIssueNumber = a.approvalIssue.GetNumber() + bodyChunks := splitLongString(a.issueBody) + 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()) return nil } @@ -244,3 +251,76 @@ 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) []string { + maxLength := 65536 + var result []string + + lines := strings.Split(input, "\n") + currentChunk := strings.Builder{} + currentLength := 0 + + for i, line := range lines { + lineLength := len(line) + if i < len(lines)-1 { + lineLength++ + } + + 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 +} +