Skip to content

Adding support for issue-body with more than 65536 words, issue body to be created as an issue comment #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from 24 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
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
run: make build
env:
VERSION: latest

- name: Test
run: make test
- name: Lint
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ tidy:

.PHONY: build
build:
@if [ -z "$$VERSION" ]; then \
@if [ -z "$(VERSION)" ]; then \
echo "VERSION is required"; \
exit 1; \
fi
docker build --platform $(TARGET_PLATFORM) -t $(IMAGE_REPO):$$VERSION .

.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:
Expand Down
85 changes: 82 additions & 3 deletions approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -109,6 +106,16 @@ func (a *approvalEnvironment) createApprovalIssue(ctx context.Context) error {
}
a.approvalIssueNumber = a.approvalIssue.GetNumber()

bodyChunks := splitLongString(a.issueBody, 65536)
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
}
Expand Down Expand Up @@ -244,3 +251,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 {
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
}

Loading