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) harness, add webhook parsing #244

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions scm/driver/harness/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit
}

func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*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/%s", harnessURI, id)
out := new(hook)
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertHook(out), res, err
}

func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Perm, *scm.Response, error) {
Expand All @@ -49,15 +53,32 @@ func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*
}

func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm.ListOptions) ([]*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?sort=display_name&order=asc&%s", harnessURI, encodeListOptions(opts))
out := []*hook{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertHookList(out), res, err
}

func (s *repositoryService) ListStatus(ctx context.Context, repo string, ref string, opts scm.ListOptions) ([]*scm.Status, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

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 @@ -69,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 @@ -97,6 +120,24 @@ type (
NumMergedPulls int `json:"num_merged_pulls"`
GitURL string `json:"git_url"`
}
hook struct {
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"`
Insecure bool `json:"insecure"`
LatestExecutionResult string `json:"latest_execution_result"`
ParentID int `json:"parent_id"`
ParentType string `json:"parent_type"`
Triggers []string `json:"triggers"`
Updated int `json:"updated"`
URL string `json:"url"`
Version int `json:"version"`
}
)

//
Expand Down Expand Up @@ -125,3 +166,22 @@ func convertRepository(src *repository) *scm.Repository {
// Updated: time.Unix(src.Updated, 0),
}
}

func convertHookList(from []*hook) []*scm.Hook {
to := []*scm.Hook{}
for _, v := range from {
to = append(to, convertHook(v))
}
return to
}

func convertHook(from *hook) *scm.Hook {
return &scm.Hook{
ID: strconv.Itoa(from.ID),
Name: from.DisplayName,
Active: from.Enabled,
Target: from.URL,
Events: from.Triggers,
SkipVerify: from.Insecure,
}
}
136 changes: 136 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 @@ -89,3 +91,137 @@ func TestRepositoryList(t *testing.T) {
t.Log(diff)
}
}

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

gock.New(gockOrigin).
Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks").
MatchParam("page", "1").
MatchParam("limit", "30").
MatchParam("sort", "display_name").
MatchParam("order", "asc").
Reply(200).
Type("application/json").
File("testdata/hooks.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)
},
},
}
got, _, err := client.Repositories.ListHooks(context.Background(), harnessRepo, scm.ListOptions{Page: 1, Size: 30})
if err != nil {
t.Error(err)
return
}

want := []*scm.Hook{}
raw, _ := ioutil.ReadFile("testdata/hooks.json.golden")
_ = json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

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

gock.New(gockOrigin).
Get("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/webhooks/6").
Reply(200).
Type("application/json").
File("testdata/hook.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)
},
},
}
got, _, err := client.Repositories.FindHook(context.Background(), harnessRepo, "6")
if err != nil {
t.Error(err)
return
}

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

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
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.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 6,
"version": 1,
"parent_id": 11,
"parent_type": "repo",
"created_by": 14,
"created": 1675867490853,
"updated": 1675867531549,
"display_name": "webhookname",
"description": "webhookdescription",
"url": "http://1.1.1.1",
"enabled": true,
"insecure": true,
"triggers": [],
"latest_execution_result": "success",
"has_secret": true
}
8 changes: 8 additions & 0 deletions scm/driver/harness/testdata/hook.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ID": "6",
"Name": "webhookname",
"Target": "http://1.1.1.1",
"Events": [],
"Active": true,
"SkipVerify": true
}
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
}
19 changes: 19 additions & 0 deletions scm/driver/harness/testdata/hooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"id": 6,
"version": 1,
"parent_id": 11,
"parent_type": "repo",
"created_by": 14,
"created": 1675867490853,
"updated": 1675867531549,
"display_name": "webhookname",
"description": "webhookdescription",
"url": "http://1.1.1.1",
"enabled": true,
"insecure": true,
"triggers": [],
"latest_execution_result": "success",
"has_secret": true
}
]
10 changes: 10 additions & 0 deletions scm/driver/harness/testdata/hooks.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"ID": "6",
"Name": "webhookname",
"Target": "http://1.1.1.1",
"Events": [],
"Active": true,
"SkipVerify": true
}
]
Loading