Skip to content

Commit

Permalink
Add harness comment (#295)
Browse files Browse the repository at this point in the history
* Add the implementation of CreateComment for Harness Code

* Add the implementation of CreateComment for Harness Code
  • Loading branch information
nassergonzalez authored Feb 21, 2024
1 parent 9d3ec6b commit 91387e9
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 2 deletions.
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changelog

## [Unreleased](https://github.com/drone/go-scm/tree/HEAD)

[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.3...HEAD)

**Closed issues:**

- Cron Jobs don't run with Gitea-scm [\#292](https://github.com/drone/go-scm/issues/292)

**Merged pull requests:**

- 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))
- Stash pr commits pagination [\#293](https://github.com/drone/go-scm/pull/293) ([raghavharness](https://github.com/raghavharness))
- Added support for branch names containing '&' and '\#' for GetFile Operations. [\#291](https://github.com/drone/go-scm/pull/291) ([senjucanon2](https://github.com/senjucanon2))

## [v1.34.3](https://github.com/drone/go-scm/tree/v1.34.3) (2023-12-20)

[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.2...v1.34.3)

**Merged pull requests:**

- 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))

## [v1.34.2](https://github.com/drone/go-scm/tree/v1.34.2) (2023-12-20)

[Full Changelog](https://github.com/drone/go-scm/compare/v1.34.1...v1.34.2)

**Merged pull requests:**

- 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))
- 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))

## [v1.34.1](https://github.com/drone/go-scm/tree/v1.34.1) (2023-12-08)

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

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

**Merged pull requests:**

- \(maint\) v1.34.1 release prep [\#287](https://github.com/drone/go-scm/pull/287) ([tphoney](https://github.com/tphoney))

## [v1.34.0](https://github.com/drone/go-scm/tree/v1.34.0) (2023-12-07)

[Full Changelog](https://github.com/drone/go-scm/compare/v1.33.0...v1.34.0)
Expand Down
65 changes: 63 additions & 2 deletions scm/driver/harness/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package harness
import (
"context"
"fmt"
"strconv"
"strings"
"time"

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

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

func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Response, error) {
Expand Down Expand Up @@ -165,6 +173,42 @@ type (
Sha string `json:"sha"`
Title string `json:"title"`
}
prComment struct {
LineEnd int `json:"line_end"`
LineEndNew bool `json:"line_end_new"`
LineStart int `json:"line_start"`
LineStartNew bool `json:"line_start_new"`
ParentID int `json:"parent_id"`
Path string `json:"path"`
SourceCommitSha string `json:"source_commit_sha"`
TargetCommitSha string `json:"target_commit_sha"`
Text string `json:"text"`
}
prCommentResponse struct {
Id int `json:"id"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Edited int64 `json:"edited"`
ParentId interface{} `json:"parent_id"`
RepoId int `json:"repo_id"`
PullreqId int `json:"pullreq_id"`
Order int `json:"order"`
SubOrder int `json:"sub_order"`
Type string `json:"type"`
Kind string `json:"kind"`
Text string `json:"text"`
Payload struct{} `json:"payload"`
Metadata interface{} `json:"metadata"`
Author struct {
Id int `json:"id"`
Uid string `json:"uid"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
Type string `json:"type"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
} `json:"author"`
}
)

// native data structure conversion
Expand Down Expand Up @@ -246,3 +290,20 @@ func convertPullRequestList(from []*pr) []*scm.PullRequest {
}
return to
}

func convertComment(comment *prCommentResponse) *scm.Comment {
return &scm.Comment{
ID: comment.Id,
Body: comment.Text,
Author: scm.User{
Login: comment.Author.Uid,
Name: comment.Author.DisplayName,
ID: strconv.Itoa(comment.Author.Id),
Email: comment.Author.Email,
Created: time.UnixMilli(comment.Author.Created),
Updated: time.UnixMilli(comment.Author.Updated),
},
Created: time.UnixMilli(comment.Created),
Updated: time.UnixMilli(comment.Updated),
}
}
44 changes: 44 additions & 0 deletions scm/driver/harness/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package harness
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-cmp/cmp/cmpopts"
"io/ioutil"
"net/http"
"testing"
Expand Down Expand Up @@ -134,3 +136,45 @@ func TestPullCreate(t *testing.T) {
t.Log(diff)
}
}

func TestPRComment(t *testing.T) {
defer gock.Off()
gock.New(gockOrigin).
Post(fmt.Sprintf("/gateway/code/api/v1/repos/%s/pullreq/1/comments", harnessRepo)).
MatchParam("accountIdentifier", harnessAccount).
MatchParam("orgIdentifier", harnessOrg).
MatchParam("projectIdentifier", harnessProject).
Reply(201).
Type("plain/text").
File("testdata/comment.json")

client, _ := New(gockOrigin, harnessAccount, harnessOrg, harnessProject)
client.Client = &http.Client{
Transport: &transport.Custom{
Before: func(r *http.Request) {
r.Header.Set("x-api-key", harnessPAT)
},
},
}

input := scm.CommentInput{
Body: "Comment to be created in the PR",
}

got, _, err := client.PullRequests.CreateComment(context.Background(), harnessRepo, 1, &input)
if err != nil {
t.Error(err)
return
}

want := new(scm.Comment)
raw, _ := ioutil.ReadFile("testdata/comment.json.golden")
_ = json.Unmarshal(raw, want)

if diff := cmp.Diff(got, want,
cmpopts.IgnoreFields(scm.Comment{}, "Created", "Updated"),
cmpopts.IgnoreFields(scm.User{}, "Created", "Updated")); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
25 changes: 25 additions & 0 deletions scm/driver/harness/testdata/comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": 123,
"created": 1708354973112,
"updated": 1708354973112,
"edited": 1708354973112,
"parent_id": null,
"repo_id": 123,
"pullreq_id": 123,
"order": 1,
"sub_order": 0,
"type": "comment",
"kind": "comment",
"text": "Comment to be created in the PR",
"payload": {},
"metadata": null,
"author": {
"id": 1,
"uid": "identifier",
"display_name": "displayName",
"email": "[email protected]",
"type": "service",
"created": 1695706039266,
"updated": 1695706039266
}
}
15 changes: 15 additions & 0 deletions scm/driver/harness/testdata/comment.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"ID": 123,
"Body": "Comment to be created in the PR",
"Author": {
"ID": "1",
"Login": "identifier",
"Name": "displayName",
"Email": "[email protected]",
"Avatar": "",
"Created": 1695706039266,
"Updated": 1695706039266
},
"Created": 1708354973112,
"Updated": 1708354973112
}

0 comments on commit 91387e9

Please sign in to comment.