Skip to content

Commit

Permalink
(feat) harness, add create / delete webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
TP Honey committed Feb 8, 2023
1 parent b8eea16 commit d6e3618
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
20 changes: 18 additions & 2 deletions scm/driver/harness/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,20 @@ func (s *repositoryService) ListStatus(ctx context.Context, repo string, ref str
}

func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *scm.HookInput) (*scm.Hook, *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/webhooks", harnessURI)
in := new(hook)
in.Enabled = true
in.DisplayName = input.Name
in.Secret = input.Secret
in.Insecure = input.SkipVerify
in.URL = input.Target
in.Triggers = append(
input.NativeEvents,
)
out := new(hook)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertHook(out), res, err
}

func (s *repositoryService) CreateStatus(ctx context.Context, repo string, ref string, input *scm.StatusInput) (*scm.Status, *scm.Response, error) {
Expand All @@ -77,7 +90,9 @@ func (s *repositoryService) UpdateHook(ctx context.Context, repo, id string, inp
}

func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
path := fmt.Sprintf("api/v1/repos/%s/webhooks/%s", harnessURI, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}

//
Expand Down Expand Up @@ -112,6 +127,7 @@ type (
DisplayName string `json:"display_name"`
Enabled bool `json:"enabled"`
HasSecret bool `json:"has_secret"`
Secret string `json:"secret"`
ID int `json:"id"`
Insecure bool `json:"insecure"`
LatestExecutionResult string `json:"latest_execution_result"`
Expand Down
64 changes: 64 additions & 0 deletions scm/driver/harness/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ package harness
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"

"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/transport"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/h2non/gock"
)

Expand Down Expand Up @@ -161,3 +163,65 @@ func TestRepositoryFindHook(t *testing.T) {
t.Log(diff)
}
}

func TestRepositoryHookCreateDelete(t *testing.T) {
if harnessPAT == "" {
defer gock.Off()

gock.New(gockOrigin).
Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks").
Reply(200).
Type("application/json").
File("testdata/hook_create.json")
}
client, _ := New(gockOrigin, harnessOrg, harnessAccount, harnessProject)
client.Client = &http.Client{
Transport: &transport.Custom{
Before: func(r *http.Request) {
r.Header.Set("x-api-key", harnessPAT)
},
},
}
in := &scm.HookInput{
Name: "drone",
Target: "https://example.com",
Secret: "topsecret",
SkipVerify: true,
}
got, _, err := client.Repositories.CreateHook(context.Background(), harnessRepo, in)
if err != nil {
t.Error(err)
return
}

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

if diff := cmp.Diff(got, want, cmpopts.IgnoreFields(scm.Hook{}, "ID")); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
// delete webhook
if harnessPAT == "" {
defer gock.Off()

gock.New(gockOrigin).
Delete(fmt.Sprintf("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks/%s", got.ID)).
Reply(204)
}
client, _ = New(gockOrigin, harnessOrg, harnessAccount, harnessProject)
client.Client = &http.Client{
Transport: &transport.Custom{
Before: func(r *http.Request) {
r.Header.Set("x-api-key", harnessPAT)
},
},
}

_, deleteErr := client.Repositories.DeleteHook(context.Background(), harnessRepo, got.ID)
if deleteErr != nil {
t.Error(deleteErr)
return
}
}
17 changes: 17 additions & 0 deletions scm/driver/harness/testdata/hook_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 9,
"version": 1,
"parent_id": 11,
"parent_type": "repo",
"created_by": 14,
"created": 1675872629243,
"updated": 1675872777592,
"display_name": "drone",
"description": "",
"url": "https://example.com",
"enabled": true,
"insecure": true,
"triggers": [],
"latest_execution_result": "success",
"has_secret": true
}
8 changes: 8 additions & 0 deletions scm/driver/harness/testdata/hook_create.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ID": "9",
"Name": "drone",
"Target": "https://example.com",
"Events": [],
"Active": true,
"SkipVerify": true
}

0 comments on commit d6e3618

Please sign in to comment.