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 user and compare branches #246

Merged
merged 1 commit into from
Feb 9, 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
17 changes: 15 additions & 2 deletions scm/driver/harness/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package harness
import (
"context"
"fmt"
"io"
"strings"
"time"

"github.com/drone/go-scm/scm"
Expand Down Expand Up @@ -68,7 +70,19 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
}

func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *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/compare/%s...%s", harnessURI, source, target)
res, err := s.client.do(ctx, "GET", path, nil, nil)
// convert response to a string
buf := new(strings.Builder)
_, _ = io.Copy(buf, res.Body)
changes := []*scm.Change{
{
Path: "not implemented",
Sha: buf.String(),
},
}
return changes, res, err
}

// native data structures
Expand All @@ -92,7 +106,6 @@ type (
Sha string `json:"sha"`
Title string `json:"title"`
}

branch struct {
Commit struct {
Author struct {
Expand Down
31 changes: 27 additions & 4 deletions scm/driver/harness/testdata/branches.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
[
{
"name": "bla",
"sha": "0c221fd126b9457d0ad2037641416083549f59c5",
"commit": {
"sha": "0c221fd126b9457d0ad2037641416083549f59c5",
"title": "Create bla_file",
"message": "Create bla_file",
"author": {
"identity": {
"name": "thomas.honey",
"email": "thomas.honey@harness.io"
},
"when": "2023-02-09T11:20:14Z"
},
"committer": {
"identity": {
"name": "Harness",
"email": "noreply@harness.io"
},
"when": "2023-02-09T11:20:14Z"
}
}
},
{
"name": "main",
"sha": "1d640265d8bdd818175fa736f0fcbad2c9b716c9",
"sha": "de2837f8911710cfb7bbb323d0de285fd2ef9155",
"commit": {
"sha": "1d640265d8bdd818175fa736f0fcbad2c9b716c9",
"sha": "de2837f8911710cfb7bbb323d0de285fd2ef9155",
"title": "delete README.2",
"message": "delete README.2\n\ndelete README.2",
"author": {
"identity": {
"name": "thomas.honey",
"email": "thomas.honey@harness.io"
},
"when": "2023-02-08T16:17:50Z"
"when": "2023-02-09T12:39:37Z"
},
"committer": {
"identity": {
"name": "Harness",
"email": "noreply@harness.io"
},
"when": "2023-02-08T16:17:50Z"
"when": "2023-02-09T12:39:37Z"
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions scm/driver/harness/testdata/branches.json.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"Name": "bla",
"Path": "refs/heads/bla",
"Sha": "0c221fd126b9457d0ad2037641416083549f59c5"
},
{
"Name": "main",
"Path": "refs/heads/main",
Expand Down
9 changes: 9 additions & 0 deletions scm/driver/harness/testdata/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"admin": true,
"blocked": true,
"created": 0,
"display_name": "1",
"email": "2",
"uid": "3",
"updated": 0
}
7 changes: 7 additions & 0 deletions scm/driver/harness/testdata/user.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ID": "3",
"Login": "2",
"Name": "1",
"Email": "2",
"Avatar": ""
}
28 changes: 11 additions & 17 deletions scm/driver/harness/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ func (s *userService) ListEmail(context.Context, scm.ListOptions) ([]*scm.Email,
//

type user struct {
ID int `json:"id"`
Login string `json:"login"`
Username string `json:"username"`
Fullname string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
Admin bool `json:"admin"`
Blocked bool `json:"blocked"`
Created int `json:"created"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
UID string `json:"uid"`
Updated int `json:"updated"`
}

//
Expand All @@ -51,16 +52,9 @@ type user struct {

func convertUser(src *user) *scm.User {
return &scm.User{
Login: userLogin(src),
Avatar: src.Avatar,
Email: src.Email,
Name: src.Fullname,
Login: src.Email,
Email: src.Email,
Name: src.DisplayName,
ID: src.UID,
}
}

func userLogin(src *user) string {
if src.Username != "" {
return src.Username
}
return src.Login
}
51 changes: 51 additions & 0 deletions scm/driver/harness/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,54 @@
// license that can be found in the LICENSE file.

package harness

import (
"context"
"encoding/json"
"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/h2non/gock"
)

func TestUsersFind(t *testing.T) {

defer gock.Off()

gock.New(gockOrigin).
Get("/gateway/code/api/v1/user").
Reply(200).
Type("application/json").
File("testdata/user.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.Users.Find(context.Background())
if err != nil {
t.Error(err)
return
}

want := new(scm.User)
raw, _ := ioutil.ReadFile("testdata/user.json.golden")
wantErr := json.Unmarshal(raw, &want)
if wantErr != nil {
t.Error(wantErr)
return
}

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