Skip to content

Commit

Permalink
(feat) handle harness org structure
Browse files Browse the repository at this point in the history
  • Loading branch information
TP Honey committed Feb 6, 2023
1 parent 1c3c0bb commit 74b60c4
Show file tree
Hide file tree
Showing 92 changed files with 55 additions and 4,181 deletions.
13 changes: 11 additions & 2 deletions scm/driver/harness/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type contentService struct {
}

func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
endpoint := fmt.Sprintf("api/v1/repos/%s/content/%s?%s", repo, path, scm.TrimRef(ref))
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/content/%s?%s&include_commit=true", repo, path, ref)
out := new(fileContent)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
// decode raw output content
Expand All @@ -43,8 +44,16 @@ func (s *contentService) Delete(ctx context.Context, repo, path string, params *
return nil, scm.ErrNotSupported
}

func buildHarnessURI(account, organization, project, repo string) (uri string) {
if account != "" {
return fmt.Sprintf("%s/%s/%s/%s/+", account, organization, project, repo)
}
return repo
}

func (s *contentService) List(ctx context.Context, repo, path, ref string, _ scm.ListOptions) ([]*scm.ContentInfo, *scm.Response, error) {
endpoint := fmt.Sprintf("api/v1/repos/%s/content/%s?%s", repo, path, scm.TrimRef(ref))
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/content/%s?%s&include_commit=true", repo, path, ref)
out := new(contentList)
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertContentInfoList(out.Content.Entries), res, err
Expand Down
33 changes: 30 additions & 3 deletions scm/driver/harness/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestContentFind(t *testing.T) {
Type("plain/text").
File("testdata/content.json")

client, _ := New(gockOrigin)
client, _ := New(gockOrigin, "", "", "")
result, _, err := client.Contents.Find(
context.Background(),
"1",
"README.md",
"",
"98189d5cf2a751a6246c24a72945ba70839f1b20",
)
if err != nil {
t.Error(err)
Expand All @@ -52,7 +52,7 @@ func TestContentList(t *testing.T) {
Type("application/json").
File("testdata/content_list.json")

client, _ := New(gockOrigin)
client, _ := New(gockOrigin, "", "", "")
got, _, err := client.Contents.List(
context.Background(),
"1",
Expand All @@ -73,3 +73,30 @@ func TestContentList(t *testing.T) {
t.Log(diff)
}
}

// func TestContentFindHarness(t *testing.T) {
// client, _ := New(gockOrigin, "px7xd_BFRCi-pfWPYXVjvw", "default", "codeciintegration")
// client.Client = &http.Client{
// Transport: &transport.Custom{
// Before: func(r *http.Request) {
// r.Header.Set("x-api-key", "")
// },
// },
// }
// result, _, err := client.Contents.Find(
// context.Background(),
// "demo",
// "README.md",
// "",
// )
// if err != nil {
// t.Error(err)
// }

// if got, want := result.Path, "README.md"; got != want {
// t.Errorf("Want file Path %q, got %q", want, got)
// }
// if got, want := string(result.Data), "# string\nstrinasdasdsag"; got != want {
// t.Errorf("Want file Data %q, got %q", want, got)
// }
// }
197 changes: 0 additions & 197 deletions scm/driver/harness/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,200 +3,3 @@
// license that can be found in the LICENSE file.

package harness

import (
"context"
"encoding/json"
"io/ioutil"
"testing"

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

//
// commit sub-tests
//

func TestGitFindCommit(t *testing.T) {
gock.New("https://try.gitea.io").
Get("/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630").
Reply(200).
Type("application/json").
File("testdata/commit.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.FindCommit(
context.Background(),
"gitea/gitea",
"c43399cad8766ee521b873a32c1652407c5a4630",
)
if err != nil {
t.Error(err)
}

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

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

func TestGitListCommits(t *testing.T) {
gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/commits").
Reply(200).
Type("application/json").
File("testdata/commits.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.ListCommits(context.Background(), "go-gitea/gitea", scm.CommitListOptions{})
if err != nil {
t.Error(err)
}

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

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

func TestGitListChanges(t *testing.T) {
client, _ := New("https://try.gitea.io")
_, _, err := client.Git.ListChanges(context.Background(), "go-gitea/gitea", "f05f642b892d59a0a9ef6a31f6c905a24b5db13a", scm.ListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

func TestGitCompareChanges(t *testing.T) {
client, _ := New("https://try.gitea.io")
_, _, err := client.Git.CompareChanges(
context.Background(),
"go-gitea/gitea",
"d293a2b9d6722dffde7998c953c3087e47a38a83",
"f05f642b892d59a0a9ef6a31f6c905a24b5db13a",
scm.ListOptions{},
)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}

//
// branch sub-tests
//

func TestGitFindBranch(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/branches/master").
Reply(200).
Type("application/json").
File("testdata/branch.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.FindBranch(context.Background(), "go-gitea/gitea", "master")
if err != nil {
t.Error(err)
}

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

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

func TestGitListBranches(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/branches").
Reply(200).
Type("application/json").
File("testdata/branches.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.ListBranches(context.Background(), "go-gitea/gitea", scm.ListOptions{})
if err != nil {
t.Error(err)
}

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

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

//
// tag sub-tests
//

func TestGitFindTag(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/git/refs/tags/v1.0.0").
Reply(200).
Type("application/json").
File("testdata/tag.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.FindTag(context.Background(), "go-gitea/gitea", "v1.0.0")
if err != nil {
t.Error(err)
return
}

want := &scm.Reference{}
raw, _ := ioutil.ReadFile("testdata/tag.json.golden")
json.Unmarshal(raw, &want)

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

func TestGitListTags(t *testing.T) {
defer gock.Off()

gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/git/refs/tags").
Reply(200).
Type("application/json").
File("testdata/tags.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.ListTags(context.Background(), "go-gitea/gitea", scm.ListOptions{})
if err != nil {
t.Error(err)
return
}

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

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
18 changes: 10 additions & 8 deletions scm/driver/harness/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand All @@ -17,16 +18,19 @@ import (
"github.com/drone/go-scm/scm"
)

// New returns a new Gitea API client.
func New(uri string) (*scm.Client, error) {
// 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
}
if !strings.HasSuffix(base.Path, "/") {
base.Path = base.Path + "/"
}
client := &wrapper{new(scm.Client)}
client := &wrapper{new(scm.Client), account, organization, project}
client.BaseURL = base
// initialize services
client.Driver = scm.DriverGitea
Expand All @@ -45,15 +49,13 @@ func New(uri string) (*scm.Client, error) {
return client.Client, nil
}

func NewDefault() *scm.Client {
client, _ := New("https://app.harness.io/gateway")
return client
}

// wraper wraps the Client to provide high level helper functions
// for making http requests and unmarshaling the response.
type wrapper struct {
*scm.Client
account string
organization string
project string
}

// do wraps the Client.Do function by creating the Request and
Expand Down
17 changes: 4 additions & 13 deletions scm/driver/harness/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,24 @@
package harness

import (
"fmt"
"testing"

"github.com/drone/go-scm/scm"
)

func TestClient(t *testing.T) {
client, err := New("https://try.gitea.io")
client, err := New(gockOrigin, "", "", "")
if err != nil {
t.Error(err)
}
if got, want := client.BaseURL.String(), "https://try.gitea.io/"; got != want {
t.Errorf("Want Client URL %q, got %q", want, got)
}
}

func TestClient_Base(t *testing.T) {
client, err := New("https://try.gitea.io/v1")
if err != nil {
t.Error(err)
}
if got, want := client.BaseURL.String(), "https://try.gitea.io/v1/"; got != want {
if got, want := client.BaseURL.String(), fmt.Sprintf("%s/", gockOrigin); got != want {
t.Errorf("Want Client URL %q, got %q", want, got)
}
}

func TestClient_Error(t *testing.T) {
_, err := New("http://a b.com/")
_, err := New("http://a b.com/", "", "", "")
if err == nil {
t.Errorf("Expect error when invalid URL")
}
Expand Down
Loading

0 comments on commit 74b60c4

Please sign in to comment.