Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/apiv1/editor_service.go
2499 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package apiv1
6
7
import (
8
"context"
9
"sort"
10
11
connect "github.com/bufbuild/connect-go"
12
"github.com/gitpod-io/gitpod/common-go/log"
13
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
14
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
15
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
16
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
17
)
18
19
func NewEditorService(pool proxy.ServerConnectionPool) *EditorService {
20
return &EditorService{
21
connectionPool: pool,
22
}
23
}
24
25
var _ v1connect.EditorServiceHandler = (*EditorService)(nil)
26
27
type EditorService struct {
28
connectionPool proxy.ServerConnectionPool
29
30
v1connect.UnimplementedEditorServiceHandler
31
}
32
33
func (s *EditorService) ListEditorOptions(ctx context.Context, req *connect.Request[v1.ListEditorOptionsRequest]) (*connect.Response[v1.ListEditorOptionsResponse], error) {
34
conn, err := getConnection(ctx, s.connectionPool)
35
if err != nil {
36
return nil, err
37
}
38
39
options, err := conn.GetIDEOptions(ctx)
40
if err != nil {
41
log.Extract(ctx).WithError(err).Error("Failed to list editor options.")
42
return nil, proxy.ConvertError(err)
43
}
44
45
// Sort the response by OrderKey
46
var keys []string
47
for key := range options.Options {
48
keys = append(keys, key)
49
}
50
sort.Slice(keys, func(i, j int) bool {
51
return options.Options[keys[i]].OrderKey < options.Options[keys[j]].OrderKey
52
})
53
54
convertedOptions := make([]*v1.EditorOption, 0, len(options.Options))
55
for _, key := range keys {
56
option := options.Options[key]
57
convertedOptions = append(convertedOptions, convertEditorOption(&option, key))
58
}
59
60
return connect.NewResponse(&v1.ListEditorOptionsResponse{
61
Result: convertedOptions,
62
}), nil
63
}
64
65
func convertEditorOption(ideOption *protocol.IDEOption, id string) *v1.EditorOption {
66
var editorType *v1.EditorOption_Type
67
switch ideOption.Type {
68
case "browser":
69
editorType = v1.EditorOption_TYPE_BROWSER.Enum()
70
case "desktop":
71
editorType = v1.EditorOption_TYPE_DESKTOP.Enum()
72
default:
73
editorType = v1.EditorOption_TYPE_UNSPECIFIED.Enum()
74
}
75
76
return &v1.EditorOption{
77
Id: id,
78
Title: ideOption.Title,
79
Type: *editorType,
80
Logo: ideOption.Logo,
81
Label: ideOption.Label,
82
Stable: &v1.EditorOption_Kind{
83
Version: ideOption.ImageVersion,
84
},
85
Latest: &v1.EditorOption_Kind{
86
Version: ideOption.LatestImageVersion,
87
},
88
}
89
}
90
91