Skip to content

Commit 2df2d3b

Browse files
author
Tyler Sullens
committed
require step_key or label for comment id
1 parent fd74cf9 commit 2df2d3b

4 files changed

Lines changed: 34 additions & 17 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ The use of this plugin requires that clusters are being used and that the secret
1111

1212
## 👩‍💻 Usage
1313

14+
>The plugin expects at least one of `BUILDKITE_STEP_KEY` or `BUILDKITE_LABEL` to be set for proper usage.
15+
1416
Add the following to your `pipeline.yml`:
1517

1618
```yaml
1719
steps:
20+
key: approval-comment
1821
command: echo "~~~ :github: Add approval comment Pull Request"
1922
plugins:
2023
- pr-commenter#v0.3.0:
@@ -24,9 +27,11 @@ Add the following to your `pipeline.yml`:
2427
2528
### Enabling "Sticky" comments
2629
27-
Set `allow-repeats: false` in order to post and update a single comment.
30+
Set `allow-repeats: false` in order to post and update a single comment. This configuration relies on `BUILDKITE_STEP_KEY` or `BUILDKITE_LABEL` being set _**and unique to the step**_.
31+
2832
```yaml
2933
steps:
34+
key: approval-comment
3035
command: echo "~~~ :github: Add approval comment Pull Request"
3136
plugins:
3237
- pr-commenter#v0.3.0:

internal/issue/comment/comment.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ type GitHubClient interface {
2424
EditComment(ctx context.Context, owner, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
2525
}
2626

27-
func NewCommenter(client GitHubClient) *Commenter {
28-
// Create a unique "id" embedded in the comment that identifies the comment generated by this pipeline+job
29-
// The generated "id" is unique to the specific pipeline+job, with the plugin message-id param allowing further uniqueness
30-
messageId := fmt.Sprintf("%s:%s:pr-commenter-buildkite-plugin", os.Getenv("BUILDKITE_PIPELINE_SLUG"), os.Getenv("BUILDKITE_LABEL"))
27+
func NewCommenter(client GitHubClient) (*Commenter, error) {
28+
// Create a unique "id" embedded in the comment that identifies the comment generated by this pipeline+step
29+
// The generated "id" is unique to the specific pipeline+step, with the plugin message-id param allowing further uniqueness
30+
stepKey, found := os.LookupEnv("BUILDKITE_STEP_KEY")
31+
if !found {
32+
stepKey, found = os.LookupEnv("BUILDKITE_LABEL")
33+
if !found {
34+
return nil, errors.New("At least one of BUILDKITE_STEP_KEY or BUILDKITE_LABEL must be set.")
35+
}
36+
}
37+
38+
messageId := fmt.Sprintf("%s:%s:pr-commenter-buildkite-plugin", os.Getenv("BUILDKITE_PIPELINE_SLUG"), stepKey)
3139
uniqueId, found := os.LookupEnv(common.PluginPrefix + "MESSAGE_ID")
3240
if found {
3341
messageId = fmt.Sprintf("%s:%s", messageId, uniqueId)
@@ -36,7 +44,7 @@ func NewCommenter(client GitHubClient) *Commenter {
3644
return &Commenter{
3745
client: client,
3846
messageId: messageId,
39-
}
47+
}, nil
4048
}
4149

4250
func (c *Commenter) formatBody(message string) string {

internal/issue/comment/comment_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ func (m *mockGitHubClient) EditComment(ctx context.Context, owner, repo string,
3030

3131
func TestPost(t *testing.T) {
3232
t.Setenv("BUILDKITE_PIPELINE_SLUG", "test-pipeline")
33-
t.Setenv("BUILDKITE_LABEL", "test-label")
33+
t.Setenv("BUILDKITE_STEP_KEY", "test")
3434
t.Setenv(common.PluginPrefix+"MESSAGE_ID", "1")
3535

3636
mockClient := &mockGitHubClient{
3737
createComment: func(ctx context.Context, owner, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
38-
if owner != "testdev" || repo != "hello" || number != 420 || *comment.Body != "Test comment\n\n<!-- test-pipeline:test-label:pr-commenter-buildkite-plugin:1 -->" {
38+
if owner != "testdev" || repo != "hello" || number != 420 || *comment.Body != "Test comment\n\n<!-- test-pipeline:test:pr-commenter-buildkite-plugin:1 -->" {
3939
t.Errorf("Unexpected arguments: owner=%s, repo=%s, number=%d, body=%s", owner, repo, number, *comment.Body)
4040
}
4141
return nil, nil, nil
4242
},
4343
}
4444

45-
commenter := comment.NewCommenter(mockClient)
45+
commenter, _ := comment.NewCommenter(mockClient)
4646

4747
err := commenter.Post(context.Background(), "testdev", "hello", "420", "Test comment")
4848
if err != nil {
@@ -52,7 +52,7 @@ func TestPost(t *testing.T) {
5252

5353
func TestPostCommentEmptyBody(t *testing.T) {
5454
mockClient := &mockGitHubClient{}
55-
commenter := comment.NewCommenter(mockClient)
55+
commenter, _ := comment.NewCommenter(mockClient)
5656

5757
err := commenter.Post(context.Background(), "testdev", "hello", "69", "")
5858
if err == nil {
@@ -62,11 +62,11 @@ func TestPostCommentEmptyBody(t *testing.T) {
6262

6363
func TestFindExistingComment_Found(t *testing.T) {
6464
t.Setenv("BUILDKITE_PIPELINE_SLUG", "test-pipeline")
65-
t.Setenv("BUILDKITE_LABEL", "test-label")
65+
t.Setenv("BUILDKITE_STEP_KEY", "test")
6666
t.Setenv(common.PluginPrefix+"MESSAGE_ID", "1")
6767

6868
expectedID := int64(123)
69-
expectedBody := "Test comment\n\n<!-- test-pipeline:test-label:pr-commenter-buildkite-plugin:1 -->"
69+
expectedBody := "Test comment\n\n<!-- test-pipeline:test:pr-commenter-buildkite-plugin:1 -->"
7070
expectedURL := "https://github.com/test/repo/pull/1#issuecomment-123"
7171

7272
mockClient := &mockGitHubClient{
@@ -84,7 +84,7 @@ func TestFindExistingComment_Found(t *testing.T) {
8484
},
8585
}
8686

87-
commenter := comment.NewCommenter(mockClient)
87+
commenter, _ := comment.NewCommenter(mockClient)
8888
result, err := commenter.FindExistingComment(context.Background(), "testdev", "hello", "320")
8989

9090
if err != nil {
@@ -100,11 +100,11 @@ func TestFindExistingComment_Found(t *testing.T) {
100100

101101
func TestUpdateComment_Success(t *testing.T) {
102102
t.Setenv("BUILDKITE_PIPELINE_SLUG", "test-pipeline")
103-
t.Setenv("BUILDKITE_LABEL", "test-label")
103+
t.Setenv("BUILDKITE_STEP_KEY", "test")
104104
t.Setenv(common.PluginPrefix+"MESSAGE_ID", "1")
105105

106106
commentID := int64(456)
107-
expectedBody := "Updated comment\n\n<!-- test-pipeline:test-label:pr-commenter-buildkite-plugin:1 -->"
107+
expectedBody := "Updated comment\n\n<!-- test-pipeline:test:pr-commenter-buildkite-plugin:1 -->"
108108

109109
mockClient := &mockGitHubClient{
110110
editComment: func(ctx context.Context, owner, repo string, id int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
@@ -118,7 +118,7 @@ func TestUpdateComment_Success(t *testing.T) {
118118
},
119119
}
120120

121-
commenter := comment.NewCommenter(mockClient)
121+
commenter, _ := comment.NewCommenter(mockClient)
122122
err := commenter.UpdateComment(context.Background(), "testdev", "hello", "Updated comment", commentID)
123123

124124
if err != nil {

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ func run() exitCode {
6060
fmt.Fprintf(os.Stderr, "Error creating GitHub client: %s\n", err)
6161
return exitError
6262
}
63-
commenter := comment.NewCommenter(client)
63+
commenter, err := comment.NewCommenter(client)
64+
if err != nil {
65+
fmt.Fprintf(os.Stderr, "Error configuring commenter: %s\n", err)
66+
return exitError
67+
}
6468

6569
message, found := os.LookupEnv(common.PluginPrefix + "MESSAGE")
6670
if !found {

0 commit comments

Comments
 (0)