Skip to content

Commit

Permalink
(feat) harness, add list webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
TP Honey committed Feb 8, 2023
1 parent 16252ff commit ff7deab
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
42 changes: 41 additions & 1 deletion scm/driver/harness/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ 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) {
Expand Down Expand Up @@ -97,6 +101,23 @@ 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"`
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 +146,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,
}
}
38 changes: 38 additions & 0 deletions scm/driver/harness/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,41 @@ 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)
}
}
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
}
]

0 comments on commit ff7deab

Please sign in to comment.