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
This commit is contained in:
Sunny 2025-04-26 12:02:02 +05:30 committed by GitHub
parent c1ebf589e2
commit bfb0ba38d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 87 additions and 6 deletions

View file

@ -27,6 +27,7 @@ jobs:
run: make build
env:
VERSION: latest
- name: Test
run: make test
- name: Lint

View file

@ -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:

View file

@ -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
}