Skip to content

Commit 91387e9

Browse files
Add harness comment (#295)
* Add the implementation of CreateComment for Harness Code * Add the implementation of CreateComment for Harness Code
1 parent 9d3ec6b commit 91387e9

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
# Changelog
22

3+
## [Unreleased](https://github.com/drone/go-scm/tree/HEAD)
4+
5+
[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.3...HEAD)
6+
7+
**Closed issues:**
8+
9+
- Cron Jobs don't run with Gitea-scm [\#292](https://github.com/drone/go-scm/issues/292)
10+
11+
**Merged pull requests:**
12+
13+
- feat: change as per new contract of webhook in harness code [\#294](https://github.com/drone/go-scm/pull/294) ([abhinav-harness](https://github.com/abhinav-harness))
14+
- Stash pr commits pagination [\#293](https://github.com/drone/go-scm/pull/293) ([raghavharness](https://github.com/raghavharness))
15+
- Added support for branch names containing '&' and '\#' for GetFile Operations. [\#291](https://github.com/drone/go-scm/pull/291) ([senjucanon2](https://github.com/senjucanon2))
16+
17+
## [v1.34.3](https://github.com/drone/go-scm/tree/v1.34.3) (2023-12-20)
18+
19+
[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.2...v1.34.3)
20+
21+
**Merged pull requests:**
22+
23+
- feat: add pr link as coming from new webhook [\#290](https://github.com/drone/go-scm/pull/290) ([abhinav-harness](https://github.com/abhinav-harness))
24+
25+
## [v1.34.2](https://github.com/drone/go-scm/tree/v1.34.2) (2023-12-20)
26+
27+
[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.1...v1.34.2)
28+
29+
**Merged pull requests:**
30+
31+
- feat: support more events in webhook parse in go-scm for gitness [\#289](https://github.com/drone/go-scm/pull/289) ([abhinav-harness](https://github.com/abhinav-harness))
32+
- fix: ref should be branch name for harness code [\#288](https://github.com/drone/go-scm/pull/288) ([abhinav-harness](https://github.com/abhinav-harness))
33+
334
## [v1.34.1](https://github.com/drone/go-scm/tree/v1.34.1) (2023-12-08)
435

536
[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.0...v1.34.1)
@@ -8,6 +39,10 @@
839

940
- fix: use opts for harness list commits [\#286](https://github.com/drone/go-scm/pull/286) ([abhinav-harness](https://github.com/abhinav-harness))
1041

42+
**Merged pull requests:**
43+
44+
- \(maint\) v1.34.1 release prep [\#287](https://github.com/drone/go-scm/pull/287) ([tphoney](https://github.com/tphoney))
45+
1146
## [v1.34.0](https://github.com/drone/go-scm/tree/v1.34.0) (2023-12-07)
1247

1348
[Full Changelog](https://github.com/drone/go-scm/compare/v1.33.0...v1.34.0)

scm/driver/harness/pr.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package harness
77
import (
88
"context"
99
"fmt"
10+
"strconv"
1011
"strings"
1112
"time"
1213

@@ -72,8 +73,15 @@ func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRe
7273
return convertPullRequest(out), res, err
7374
}
7475

75-
func (s *pullService) CreateComment(context.Context, string, int, *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
76-
return nil, nil, scm.ErrNotSupported
76+
func (s *pullService) CreateComment(ctx context.Context, repo string, prNumber int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
77+
harnessQueryParams := fmt.Sprintf("?accountIdentifier=%s&orgIdentifier=%s&projectIdentifier=%s", s.client.account, s.client.organization, s.client.project)
78+
path := fmt.Sprintf("api/v1/repos/%s/pullreq/%d/comments%s", repo, prNumber, harnessQueryParams)
79+
in := &prComment{
80+
Text: input.Body,
81+
}
82+
out := new(prCommentResponse)
83+
res, err := s.client.do(ctx, "POST", path, in, out)
84+
return convertComment(out), res, err
7785
}
7886

7987
func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Response, error) {
@@ -165,6 +173,42 @@ type (
165173
Sha string `json:"sha"`
166174
Title string `json:"title"`
167175
}
176+
prComment struct {
177+
LineEnd int `json:"line_end"`
178+
LineEndNew bool `json:"line_end_new"`
179+
LineStart int `json:"line_start"`
180+
LineStartNew bool `json:"line_start_new"`
181+
ParentID int `json:"parent_id"`
182+
Path string `json:"path"`
183+
SourceCommitSha string `json:"source_commit_sha"`
184+
TargetCommitSha string `json:"target_commit_sha"`
185+
Text string `json:"text"`
186+
}
187+
prCommentResponse struct {
188+
Id int `json:"id"`
189+
Created int64 `json:"created"`
190+
Updated int64 `json:"updated"`
191+
Edited int64 `json:"edited"`
192+
ParentId interface{} `json:"parent_id"`
193+
RepoId int `json:"repo_id"`
194+
PullreqId int `json:"pullreq_id"`
195+
Order int `json:"order"`
196+
SubOrder int `json:"sub_order"`
197+
Type string `json:"type"`
198+
Kind string `json:"kind"`
199+
Text string `json:"text"`
200+
Payload struct{} `json:"payload"`
201+
Metadata interface{} `json:"metadata"`
202+
Author struct {
203+
Id int `json:"id"`
204+
Uid string `json:"uid"`
205+
DisplayName string `json:"display_name"`
206+
Email string `json:"email"`
207+
Type string `json:"type"`
208+
Created int64 `json:"created"`
209+
Updated int64 `json:"updated"`
210+
} `json:"author"`
211+
}
168212
)
169213

170214
// native data structure conversion
@@ -246,3 +290,20 @@ func convertPullRequestList(from []*pr) []*scm.PullRequest {
246290
}
247291
return to
248292
}
293+
294+
func convertComment(comment *prCommentResponse) *scm.Comment {
295+
return &scm.Comment{
296+
ID: comment.Id,
297+
Body: comment.Text,
298+
Author: scm.User{
299+
Login: comment.Author.Uid,
300+
Name: comment.Author.DisplayName,
301+
ID: strconv.Itoa(comment.Author.Id),
302+
Email: comment.Author.Email,
303+
Created: time.UnixMilli(comment.Author.Created),
304+
Updated: time.UnixMilli(comment.Author.Updated),
305+
},
306+
Created: time.UnixMilli(comment.Created),
307+
Updated: time.UnixMilli(comment.Updated),
308+
}
309+
}

scm/driver/harness/pr_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package harness
77
import (
88
"context"
99
"encoding/json"
10+
"fmt"
11+
"github.com/google/go-cmp/cmp/cmpopts"
1012
"io/ioutil"
1113
"net/http"
1214
"testing"
@@ -134,3 +136,45 @@ func TestPullCreate(t *testing.T) {
134136
t.Log(diff)
135137
}
136138
}
139+
140+
func TestPRComment(t *testing.T) {
141+
defer gock.Off()
142+
gock.New(gockOrigin).
143+
Post(fmt.Sprintf("/gateway/code/api/v1/repos/%s/pullreq/1/comments", harnessRepo)).
144+
MatchParam("accountIdentifier", harnessAccount).
145+
MatchParam("orgIdentifier", harnessOrg).
146+
MatchParam("projectIdentifier", harnessProject).
147+
Reply(201).
148+
Type("plain/text").
149+
File("testdata/comment.json")
150+
151+
client, _ := New(gockOrigin, harnessAccount, harnessOrg, harnessProject)
152+
client.Client = &http.Client{
153+
Transport: &transport.Custom{
154+
Before: func(r *http.Request) {
155+
r.Header.Set("x-api-key", harnessPAT)
156+
},
157+
},
158+
}
159+
160+
input := scm.CommentInput{
161+
Body: "Comment to be created in the PR",
162+
}
163+
164+
got, _, err := client.PullRequests.CreateComment(context.Background(), harnessRepo, 1, &input)
165+
if err != nil {
166+
t.Error(err)
167+
return
168+
}
169+
170+
want := new(scm.Comment)
171+
raw, _ := ioutil.ReadFile("testdata/comment.json.golden")
172+
_ = json.Unmarshal(raw, want)
173+
174+
if diff := cmp.Diff(got, want,
175+
cmpopts.IgnoreFields(scm.Comment{}, "Created", "Updated"),
176+
cmpopts.IgnoreFields(scm.User{}, "Created", "Updated")); diff != "" {
177+
t.Errorf("Unexpected Results")
178+
t.Log(diff)
179+
}
180+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"id": 123,
3+
"created": 1708354973112,
4+
"updated": 1708354973112,
5+
"edited": 1708354973112,
6+
"parent_id": null,
7+
"repo_id": 123,
8+
"pullreq_id": 123,
9+
"order": 1,
10+
"sub_order": 0,
11+
"type": "comment",
12+
"kind": "comment",
13+
"text": "Comment to be created in the PR",
14+
"payload": {},
15+
"metadata": null,
16+
"author": {
17+
"id": 1,
18+
"uid": "identifier",
19+
"display_name": "displayName",
20+
"email": "[email protected]",
21+
"type": "service",
22+
"created": 1695706039266,
23+
"updated": 1695706039266
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"ID": 123,
3+
"Body": "Comment to be created in the PR",
4+
"Author": {
5+
"ID": "1",
6+
"Login": "identifier",
7+
"Name": "displayName",
8+
"Email": "[email protected]",
9+
"Avatar": "",
10+
"Created": 1695706039266,
11+
"Updated": 1695706039266
12+
},
13+
"Created": 1708354973112,
14+
"Updated": 1708354973112
15+
}

0 commit comments

Comments
 (0)