|
| 1 | +// Copyright 2021-2025 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/bufbuild/modules/internal/githubutil" |
| 25 | + "github.com/google/go-github/v64/github" |
| 26 | +) |
| 27 | + |
| 28 | +// prReviewComment represents a comment to be posted or patched on a specific line in a PR. |
| 29 | +type prReviewComment struct { |
| 30 | + filePath string // File path in the PR (e.g., "modules/sync/bufbuild/protovalidate/state.json") |
| 31 | + lineNumber int // Line number in the diff |
| 32 | + body string // Comment body (casdiff output) |
| 33 | +} |
| 34 | + |
| 35 | +// commentKey uniquely identifies a comment by file and line. |
| 36 | +type commentKey struct { |
| 37 | + path string |
| 38 | + line int |
| 39 | +} |
| 40 | + |
| 41 | +// postReviewComments posts review comments to specific lines in the PR diff. If a bot comment |
| 42 | +// already exists at the same file/line, it is updated instead of creating a duplicate. |
| 43 | +func postReviewComments(ctx context.Context, prNumber int, gitCommitID string, comments ...prReviewComment) error { |
| 44 | + client := githubutil.NewClient(ctx) |
| 45 | + |
| 46 | + existingPRComments, err := listExistingBotComments(ctx, client, prNumber) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("list existing bot comments: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + var errsPosting []error |
| 52 | + for _, comment := range comments { |
| 53 | + key := commentKey{path: comment.filePath, line: comment.lineNumber} |
| 54 | + if prCommentID, alreadyPosted := existingPRComments[key]; alreadyPosted { |
| 55 | + if err := updateReviewComment(ctx, client, prCommentID, comment.body); err != nil { |
| 56 | + errsPosting = append(errsPosting, fmt.Errorf("updating existing comment in %v: %w", key, err)) |
| 57 | + } |
| 58 | + } else { |
| 59 | + if err := postSingleReviewComment(ctx, client, prNumber, gitCommitID, comment); err != nil { |
| 60 | + errsPosting = append(errsPosting, fmt.Errorf("posting new comment in %v: %w", key, err)) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return errors.Join(errsPosting...) |
| 65 | +} |
| 66 | + |
| 67 | +// listExistingBotComments returns a map of (path, line) → commentID for all comments left by |
| 68 | +// github-actions[bot] on the PR. Sorted ascending by creation time so that when multiple bot |
| 69 | +// comments exist at the same file+line, the highest-ID (most recently created) one wins. |
| 70 | +func listExistingBotComments(ctx context.Context, client *githubutil.Client, prNumber int) (map[commentKey]int64, error) { |
| 71 | + const githubActionsBotUsername = "github-actions[bot]" |
| 72 | + result := make(map[commentKey]int64) |
| 73 | + opts := &github.PullRequestListCommentsOptions{ |
| 74 | + Sort: "created", |
| 75 | + Direction: "asc", |
| 76 | + ListOptions: github.ListOptions{PerPage: 100}, |
| 77 | + } |
| 78 | + for { |
| 79 | + comments, resp, err := client.GitHub.PullRequests.ListComments( |
| 80 | + ctx, |
| 81 | + string(githubutil.GithubOwnerBufbuild), |
| 82 | + string(githubutil.GithubRepoModules), |
| 83 | + prNumber, |
| 84 | + opts, |
| 85 | + ) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("list PR comments: %w", err) |
| 88 | + } |
| 89 | + for _, comment := range comments { |
| 90 | + if comment.GetUser().GetLogin() == githubActionsBotUsername { |
| 91 | + key := commentKey{path: comment.GetPath(), line: comment.GetLine()} |
| 92 | + result[key] = comment.GetID() |
| 93 | + } |
| 94 | + } |
| 95 | + if resp.NextPage == 0 { |
| 96 | + break |
| 97 | + } |
| 98 | + opts.Page = resp.NextPage |
| 99 | + } |
| 100 | + return result, nil |
| 101 | +} |
| 102 | + |
| 103 | +func postSingleReviewComment(ctx context.Context, client *githubutil.Client, prNumber int, gitCommitID string, comment prReviewComment) error { |
| 104 | + body := fmt.Sprintf("_[Posted at %s]_\n\n%s", time.Now().Format(time.RFC3339), comment.body) |
| 105 | + created, _, err := client.GitHub.PullRequests.CreateComment( |
| 106 | + ctx, |
| 107 | + string(githubutil.GithubOwnerBufbuild), |
| 108 | + string(githubutil.GithubRepoModules), |
| 109 | + prNumber, |
| 110 | + &github.PullRequestComment{ |
| 111 | + CommitID: &gitCommitID, |
| 112 | + Path: &comment.filePath, |
| 113 | + Line: &comment.lineNumber, |
| 114 | + Body: &body, |
| 115 | + }, |
| 116 | + ) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("create PR comment: %w", err) |
| 119 | + } |
| 120 | + fmt.Fprintf(os.Stdout, "Posted comment: %s\n", created.GetHTMLURL()) |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func updateReviewComment(ctx context.Context, client *githubutil.Client, prCommentID int64, body string) error { |
| 125 | + body = fmt.Sprintf("_[Updated at %s]_\n\n%s", time.Now().Format(time.RFC3339), body) |
| 126 | + updated, _, err := client.GitHub.PullRequests.EditComment( |
| 127 | + ctx, |
| 128 | + string(githubutil.GithubOwnerBufbuild), |
| 129 | + string(githubutil.GithubRepoModules), |
| 130 | + prCommentID, |
| 131 | + &github.PullRequestComment{ |
| 132 | + Body: &body, |
| 133 | + }, |
| 134 | + ) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("edit PR comment: %w", err) |
| 137 | + } |
| 138 | + fmt.Fprintf(os.Stdout, "Updated comment: %s\n", updated.GetHTMLURL()) |
| 139 | + return nil |
| 140 | +} |
0 commit comments