By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

Fix PR notifications

roundabout,
created on Tuesday, 25 June 2024, 08:06:59 (1719302819), received on Wednesday, 31 July 2024, 06:54:50 (1722408890)
Author identity: vlad <vlad.muntoiu@gmail.com>

d1a3137c1a2341999f1fbf704ff702ed9d9b713c

app.py

@@ -324,8 +324,8 @@ def notifications():

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                 notifications=UserNotification.query.filter_by(
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                         user_username=flask.session.get("username")
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    ).order_by(UserNotification.id.desc()),
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                             db=db, Commit=Commit, Post=Post
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                             )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                             db=db, Commit=Commit, Post=Post, PullRequest=PullRequest
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            @app.route("/notifications/<int:notification_id>/read", methods=["POST"])
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -1485,6 +1485,19 @@ def repository_prs(username, repository):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    db.session.add(pull_request)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    db.session.commit()
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # Create the notification
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                notification = Notification({"type": "pr", "head": pull_request.head.route, "base": pull_request.base.route, "pr": pull_request.id})
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                db.session.add(notification)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # Send a notification to all users who have enabled PR notifications for this repo
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                for relationship in RepoFavourite.query.filter_by(repo_route=pull_request.base.route, notify_pr=True).all():
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    user = relationship.user
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    user_notification = UserNotification(user, notification, 1)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    db.session.add(user_notification)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    celery_tasks.send_notification.apply_async(args=[user_notification.id])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    return flask.redirect(".", 303)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

celery_tasks.py

@@ -63,7 +63,7 @@ def send_notification(user_notification_id):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    "pr",
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    username=user.username,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    pr=pr,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/repo/" + notification.data.get("repo") + "/prs/"
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + notification.data.get("base") + "/prs/"
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                )
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        mail.sendmail(config.NOTIFICATION_EMAIL, user.email, message)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

email_templates/pr.txt

@@ -1,6 +1,6 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            {% extends "mail.txt" %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            {% block content %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        # Merge {{ pr.head_route }}:{{ pr.head_branch }} into {{ pr.base_route }}:{{ pr.base_branch }}?</h1>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        # Merge {{ pr.head_route }}:{{ pr.head_branch }} into {{ pr.base_route }}:{{ pr.base_branch }}?
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            Requested by {{ pr.owner.username }}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            {% endblock %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

models.py

@@ -424,18 +424,6 @@ with (app.app_context()):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        self.base_branch = base_branch
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        self.owner = owner
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    # Create the notification
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    notification = Notification({"type": "pr", "head": head.route, "base": base.route, "pr": self.id})
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    db.session.add(notification)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    db.session.commit()              # save the notification to get the ID
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    # Send a notification to all users who have enabled PR notifications for this repo
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    for relationship in RepoFavourite.query.filter_by(repo_route=base.route, notify_pr=True).all():
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        user = relationship.user
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        user_notification = UserNotification(user, notification, 1)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        db.session.add(user_notification)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        celery_tasks.send_notification.apply_async(args=[user_notification.id])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                class EmailChangeRequest(db.Model):
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

templates/notifications.html

@@ -34,8 +34,8 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    {% trans %}in{% endtrans %} <a href="{{ post.repo.route }}">{{ post.repo.owner.username }}/{{ post.repo.name }}</a>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                </p>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                            {% elif notification.notification.data["type"] == "pr" %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            {% set pr = db.session.get(PullRequest, notification.notification.data["repo"] + "/" + notification.notification.data["pr"]) %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            <h2><a href="{{ notification.notification.data['repo'] + '/prs/' }}">{{ pr.head_route }}:{{ pr.head_branch }} → {{ pr.base_route }}:{{ pr.base_branch }}</a></h2>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            {% set pr = db.session.get(PullRequest, notification.notification.data["pr"]|int) %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            <h2><a href="{{ notification.notification.data['base'] + '/prs/' }}">{{ pr.head_route }}:{{ pr.head_branch }} → {{ pr.base_route }}:{{ pr.base_branch }}</a></h2>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                <p>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    {% trans %}Requested by{% endtrans %} <a href="/{{ pr.owner.username }}">{{ pr.owner.username }}</a>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    {% trans %}in{% endtrans %} <a href="{{ pr.base_route }}">{{ pr.base.username }}/{{ pr.base.name }}</a>