-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UBERF-7924: Fix workspace variable in logs + reuse installation accou…
…nt (#6376)
- Loading branch information
Showing
4 changed files
with
258 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Account, Ref } from '@hcengineering/core' | ||
import type { Collection, FindCursor } from 'mongodb' | ||
import type { GithubUserRecord } from './types' | ||
|
||
export class UserManager { | ||
userCache = new Map<string, GithubUserRecord>() | ||
refUserCache = new Map<string, GithubUserRecord>() | ||
|
||
constructor (readonly usersCollection: Collection<GithubUserRecord>) {} | ||
|
||
public async getUsers (workspace: string): Promise<GithubUserRecord[]> { | ||
return await this.usersCollection | ||
.find<GithubUserRecord>({ | ||
[`accounts.${workspace}`]: { $exists: true } | ||
}) | ||
.toArray() | ||
} | ||
|
||
async getAccount (login: string): Promise<GithubUserRecord | undefined> { | ||
let res = this.userCache.get(login) | ||
if (res !== undefined) { | ||
return res | ||
} | ||
res = (await this.usersCollection.findOne({ _id: login })) ?? undefined | ||
if (res !== undefined) { | ||
if (this.userCache.size > 1000) { | ||
this.userCache.clear() | ||
} | ||
this.userCache.set(login, res) | ||
} | ||
return res | ||
} | ||
|
||
async getAccountByRef (workspace: string, ref: Ref<Account>): Promise<GithubUserRecord | undefined> { | ||
const key = `${workspace}.${ref}` | ||
let rec = this.refUserCache.get(key) | ||
if (rec !== undefined) { | ||
return rec | ||
} | ||
rec = (await this.usersCollection.findOne({ [`accounts.${workspace}`]: ref })) ?? undefined | ||
if (rec !== undefined) { | ||
if (this.refUserCache.size > 1000) { | ||
this.refUserCache.clear() | ||
} | ||
this.refUserCache.set(key, rec) | ||
} | ||
return rec | ||
} | ||
|
||
async updateUser (dta: GithubUserRecord): Promise<void> { | ||
this.userCache.clear() | ||
this.refUserCache.clear() | ||
await this.usersCollection.updateOne({ _id: dta._id }, { $set: dta } as any) | ||
} | ||
|
||
async insertUser (dta: GithubUserRecord): Promise<void> { | ||
await this.usersCollection.insertOne(dta) | ||
} | ||
|
||
async removeUser (login: string): Promise<void> { | ||
this.userCache.clear() | ||
this.refUserCache.clear() | ||
await this.usersCollection.deleteOne({ _id: login }) | ||
} | ||
|
||
getAllUsers (): FindCursor<GithubUserRecord> { | ||
return this.usersCollection.find({}) | ||
} | ||
} |
Oops, something went wrong.