4
4
* See License.AGPL.txt in the project root for license information.
5
5
*/
6
6
7
- import { Timestamp , toPlainMessage } from "@bufbuild/protobuf" ;
7
+ import { Timestamp , toPlainMessage , Duration } from "@bufbuild/protobuf" ;
8
8
import { Code , ConnectError } from "@connectrpc/connect" ;
9
9
import {
10
10
FailedPreconditionDetails ,
@@ -25,6 +25,15 @@ import {
25
25
AuthProviderType ,
26
26
OAuth2Config ,
27
27
} from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb" ;
28
+ import {
29
+ Identity ,
30
+ User ,
31
+ User_EmailNotificationSettings ,
32
+ User_RoleOrPermission ,
33
+ User_UserFeatureFlag ,
34
+ User_WorkspaceAutostartOption ,
35
+ User_WorkspaceTimeoutSettings ,
36
+ } from "@gitpod/public-api/lib/gitpod/v1/user_pb" ;
28
37
import {
29
38
BranchMatchingStrategy ,
30
39
Configuration ,
@@ -73,6 +82,8 @@ import {
73
82
UnauthorizedRepositoryAccessError ,
74
83
} from "./messaging/error" ;
75
84
import {
85
+ User as UserProtocol ,
86
+ Identity as IdentityProtocol ,
76
87
AuthProviderEntry as AuthProviderProtocol ,
77
88
AuthProviderInfo ,
78
89
CommitContext ,
@@ -89,6 +100,9 @@ import {
89
100
Token ,
90
101
SuggestedRepository as SuggestedRepositoryProtocol ,
91
102
UserSSHPublicKeyValue ,
103
+ NamedWorkspaceFeatureFlag ,
104
+ WorkspaceAutostartOption ,
105
+ IDESettings ,
92
106
} from "./protocol" ;
93
107
import {
94
108
OrgMemberInfo ,
@@ -109,6 +123,8 @@ import {
109
123
} from "./workspace-instance" ;
110
124
import { Author , Commit } from "@gitpod/public-api/lib/gitpod/v1/scm_pb" ;
111
125
import type { DeepPartial } from "./util/deep-partial" ;
126
+ import { RoleOrPermission } from "./permission" ;
127
+ import { parseGoDurationToMs } from "./util/timeutil" ;
112
128
113
129
export type PartialConfiguration = DeepPartial < Configuration > & Pick < Configuration , "id" > ;
114
130
@@ -910,4 +926,152 @@ export class PublicAPIConverter {
910
926
result . lastUsedTime = Timestamp . fromDate ( new Date ( sshKey . lastUsedTime || sshKey . creationTime ) ) ;
911
927
return result ;
912
928
}
929
+
930
+ toUser ( from : UserProtocol ) : User {
931
+ const {
932
+ id,
933
+ name,
934
+ creationDate,
935
+ identities,
936
+ additionalData,
937
+ avatarUrl,
938
+ featureFlags,
939
+ organizationId,
940
+ rolesOrPermissions,
941
+ usageAttributionId,
942
+ blocked,
943
+ lastVerificationTime,
944
+ verificationPhoneNumber,
945
+ } = from ;
946
+ const {
947
+ disabledClosedTimeout,
948
+ dotfileRepo,
949
+ emailNotificationSettings,
950
+ ideSettings,
951
+ profile,
952
+ workspaceAutostartOptions,
953
+ workspaceClasses,
954
+ workspaceTimeout,
955
+ } = additionalData || { } ;
956
+
957
+ return new User ( {
958
+ id,
959
+ name,
960
+ createdAt : this . toTimestamp ( creationDate ) ,
961
+ avatarUrl,
962
+ organizationId,
963
+ usageAttributionId,
964
+ blocked,
965
+ identities : identities ?. map ( ( i ) => this . toIdentity ( i ) ) ,
966
+ rolesOrPermissions : rolesOrPermissions ?. map ( ( rp ) => this . toRoleOrPermission ( rp ) ) ,
967
+ workspaceFeatureFlags : featureFlags ?. permanentWSFeatureFlags ?. map ( ( ff ) => this . toUserFeatureFlags ( ff ) ) ,
968
+ timeoutSettings : new User_WorkspaceTimeoutSettings ( {
969
+ disabledDisconnected : disabledClosedTimeout ,
970
+ inactivity : this . toDuration ( workspaceTimeout ) ,
971
+ // disconnected: this.toDuration(workspaceTimeout), // FIXME(at) how to map this properly?
972
+ } ) ,
973
+ dotfileRepo,
974
+ emailNotificationSettings : new User_EmailNotificationSettings ( {
975
+ allowsChangelogMail : emailNotificationSettings ?. allowsChangelogMail ,
976
+ allowsDevxMail : emailNotificationSettings ?. allowsDevXMail ,
977
+ allowsOnboardingMail : emailNotificationSettings ?. allowsOnboardingMail ,
978
+ } ) ,
979
+ editorSettings : this . toEditorReference ( ideSettings ) ,
980
+ lastVerificationTime : this . toTimestamp ( lastVerificationTime ) ,
981
+ verificationPhoneNumber,
982
+ workspaceClass : workspaceClasses ?. regular ,
983
+ workspaceAutostartOptions : workspaceAutostartOptions ?. map ( ( o ) => this . toWorkspaceAutostartOption ( o ) ) ,
984
+ profile,
985
+ } ) ;
986
+ }
987
+
988
+ toDuration ( from ?: string ) : Duration | undefined {
989
+ if ( ! from ) {
990
+ return undefined ;
991
+ }
992
+ const millis = parseGoDurationToMs ( from ) ;
993
+ const seconds = BigInt ( Math . floor ( millis / 1000 ) ) ;
994
+ const nanos = ( millis % 1000 ) * 1000000 ;
995
+ return new Duration ( {
996
+ seconds,
997
+ nanos,
998
+ } ) ;
999
+ }
1000
+
1001
+ toTimestamp ( from ?: string | undefined ) : Timestamp | undefined {
1002
+ return from ? Timestamp . fromDate ( new Date ( from ) ) : undefined ;
1003
+ }
1004
+
1005
+ toIdentity ( from : IdentityProtocol ) : Identity {
1006
+ const { authId, authName, authProviderId, lastSigninTime, primaryEmail } = from ;
1007
+ return new Identity ( {
1008
+ authProviderId,
1009
+ authId,
1010
+ authName,
1011
+ lastSigninTime : this . toTimestamp ( lastSigninTime ) ,
1012
+ primaryEmail,
1013
+ } ) ;
1014
+ }
1015
+
1016
+ toRoleOrPermission ( from : RoleOrPermission ) : User_RoleOrPermission {
1017
+ switch ( from ) {
1018
+ case "admin" :
1019
+ return User_RoleOrPermission . ADMIN ;
1020
+ case "devops" :
1021
+ return User_RoleOrPermission . DEVOPS ;
1022
+ case "viewer" :
1023
+ return User_RoleOrPermission . VIEWER ;
1024
+ case "developer" :
1025
+ return User_RoleOrPermission . DEVELOPER ;
1026
+ case "registry-access" :
1027
+ return User_RoleOrPermission . REGISTRY_ACCESS ;
1028
+ case "admin-permissions" :
1029
+ return User_RoleOrPermission . ADMIN_PERMISSIONS ;
1030
+ case "admin-users" :
1031
+ return User_RoleOrPermission . ADMIN_USERS ;
1032
+ case "admin-workspace-content" :
1033
+ return User_RoleOrPermission . ADMIN_WORKSPACE_CONTENT ;
1034
+ case "admin-workspaces" :
1035
+ return User_RoleOrPermission . ADMIN_WORKSPACES ;
1036
+ case "admin-projects" :
1037
+ return User_RoleOrPermission . ADMIN_PROJECTS ;
1038
+ case "new-workspace-cluster" :
1039
+ return User_RoleOrPermission . NEW_WORKSPACE_CLUSTER ;
1040
+ }
1041
+ return User_RoleOrPermission . UNSPECIFIED ;
1042
+ }
1043
+
1044
+ toUserFeatureFlags ( from : NamedWorkspaceFeatureFlag ) : User_UserFeatureFlag {
1045
+ switch ( from ) {
1046
+ case "full_workspace_backup" :
1047
+ return User_UserFeatureFlag . FULL_WORKSPACE_BACKUP ;
1048
+ case "workspace_class_limiting" :
1049
+ return User_UserFeatureFlag . WORKSPACE_CLASS_LIMITING ;
1050
+ case "workspace_connection_limiting" :
1051
+ return User_UserFeatureFlag . WORKSPACE_CONNECTION_LIMITING ;
1052
+ case "workspace_psi" :
1053
+ return User_UserFeatureFlag . WORKSPACE_PSI ;
1054
+ }
1055
+ return User_UserFeatureFlag . UNSPECIFIED ;
1056
+ }
1057
+
1058
+ toEditorReference ( from ?: IDESettings ) : EditorReference | undefined {
1059
+ if ( ! from ) {
1060
+ return undefined ;
1061
+ }
1062
+ return new EditorReference ( {
1063
+ name : from . defaultIde ,
1064
+ version : from . useLatestVersion ? "latest" : "stable" ,
1065
+ } ) ;
1066
+ }
1067
+
1068
+ toWorkspaceAutostartOption ( from : WorkspaceAutostartOption ) : User_WorkspaceAutostartOption {
1069
+ return new User_WorkspaceAutostartOption ( {
1070
+ cloneUrl : from . cloneURL ,
1071
+ editorSettings : this . toEditorReference ( from . ideSettings ) ,
1072
+ organizationId : from . organizationId ,
1073
+ region : from . region ,
1074
+ workspaceClass : from . workspaceClass ,
1075
+ } ) ;
1076
+ }
913
1077
}
0 commit comments