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 finduser #250

Merged
merged 1 commit into from
Feb 13, 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
52 changes: 45 additions & 7 deletions scm/driver/harness/testdata/user.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
{
"admin": true,
"blocked": true,
"created": 0,
"display_name": "1",
"email": "2",
"uid": "3",
"updated": 0
"status": "SUCCESS",
"data": {
"uuid": "0Nnoezs6RGa_fOWvG_Ta4w",
"name": "thomas.honey",
"email": "[email protected]",
"token": null,
"defaultAccountId": "px7xd_BFRCi-pfWPYXVjvw",
"intent": null,
"accounts": [
{
"uuid": "px7xd_BFRCi-pfWPYXVjvw",
"accountName": "harness-dev",
"companyName": "harness-dev",
"defaultExperience": "NG",
"createdFromNG": false,
"nextGenEnabled": true
},
{
"uuid": "Ws0xvw71Sm2YmpSC7A8z4g",
"accountName": "OPA-Governance",
"companyName": "Feature-Flag",
"defaultExperience": "NG",
"createdFromNG": false,
"nextGenEnabled": true
}
],
"admin": false,
"twoFactorAuthenticationEnabled": false,
"emailVerified": true,
"locked": false,
"disabled": false,
"signupAction": null,
"edition": null,
"billingFrequency": null,
"utmInfo": {
"utmSource": null,
"utmContent": null,
"utmMedium": null,
"utmTerm": null,
"utmCampaign": null
},
"externallyManaged": false
},
"metaData": null,
"correlationId": "c4014fdb-10a1-4dc4-ace0-6fad93544993"
}
8 changes: 4 additions & 4 deletions scm/driver/harness/testdata/user.json.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ID": "3",
"Login": "2",
"Name": "1",
"Email": "2",
"ID": "0Nnoezs6RGa_fOWvG_Ta4w",
"Login": "[email protected]",
"Name": "thomas.honey",
"Email": "[email protected]",
"Avatar": ""
}
72 changes: 56 additions & 16 deletions scm/driver/harness/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package harness

import (
"context"
"fmt"
"strings"

"github.com/drone/go-scm/scm"
)
Expand All @@ -15,9 +17,19 @@ type userService struct {
}

func (s *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
out := new(user)
res, err := s.client.do(ctx, "GET", "api/v1/user", nil, out)
return convertUser(out), res, err
out := new(harnessUser)
// the following is for the corporate version of Harness code
tempUserService := *s
// get the basepath
basePath := tempUserService.client.BaseURL.Path
// use the NG user endpoint
basePath = strings.Replace(basePath, "code", "ng", 1)
// set the new basepath
tempUserService.client.BaseURL.Path = basePath
// set the path
path := fmt.Sprintf("api/user/currentUser")
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertHarnessUser(out), res, err
}

func (s *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
Expand All @@ -36,25 +48,53 @@ func (s *userService) ListEmail(context.Context, scm.ListOptions) ([]*scm.Email,
// native data structures
//

type user struct {
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"`
type harnessUser struct {
Status string `json:"status"`
Data struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Email string `json:"email"`
Token interface{} `json:"token"`
Defaultaccountid string `json:"defaultAccountId"`
Intent interface{} `json:"intent"`
Accounts []struct {
UUID string `json:"uuid"`
Accountname string `json:"accountName"`
Companyname string `json:"companyName"`
Defaultexperience string `json:"defaultExperience"`
Createdfromng bool `json:"createdFromNG"`
Nextgenenabled bool `json:"nextGenEnabled"`
} `json:"accounts"`
Admin bool `json:"admin"`
Twofactorauthenticationenabled bool `json:"twoFactorAuthenticationEnabled"`
Emailverified bool `json:"emailVerified"`
Locked bool `json:"locked"`
Disabled bool `json:"disabled"`
Signupaction interface{} `json:"signupAction"`
Edition interface{} `json:"edition"`
Billingfrequency interface{} `json:"billingFrequency"`
Utminfo struct {
Utmsource interface{} `json:"utmSource"`
Utmcontent interface{} `json:"utmContent"`
Utmmedium interface{} `json:"utmMedium"`
Utmterm interface{} `json:"utmTerm"`
Utmcampaign interface{} `json:"utmCampaign"`
} `json:"utmInfo"`
Externallymanaged bool `json:"externallyManaged"`
} `json:"data"`
Metadata interface{} `json:"metaData"`
Correlationid string `json:"correlationId"`
}

//
// native data structure conversion
//

func convertUser(src *user) *scm.User {
func convertHarnessUser(src *harnessUser) *scm.User {
return &scm.User{
Login: src.Email,
Email: src.Email,
Name: src.DisplayName,
ID: src.UID,
Login: src.Data.Email,
Email: src.Data.Email,
Name: src.Data.Name,
ID: src.Data.UUID,
}
}
17 changes: 10 additions & 7 deletions scm/driver/harness/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/drone/go-scm/scm"
Expand All @@ -18,15 +19,17 @@ import (
)

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

defer gock.Off()

gock.New(gockOrigin).
Get("/gateway/code/api/v1/user").
Reply(200).
Type("application/json").
File("testdata/user.json")
harnessUserOrigin := strings.Replace(gockOrigin, "code", "ng", 1)

gock.New(harnessUserOrigin).
Get("/gateway/ng/api/user/currentUser").
Reply(200).
Type("application/json").
File("testdata/user.json")
}
client, _ := New(gockOrigin, harnessOrg, harnessAccount, harnessProject)
client.Client = &http.Client{
Transport: &transport.Custom{
Expand Down