-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitHubCache.ts
More file actions
187 lines (149 loc) · 6.28 KB
/
gitHubCache.ts
File metadata and controls
187 lines (149 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { CacheClient } from "../app.ts";
import { ILogger } from "../logging.ts";
import { AddMemberResponse, CopilotAddResponse, GitHubTeamId, InstalledClient, OrgConfigResponse, OrgInvite, OrgRoles, RemoveMemberResponse, Response } from "./gitHubTypes.ts";
export class GitHubClientCache implements InstalledClient {
client: InstalledClient;
cacheClient: CacheClient;
logger:ILogger;
constructor(client: InstalledClient, cacheClient: CacheClient, logger:ILogger) {
this.client = client;
this.cacheClient = cacheClient;
this.logger = logger;
}
AddTeamsToCopilotSubscription(teamNames: string[]): Response<CopilotAddResponse[]> {
return this.client.AddTeamsToCopilotSubscription(teamNames);
}
ListPendingInvitesForTeam(teamName: string): Response<OrgInvite[]> {
return this.client.ListPendingInvitesForTeam(teamName);
}
CancelOrgInvite(invite: OrgInvite): Response<unknown> {
return this.client.CancelOrgInvite(invite);
}
GetPendingOrgInvites(): Response<OrgInvite[]> {
return this.client.GetPendingOrgInvites();
}
SetOrgRole(id: string, role: OrgRoles): Response<unknown> {
return this.client.SetOrgRole(id, role);
}
GetCurrentOrgName(): string {
return this.client.GetCurrentOrgName();
}
GetCurrentRateLimit(): Promise<{ remaining: number; }> {
return this.client.GetCurrentRateLimit();
}
AddOrgMember(id: string): Response<unknown> {
return this.client.AddOrgMember(id);
}
async IsUserMember(id: string): Response<boolean> {
const cacheKey = `github-member-1:${id}-${this.GetCurrentOrgName()}`;
const result = await this.cacheClient.get(cacheKey);
if (result) {
this.ReportCacheHit({
cacheKey: cacheKey,
operation: "IsUserMember",
value: result,
user: id
});
return {
successful: true,
data: Boolean(result)
}
}
const actualResult = await this.client.IsUserMember(id);
if (actualResult.successful) {
// TODO: switch all cache expirations to application configuration values.
const userIsMember = actualResult.data;
if(userIsMember) {
await this.cacheClient.set(cacheKey, userIsMember.toString(), {
EX: 172800 // Expire every 2 days
});
}
else {
// If membership check comes back as "not a member," we still want to cache
// the value so that we aren't hitting GitHub's APIs "too much."
// With that being said, we don't want to cache it for "too long" as then
// it will start to cause user abrasion.
await this.cacheClient.set(cacheKey, userIsMember.toString(), {
EX: 1800 // Expire every 30 minutes
// It is unlikely that 30 minutes will cause much pain
});
}
}
return actualResult;
}
GetAllTeams(): Response<GitHubTeamId[]> {
return this.client.GetAllTeams();
}
AddTeamMember(team: string, id: string): AddMemberResponse {
return this.client.AddTeamMember(team, id);
}
CreateTeam(teamName: string, description: string): Response<unknown> {
return this.client.CreateTeam(teamName, description);
}
async DoesUserExist(gitHubId: string): Response<string> {
const cacheKey = `github-user-2:${gitHubId}`;
const result = await this.cacheClient.get(cacheKey);
if (result) {
this.ReportCacheHit({
operation: "DoesUserExist",
user: gitHubId,
value: result,
cacheKey: cacheKey
});
return JSON.parse(result);
}
const actualResult = await this.client.DoesUserExist(gitHubId);
if (actualResult.successful) {
await this.cacheClient.set(cacheKey, JSON.stringify(actualResult), {
EX: 2592000 // Expire every 30 days
});
}
else {
// While this caching logic is a bit more complex than I'd like
// (i.e., contains conditional), I believe it is appropriate given
// the context. A user is much more likely to exist for a long
// time than *not exist,* though it is also likely that a user will
// not exist for a short period of time once they realize they do
// not exist...
await this.cacheClient.set(cacheKey, JSON.stringify(actualResult), {
EX: 1800 // Expire every 30 minutes
// It is unlikely that 30 minutes will cause much pain
});
}
return actualResult;
}
ListCurrentMembersOfGitHubTeam(team: string): Response<string[]> {
return this.client.ListCurrentMembersOfGitHubTeam(team);
}
RemoveTeamMemberAsync(team: string, user: string): RemoveMemberResponse {
return this.client.RemoveTeamMemberAsync(team, user);
}
UpdateTeamDetails(team: string, description: string): Response<unknown> {
return this.client.UpdateTeamDetails(team, description);
}
AddSecurityManagerTeam(team: string): Promise<unknown> {
return this.client.AddSecurityManagerTeam(team);
}
GetConfigurationForInstallation(): OrgConfigResponse {
return this.client.GetConfigurationForInstallation();
}
private ReportCacheHit(props: {operation: string, user?: string, team?:string, value: string, cacheKey:string}) {
const properties:any = {
"Group": "GitHub",
"Operation": props.operation,
"Org": this.GetCurrentOrgName(),
"Value": props.value,
"CacheKey": props.cacheKey
};
if(props.user) {
properties["User"] = props.user;
}
if(props.team) {
properties["Team"] = props.team;
}
this.logger.ReportEvent({
Name:"CacheHit",
properties: properties
});
}
}