-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[papi] List editor options #18530
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
Merged
Merged
[papi] List editor options #18530
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
71d1bb3
options.init
filiptronicek 4e3fbbb
Respond with options
filiptronicek 225a4df
Move to editor service
filiptronicek f88fc14
Unused imports
filiptronicek 84e6d8c
Merge branch 'main' into ft/list-editors
filiptronicek 4906723
Mount ot the correct service
filiptronicek 58d9190
Sort by orderkey
filiptronicek b9cc617
Merge branch 'main' into ft/list-editors
filiptronicek c7d0688
Simplify enum name
filiptronicek 66ca2a5
Add tests
filiptronicek 3ab79e1
Options -> `result` for consistency
filiptronicek 218eba6
fix result field
filiptronicek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package apiv1 | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
connect "github.com/bufbuild/connect-go" | ||
"github.com/gitpod-io/gitpod/common-go/log" | ||
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1" | ||
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect" | ||
protocol "github.com/gitpod-io/gitpod/gitpod-protocol" | ||
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy" | ||
) | ||
|
||
func NewEditorService(pool proxy.ServerConnectionPool) *EditorService { | ||
return &EditorService{ | ||
connectionPool: pool, | ||
} | ||
} | ||
|
||
var _ v1connect.EditorServiceHandler = (*EditorService)(nil) | ||
|
||
type EditorService struct { | ||
connectionPool proxy.ServerConnectionPool | ||
|
||
v1connect.UnimplementedEditorServiceHandler | ||
} | ||
|
||
func (s *EditorService) ListEditorOptions(ctx context.Context, req *connect.Request[v1.ListEditorOptionsRequest]) (*connect.Response[v1.ListEditorOptionsResponse], error) { | ||
conn, err := getConnection(ctx, s.connectionPool) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
options, err := conn.GetIDEOptions(ctx) | ||
if err != nil { | ||
log.Extract(ctx).WithError(err).Error("Failed to list editor options.") | ||
return nil, proxy.ConvertError(err) | ||
} | ||
|
||
// Sort the response by OrderKey | ||
var keys []string | ||
for key := range options.Options { | ||
keys = append(keys, key) | ||
} | ||
sort.Slice(keys, func(i, j int) bool { | ||
return options.Options[keys[i]].OrderKey < options.Options[keys[j]].OrderKey | ||
}) | ||
|
||
convertedOptions := make([]*v1.EditorOption, 0, len(options.Options)) | ||
for _, key := range keys { | ||
option := options.Options[key] | ||
convertedOptions = append(convertedOptions, convertEditorOption(&option, key)) | ||
} | ||
|
||
return connect.NewResponse(&v1.ListEditorOptionsResponse{ | ||
Result: convertedOptions, | ||
}), nil | ||
} | ||
|
||
func convertEditorOption(ideOption *protocol.IDEOption, id string) *v1.EditorOption { | ||
var editorType *v1.EditorOption_Type | ||
switch ideOption.Type { | ||
case "browser": | ||
editorType = v1.EditorOption_TYPE_BROWSER.Enum() | ||
case "desktop": | ||
editorType = v1.EditorOption_TYPE_DESKTOP.Enum() | ||
default: | ||
editorType = v1.EditorOption_TYPE_UNSPECIFIED.Enum() | ||
} | ||
|
||
return &v1.EditorOption{ | ||
Id: id, | ||
Title: ideOption.Title, | ||
Type: *editorType, | ||
Logo: ideOption.Logo, | ||
Label: ideOption.Label, | ||
Stable: &v1.EditorOption_Kind{ | ||
Version: ideOption.ImageVersion, | ||
}, | ||
Latest: &v1.EditorOption_Kind{ | ||
Version: ideOption.LatestImageVersion, | ||
}, | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
components/public-api-server/pkg/apiv1/editor_service_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package apiv1 | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/bufbuild/connect-go" | ||
"github.com/gitpod-io/gitpod/components/public-api/go/config" | ||
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1" | ||
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect" | ||
protocol "github.com/gitpod-io/gitpod/gitpod-protocol" | ||
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth" | ||
"github.com/gitpod-io/gitpod/public-api-server/pkg/jws" | ||
"github.com/gitpod-io/gitpod/public-api-server/pkg/jws/jwstest" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEditorService_ListEditorOptions(t *testing.T) { | ||
t.Run("proxies request to server", func(t *testing.T) { | ||
serverMock, client := setupEditorService(t) | ||
|
||
serverMock.EXPECT().GetIDEOptions(gomock.Any()).Return(&protocol.IDEOptions{Options: map[string]protocol.IDEOption{ | ||
"code": { | ||
OrderKey: "02", | ||
Title: "VS Code", | ||
Logo: "https://gitpod.io/icons/vscode.svg", | ||
ImageVersion: "1.68.0", | ||
LatestImageVersion: "1.69.0", | ||
}, | ||
"theia": { | ||
OrderKey: "01", | ||
Title: "Theia", | ||
Logo: "https://gitpod.io/icons/theia.svg", | ||
ImageVersion: "1.68.0", | ||
}, | ||
}, DefaultIde: "", DefaultDesktopIde: "", Clients: map[string]protocol.IDEClient{}}, nil) | ||
|
||
retrieved, err := client.ListEditorOptions(context.Background(), connect.NewRequest(&v1.ListEditorOptionsRequest{})) | ||
require.NoError(t, err) | ||
requireEqualProto(t, &v1.ListEditorOptionsResponse{ | ||
Result: []*v1.EditorOption{ | ||
{ | ||
Title: "Theia", | ||
Id: "theia", | ||
Logo: "https://gitpod.io/icons/theia.svg", | ||
Stable: &v1.EditorOption_Kind{ | ||
Version: "1.68.0", | ||
}, | ||
Latest: &v1.EditorOption_Kind{}, | ||
}, | ||
{ | ||
Title: "VS Code", | ||
Id: "code", | ||
Logo: "https://gitpod.io/icons/vscode.svg", | ||
Stable: &v1.EditorOption_Kind{ | ||
Version: "1.68.0", | ||
}, | ||
Latest: &v1.EditorOption_Kind{ | ||
Version: "1.69.0", | ||
}, | ||
}, | ||
}, | ||
}, retrieved.Msg) | ||
}) | ||
} | ||
|
||
func setupEditorService(t *testing.T) (*protocol.MockAPIInterface, v1connect.EditorServiceClient) { | ||
t.Helper() | ||
|
||
ctrl := gomock.NewController(t) | ||
t.Cleanup(ctrl.Finish) | ||
|
||
serverMock := protocol.NewMockAPIInterface(ctrl) | ||
|
||
svc := NewEditorService(&FakeServerConnPool{ | ||
api: serverMock, | ||
}) | ||
|
||
keyset := jwstest.GenerateKeySet(t) | ||
rsa256, err := jws.NewRSA256(keyset) | ||
require.NoError(t, err) | ||
|
||
_, handler := v1connect.NewEditorServiceHandler(svc, connect.WithInterceptors(auth.NewServerInterceptor(config.SessionConfig{ | ||
Issuer: "unitetest.com", | ||
Cookie: config.CookieConfig{ | ||
Name: "cookie_jwt", | ||
}, | ||
}, rsa256))) | ||
|
||
srv := httptest.NewServer(handler) | ||
t.Cleanup(srv.Close) | ||
|
||
client := v1connect.NewEditorServiceClient(http.DefaultClient, srv.URL, connect.WithInterceptors( | ||
auth.NewClientInterceptor("auth-token"), | ||
)) | ||
|
||
return serverMock, client | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.