Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Memoize email to user lookup from Slack #4637

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions baker/DeployUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../settings/serverSettings.js"
import { SiteBaker } from "../baker/SiteBaker.js"
import { WebClient } from "@slack/web-api"
import { DeployChange, DeployMetadata } from "@ourworldindata/utils"
import { DeployChange, DeployMetadata, memoize } from "@ourworldindata/utils"
import { KnexReadonlyTransaction } from "../db/db.js"

const deployQueueServer = new DeployQueueServer()
Expand Down Expand Up @@ -127,20 +127,26 @@ const getEmailSlackMentionsMap = async (
* Get a Slack mention for a given email address. Format it according to the
* Slack API requirements to mention a user in a message
* (https://api.slack.com/reference/surfaces/formatting#mentioning-users).
* Slack has a tight limit of 20 requests per minute for email lookups, so we
* memoize this.
*/
const getSlackMentionByEmail = async (
email: string | undefined,
slackClient: WebClient
): Promise<string | undefined> => {
if (!email || email === "[email protected]") return
const getSlackMentionByEmail = memoize(
async (
email: string | undefined,
slackClient: WebClient
): Promise<string | undefined> => {
if (!email || email === "[email protected]") return

try {
const response = await slackClient.users.lookupByEmail({ email })
return response.user?.id ? `<@${response.user.id}>` : undefined
} catch (error) {
throw new Error(`Error looking up email "${email}" in slack: ${error}`)
try {
const response = await slackClient.users.lookupByEmail({ email })
return response.user?.id ? `<@${response.user.id}>` : undefined
} catch (error) {
throw new Error(
`Error looking up email "${email}" in slack: ${error}`
)
}
}
}
)

const MAX_SUCCESSIVE_FAILURES = 2

Expand Down