Blame progit/lib/notify.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
"""
Pierre-Yves Chibon 8a5345
 (c) 2014-2015 - Copyright Red Hat Inc
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
 Authors:
Pierre-Yves Chibon d8781e
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
progit notifications.
Pierre-Yves Chibon d8781e
"""
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
import smtplib
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
import progit
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
from email.mime.text import MIMEText
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon 67cc89
def send_email(text, subject, to_mail, from_mail=None, mail_id=None,
Pierre-Yves Chibon 67cc89
               in_reply_to=None):
Pierre-Yves Chibon d8781e
    ''' Send an email with the specified information.
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
    :arg text: the content of the email to send
Pierre-Yves Chibon d8781e
    :arg subject: the subject of the email
Pierre-Yves Chibon d8781e
    :arg to_mail: a string representing a list of recipient separated by a
Pierre-Yves Chibon d8781e
        coma
Pierre-Yves Chibon d8781e
    :kwarg from_mail: the email address the email is sent from.
Pierre-Yves Chibon d8781e
        Defaults to nobody@progit
Pierre-Yves Chibon 67cc89
    :kwarg mail_id: if defined, the header `mail-id` is set with this value
Pierre-Yves Chibon 67cc89
    :kwarg in_reply_to: if defined, the header `In-Reply-To` is set with
Pierre-Yves Chibon 67cc89
        this value
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
    '''
Pierre-Yves Chibon d8781e
    msg = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
Pierre-Yves Chibon d8781e
    msg['Subject'] = '[Progit] %s' % subject
Pierre-Yves Chibon d8781e
    if not from_mail:
Pierre-Yves Chibon d8781e
        from_email = 'progit@fedoraproject.org'
Pierre-Yves Chibon d8781e
    msg['From'] = from_email
Pierre-Yves Chibon 977702
    msg['Bcc'] = to_mail.replace(',', ', ')
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon 67cc89
    if mail_id:
Pierre-Yves Chibon 67cc89
        msg['mail-id'] = mail_id
Pierre-Yves Chibon d89746
        msg['Message-Id'] = '<%s>' % mail_id
Pierre-Yves Chibon 67cc89
Pierre-Yves Chibon 67cc89
    if in_reply_to:
Pierre-Yves Chibon 67cc89
        msg['In-Reply-To'] = '<%s>' % in_reply_to
Pierre-Yves Chibon 67cc89
Pierre-Yves Chibon d8781e
    # Send the message via our own SMTP server, but don't include the
Pierre-Yves Chibon d8781e
    # envelope header.
Pierre-Yves Chibon d8781e
    smtp = smtplib.SMTP(progit.APP.config['SMTP_SERVER'])
Pierre-Yves Chibon d8781e
    smtp.sendmail(
Pierre-Yves Chibon d8781e
        from_email,
Pierre-Yves Chibon d8781e
        to_mail.split(','),
Pierre-Yves Chibon d8781e
        msg.as_string())
Pierre-Yves Chibon d8781e
    smtp.quit()
Pierre-Yves Chibon d8781e
    return msg
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon f0c222
def notify_new_comment(comment):
Pierre-Yves Chibon d8781e
    ''' Notify the people following an issue that a new comment was added
Pierre-Yves Chibon d8781e
    to the issue.
Pierre-Yves Chibon d8781e
    '''
Pierre-Yves Chibon d8781e
    text = """
Pierre-Yves Chibon 20ba4e
%s added a new comment to an issue you are following.
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
New comment:
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
``
Pierre-Yves Chibon d8781e
%s
Pierre-Yves Chibon d8781e
``
Pierre-Yves Chibon d8781e
Pierre-Yves Chibon d8781e
%s
Pierre-Yves Chibon d8781e
""" % (
Pierre-Yves Chibon d8781e
    comment.user.user,
Pierre-Yves Chibon d8781e
    comment.comment,
Pierre-Yves Chibon d8781e
    '%s/%s/issue/%s' % (
Pierre-Yves Chibon d8781e
        progit.APP.config['APP_URL'],
Pierre-Yves Chibon d8781e
        comment.issue.project.name,
Pierre-Yves Chibon f0c222
        comment.issue.id,
Pierre-Yves Chibon d8781e
    ),
Pierre-Yves Chibon d8781e
    )
Pierre-Yves Chibon d8781e
    mail_to = set([cmt.user.emails[0].email for cmt in comment.issue.comments])
Pierre-Yves Chibon d8781e
    mail_to.add(comment.issue.project.user.emails[0].email)
Pierre-Yves Chibon b12da0
    if comment.issue.assignee and comment.issue.assignee.emails:
Pierre-Yves Chibon 643da6
        mail_to.add(comment.issue.assignee.emails[0].email)
Pierre-Yves Chibon d8781e
    send_email(
Pierre-Yves Chibon d8781e
        text,
Pierre-Yves Chibon d8781e
        'Update to issue `%s`' % comment.issue.title,
Pierre-Yves Chibon 5d4b74
        ','.join(mail_to),
Pierre-Yves Chibon 5d4b74
        in_reply_to=comment.issue.mail_id,
Pierre-Yves Chibon d8781e
    )
Pierre-Yves Chibon cf312a
Pierre-Yves Chibon cf312a
Pierre-Yves Chibon f0c222
def notify_new_issue(issue):
Pierre-Yves Chibon cf312a
    ''' Notify the people following a project that a new issue was added
Pierre-Yves Chibon cf312a
    to it.
Pierre-Yves Chibon cf312a
    '''
Pierre-Yves Chibon cf312a
    text = """
Pierre-Yves Chibon cf312a
%s reported a new issue against the project: `%s` that you are following.
Pierre-Yves Chibon cf312a
Pierre-Yves Chibon cf312a
New issue:
Pierre-Yves Chibon cf312a
Pierre-Yves Chibon cf312a
``
Pierre-Yves Chibon cf312a
%s
Pierre-Yves Chibon cf312a
``
Pierre-Yves Chibon cf312a
Pierre-Yves Chibon cf312a
%s
Pierre-Yves Chibon cf312a
""" % (
Pierre-Yves Chibon cf312a
    issue.user.user,
Pierre-Yves Chibon cf312a
    issue.project.name,
Pierre-Yves Chibon cf312a
    issue.content,
Pierre-Yves Chibon cf312a
    '%s/%s/issue/%s' % (
Pierre-Yves Chibon cf312a
        progit.APP.config['APP_URL'],
Pierre-Yves Chibon cf312a
        issue.project.name,
Pierre-Yves Chibon f0c222
        issue.id,
Pierre-Yves Chibon cf312a
    ),
Pierre-Yves Chibon cf312a
    )
Pierre-Yves Chibon cf312a
    mail_to = set([cmt.user.emails[0].email for cmt in issue.comments])
Pierre-Yves Chibon cf312a
    mail_to.add(issue.project.user.emails[0].email)
Pierre-Yves Chibon 643da6
    if issue.assignee:
Pierre-Yves Chibon 643da6
        mail_to.add(issue.assignee.emails[0].email)
Pierre-Yves Chibon d54813
    for prouser in issue.project.users:
Pierre-Yves Chibon a1c51a
        if prouser.user.emails:
Pierre-Yves Chibon a1c51a
            mail_to.add(prouser.user.emails[0].email)
Pierre-Yves Chibon a1c51a
Pierre-Yves Chibon cf312a
    send_email(
Pierre-Yves Chibon cf312a
        text,
Pierre-Yves Chibon cf312a
        'New issue `%s`' % issue.title,
Pierre-Yves Chibon 6fb585
        ','.join(mail_to),
Pierre-Yves Chibon 6fb585
        mail_id=issue.mail_id,
Pierre-Yves Chibon cf312a
    )
Pierre-Yves Chibon 4cfb2a
Pierre-Yves Chibon 4cfb2a
Pierre-Yves Chibon 83f04b
def notify_assigned_issue(issue, new_assignee, username):
Pierre-Yves Chibon ff30f1
    ''' Notify the people following an issue that the assignee changed.
Pierre-Yves Chibon ff30f1
    '''
Pierre-Yves Chibon e52ecf
    action = 'reset'
Pierre-Yves Chibon 186206
    if new_assignee:
Pierre-Yves Chibon bda6e4
        action = 'assigned to `%s`' % new_assignee.user
Pierre-Yves Chibon ff30f1
    text = """
Pierre-Yves Chibon 186206
The issue: `%s` of project: `%s` has been %s by %s.
Pierre-Yves Chibon ff30f1
Pierre-Yves Chibon ff30f1
%s
Pierre-Yves Chibon ff30f1
""" % (
Pierre-Yves Chibon ff30f1
    issue.title,
Pierre-Yves Chibon ff30f1
    issue.project.name,
Pierre-Yves Chibon 186206
    action,
Pierre-Yves Chibon 83f04b
    username,
Pierre-Yves Chibon ff30f1
    '%s/%s/issue/%s' % (
Pierre-Yves Chibon ff30f1
        progit.APP.config['APP_URL'],
Pierre-Yves Chibon ff30f1
        issue.project.name,
Pierre-Yves Chibon f0c222
        issue.id,
Pierre-Yves Chibon ff30f1
    ),
Pierre-Yves Chibon ff30f1
    )
Pierre-Yves Chibon ff30f1
    mail_to = set([cmt.user.emails[0].email for cmt in issue.comments])
Pierre-Yves Chibon ff30f1
    mail_to.add(issue.project.user.emails[0].email)
Pierre-Yves Chibon ff30f1
    if issue.assignee and issue.assignee.emails:
Pierre-Yves Chibon ff30f1
        mail_to.add(issue.assignee.emails[0].email)
Pierre-Yves Chibon 2297b2
    if new_assignee and new_assignee.emails:
Pierre-Yves Chibon 2297b2
        mail_to.add(new_assignee.emails[0].email)
Pierre-Yves Chibon ff30f1
Pierre-Yves Chibon ff30f1
    send_email(
Pierre-Yves Chibon ff30f1
        text,
Pierre-Yves Chibon ff30f1
        'Issue `%s` assigned' % issue.title,
Pierre-Yves Chibon ff30f1
        ','.join(mail_to),
Pierre-Yves Chibon ff30f1
        mail_id=issue.mail_id,
Pierre-Yves Chibon ff30f1
    )
Pierre-Yves Chibon ff30f1
Pierre-Yves Chibon ff30f1
Pierre-Yves Chibon f0c222
def notify_new_pull_request(request):
Pierre-Yves Chibon 4cfb2a
    ''' Notify the people following a project that a new pull-request was
Pierre-Yves Chibon 4cfb2a
    added to it.
Pierre-Yves Chibon 4cfb2a
    '''
Pierre-Yves Chibon 4cfb2a
    text = """
Pierre-Yves Chibon 4cfb2a
%s opened a new pull-request against the project: `%s` that you are following.
Pierre-Yves Chibon 4cfb2a
Pierre-Yves Chibon 4cfb2a
New pull-request:
Pierre-Yves Chibon 4cfb2a
Pierre-Yves Chibon 4cfb2a
``
Pierre-Yves Chibon 4cfb2a
%s
Pierre-Yves Chibon 4cfb2a
``
Pierre-Yves Chibon 4cfb2a
Pierre-Yves Chibon 4cfb2a
%s
Pierre-Yves Chibon 4cfb2a
""" % (
Pierre-Yves Chibon 4cfb2a
    request.user.user,
Pierre-Yves Chibon 4cfb2a
    request.repo.name,
Pierre-Yves Chibon 4cfb2a
    request.title,
Pierre-Yves Chibon 4cfb2a
    '%s/%s/request-pull/%s' % (
Pierre-Yves Chibon 4cfb2a
        progit.APP.config['APP_URL'],
Pierre-Yves Chibon 4cfb2a
        request.repo.name,
Pierre-Yves Chibon f0c222
        request.id,
Pierre-Yves Chibon 4cfb2a
    ),
Pierre-Yves Chibon 4cfb2a
    )
Pierre-Yves Chibon 4cfb2a
    mail_to = set([cmt.user.emails[0].email for cmt in request.comments])
Pierre-Yves Chibon 4cfb2a
    mail_to.add(request.repo.user.emails[0].email)
Pierre-Yves Chibon 4cfb2a
    for prouser in request.repo.users:
Pierre-Yves Chibon 4cfb2a
        if prouser.user.emails:
Pierre-Yves Chibon 4cfb2a
            mail_to.add(prouser.user.emails[0].email)
Pierre-Yves Chibon 4cfb2a
Pierre-Yves Chibon 4cfb2a
    send_email(
Pierre-Yves Chibon 4cfb2a
        text,
Pierre-Yves Chibon 4cfb2a
        'Pull-Request #%s `%s`' % (request.id, request.title),
Pierre-Yves Chibon 4cfb2a
        ','.join(mail_to),
Pierre-Yves Chibon 4cfb2a
        mail_id=request.mail_id,
Pierre-Yves Chibon 4cfb2a
    )
Pierre-Yves Chibon 688943
Pierre-Yves Chibon 688943
Pierre-Yves Chibon f0c222
def notify_merge_pull_request(request, user):
Pierre-Yves Chibon 688943
    ''' Notify the people following a project that a pull-request was merged
Pierre-Yves Chibon 688943
    in it.
Pierre-Yves Chibon 688943
    '''
Pierre-Yves Chibon 688943
    text = """
Pierre-Yves Chibon 688943
%s merged a pull-request against the project: `%s` that you are following.
Pierre-Yves Chibon 688943
Pierre-Yves Chibon 688943
Merged pull-request:
Pierre-Yves Chibon 688943
Pierre-Yves Chibon 688943
``
Pierre-Yves Chibon 688943
%s
Pierre-Yves Chibon 688943
``
Pierre-Yves Chibon 688943
Pierre-Yves Chibon 688943
%s
Pierre-Yves Chibon 688943
""" % (
Pierre-Yves Chibon df805b
    user.username,
Pierre-Yves Chibon 688943
    request.repo.name,
Pierre-Yves Chibon 688943
    request.title,
Pierre-Yves Chibon 688943
    '%s/%s/request-pull/%s' % (
Pierre-Yves Chibon 688943
        progit.APP.config['APP_URL'],
Pierre-Yves Chibon 688943
        request.repo.name,
Pierre-Yves Chibon f0c222
        request.id,
Pierre-Yves Chibon 688943
    ),
Pierre-Yves Chibon 688943
    )
Pierre-Yves Chibon 688943
    mail_to = set([cmt.user.emails[0].email for cmt in request.comments])
Pierre-Yves Chibon 688943
    mail_to.add(request.repo.user.emails[0].email)
Pierre-Yves Chibon 688943
    for prouser in request.repo.users:
Pierre-Yves Chibon 688943
        if prouser.user.emails:
Pierre-Yves Chibon 688943
            mail_to.add(prouser.user.emails[0].email)
Pierre-Yves Chibon 688943
Pierre-Yves Chibon 688943
    send_email(
Pierre-Yves Chibon 688943
        text,
Pierre-Yves Chibon 688943
        'Pull-Request #%s `%s`' % (request.id, request.title),
Pierre-Yves Chibon 688943
        ','.join(mail_to),
Pierre-Yves Chibon 688943
        mail_id=request.mail_id,
Pierre-Yves Chibon 688943
    )
Johan Cwiklinski 86d9c4
Johan Cwiklinski 86d9c4
Pierre-Yves Chibon f0c222
def notify_cancelled_pull_request(request, user):
Johan Cwiklinski 86d9c4
    ''' Notify the people following a project that a pull-request was
Johan Cwiklinski 86d9c4
    cancelled in it.
Johan Cwiklinski 86d9c4
    '''
Johan Cwiklinski 86d9c4
    text = """
Johan Cwiklinski 86d9c4
%s canceled a pull-request against the project: `%s` that you are following.
Johan Cwiklinski 86d9c4
Johan Cwiklinski 86d9c4
Cancelled pull-request:
Johan Cwiklinski 86d9c4
Johan Cwiklinski 86d9c4
``
Johan Cwiklinski 86d9c4
%s
Johan Cwiklinski 86d9c4
``
Johan Cwiklinski 86d9c4
Johan Cwiklinski 86d9c4
%s
Johan Cwiklinski 86d9c4
""" % (
Pierre-Yves Chibon df805b
    user.username,
Johan Cwiklinski 86d9c4
    request.repo.name,
Johan Cwiklinski 86d9c4
    request.title,
Johan Cwiklinski 86d9c4
    '%s/%s/request-pull/%s' % (
Johan Cwiklinski 86d9c4
        progit.APP.config['APP_URL'],
Johan Cwiklinski 86d9c4
        request.repo.name,
Pierre-Yves Chibon f0c222
        request.id,
Johan Cwiklinski 86d9c4
    ),
Johan Cwiklinski 86d9c4
    )
Johan Cwiklinski 86d9c4
    mail_to = set([cmt.user.emails[0].email for cmt in request.comments])
Johan Cwiklinski 86d9c4
    mail_to.add(request.repo.user.emails[0].email)
Johan Cwiklinski 86d9c4
    for prouser in request.repo.users:
Johan Cwiklinski 86d9c4
        if prouser.user.emails:
Johan Cwiklinski 86d9c4
            mail_to.add(prouser.user.emails[0].email)
Johan Cwiklinski 86d9c4
Johan Cwiklinski 86d9c4
    send_email(
Johan Cwiklinski 86d9c4
        text,
Johan Cwiklinski 86d9c4
        'Pull-Request #%s `%s`' % (request.id, request.title),
Johan Cwiklinski 86d9c4
        ','.join(mail_to),
Johan Cwiklinski 86d9c4
        mail_id=request.mail_id,
Johan Cwiklinski 86d9c4
    )