Skip to content

Commit 2cb7f8f

Browse files
Merge pull request #4711 from linuxfoundation/unicron-port-py-apis-to-go
Added test coverage that compares '/vN/projects<uuid>' for N=2 and N=4
2 parents 4e8bab2 + b217458 commit 2cb7f8f

File tree

21 files changed

+1214
-21
lines changed

21 files changed

+1214
-21
lines changed

cla-backend-go/auth/auth0.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Validator struct {
2424
}
2525

2626
// NewAuthValidator creates a new auth0 validator based on the specified parameters
27-
func NewAuthValidator(domain, clientID, usernameClaim, algorithm string) (Validator, error) { // nolint
27+
func NewAuthValidator(domain, clientID, usernameClaim, nameClaim, emailClaim, algorithm string) (Validator, error) { // nolint
2828
if domain == "" {
2929
return Validator{}, errors.New("missing Domain")
3030
}
@@ -43,8 +43,8 @@ func NewAuthValidator(domain, clientID, usernameClaim, algorithm string) (Valida
4343
usernameClaim: usernameClaim,
4444
algorithm: algorithm,
4545
wellKnownURL: "https://" + path.Join(domain, ".well-known/jwks.json"),
46-
nameClaim: "name",
47-
emailClaim: "email",
46+
nameClaim: nameClaim,
47+
emailClaim: emailClaim,
4848
}
4949

5050
return validator, nil

cla-backend-go/cmd/response_metrics.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"sync"
78
"time"
89

910
"github.com/linuxfoundation/easycla/cla-backend-go/utils"
@@ -18,32 +19,36 @@ type responseMetrics struct {
1819
expire time.Time
1920
}
2021

21-
var reqMap = make(map[string]*responseMetrics, 5)
22+
var reqMap sync.Map
2223

2324
// requestStart holds the request ID, method and timing information in a small structure
2425
func requestStart(reqID, method string) {
2526
now, _ := utils.CurrentTime()
26-
reqMap[reqID] = &responseMetrics{
27+
rm := &responseMetrics{
2728
reqID: reqID,
2829
method: method,
2930
start: now,
3031
elapsed: 0,
3132
expire: now.Add(time.Minute * 5),
3233
}
34+
reqMap.Store(reqID, rm)
3335
}
3436

3537
// getRequestMetrics returns the response metrics based on the request id value
3638
func getRequestMetrics(reqID string) *responseMetrics {
37-
if x, found := reqMap[reqID]; found {
39+
if val, found := reqMap.Load(reqID); found {
40+
rm, ok := val.(*responseMetrics)
41+
if !ok {
42+
return nil
43+
}
3844
now, _ := utils.CurrentTime()
39-
x.elapsed = now.Sub(x.start)
40-
return x
45+
rm.elapsed = now.Sub(rm.start)
46+
return rm
4147
}
42-
4348
return nil
4449
}
4550

4651
// clearRequestMetrics removes the request from the map
4752
func clearRequestMetrics(reqID string) {
48-
delete(reqMap, reqID)
53+
reqMap.Delete(reqID)
4954
}

cla-backend-go/cmd/server.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,24 @@ func server(localMode bool) http.Handler {
236236
}
237237

238238
// LG: to test with manual tokens
239-
// configFile.Auth0.UsernameClaim = "http://lfx.dev/claims/username"
239+
customClaimUsername := os.Getenv("AUTH0_USERNAME_CLAIM_CLI")
240+
if customClaimUsername != "" {
241+
configFile.Auth0.UsernameClaim = customClaimUsername
242+
}
243+
nameClaimName := os.Getenv("AUTH0_NAME_CLAIM_CLI")
244+
if nameClaimName == "" {
245+
nameClaimName = "name"
246+
}
247+
emailClaimName := os.Getenv("AUTH0_EMAIL_CLAIM_CLI")
248+
if emailClaimName == "" {
249+
emailClaimName = "email"
250+
}
240251
authValidator, err := auth.NewAuthValidator(
241252
configFile.Auth0.Domain,
242253
configFile.Auth0.ClientID,
243254
configFile.Auth0.UsernameClaim,
255+
nameClaimName,
256+
emailClaimName,
244257
configFile.Auth0.Algorithm)
245258
if err != nil {
246259
logrus.Panic(err)
@@ -336,7 +349,7 @@ func server(localMode bool) http.Handler {
336349
// Setup our API handlers
337350
users.Configure(api, usersService, eventsService)
338351
project.Configure(api, v1ProjectService, eventsService, gerritService, v1RepositoriesService, v1SignaturesService)
339-
v2Project.Configure(v2API, v1ProjectService, v2ProjectService, eventsService)
352+
v2Project.Configure(v2API, v1ProjectService, v2ProjectService, eventsService, v1ProjectClaGroupService, v2RepositoriesService, gerritService)
340353
health.Configure(api, healthService)
341354
v2Health.Configure(v2API, healthService)
342355
template.Configure(api, templateService, eventsService)

cla-backend-go/project/mocks/mock_repo.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cla-backend-go/project/mocks/mock_service.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cla-backend-go/project/repository/repository.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
type ProjectRepository interface { //nolint
5151
CreateCLAGroup(ctx context.Context, claGroupModel *models.ClaGroup) (*models.ClaGroup, error)
5252
GetCLAGroupByID(ctx context.Context, claGroupID string, loadRepoDetails bool) (*models.ClaGroup, error)
53+
GetCLAGroupByIDCompat(ctx context.Context, claGroupID string, loadRepoDetails bool) (*models.ClaGroup, error)
5354
GetCLAGroupsByExternalID(ctx context.Context, params *project.GetProjectsByExternalIDParams, loadRepoDetails bool) (*models.ClaGroups, error)
5455
GetCLAGroupByName(ctx context.Context, claGroupName string) (*models.ClaGroup, error)
5556
GetExternalCLAGroup(ctx context.Context, claGroupExternalID string) (*models.ClaGroup, error)
@@ -149,7 +150,7 @@ func (repo *repo) CreateCLAGroup(ctx context.Context, claGroupModel *models.ClaG
149150
return claGroupModel, nil
150151
}
151152

152-
func (repo *repo) getCLAGroupByID(ctx context.Context, claGroupID string, loadCLAGroupDetails bool) (*models.ClaGroup, error) {
153+
func (repo *repo) getCLAGroupByID(ctx context.Context, claGroupID string, loadCLAGroupDetails bool, claEnabledDefaultIsTrue bool) (*models.ClaGroup, error) {
153154
f := logrus.Fields{
154155
"functionName": "project.repository.getCLAGroupByID",
155156
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
@@ -188,19 +189,36 @@ func (repo *repo) getCLAGroupByID(ctx context.Context, claGroupID string, loadCL
188189
return nil, &utils.CLAGroupNotFound{CLAGroupID: claGroupID}
189190
}
190191
var dbModel models2.DBProjectModel
191-
err = dynamodbattribute.UnmarshalMap(results.Items[0], &dbModel)
192+
rawItem := results.Items[0]
193+
err = dynamodbattribute.UnmarshalMap(rawItem, &dbModel)
192194
if err != nil {
193195
log.WithFields(f).Warnf("error unmarshalling db cla group model, error: %+v", err)
194196
return nil, err
195197
}
198+
if claEnabledDefaultIsTrue {
199+
// If missing, assume true like Pynamo default=True
200+
if _, ok := rawItem["project_icla_enabled"]; !ok {
201+
dbModel.ProjectIclaEnabled = true
202+
}
203+
if _, ok := rawItem["project_ccla_enabled"]; !ok {
204+
dbModel.ProjectCclaEnabled = true
205+
}
206+
}
196207

197208
// Convert the database model to an API response model
198209
return repo.buildCLAGroupModel(ctx, dbModel, loadCLAGroupDetails), nil
199210
}
200211

201212
// GetCLAGroupByID returns the cla group model associated for the specified claGroupID
202213
func (repo *repo) GetCLAGroupByID(ctx context.Context, claGroupID string, loadRepoDetails bool) (*models.ClaGroup, error) {
203-
return repo.getCLAGroupByID(ctx, claGroupID, loadRepoDetails)
214+
return repo.getCLAGroupByID(ctx, claGroupID, loadRepoDetails, false)
215+
}
216+
217+
// GetCLAGroupByIDCompat returns the cla group model associated for the specified claGroupID
218+
func (repo *repo) GetCLAGroupByIDCompat(ctx context.Context, claGroupID string, loadRepoDetails bool) (*models.ClaGroup, error) {
219+
// Uses compatible mode (with python v2): claEnabledDefaultIsTrue - means if project_ccla_enabled or project_icla_enabled
220+
// aren't set on dynamoDB item - they will default to true as in Py V2 API
221+
return repo.getCLAGroupByID(ctx, claGroupID, loadRepoDetails, true)
204222
}
205223

206224
// GetCLAGroupsByExternalID queries the database and returns a list of the cla groups
@@ -383,7 +401,7 @@ func (repo *repo) GetClaGroupByProjectSFID(ctx context.Context, projectSFID stri
383401

384402
log.WithFields(f).Debugf("found CLA Group ID: %s for project SFID: %s", claGroupProject.ClaGroupID, projectSFID)
385403

386-
return repo.getCLAGroupByID(ctx, claGroupProject.ClaGroupID, loadRepoDetails)
404+
return repo.getCLAGroupByID(ctx, claGroupProject.ClaGroupID, loadRepoDetails, false)
387405
}
388406

389407
// GetCLAGroupByName returns the project model associated for the specified project name

cla-backend-go/project/service/service.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Service interface {
3030
CreateCLAGroup(ctx context.Context, project *models.ClaGroup) (*models.ClaGroup, error)
3131
GetCLAGroups(ctx context.Context, params *project.GetProjectsParams) (*models.ClaGroups, error)
3232
GetCLAGroupByID(ctx context.Context, claGroupID string) (*models.ClaGroup, error)
33+
GetCLAGroupByIDCompat(ctx context.Context, claGroupID string) (*models.ClaGroup, error)
3334
GetCLAGroupsByExternalSFID(ctx context.Context, projectSFID string) (*models.ClaGroups, error)
3435
GetCLAGroupsByExternalID(ctx context.Context, params *project.GetProjectsByExternalIDParams) (*models.ClaGroups, error)
3536
GetCLAGroupByName(ctx context.Context, projectName string) (*models.ClaGroup, error)
@@ -75,6 +76,16 @@ func (s ProjectService) GetCLAGroups(ctx context.Context, params *project.GetPro
7576

7677
// GetCLAGroupByID service method
7778
func (s ProjectService) GetCLAGroupByID(ctx context.Context, claGroupID string) (*models.ClaGroup, error) {
79+
return s.getCLAGroupByID(ctx, claGroupID, false)
80+
}
81+
82+
// GetCLAGroupByIDCompat service method
83+
func (s ProjectService) GetCLAGroupByIDCompat(ctx context.Context, claGroupID string) (*models.ClaGroup, error) {
84+
return s.getCLAGroupByID(ctx, claGroupID, true)
85+
}
86+
87+
// getCLAGroupByID service method
88+
func (s ProjectService) getCLAGroupByID(ctx context.Context, claGroupID string, claEnabledDefaultIsTrue bool) (*models.ClaGroup, error) {
7889
f := logrus.Fields{
7990
"functionName": "GetCLAGroupByID",
8091
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
@@ -83,7 +94,15 @@ func (s ProjectService) GetCLAGroupByID(ctx context.Context, claGroupID string)
8394
}
8495

8596
log.WithFields(f).Debug("locating CLA Group by ID...")
86-
project, err := s.repo.GetCLAGroupByID(ctx, claGroupID, repository.LoadRepoDetails)
97+
var (
98+
project *models.ClaGroup
99+
err error
100+
)
101+
if claEnabledDefaultIsTrue {
102+
project, err = s.repo.GetCLAGroupByIDCompat(ctx, claGroupID, repository.LoadRepoDetails)
103+
} else {
104+
project, err = s.repo.GetCLAGroupByID(ctx, claGroupID, repository.LoadRepoDetails)
105+
}
87106
if err != nil {
88107
return nil, err
89108
}

cla-backend-go/swagger/cla.v2.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,38 @@ paths:
889889
tags:
890890
- project
891891

892+
/project-compat/{projectID}:
893+
parameters:
894+
- $ref: "#/parameters/x-request-id"
895+
- name: projectID
896+
in: path
897+
type: string
898+
required: true
899+
pattern: '^[a-fA-F0-9]{8}-?[a-fA-F0-9]{4}-?4[a-fA-F0-9]{3}-?[89ab][a-fA-F0-9]{3}-?[a-fA-F0-9]{12}$' # uuidv4
900+
get:
901+
summary: Get project by ID (returns data in the same format as Py V2 API)
902+
security: [ ]
903+
operationId: getProjectCompat
904+
responses:
905+
'200':
906+
description: 'Success'
907+
headers:
908+
x-request-id:
909+
type: string
910+
description: The unique request ID value - assigned/set by the API Gateway based on the session
911+
schema:
912+
$ref: '#/definitions/project-compat'
913+
'400':
914+
$ref: '#/responses/invalid-request'
915+
'401':
916+
$ref: '#/responses/unauthorized'
917+
'403':
918+
$ref: '#/responses/forbidden'
919+
'404':
920+
$ref: '#/responses/not-found'
921+
tags:
922+
- project
923+
892924
/events/recent:
893925
get:
894926
summary: List recent events - requires Admin-level access
@@ -4956,6 +4988,9 @@ definitions:
49564988
sf-project-summary:
49574989
$ref: './common/sf-project-summary.yaml'
49584990

4991+
project-compat:
4992+
$ref: './common/project-compat.yaml'
4993+
49594994
# ---------------------------------------------------------------------------
49604995
# CLA Template Definitions
49614996
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)