notify script: handle organizations

Mail every owners of an organization when a repository belongs to an
organization.
This commit is contained in:
Pierre-Louis Bonicoli 2022-02-26 01:17:10 +01:00
parent f95b79375f
commit 936e5c99c5
Signed by: pilou
GPG Key ID: 06914C4A5EDAA6DD

View File

@ -103,10 +103,21 @@ def db_config_from_gitea():
def fetch_mail_from_db(cur, user):
query = "select email from public.user where name = %s;"
query = "select uid, email from public.user where name = %s;"
cur.execute(query, (user,))
return cur.fetchone()[0]
owner = cur.fetchone()[0]
owners = [owner[1]]
# Organizations don't have an email address
if not owner[1]:
logging.debug("'%s' is an organization", user)
query = "select uid from org_user where org_id = %s;"
query = "select public.user.email from org_user INNER JOIN public.user"\
"ON org_user.uid = public.user.id where org_user.org_id = %s;"
cur.execute(query, (user,))
owners = cur.fetchall()
return owners
def notify(smtp, repositories, agir_key, fetch_mail):
headers = {"X-Redmine-API-Key": agir_key, "Content-Type": "application/json"}
@ -138,8 +149,8 @@ def notify(smtp, repositories, agir_key, fetch_mail):
raise ValueError(f"URL not recognized ({url})")
user, repository = urlsplit(url).path.split("/")[1:3]
assert user and repository, f"Can not fetch user and repository from {url}"
user_email = fetch_mail(user)
logging.debug("Mail for '%s' is '%s'", user, user_email)
user_emails = fetch_mail(user)
logging.debug("Mail for '%s' is '%s'", user, ' '.join(user_emails))
subject = SUBJECT_TMPL.substitute(repository=repository)
message = MAIL_TMPL.substitute(
user=user, repository=repository, animsys=animsys
@ -149,11 +160,11 @@ def notify(smtp, repositories, agir_key, fetch_mail):
email.set_content(message)
email["Subject"] = subject
email["From"] = FORGE_ML
email["To"] = user_email
email["To"] = ', '.join(user_emails)
email["CC"] = FORGE_ML
smtp.send_message(email)
logging.debug("Mail to '%s' (%s) sent for %s.", user, user_email, url)
logging.debug("Mail to '%s' (%s) sent for %s.", user, email["To"], url)
issue_api = f"{AGIR_URL}/issues/5615.json"
req = requests.put(