notify script: handle organizations
Mail every owners of an organization when a repository belongs to an organization.
This commit is contained in:
parent
f95b79375f
commit
aed354c99a
@ -103,9 +103,29 @@ def db_config_from_gitea():
|
|||||||
|
|
||||||
|
|
||||||
def fetch_mail_from_db(cur, user):
|
def fetch_mail_from_db(cur, user):
|
||||||
query = "select email from public.user where name = %s;"
|
query = "select id, email from public.user where name = %s;"
|
||||||
cur.execute(query, (user,))
|
cur.execute(query, (user,))
|
||||||
return cur.fetchone()[0]
|
uid, mail = cur.fetchone()
|
||||||
|
owners = [mail]
|
||||||
|
|
||||||
|
# Organizations don't have an email address
|
||||||
|
if not mail:
|
||||||
|
logging.debug("'%s' is an organization", user)
|
||||||
|
# Fetch organization id
|
||||||
|
query = (
|
||||||
|
"select id from public.team where org_id = %s and lower_name = 'owners';"
|
||||||
|
)
|
||||||
|
cur.execute(query, (uid,))
|
||||||
|
team_id = cur.fetchone()[0]
|
||||||
|
|
||||||
|
query = (
|
||||||
|
"select public.user.email from team_user INNER JOIN public.user"
|
||||||
|
" ON team_user.uid = public.user.id where team_user.team_id = %s;"
|
||||||
|
)
|
||||||
|
cur.execute(query, (team_id,))
|
||||||
|
owners = [x[0] for x in cur.fetchall()]
|
||||||
|
|
||||||
|
return owners
|
||||||
|
|
||||||
|
|
||||||
def notify(smtp, repositories, agir_key, fetch_mail):
|
def notify(smtp, repositories, agir_key, fetch_mail):
|
||||||
@ -138,8 +158,9 @@ def notify(smtp, repositories, agir_key, fetch_mail):
|
|||||||
raise ValueError(f"URL not recognized ({url})")
|
raise ValueError(f"URL not recognized ({url})")
|
||||||
user, repository = urlsplit(url).path.split("/")[1:3]
|
user, repository = urlsplit(url).path.split("/")[1:3]
|
||||||
assert user and repository, f"Can not fetch user and repository from {url}"
|
assert user and repository, f"Can not fetch user and repository from {url}"
|
||||||
user_email = fetch_mail(user)
|
user_emails = fetch_mail(user)
|
||||||
logging.debug("Mail for '%s' is '%s'", user, user_email)
|
assert user_emails, f"Unable to identify owners of {url}"
|
||||||
|
logging.debug("Mail for '%s' is '%s'", user, " ".join(user_emails))
|
||||||
subject = SUBJECT_TMPL.substitute(repository=repository)
|
subject = SUBJECT_TMPL.substitute(repository=repository)
|
||||||
message = MAIL_TMPL.substitute(
|
message = MAIL_TMPL.substitute(
|
||||||
user=user, repository=repository, animsys=animsys
|
user=user, repository=repository, animsys=animsys
|
||||||
@ -149,11 +170,11 @@ def notify(smtp, repositories, agir_key, fetch_mail):
|
|||||||
email.set_content(message)
|
email.set_content(message)
|
||||||
email["Subject"] = subject
|
email["Subject"] = subject
|
||||||
email["From"] = FORGE_ML
|
email["From"] = FORGE_ML
|
||||||
email["To"] = user_email
|
email["To"] = ", ".join(user_emails)
|
||||||
email["CC"] = FORGE_ML
|
email["CC"] = FORGE_ML
|
||||||
|
|
||||||
smtp.send_message(email)
|
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"
|
issue_api = f"{AGIR_URL}/issues/5615.json"
|
||||||
req = requests.put(
|
req = requests.put(
|
||||||
|
Loading…
Reference in New Issue
Block a user