Repository navigation
Add PR comment, reaction and user permission commands #16
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
Andrey Hura (andrey-hura)
merged 2 commits into
main
from
andrey-hura/add-pr-comment-commands
Nov 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-github/v62/github" | ||
| ) | ||
|
|
||
| // Reactions accepted by the GitHub API for issue and pull request comments. | ||
| var allowedReactions = []string{"+1", "-1", "laugh", "confused", "heart", "hooray", "rocket", "eyes"} | ||
|
|
||
| // commentMarkerTag returns the hidden HTML comment used to find a comment again on later runs. | ||
| // GitHub does not render it, so it never shows up in the PR conversation. | ||
| func commentMarkerTag(marker string) string { | ||
| return fmt.Sprintf("<!-- harness-github-api-plugin:%s -->", marker) | ||
| } | ||
|
|
||
| // resolveCommentBody returns the comment body from COMMENT_BODY, or from the file named by | ||
| // COMMENT_BODY_FILE when it is set. A file lets an earlier Run step in the same stage write a long, | ||
| // multiline report that would be awkward to pass through a plugin setting. | ||
| func resolveCommentBody(body string, bodyFile string) string { | ||
| if bodyFile == "" { | ||
| return body | ||
| } | ||
| content, err := os.ReadFile(bodyFile) | ||
| failOnErr(err) | ||
| return string(content) | ||
| } | ||
|
|
||
| // upsertPrComment creates a comment on a pull request. When marker is set, it first looks for a | ||
| // comment the token owner posted earlier with the same marker and edits that one instead, so a | ||
| // pipeline can keep a single comment per PR up to date across its stages and runs. | ||
| func upsertPrComment(client *github.Client, ctx *context.Context, repositoryName string, repositoryOwner string, | ||
| prNumberString string, body string, marker string) map[string]string { | ||
| prNumber, err := strconv.Atoi(prNumberString) | ||
| failOnErr(err) | ||
|
|
||
| if marker != "" { | ||
| tag := commentMarkerTag(marker) | ||
| body = body + "\n\n" + tag | ||
|
|
||
| existing := findMarkedComment(client, ctx, repositoryName, repositoryOwner, prNumber, tag) | ||
| if existing != nil { | ||
| fmt.Printf("Updating comment %d (marker %q)\n", existing.GetID(), marker) | ||
| comment, _, err := client.Issues.EditComment(*ctx, repositoryOwner, repositoryName, existing.GetID(), | ||
| &github.IssueComment{Body: &body}) | ||
| failOnErr(err) | ||
| return commentFields(comment, "updated") | ||
| } | ||
| } | ||
|
|
||
| fmt.Printf("Creating comment on pull request %d\n", prNumber) | ||
| comment, _, err := client.Issues.CreateComment(*ctx, repositoryOwner, repositoryName, prNumber, | ||
| &github.IssueComment{Body: &body}) | ||
| failOnErr(err) | ||
| return commentFields(comment, "created") | ||
| } | ||
|
|
||
| // findMarkedComment returns the most recent comment on the PR that carries tag and was written by the | ||
| // token owner, or nil. Comments by other users are ignored: the token usually cannot edit them, and | ||
| // someone quoting the bot's comment must not hijack it. | ||
| func findMarkedComment(client *github.Client, ctx *context.Context, repositoryName string, repositoryOwner string, | ||
| prNumber int, tag string) *github.IssueComment { | ||
| me, _, err := client.Users.Get(*ctx, "") | ||
| failOnErr(err) | ||
|
|
||
| var found *github.IssueComment | ||
| opts := &github.IssueListCommentsOptions{ListOptions: github.ListOptions{PerPage: 100}} | ||
| for { | ||
| comments, resp, err := client.Issues.ListComments(*ctx, repositoryOwner, repositoryName, prNumber, opts) | ||
| failOnErr(err) | ||
| for _, c := range comments { | ||
| if c.GetUser().GetLogin() == me.GetLogin() && strings.Contains(c.GetBody(), tag) { | ||
| found = c | ||
| } | ||
| } | ||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
| opts.Page = resp.NextPage | ||
| } | ||
| return found | ||
| } | ||
|
|
||
| func commentFields(comment *github.IssueComment, action string) map[string]string { | ||
| return map[string]string{ | ||
| "COMMENT_ID": strconv.FormatInt(comment.GetID(), 10), | ||
| "COMMENT_URL": comment.GetHTMLURL(), | ||
| "COMMENT_ACTION": action, | ||
| } | ||
| } | ||
|
|
||
| // addReaction adds an emoji reaction to an issue or pull request comment, e.g. to acknowledge the | ||
| // comment that triggered a pipeline. GitHub returns the existing reaction if it is already there. | ||
| func addReaction(client *github.Client, ctx *context.Context, repositoryName string, repositoryOwner string, | ||
| commentIdString string, reaction string) { | ||
| if !slices.Contains(allowedReactions, reaction) { | ||
| log.Fatalf("Unsupported reaction %q, use one of: %s", reaction, strings.Join(allowedReactions, ", ")) | ||
| } | ||
| commentId, err := strconv.ParseInt(commentIdString, 10, 64) | ||
| failOnErr(err) | ||
| _, _, err = client.Reactions.CreateIssueCommentReaction(*ctx, repositoryOwner, repositoryName, commentId, reaction) | ||
| failOnErr(err) | ||
| } | ||
|
|
||
| // getUserPermission reports a user's permission on the repository, so a pipeline started by a PR | ||
| // comment can check that the commenter is allowed to run it. A user GitHub does not know is reported | ||
| // as "none" instead of failing the step, so the caller decides what to do. | ||
| func getUserPermission(client *github.Client, ctx *context.Context, repositoryName string, repositoryOwner string, | ||
| username string) map[string]string { | ||
| permission := "none" | ||
| level, resp, err := client.Repositories.GetPermissionLevel(*ctx, repositoryOwner, repositoryName, username) | ||
| if err != nil { | ||
| if resp == nil || resp.StatusCode != 404 { | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("User %s not found, reporting permission none\n", username) | ||
| } else { | ||
| permission = level.GetPermission() | ||
| } | ||
| return map[string]string{ | ||
| "PERMISSION": permission, | ||
| "HAS_WRITE_ACCESS": strconv.FormatBool(permission == "admin" || permission == "write"), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null optional settings are not ignored
Medium Severity
When
COMMENT_BODY_FILEorCOMMENT_MARKERis omitted, Harness can still pass the literal stringnull, which this plugin already treats as unset forPR_LABELS.resolveCommentBodythen tries to open a file namednulland fails the step, and anullmarker is treated as a real upsert key instead of always creating a comment.suggestion
if bodyFile == "" || bodyFile == "null" {
return body
}
Additional Locations (1)
comments.go#L43-L44Reviewed by Cursor Bugbot for commit 93e5866. Configure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the latest commit: a new
optionalEnvhelper treatsnullas unset forCOMMENT_BODY,COMMENT_BODY_FILEandCOMMENT_MARKER. CoveringCOMMENT_BODYtoo means anullbody now fails with the "needs COMMENT_BODY or COMMENT_BODY_FILE" error instead of postingnullas a comment.