Skip to content
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

feat: change as per new contract of webhook in harness code #294

Merged
merged 8 commits into from
Feb 9, 2024
15 changes: 10 additions & 5 deletions scm/driver/harness/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package harness
import (
"context"
"fmt"
"strings"
"time"

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

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

func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
Expand Down Expand Up @@ -209,8 +214,8 @@ func convertChange(src *fileDiff) *scm.Change {
return &scm.Change{
Path: src.Path,
PrevFilePath: src.OldPath,
Added: src.Status == "ADDED",
Renamed: src.Status == "RENAMED",
Deleted: src.Status == "DELETED",
Added: strings.EqualFold(src.Status, "ADDED"),
Renamed: strings.EqualFold(src.Status, "RENAMED"),
Deleted: strings.EqualFold(src.Status, "DELETED"),
}
}
4 changes: 0 additions & 4 deletions scm/driver/harness/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand All @@ -20,9 +19,6 @@ import (

// New returns a new gitness API client.
func New(uri, account, organization, project string) (*scm.Client, error) {
if !((organization == "" && account == "" && project == "") || (organization != "" && account != "" && project != "")) {
return nil, fmt.Errorf("harness account, organization and project are required")
}
base, err := url.Parse(uri)
if err != nil {
return nil, err
Expand Down
43 changes: 40 additions & 3 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"
"strings"
"time"

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

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

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

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

func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
Expand Down Expand Up @@ -209,3 +218,31 @@ func convertCommit(src *commit) *scm.Commit {
},
}
}

func convertFileDiffs(diff []*fileDiff) []*scm.Change {
var dst []*scm.Change
for _, v := range diff {
dst = append(dst, convertFileDiff(v))
}
return dst
}

func convertFileDiff(diff *fileDiff) *scm.Change {
return &scm.Change{
Path: diff.Path,
Added: strings.EqualFold(diff.Status, "ADDED"),
Renamed: strings.EqualFold(diff.Status, "RENAMED"),
Deleted: strings.EqualFold(diff.Status, "DELETED"),
Sha: diff.SHA,
BlobID: "",
PrevFilePath: diff.OldPath,
}
}

func convertPullRequestList(from []*pr) []*scm.PullRequest {
to := []*scm.PullRequest{}
for _, v := range from {
to = append(to, convertPullRequest(v))
}
return to
}
12 changes: 5 additions & 7 deletions scm/driver/harness/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *
path := fmt.Sprintf("api/v1/repos/%s/webhooks", harnessURI)
in := new(hook)
in.Enabled = true
in.DisplayName = input.Name
in.UID = input.Name
in.Identifier = input.Name
in.Secret = input.Secret
in.Insecure = input.SkipVerify
in.URL = input.Target
Expand Down Expand Up @@ -130,12 +129,10 @@ type (
Created int `json:"created"`
CreatedBy int `json:"created_by"`
Description string `json:"description"`
DisplayName string `json:"display_name"`
Enabled bool `json:"enabled"`
HasSecret bool `json:"has_secret"`
Secret string `json:"secret"`
ID int `json:"id"`
UID string `json:"uid"`
Identifier string `json:"identifier"`
Insecure bool `json:"insecure"`
LatestExecutionResult string `json:"latest_execution_result"`
ParentID int `json:"parent_id"`
Expand Down Expand Up @@ -184,8 +181,9 @@ func convertHookList(from []*hook) []*scm.Hook {

func convertHook(from *hook) *scm.Hook {
return &scm.Hook{
ID: strconv.Itoa(from.ID),
Name: from.DisplayName,
// keeping id same as name
ID: from.Identifier,
Name: from.Identifier,
Active: from.Enabled,
Target: from.URL,
Events: from.Triggers,
Expand Down
3 changes: 1 addition & 2 deletions scm/driver/harness/testdata/hook.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"id": 6,
"version": 1,
"parent_id": 11,
"parent_type": "repo",
"created_by": 14,
"created": 1675867490853,
"updated": 1675867531549,
"display_name": "webhookname",
"identifier": "webhookname",
"description": "webhookdescription",
"url": "http://1.1.1.1",
"enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion scm/driver/harness/testdata/hook.json.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ID": "6",
"ID": "webhookname",
"Name": "webhookname",
"Target": "http://1.1.1.1",
"Events": [],
Expand Down
2 changes: 1 addition & 1 deletion scm/driver/harness/testdata/hook_create.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"created_by": 14,
"created": 1675872629243,
"updated": 1675872777592,
"display_name": "drone",
"identifier": "drone",
"description": "",
"url": "https://example.com",
"enabled": true,
Expand Down
3 changes: 1 addition & 2 deletions scm/driver/harness/testdata/hooks.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[
{
"id": 6,
"version": 1,
"parent_id": 11,
"parent_type": "repo",
"created_by": 14,
"created": 1675867490853,
"updated": 1675867531549,
"display_name": "webhookname",
"identifier": "webhookname",
"description": "webhookdescription",
"url": "http://1.1.1.1",
"enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion scm/driver/harness/testdata/hooks.json.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"ID": "6",
"ID": "webhookname",
"Name": "webhookname",
"Target": "http://1.1.1.1",
"Events": [],
Expand Down
18 changes: 18 additions & 0 deletions scm/driver/harness/testdata/webhooks/branch_create.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
"when": "2023-02-01T13:21:15-08:00"
}
},
"head_commit": {
"sha": "aeafa0e2e4ec6909ad75cb8fad57c0b1eb5986e6",
"message": "version 4",
"author": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-02-01T13:21:15-08:00"
},
"committer": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-02-01T13:21:15-08:00"
}
},
"old_sha": "0000000000000000000000000000000000000000",
"forced": false
}
18 changes: 18 additions & 0 deletions scm/driver/harness/testdata/webhooks/branch_updated.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
"when": "2023-12-05T11:59:39Z"
}
},
"head_commit": {
"sha": "92e21bfcddc1418079cddbb518ad6fd72917798a",
"message": "Create asdsad (#2)",
"author": {
"identity": {
"name": "abhinav.singh@harness.io",
"email": "abhinav.singh@harness.io"
},
"when": "2023-12-05T11:59:39Z"
},
"committer": {
"identity": {
"name": "Harness",
"email": "noreply@harness.io"
},
"when": "2023-12-05T11:59:39Z"
}
},
"old_sha": "a273c385628167932e10caaa58e12550c491f241",
"forced": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@
"when": "2023-02-01T13:28:55-08:00"
}
},
"head_commit": {
"sha": "f74f3d2a88d1b7cb19ff3bf069aa423763d341ef",
"message": "updated b2",
"author": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-02-01T13:28:55-08:00"
},
"committer": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-02-01T13:28:55-08:00"
}
},
"old_sha": "d74b1ebfe520ac01b209dd9085f005884cc9f4cd",
"forced": false
}
18 changes: 18 additions & 0 deletions scm/driver/harness/testdata/webhooks/pull_request_closed.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,23 @@
},
"when": "2023-12-20T13:10:47+05:30"
}
},
"head_commit": {
"sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574",
"message": "",
"author": {
"identity": {
"name": "Administrator",
"email": "admin@gitness.io"
},
"when": "2023-12-20T13:10:47+05:30"
},
"committer": {
"identity": {
"name": "Gitness",
"email": "system@gitness.io"
},
"when": "2023-12-20T13:10:47+05:30"
}
}
}
18 changes: 18 additions & 0 deletions scm/driver/harness/testdata/webhooks/pull_request_merged.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,23 @@
},
"when": "2023-12-20T13:40:52+05:30"
}
},
"head_commit": {
"sha": "4ec41187008f77222a60dfa21cdbd980f6490443",
"message": "",
"author": {
"identity": {
"name": "Administrator",
"email": "admin@gitness.io"
},
"when": "2023-12-20T13:40:52+05:30"
},
"committer": {
"identity": {
"name": "Gitness",
"email": "system@gitness.io"
},
"when": "2023-12-20T13:40:52+05:30"
}
}
}
18 changes: 18 additions & 0 deletions scm/driver/harness/testdata/webhooks/pull_request_opened.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,23 @@
},
"when": "2023-01-31T22:01:55-08:00"
}
},
"head_commit": {
"sha": "d74b1ebfe520ac01b209dd9085f005884cc9f4cd",
"message": "Update b.txt",
"author": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-01-31T22:01:55-08:00"
},
"committer": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-01-31T22:01:55-08:00"
}
}
}
18 changes: 18 additions & 0 deletions scm/driver/harness/testdata/webhooks/pull_request_reopened.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,23 @@
},
"when": "2023-02-01T13:28:55-08:00"
}
},
"head_commit": {
"sha": "f74f3d2a88d1b7cb19ff3bf069aa423763d341ef",
"message": "updated b2",
"author": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-02-01T13:28:55-08:00"
},
"committer": {
"identity": {
"name": "Admin",
"email": "admin@harness.io"
},
"when": "2023-02-01T13:28:55-08:00"
}
}
}
Loading