aboutsummaryrefslogtreecommitdiffstats
path: root/email_alert.py
blob: 4520196a12274ef812d68efdf8a92895f298c243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright (C) 2024 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
""" Module contains logic for sending email alerts for git commit authors """

import urllib
import smtplib
from email.message import EmailMessage
from pygerrit2 import GerritRestAPI


def get_authors(url, project, shas):
    """ Fetches authors for given shas"""
    https_url = f"https://{url}"
    authors = []

    rest_api = GerritRestAPI(https_url, auth=None)
    encoded_project = urllib.parse.quote(project, safe='')
    for sha in shas:
        res = rest_api.get(f"/projects/{encoded_project}/commits/{sha}")
        authors.append(res["author"]["email"])

    return authors


def send_email(smtp_server, sender, authors, cc, subject, message):
    # pylint: disable=R0913
    """ Sends email for authors """
    msg = EmailMessage()
    msg.set_content(message)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['Cc'] = cc
    msg['To'] = ', '.join(authors)

    s = smtplib.SMTP(smtp_server)
    s.send_message(msg)
    s.quit()