Skip to content

Commit

Permalink
Revert "Make slight optimization on listAllNodes endpoint (#55)"
Browse files Browse the repository at this point in the history
This reverts commit bb3d04b.
  • Loading branch information
james03160927 committed Jul 2, 2024
1 parent 76c4b46 commit ffd539b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 51 deletions.
50 changes: 4 additions & 46 deletions integration-tests/registry_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"registry-backend/config"
"registry-backend/drip"
"registry-backend/ent"
"registry-backend/ent/nodeversion"
"registry-backend/ent/schema"
"registry-backend/mock/gateways"
"registry-backend/server/implementation"
drip_authorization "registry-backend/server/middleware/authorization"
Expand Down Expand Up @@ -703,38 +701,6 @@ func TestRegistry(t *testing.T) {
})

t.Run("List Nodes", func(t *testing.T) {
nodeVersionLiteral = "1.1.0"

t.Run("Create New Node Version", func(t *testing.T) {
mockStorageService.On("GenerateSignedURL", mock.Anything, mock.Anything).Return("test-url", nil)
mockStorageService.On("GetFileUrl", mock.Anything, mock.Anything, mock.Anything).Return("test-url", nil)
res, err := withMiddleware(authz, impl.PublishNodeVersion)(ctx, drip.PublishNodeVersionRequestObject{
PublisherId: publisherId,
NodeId: nodeId,
Body: &drip.PublishNodeVersionJSONRequestBody{
Node: drip.Node{
Id: &nodeId,
Description: &nodeDescription,
Author: &nodeAuthor,
License: &nodeLicense,
Name: &nodeName,
Tags: &nodeTags,
Repository: &source_code_repo,
},
NodeVersion: drip.NodeVersion{
Version: &nodeVersionLiteral,
Changelog: createdNodeVersion.Changelog,
Dependencies: &dependencies,
},
PersonalAccessToken: *createPersonalAccessTokenResponse.(drip.CreatePersonalAccessToken201JSONResponse).Token,
},
})
require.NoError(t, err, "should return created node version")
require.IsType(t, drip.PublishNodeVersion201JSONResponse{}, res)
res200 := res.(drip.PublishNodeVersion201JSONResponse)
createdNodeVersion = *res200.NodeVersion
})

resNodes, err := withMiddleware(authz, impl.ListAllNodes)(ctx, drip.ListAllNodesRequestObject{})
require.NoError(t, err, "should not return error")
require.IsType(t, drip.ListAllNodes200JSONResponse{}, resNodes, "should return 200 server response")
Expand Down Expand Up @@ -762,7 +728,6 @@ func TestRegistry(t *testing.T) {
}
expectedNode.LatestVersion.DownloadUrl = (*resNodes200.Nodes)[0].LatestVersion.DownloadUrl // generated
expectedNode.LatestVersion.Deprecated = (*resNodes200.Nodes)[0].LatestVersion.Deprecated // generated
expectedNode.LatestVersion.CreatedAt = (*resNodes200.Nodes)[0].LatestVersion.CreatedAt // generated
expectedNode.Publisher.CreatedAt = (*resNodes200.Nodes)[0].Publisher.CreatedAt
assert.Equal(t, expectedNode, (*resNodes200.Nodes)[0])
})
Expand Down Expand Up @@ -840,19 +805,13 @@ func TestRegistry(t *testing.T) {
require.NoError(t, err)
require.IsType(t, drip.GetNodeVersion200JSONResponse{}, res)

nodesToScans, err := client.NodeVersion.Query().Where(nodeversion.StatusEQ(schema.NodeVersionStatusPending)).Count(ctx)
require.NoError(t, err)

newNodeScanned := false
nodesScanned := 0
handled := true
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := dripservices_registry.ScanRequest{}
require.NoError(t, json.NewDecoder(r.Body).Decode(&req))
if *res.(drip.GetNodeVersion200JSONResponse).DownloadUrl == req.URL {
newNodeScanned = true
}
nodesScanned++
assert.Equal(t, *res.(drip.GetNodeVersion200JSONResponse).DownloadUrl, req.URL)

handled = true
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(s.Close)
Expand All @@ -868,8 +827,7 @@ func TestRegistry(t *testing.T) {
})
require.NoError(t, err)
require.IsType(t, drip.SecurityScan200Response{}, scanres)
assert.True(t, newNodeScanned)
assert.Equal(t, nodesToScans, nodesScanned)
assert.True(t, handled)
})
})
}
Expand Down
11 changes: 8 additions & 3 deletions server/implementation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,14 @@ func (s *DripStrictServerImplementation) ListAllNodes(
for _, dbNode := range nodeResults.Nodes {
apiNode := mapper.DbNodeToApiNode(dbNode)

// attach information of latest version if available
if len(dbNode.Edges.Versions) > 0 {
apiNode.LatestVersion = mapper.DbNodeVersionToApiNodeVersion(dbNode.Edges.Versions[0])
// Fetch the latest version if available
if dbNode.Edges.Versions != nil && len(dbNode.Edges.Versions) > 0 {
latestVersion, err := s.RegistryService.GetLatestNodeVersion(ctx, s.Client, dbNode.ID)
if err == nil {
apiNode.LatestVersion = mapper.DbNodeVersionToApiNodeVersion(latestVersion)
} else {
log.Ctx(ctx).Error().Msgf("Failed to get latest version for node %s w/ err: %v", dbNode.ID, err)
}
}

// Map publisher information
Expand Down
3 changes: 1 addition & 2 deletions services/registry/registry_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *RegistryService) ListNodes(ctx context.Context, client *ent.Client, pag
query := client.Node.Query().
WithPublisher().
WithVersions(func(q *ent.NodeVersionQuery) {
q.Order(ent.Desc(nodeversion.FieldCreateTime)).Limit(1)
q.Order(ent.Desc(nodeversion.FieldCreateTime))
})

// Apply filters if provided
Expand Down Expand Up @@ -841,7 +841,6 @@ func (s *RegistryService) PerformSecurityCheck(ctx context.Context, client *ent.
issues, err := sendScanRequest(s.config.SecretScannerURL, nodeVersion.Edges.StorageFile.FileURL)
if err != nil {
if strings.Contains(err.Error(), "404") {
log.Ctx(ctx).Info().Msgf("Node zip file doesn’t exist %s@%s. Updating to deleted.", nodeVersion.NodeID, nodeVersion.Version)
err := nodeVersion.Update().SetStatus(schema.NodeVersionStatusDeleted).SetStatusReason("Node zip file doesn’t exist").Exec(ctx)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msgf("failed to update node version status to active")
Expand Down

0 comments on commit ffd539b

Please sign in to comment.