Skip to content

Commit 9d3ec6b

Browse files
feat: change as per new contract of webhook in harness code (#294)
* feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code * feat: change as per new contract of webhook in harness code
1 parent 294a5aa commit 9d3ec6b

18 files changed

+252
-70
lines changed

scm/driver/harness/git.go

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

1213
"github.com/drone/go-scm/scm"
@@ -72,8 +73,12 @@ func (s *gitService) ListTags(ctx context.Context, repo string, _ scm.ListOption
7273
return nil, nil, scm.ErrNotSupported
7374
}
7475

75-
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
76-
return nil, nil, scm.ErrNotSupported
76+
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
77+
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
78+
path := fmt.Sprintf("api/v1/repos/%s/commits/%s/diff?%s", harnessURI, ref, encodeListOptions(opts))
79+
out := []*fileDiff{}
80+
res, err := s.client.do(ctx, "POST", path, nil, &out)
81+
return convertFileDiffs(out), res, err
7782
}
7883

7984
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
@@ -209,8 +214,8 @@ func convertChange(src *fileDiff) *scm.Change {
209214
return &scm.Change{
210215
Path: src.Path,
211216
PrevFilePath: src.OldPath,
212-
Added: src.Status == "ADDED",
213-
Renamed: src.Status == "RENAMED",
214-
Deleted: src.Status == "DELETED",
217+
Added: strings.EqualFold(src.Status, "ADDED"),
218+
Renamed: strings.EqualFold(src.Status, "RENAMED"),
219+
Deleted: strings.EqualFold(src.Status, "DELETED"),
215220
}
216221
}

scm/driver/harness/harness.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"encoding/json"
1111
"errors"
12-
"fmt"
1312
"io"
1413
"net/http"
1514
"net/url"
@@ -20,9 +19,6 @@ import (
2019

2120
// New returns a new gitness API client.
2221
func New(uri, account, organization, project string) (*scm.Client, error) {
23-
if !((organization == "" && account == "" && project == "") || (organization != "" && account != "" && project != "")) {
24-
return nil, fmt.Errorf("harness account, organization and project are required")
25-
}
2622
base, err := url.Parse(uri)
2723
if err != nil {
2824
return nil, err

scm/driver/harness/pr.go

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

1213
"github.com/drone/go-scm/scm"
@@ -30,7 +31,11 @@ func (s *pullService) FindComment(context.Context, string, int, int) (*scm.Comme
3031
}
3132

3233
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
33-
return nil, nil, scm.ErrNotSupported
34+
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
35+
path := fmt.Sprintf("api/v1/repos/%s/pullreq?%s", harnessURI, encodePullRequestListOptions(opts))
36+
out := []*pr{}
37+
res, err := s.client.do(ctx, "GET", path, nil, &out)
38+
return convertPullRequestList(out), res, err
3439
}
3540

3641
func (s *pullService) ListComments(context.Context, string, int, scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
@@ -45,8 +50,12 @@ func (s *pullService) ListCommits(ctx context.Context, repo string, index int, o
4550
return convertCommits(out), res, err
4651
}
4752

48-
func (s *pullService) ListChanges(context.Context, string, int, scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
49-
return nil, nil, scm.ErrNotSupported
53+
func (s *pullService) ListChanges(ctx context.Context, repo string, number int, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
54+
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
55+
path := fmt.Sprintf("api/v1/repos/%s/pullreq/%d/diff", harnessURI, number)
56+
out := []*fileDiff{}
57+
res, err := s.client.do(ctx, "POST", path, nil, &out)
58+
return convertFileDiffs(out), res, err
5059
}
5160

5261
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
@@ -209,3 +218,31 @@ func convertCommit(src *commit) *scm.Commit {
209218
},
210219
}
211220
}
221+
222+
func convertFileDiffs(diff []*fileDiff) []*scm.Change {
223+
var dst []*scm.Change
224+
for _, v := range diff {
225+
dst = append(dst, convertFileDiff(v))
226+
}
227+
return dst
228+
}
229+
230+
func convertFileDiff(diff *fileDiff) *scm.Change {
231+
return &scm.Change{
232+
Path: diff.Path,
233+
Added: strings.EqualFold(diff.Status, "ADDED"),
234+
Renamed: strings.EqualFold(diff.Status, "RENAMED"),
235+
Deleted: strings.EqualFold(diff.Status, "DELETED"),
236+
Sha: diff.SHA,
237+
BlobID: "",
238+
PrevFilePath: diff.OldPath,
239+
}
240+
}
241+
242+
func convertPullRequestList(from []*pr) []*scm.PullRequest {
243+
to := []*scm.PullRequest{}
244+
for _, v := range from {
245+
to = append(to, convertPullRequest(v))
246+
}
247+
return to
248+
}

scm/driver/harness/repo.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *
7474
path := fmt.Sprintf("api/v1/repos/%s/webhooks", harnessURI)
7575
in := new(hook)
7676
in.Enabled = true
77-
in.DisplayName = input.Name
78-
in.UID = input.Name
77+
in.Identifier = input.Name
7978
in.Secret = input.Secret
8079
in.Insecure = input.SkipVerify
8180
in.URL = input.Target
@@ -130,12 +129,10 @@ type (
130129
Created int `json:"created"`
131130
CreatedBy int `json:"created_by"`
132131
Description string `json:"description"`
133-
DisplayName string `json:"display_name"`
134132
Enabled bool `json:"enabled"`
135133
HasSecret bool `json:"has_secret"`
136134
Secret string `json:"secret"`
137-
ID int `json:"id"`
138-
UID string `json:"uid"`
135+
Identifier string `json:"identifier"`
139136
Insecure bool `json:"insecure"`
140137
LatestExecutionResult string `json:"latest_execution_result"`
141138
ParentID int `json:"parent_id"`
@@ -184,8 +181,9 @@ func convertHookList(from []*hook) []*scm.Hook {
184181

185182
func convertHook(from *hook) *scm.Hook {
186183
return &scm.Hook{
187-
ID: strconv.Itoa(from.ID),
188-
Name: from.DisplayName,
184+
// keeping id same as name
185+
ID: from.Identifier,
186+
Name: from.Identifier,
189187
Active: from.Enabled,
190188
Target: from.URL,
191189
Events: from.Triggers,

scm/driver/harness/testdata/hook.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
2-
"id": 6,
32
"version": 1,
43
"parent_id": 11,
54
"parent_type": "repo",
65
"created_by": 14,
76
"created": 1675867490853,
87
"updated": 1675867531549,
9-
"display_name": "webhookname",
8+
"identifier": "webhookname",
109
"description": "webhookdescription",
1110
"url": "http://1.1.1.1",
1211
"enabled": true,

scm/driver/harness/testdata/hook.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"ID": "6",
2+
"ID": "webhookname",
33
"Name": "webhookname",
44
"Target": "http://1.1.1.1",
55
"Events": [],

scm/driver/harness/testdata/hook_create.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"created_by": 14,
77
"created": 1675872629243,
88
"updated": 1675872777592,
9-
"display_name": "drone",
9+
"identifier": "drone",
1010
"description": "",
1111
"url": "https://example.com",
1212
"enabled": true,

scm/driver/harness/testdata/hooks.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
[
22
{
3-
"id": 6,
43
"version": 1,
54
"parent_id": 11,
65
"parent_type": "repo",
76
"created_by": 14,
87
"created": 1675867490853,
98
"updated": 1675867531549,
10-
"display_name": "webhookname",
9+
"identifier": "webhookname",
1110
"description": "webhookdescription",
1211
"url": "http://1.1.1.1",
1312
"enabled": true,

scm/driver/harness/testdata/hooks.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"ID": "6",
3+
"ID": "webhookname",
44
"Name": "webhookname",
55
"Target": "http://1.1.1.1",
66
"Events": [],

scm/driver/harness/testdata/webhooks/branch_create.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@
4545
"when": "2023-02-01T13:21:15-08:00"
4646
}
4747
},
48+
"head_commit": {
49+
"sha": "aeafa0e2e4ec6909ad75cb8fad57c0b1eb5986e6",
50+
"message": "version 4",
51+
"author": {
52+
"identity": {
53+
"name": "Admin",
54+
"email": "[email protected]"
55+
},
56+
"when": "2023-02-01T13:21:15-08:00"
57+
},
58+
"committer": {
59+
"identity": {
60+
"name": "Admin",
61+
"email": "[email protected]"
62+
},
63+
"when": "2023-02-01T13:21:15-08:00"
64+
}
65+
},
4866
"old_sha": "0000000000000000000000000000000000000000",
4967
"forced": false
5068
}

0 commit comments

Comments
 (0)