Skip to content
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

error: body is too long (maximum is 65536 characters) #93

Open
kishaningithub opened this issue May 2, 2023 · 5 comments
Open

error: body is too long (maximum is 65536 characters) #93

kishaningithub opened this issue May 2, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@kishaningithub
Copy link

Problem

If pr comments are long, github API throws body is too long (maximum is 65536 characters) error

Error: Validation Failed: {"resource":"IssueComment","code":"unprocessable","field":"data","message":"Body is too long (maximum is 65536 characters)"}

Reproducible example file below, note that i am using the newly added message-path parameter to send the PR comment as a file.

name: PR comment test

on:
  push:
  workflow_call:

jobs:
  pr_comment_test:
    name: pr_command_help
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: comment message
        run: |
          wget --quiet https://github.com/kishaningithub/randomtext/releases/download/v1.2.1/randomtext_1.2.1_linux_amd64.deb
          sudo dpkg -i randomtext_1.2.1_linux_amd64.deb
          
          echo "Available commands (300 KB)" > pr-comment.txt
          randomtext -size 300KB -type words >> pr-comment.txt

          du -sh pr-comment.txt

      - name: Post apply output to GitHub PR
        uses: mshick/add-pr-comment@v2
        with:
          allow-repeats: true
          message-path: "pr-comment.txt"

Run Link: https://github.com/kishaningithub/github-actions-expermiments/actions/runs/4860867805/jobs/8665212807?pr=1

workflow file: https://github.com/kishaningithub/github-actions-expermiments/actions/runs/4860867805/workflow?pr=1

Use case

  • I am using this action to post output of terraform plan as a PR comment, this can get pretty long depending on the number of resources being affected
@kishaningithub
Copy link
Author

It seems that this 65536 chars is a hard limit https://github.com/orgs/community/discussions/41331 on the gh api

one very interesting this is that this is actually misleading, if you see the following run in the same repo of mine, i am able to pass a 200 KB txt file inside which is higher than 65536 chars (~65 KB)

Links

Screenshot

image

@kishaningithub
Copy link
Author

kishaningithub commented May 3, 2023

We can probably have a function like below to splits the comment into multiple comments based on maxSize which can be 65536(though in actuality it seems to extend till ~200 KB) and sepEnd and sepStart variables can say what must be the separator when the comment gets split into multiple comments

example of sepStart - Continued from previous comment
example of sepEnd - Output length greater than max comment size. Continued in next comment.

The return value of this can be used to make multiple calls to the create comment API. Same can also probably bedone for the message-path and message-paths variants

function SplitComment(comment, maxSize, sepEnd, sepStart) {
  if (comment.length <= maxSize) {
    return [comment];
  }

  const maxWithSep = maxSize - sepEnd.length - sepStart.length;
  const comments = [];
  const numComments = Math.ceil(comment.length / maxWithSep);

  for (let i = 0; i < numComments; i++) {
    const start = i * maxWithSep;
    const end = Math.min(comment.length, (i + 1) * maxWithSep);
    let portion = comment.substring(start, end);

    if (i < numComments - 1) {
      portion += sepEnd;
    }
    if (i > 0) {
      portion = sepStart + portion;
    }

    comments.push(portion);
  }

  return comments;
}

@mshick
Copy link
Owner

mshick commented May 4, 2023

Interesting issue. I intend to address this by checking the message size and breaking the message into parts if we hit the limit.

I'll make this behavior optional, the default will be to truncate the message with ... to avoid the error.

@mshick mshick added the enhancement New feature or request label May 4, 2023
@mshick mshick moved this to Todo in add-pr-comment May 5, 2023
@kubukoz
Copy link

kubukoz commented May 18, 2023

hey @mshick, have you made any progress on that? Happy to help if you don't have the bandwidth.

@ljetten
Copy link

ljetten commented May 16, 2024

Oops, this looks misleading due to me merging a PR on a fork I made to check the tests would pass.
The one line above this comment is the PR I submitted to the actual repository..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

4 participants