From 202d703fe023a663f213270a6aa2d9252fd2d0ad Mon Sep 17 00:00:00 2001 From: "Laurie T. Malau" Date: Fri, 6 May 2022 09:51:56 +0000 Subject: [PATCH] Transform getWorkspace response --- components/public-api-server/go.mod | 1 + components/public-api-server/go.sum | 1 + .../public-api-server/pkg/apiv1/workspace.go | 18 ++++---- .../pkg/apiv1/workspace_test.go | 44 +++++++++++++++---- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/components/public-api-server/go.mod b/components/public-api-server/go.mod index 70d4a8e639c6cd..0c7963ddcbf359 100644 --- a/components/public-api-server/go.mod +++ b/components/public-api-server/go.mod @@ -6,6 +6,7 @@ require ( github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000 github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000 github.com/gitpod-io/gitpod/public-api v0.0.0-00010101000000-000000000000 + github.com/google/go-cmp v0.5.7 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.4.0 diff --git a/components/public-api-server/go.sum b/components/public-api-server/go.sum index 1310575d846ca6..4d30d12bc771b2 100644 --- a/components/public-api-server/go.sum +++ b/components/public-api-server/go.sum @@ -123,6 +123,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= diff --git a/components/public-api-server/pkg/apiv1/workspace.go b/components/public-api-server/pkg/apiv1/workspace.go index f65280e270b7cf..4ad06de183b470 100644 --- a/components/public-api-server/pkg/apiv1/workspace.go +++ b/components/public-api-server/pkg/apiv1/workspace.go @@ -37,22 +37,24 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR return nil, status.Error(codes.Internal, "failed to establish connection to downstream services") } - // TODO(milan): Use resulting workspace and transform it to public API response - _, err = server.GetWorkspace(ctx, r.GetWorkspaceId()) + workspace, err := server.GetWorkspace(ctx, r.GetWorkspaceId()) if err != nil { return nil, status.Error(codes.NotFound, "failed to get workspace") } return &v1.GetWorkspaceResponse{ Result: &v1.Workspace{ - WorkspaceId: r.GetWorkspaceId(), - OwnerId: "mock_owner", - ProjectId: "mock_project_id", + WorkspaceId: workspace.Workspace.ID, + OwnerId: workspace.Workspace.OwnerID, + ProjectId: "", Context: &v1.WorkspaceContext{ - ContextUrl: "https://github.com/gitpod-io/gitpod", - Details: nil, + ContextUrl: workspace.Workspace.ContextURL, + Details: &v1.WorkspaceContext_Git_{Git: &v1.WorkspaceContext_Git{ + NormalizedContextUrl: workspace.Workspace.ContextURL, + Commit: "", + }}, }, - Description: "This is a mock response", + Description: workspace.Workspace.Description, }, }, nil } diff --git a/components/public-api-server/pkg/apiv1/workspace_test.go b/components/public-api-server/pkg/apiv1/workspace_test.go index a78e4a6628011c..f606f8dc38ed51 100644 --- a/components/public-api-server/pkg/apiv1/workspace_test.go +++ b/components/public-api-server/pkg/apiv1/workspace_test.go @@ -10,13 +10,14 @@ import ( "github.com/gitpod-io/gitpod/common-go/baseserver" gitpod "github.com/gitpod-io/gitpod/gitpod-protocol" v1 "github.com/gitpod-io/gitpod/public-api/v1" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/testing/protocmp" "testing" ) @@ -24,6 +25,9 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) { const ( bearerToken = "bearer-token-for-tests" foundWorkspaceID = "easycz-seer-xl8o1zacpyw" + contextURL = "https://github.com/gitpod/pull/1111" + ownerID = "c6zz4631-3bbc-4hj4-be80-3dd05c66ee4e" + description = "This is the description" ) srv := baseserver.NewForTests(t) @@ -32,7 +36,26 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) { api: &FakeGitpodAPI{workspaces: map[string]*gitpod.WorkspaceInfo{ foundWorkspaceID: { LatestInstance: &gitpod.WorkspaceInstance{}, - Workspace: &gitpod.Workspace{}, + Workspace: &gitpod.Workspace{ + BaseImageNameResolved: "", + BasedOnPrebuildID: "", + BasedOnSnapshotID: "", + Config: nil, + ContentDeletedTime: "", + Context: nil, + ContextURL: contextURL, + CreationTime: "", + Deleted: false, + Description: description, + ID: foundWorkspaceID, + ImageNameResolved: "", + ImageSource: nil, + OwnerID: ownerID, + Pinned: false, + Shareable: false, + SoftDeleted: "", + Type: "", + }, }, }}, } @@ -60,13 +83,16 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) { Response: &v1.GetWorkspaceResponse{ Result: &v1.Workspace{ WorkspaceId: foundWorkspaceID, - OwnerId: "mock_owner", - ProjectId: "mock_project_id", + OwnerId: ownerID, + ProjectId: "", Context: &v1.WorkspaceContext{ - ContextUrl: "https://github.com/gitpod-io/gitpod", - Details: nil, + ContextUrl: contextURL, + Details: &v1.WorkspaceContext_Git_{Git: &v1.WorkspaceContext_Git{ + NormalizedContextUrl: contextURL, + Commit: "", + }}, }, - Description: "This is a mock response", + Description: description, }, }, }, @@ -84,7 +110,9 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) { WorkspaceId: scenario.WorkspaceID, }) require.Equal(t, scenario.ErrorCode, status.Code(err), "status code must match") - require.True(t, proto.Equal(scenario.Response, resp)) + if diff := cmp.Diff(scenario.Response, resp, protocmp.Transform()); diff != "" { + t.Errorf("unexpected difference:\n%v", diff) + } }) }