Skip to content

Commit baeacd3

Browse files
authored
Optimize GitHub integration (#1796)
1 parent 6156a92 commit baeacd3

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

services/libs/integrations/src/integrations/github/processStream.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,50 @@ async function getMemberData(ctx: IProcessStreamContext, login: string): Promise
120120
})
121121
}
122122

123+
async function getOrganizationData(ctx: IProcessStreamContext, company: string): Promise<any> {
124+
if (company === '' || company === null || company === undefined) {
125+
return null
126+
}
127+
128+
const cache = ctx.globalCache
129+
const prefix = (x: string) => `github-org:${x}`
130+
131+
const existing = await cache.get(prefix(company))
132+
if (existing) {
133+
if (existing === 'null') {
134+
return null
135+
}
136+
137+
return JSON.parse(existing)
138+
}
139+
140+
const token = await getGithubToken(ctx)
141+
const fromAPI = await getOrganization(company, token, getTokenRotator(ctx), {
142+
concurrentRequestLimiter: getConcurrentRequestLimiter(ctx),
143+
integrationId: ctx.integration.id,
144+
})
145+
146+
if (fromAPI) {
147+
await cache.set(prefix(company), JSON.stringify(fromAPI), 24 * 60 * 60)
148+
return fromAPI
149+
}
150+
151+
await cache.set(prefix(company), 'null', 24 * 60 * 60)
152+
return null
153+
}
154+
123155
async function getMemberEmail(ctx: IProcessStreamContext, login: string): Promise<string> {
124156
if (IS_TEST_ENV) {
125157
return ''
126158
}
127159

128-
// here we use cache for tenantId-integrationType
160+
// here we use global cache - it is shared between all integrations
129161
// So in LFX case different integration will have access to the same cache
130162
// But this is fine
131-
const cache = ctx.cache
163+
const cache = ctx.globalCache
164+
const prefix = (x: string) => `github-login:${x}`
132165

133-
const existing = await cache.get(login)
166+
const existing = await cache.get(prefix(login))
134167
if (existing) {
135168
if (existing === 'null') {
136169
return ''
@@ -142,11 +175,11 @@ async function getMemberEmail(ctx: IProcessStreamContext, login: string): Promis
142175
const member = await getMemberData(ctx, login)
143176
const email = (member && member.email ? member.email : '').trim()
144177
if (email && email.length > 0) {
145-
await cache.set(login, email, 60 * 60)
178+
await cache.set(prefix(login), email, 60 * 60)
146179
return email
147180
}
148181

149-
await cache.set(login, 'null', 60 * 60)
182+
await cache.set(prefix(login), 'null', 60 * 60)
150183
return ''
151184
}
152185

@@ -179,12 +212,7 @@ export const prepareMember = async (
179212
orgs = [{ name: 'crowd.dev' }]
180213
} else {
181214
const company = memberFromApi.company.replace('@', '').trim()
182-
const token = await getGithubToken(ctx)
183-
const fromAPI = await getOrganization(company, token, getTokenRotator(ctx), {
184-
concurrentRequestLimiter: getConcurrentRequestLimiter(ctx),
185-
integrationId: ctx.integration.id,
186-
})
187-
215+
const fromAPI = await getOrganizationData(ctx, company)
188216
orgs = fromAPI
189217
}
190218
}

0 commit comments

Comments
 (0)