Path: blob/main/components/public-api-server/pkg/apiv1/editor_service.go
2499 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package apiv156import (7"context"8"sort"910connect "github.com/bufbuild/connect-go"11"github.com/gitpod-io/gitpod/common-go/log"12v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"13"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"14protocol "github.com/gitpod-io/gitpod/gitpod-protocol"15"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"16)1718func NewEditorService(pool proxy.ServerConnectionPool) *EditorService {19return &EditorService{20connectionPool: pool,21}22}2324var _ v1connect.EditorServiceHandler = (*EditorService)(nil)2526type EditorService struct {27connectionPool proxy.ServerConnectionPool2829v1connect.UnimplementedEditorServiceHandler30}3132func (s *EditorService) ListEditorOptions(ctx context.Context, req *connect.Request[v1.ListEditorOptionsRequest]) (*connect.Response[v1.ListEditorOptionsResponse], error) {33conn, err := getConnection(ctx, s.connectionPool)34if err != nil {35return nil, err36}3738options, err := conn.GetIDEOptions(ctx)39if err != nil {40log.Extract(ctx).WithError(err).Error("Failed to list editor options.")41return nil, proxy.ConvertError(err)42}4344// Sort the response by OrderKey45var keys []string46for key := range options.Options {47keys = append(keys, key)48}49sort.Slice(keys, func(i, j int) bool {50return options.Options[keys[i]].OrderKey < options.Options[keys[j]].OrderKey51})5253convertedOptions := make([]*v1.EditorOption, 0, len(options.Options))54for _, key := range keys {55option := options.Options[key]56convertedOptions = append(convertedOptions, convertEditorOption(&option, key))57}5859return connect.NewResponse(&v1.ListEditorOptionsResponse{60Result: convertedOptions,61}), nil62}6364func convertEditorOption(ideOption *protocol.IDEOption, id string) *v1.EditorOption {65var editorType *v1.EditorOption_Type66switch ideOption.Type {67case "browser":68editorType = v1.EditorOption_TYPE_BROWSER.Enum()69case "desktop":70editorType = v1.EditorOption_TYPE_DESKTOP.Enum()71default:72editorType = v1.EditorOption_TYPE_UNSPECIFIED.Enum()73}7475return &v1.EditorOption{76Id: id,77Title: ideOption.Title,78Type: *editorType,79Logo: ideOption.Logo,80Label: ideOption.Label,81Stable: &v1.EditorOption_Kind{82Version: ideOption.ImageVersion,83},84Latest: &v1.EditorOption_Kind{85Version: ideOption.LatestImageVersion,86},87}88}899091