Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/attribution.ts
2498 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Organization } from "./teams-projects-protocol";
8
9
export type AttributionId = TeamAttributionId;
10
export type AttributionTarget = "user" | "team";
11
12
export interface TeamAttributionId {
13
kind: "team";
14
teamId: string;
15
}
16
17
export namespace AttributionId {
18
const SEPARATOR = ":";
19
20
export function createFromOrganizationId(organizationId: string): AttributionId {
21
return { kind: "team", teamId: organizationId };
22
}
23
24
export function create(organization: Organization): AttributionId {
25
return createFromOrganizationId(organization.id);
26
}
27
28
export function parse(s: string): AttributionId | undefined {
29
if (!s) {
30
return undefined;
31
}
32
const parts = s.split(":");
33
if (parts.length !== 2) {
34
return undefined;
35
}
36
switch (parts[0]) {
37
case "team":
38
return { kind: "team", teamId: parts[1] };
39
}
40
return undefined;
41
}
42
43
export function render(id: AttributionId): string {
44
switch (id.kind) {
45
case "team":
46
return `team${SEPARATOR}${id.teamId}`;
47
}
48
// allthough grayed as unreachable it is reachable at runtime
49
throw new Error("invalid attributionId kind : " + id.kind);
50
}
51
52
export function equals(a: AttributionId, b: AttributionId): boolean {
53
return render(a) === render(b);
54
}
55
}
56
57