From f8f38dff4e0e8cd7f7d847b35b96de21ea28d426 Mon Sep 17 00:00:00 2001 From: TP Honey Date: Mon, 13 Feb 2023 12:01:02 +0000 Subject: [PATCH] (feat) harness, add finduser --- scm/driver/harness/testdata/user.json | 52 ++++++++++++-- scm/driver/harness/testdata/user.json.golden | 8 +-- scm/driver/harness/user.go | 72 +++++++++++++++----- scm/driver/harness/user_test.go | 17 +++-- 4 files changed, 115 insertions(+), 34 deletions(-) diff --git a/scm/driver/harness/testdata/user.json b/scm/driver/harness/testdata/user.json index 459c42c39..21ea693f2 100644 --- a/scm/driver/harness/testdata/user.json +++ b/scm/driver/harness/testdata/user.json @@ -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": "thomas.honey@harness.io", + "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" } \ No newline at end of file diff --git a/scm/driver/harness/testdata/user.json.golden b/scm/driver/harness/testdata/user.json.golden index 1c38875a7..5f81bfeba 100644 --- a/scm/driver/harness/testdata/user.json.golden +++ b/scm/driver/harness/testdata/user.json.golden @@ -1,7 +1,7 @@ { - "ID": "3", - "Login": "2", - "Name": "1", - "Email": "2", + "ID": "0Nnoezs6RGa_fOWvG_Ta4w", + "Login": "thomas.honey@harness.io", + "Name": "thomas.honey", + "Email": "thomas.honey@harness.io", "Avatar": "" } \ No newline at end of file diff --git a/scm/driver/harness/user.go b/scm/driver/harness/user.go index 3bdd3514d..500cad249 100644 --- a/scm/driver/harness/user.go +++ b/scm/driver/harness/user.go @@ -6,6 +6,8 @@ package harness import ( "context" + "fmt" + "strings" "github.com/drone/go-scm/scm" ) @@ -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) { @@ -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, } } diff --git a/scm/driver/harness/user_test.go b/scm/driver/harness/user_test.go index 755294de3..886494c13 100644 --- a/scm/driver/harness/user_test.go +++ b/scm/driver/harness/user_test.go @@ -9,6 +9,7 @@ import ( "encoding/json" "io/ioutil" "net/http" + "strings" "testing" "github.com/drone/go-scm/scm" @@ -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{