feat: Add --max-comments-per-command configuration (#3905)

Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
Co-authored-by: Benoit Toulme <benoit.toulme@gmail.com>
This commit is contained in:
David Glasser
2024-06-26 20:13:09 -07:00
committed by GitHub
parent a0d6938fe3
commit 5a918e382c
16 changed files with 130 additions and 76 deletions

View File

@@ -41,11 +41,12 @@ var (
// GithubClient is used to perform GitHub actions.
type GithubClient struct {
user string
client *github.Client
v4Client *githubv4.Client
ctx context.Context
config GithubConfig
user string
client *github.Client
v4Client *githubv4.Client
ctx context.Context
config GithubConfig
maxCommentsPerCommand int
}
// GithubAppTemporarySecrets holds app credentials obtained from github after creation.
@@ -76,7 +77,8 @@ type GithubPRReviewSummary struct {
}
// NewGithubClient returns a valid GitHub client.
func NewGithubClient(hostname string, credentials GithubCredentials, config GithubConfig, logger logging.SimpleLogging) (*GithubClient, error) {
func NewGithubClient(hostname string, credentials GithubCredentials, config GithubConfig, maxCommentsPerCommand int, logger logging.SimpleLogging) (*GithubClient, error) {
logger.Debug("Creating new GitHub client for host: %s", hostname)
transport, err := credentials.Client()
if err != nil {
@@ -108,11 +110,12 @@ func NewGithubClient(hostname string, credentials GithubCredentials, config Gith
return nil, errors.Wrap(err, "getting user")
}
return &GithubClient{
user: user,
client: client,
v4Client: v4Client,
ctx: context.Background(),
config: config,
user: user,
client: client,
v4Client: v4Client,
ctx: context.Background(),
config: config,
maxCommentsPerCommand: maxCommentsPerCommand,
}, nil
}
@@ -191,7 +194,10 @@ func (g *GithubClient) CreateComment(logger logging.SimpleLogging, repo models.R
"```diff\n"
}
comments := common.SplitComment(comment, maxCommentLength, sepEnd, sepStart)
truncationHeader := "\n```\n</details>" +
"\n<br>\n\n**Warning**: Command output is larger than the maximum number of comments per command. Output truncated.\n\n[..]\n"
comments := common.SplitComment(comment, maxCommentLength, sepEnd, sepStart, g.maxCommentsPerCommand, truncationHeader)
for i := range comments {
_, resp, err := g.client.Issues.CreateComment(g.ctx, repo.Owner, repo.Name, pullNum, &github.IssueComment{Body: &comments[i]})
if resp != nil {