Skip to content

Commit

Permalink
uberf-9236: reuse known user names
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Zinoviev <[email protected]>
  • Loading branch information
lexiv0re committed Jan 22, 2025
1 parent a1e3e31 commit e806f84
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
44 changes: 27 additions & 17 deletions dev/tool/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,34 @@ export async function fillGithubUsers (ctx: MeasureContext, db: AccountDB): Prom
while (true) {
try {
if (account.githubId == null) break

const githubUserRes = await getGithubUser(account.githubId)
if (githubUserRes.code === 200 && githubUserRes.login != null) {
await db.account.updateOne({ _id: account._id }, { githubUser: githubUserRes.login })
ctx.info('github user added', { githubId: account.githubId, githubUser: githubUserRes.login })
break
} else if (githubUserRes.code === 404) {
ctx.info('github user not found', { githubId: account.githubId })
break
} else if (githubUserRes.code === 403) {
const timeout =
githubUserRes.rateLimitReset != null
? githubUserRes.rateLimitReset - Date.now() + 1000
: defaultRetryTimeout
ctx.info('rate limit exceeded. Retrying in ', { githubId: account.githubId, retryTimeout: timeout })
await new Promise((resolve) => setTimeout(resolve, timeout))
let username: string | undefined
if ((account.email).startsWith('github:')) {
username = account.email.slice(7)
} else {
ctx.error('failed to get github user', { githubId: account.githubId, ...githubUserRes })
const githubUserRes = await getGithubUser(account.githubId)
if (githubUserRes.code === 200 && githubUserRes.login != null) {
username = githubUserRes.login
} else if (githubUserRes.code === 404) {
ctx.info('github user not found', { githubId: account.githubId })
break
} else if (githubUserRes.code === 403) {
const timeout =
githubUserRes.rateLimitReset != null
? githubUserRes.rateLimitReset - Date.now() + 1000
: defaultRetryTimeout
ctx.info('rate limit exceeded. Retrying in ', {
githubId: account.githubId,
retryTimeoutMin: Math.ceil(timeout / (1000 * 60))
})
await new Promise((resolve) => setTimeout(resolve, timeout))
} else {
ctx.error('failed to get github user', { githubId: account.githubId, ...githubUserRes })
break
}
}
if (username != null) {
await db.account.updateOne({ _id: account._id }, { githubUser: username.toLowerCase() })
ctx.info('github user added', { githubId: account.githubId, githubUser: username.toLowerCase() })
break
}
} catch (err: any) {
Expand Down
9 changes: 5 additions & 4 deletions pods/authProviders/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ export function registerGithub (
async (ctx, next) => {
try {
let email = ctx.state.user.emails?.[0]?.value
const username = ctx.state.user.username.toLowerCase()
if (email == null || email === '') {
email = `github:${ctx.state.user.username}`
email = `github:${username}`
}

const [first, last] = ctx.state.user.displayName?.split(' ') ?? [ctx.state.user.username, '']
const [first, last] = ctx.state.user.displayName?.split(' ') ?? [username, '']
measureCtx.info('Provider auth handler', { email, type: 'github' })
if (email !== undefined) {
let loginInfo: LoginInfo | null
Expand All @@ -77,7 +78,7 @@ export function registerGithub (
if (state.inviteId != null && state.inviteId !== '') {
loginInfo = await joinWithProvider(measureCtx, db, null, email, first, last, state.inviteId as any, {
githubId: ctx.state.user.id,
githubUser: ctx.state.user.username
githubUser: username
})
} else {
loginInfo = await loginWithProvider(
Expand All @@ -89,7 +90,7 @@ export function registerGithub (
last,
{
githubId: ctx.state.user.id,
githubUser: ctx.state.user.username
githubUser: username
},
signUpDisabled
)
Expand Down

0 comments on commit e806f84

Please sign in to comment.