@@ -16,7 +16,7 @@ import {
16
16
WORKSPACE_TIMEOUT_EXTENDED ,
17
17
WORKSPACE_TIMEOUT_EXTENDED_ALT ,
18
18
} from "@gitpod/gitpod-protocol" ;
19
- import { TermsAcceptanceDB , UserDB } from "@gitpod/gitpod-db/lib" ;
19
+ import { ProjectDB , TermsAcceptanceDB , UserDB } from "@gitpod/gitpod-db/lib" ;
20
20
import { HostContextProvider } from "../auth/host-context-provider" ;
21
21
import { log } from "@gitpod/gitpod-protocol/lib/util/logging" ;
22
22
import { Config } from "../config" ;
@@ -63,6 +63,7 @@ export class UserService {
63
63
@inject ( Config ) protected readonly config : Config ;
64
64
@inject ( TermsAcceptanceDB ) protected readonly termsAcceptanceDb : TermsAcceptanceDB ;
65
65
@inject ( TermsProvider ) protected readonly termsProvider : TermsProvider ;
66
+ @inject ( ProjectDB ) protected readonly projectDb : ProjectDB ;
66
67
67
68
/**
68
69
* Takes strings in the form of <authHost>/<authName> and returns the matching User
@@ -205,6 +206,28 @@ export class UserService {
205
206
return false ;
206
207
}
207
208
209
+ /**
210
+ * Identifies the team to which a workspace instance's running time should be attributed to
211
+ * (e.g. for usage analytics or billing purposes).
212
+ * If no specific team is identified, the usage will be attributed to the user instead (default).
213
+ *
214
+ * @param user
215
+ * @param projectId
216
+ */
217
+ async getWorkspaceUsageAttributionTeamId ( user : User , projectId ?: string ) : Promise < string | undefined > {
218
+ if ( ! projectId ) {
219
+ // No project -- attribute to the user.
220
+ return undefined ;
221
+ }
222
+ const project = await this . projectDb . findProjectById ( projectId ) ;
223
+ if ( ! project ?. teamId ) {
224
+ // The project doesn't exist, or it isn't owned by a team -- attribute to the user.
225
+ return undefined ;
226
+ }
227
+ // Attribute workspace usage to the team that currently owns this project.
228
+ return project . teamId ;
229
+ }
230
+
208
231
/**
209
232
* This might throw `AuthException`s.
210
233
*
0 commit comments