From a97e47bff0df163d3f1ed30db527b6909e707457 Mon Sep 17 00:00:00 2001
From: Florent LE BOULCH <fleboulch@hellowork.com>
Date: Fri, 17 May 2024 10:18:39 +0200
Subject: [PATCH 1/2] fix: make emails configurable

---
 .env.test                  | 1 +
 README.md                  | 6 ++++++
 src/core/services/slack.ts | 9 +++++----
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/.env.test b/.env.test
index fd8b358..77234f4 100644
--- a/.env.test
+++ b/.env.test
@@ -7,3 +7,4 @@ POSTGRES_PORT=54320
 POSTGRES_USER=root
 SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN=SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN
 SLACK_SIGNING_SECRET=SLACK_SIGNING_SECRET
+EMAIL_PATTERNS=@manomano.com,@prt.manomano.com
diff --git a/README.md b/README.md
index ee4b1d0..bd6a23b 100644
--- a/README.md
+++ b/README.md
@@ -202,6 +202,12 @@ Create a `.env` file containing the following variables:
 
     ![OAuth token](docs/assets/slack/oauth-token.png)
 
+- `EMAIL_PATTERNS`
+  Email patterns of your organization (separated by comma),
+  for instance `@my.org,@contractors.my.org`.
+  Used to find Slack users from Gitlab username.
+  Note that this means your org has to use the same naming scheme for emails and gitlab username.
+
 If you want Homer to connect to an **external PostgreSQL database**, you can set
 the following variables:
 
diff --git a/src/core/services/slack.ts b/src/core/services/slack.ts
index a7618d7..03f4fc9 100644
--- a/src/core/services/slack.ts
+++ b/src/core/services/slack.ts
@@ -8,6 +8,7 @@ import { logger } from './logger';
 const SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN = getEnvVariable(
   'SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN'
 );
+const EMAIL_PATTERNS = getEnvVariable('EMAIL_PATTERNS');
 
 // This client should be used for everything else.
 export const slackBotWebClient = new WebClient(
@@ -94,10 +95,10 @@ export async function fetchSlackUserFromGitlabUser({
 export async function fetchSlackUserFromGitlabUsername(
   username: string
 ): Promise<SlackUser | undefined> {
-  return fetchSlackUserFromEmails([
-    `${username}@manomano.com`,
-    `${username}@prt.manomano.com`,
-  ]);
+  const emails = EMAIL_PATTERNS.split(',').map(
+    (emailPattern) => `${username}${emailPattern}`
+  );
+  return fetchSlackUserFromEmails(emails);
 }
 
 export async function fetchSlackUserFromId(

From 88765f7c4f9412c08591e73e4d4b4b67191b247a Mon Sep 17 00:00:00 2001
From: Florent LE BOULCH <florent.le.boulch@gmail.com>
Date: Mon, 27 May 2024 10:47:37 +0200
Subject: [PATCH 2/2] fixup! fix: make emails configurable

---
 .env.test                  | 2 +-
 README.md                  | 6 +++---
 src/core/services/slack.ts | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/.env.test b/.env.test
index 77234f4..fc44bcc 100644
--- a/.env.test
+++ b/.env.test
@@ -7,4 +7,4 @@ POSTGRES_PORT=54320
 POSTGRES_USER=root
 SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN=SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN
 SLACK_SIGNING_SECRET=SLACK_SIGNING_SECRET
-EMAIL_PATTERNS=@manomano.com,@prt.manomano.com
+EMAIL_DOMAINS=my-domain.com,ext.my-domain.com
diff --git a/README.md b/README.md
index bd6a23b..13be23b 100644
--- a/README.md
+++ b/README.md
@@ -202,9 +202,9 @@ Create a `.env` file containing the following variables:
 
     ![OAuth token](docs/assets/slack/oauth-token.png)
 
-- `EMAIL_PATTERNS`
-  Email patterns of your organization (separated by comma),
-  for instance `@my.org,@contractors.my.org`.
+- `EMAIL_DOMAINS`
+  Email domains of your organization (separated by comma),
+  for instance `my-domain.com,ext.my-domain.com`.
   Used to find Slack users from Gitlab username.
   Note that this means your org has to use the same naming scheme for emails and gitlab username.
 
diff --git a/src/core/services/slack.ts b/src/core/services/slack.ts
index 03f4fc9..6787f5a 100644
--- a/src/core/services/slack.ts
+++ b/src/core/services/slack.ts
@@ -8,7 +8,7 @@ import { logger } from './logger';
 const SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN = getEnvVariable(
   'SLACK_BOT_USER_O_AUTH_ACCESS_TOKEN'
 );
-const EMAIL_PATTERNS = getEnvVariable('EMAIL_PATTERNS');
+const EMAIL_DOMAINS = getEnvVariable('EMAIL_DOMAINS');
 
 // This client should be used for everything else.
 export const slackBotWebClient = new WebClient(
@@ -95,8 +95,8 @@ export async function fetchSlackUserFromGitlabUser({
 export async function fetchSlackUserFromGitlabUsername(
   username: string
 ): Promise<SlackUser | undefined> {
-  const emails = EMAIL_PATTERNS.split(',').map(
-    (emailPattern) => `${username}${emailPattern}`
+  const emails = EMAIL_DOMAINS.split(',').map(
+    (emailDomain) => `${username}@${emailDomain}`
   );
   return fetchSlackUserFromEmails(emails);
 }