Commit 4ae77748 authored by Tom Pillot's avatar Tom Pillot
Browse files

Send mail to the admins when forge sync fails

parent 71ba3dba
......@@ -28,6 +28,7 @@ SERVER_EMAIL = f'{EMAIL_USER}+{PROJECT}@{EMAIL_FQDN}'
DEFAULT_FROM_EMAIL = f'{PROJECT_VERBOSE} <{EMAIL_USER}@{EMAIL_FQDN}>'
EMAIL_BACKEND = 'django.core.mail.backends.%s' % ('filebased.EmailBackend' if DEBUG else 'smtp.EmailBackend')
EMAIL_SUBJECT_PREFIX = f'[{PROJECT_VERBOSE}] '
EMAIL_FILE_PATH = 'tmp/email-messages/'
ADMINS = ((os.environ.get('ADMIN_NAME',
f'{PROJECT_VERBOSE} webmaster'), os.environ.get('ADMIN_MAIL', f'webmaster@{DOMAIN_NAME}')), )
......
"""Views for dashboard_apps."""
import hmac
import logging
import re
import traceback
from hashlib import sha1
from ipaddress import ip_address, ip_network
from json import loads
......@@ -10,6 +13,7 @@ import github
import requests
from autoslug.utils import slugify
from django.conf import settings
from django.core.mail import mail_admins
from django.http import HttpRequest
from django.http.response import (HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseRedirect,
HttpResponseServerError)
......@@ -22,6 +26,8 @@ from rainboard.models import Namespace, Project
from rainboard.utils import SOURCES
from . import models
logger = logging.getLogger('gh.views')
def check_suite(request: HttpRequest, rep: str) -> HttpResponse:
"""Manage Github's check suites."""
......@@ -156,7 +162,9 @@ def push(request: HttpRequest, source: SOURCES, rep: str) -> HttpResponse:
git_repo.git.push(gl_remote_name, branch)
except git.exc.GitCommandError:
# Probably failed because of a force push
# TODO: send an email to the admins ?
message = traceback.format_exc()
message = re.sub(r'://.*@', '://[REDACTED]@', message) # Hide access tokens in the mail
mail_admins(f'Forge sync failed for {namespace.slug}/{project.slug}', message)
raise
return HttpResponse(rep)
......@@ -178,8 +186,7 @@ def pipeline(request: HttpRequest, rep: str) -> HttpResponse:
if gl_status in ['pending', 'success', 'failed']:
gh_status = gl_status if gl_status != 'failed' else 'failure'
if branch.startswith('pr/'):
gh_repo.get_commit(sha=commit).create_status(state=gh_status, target_url=ci_web_url,
context='gitlab-ci')
gh_repo.get_commit(sha=commit).create_status(state=gh_status, target_url=ci_web_url, context='gitlab-ci')
else:
try:
gh_commit = gh_repo.get_branch(branch).commit
......
......@@ -3,6 +3,7 @@ import doctest
from django.test import TestCase
from django.urls import reverse
from dashboard import settings
from . import models, utils
......@@ -22,8 +23,8 @@ class RainboardTests(TestCase):
main_namespace=models.Namespace.objects.get(slug='gepetto'),
main_forge=models.Forge.objects.get(source=utils.SOURCES.github),
license=models.License.objects.first())
self.assertEqual(models.License.objects.count(), license_count+1)
self.assertEqual(models.Project.objects.count(), project_count+1)
self.assertEqual(models.License.objects.count(), license_count + 1)
self.assertEqual(models.Project.objects.count(), project_count + 1)
project = models.Project.objects.get(name='rainboard tests')
......@@ -36,9 +37,11 @@ class RainboardTests(TestCase):
self.assertIn(chunk, badges)
# Test Middleware
response = self.client.get(reverse('rainboard:project', kwargs={'slug': project.slug}),
HTTP_X_FORWARDED_FOR='9.9.9.9')
self.assertEqual(response.status_code, 302)
if f'{settings.PROJECT}.middleware.LAASPermsMiddleware' in settings.MIDDLEWARE:
response = self.client.get(reverse('rainboard:project', kwargs={'slug': project.slug}),
HTTP_X_FORWARDED_FOR='9.9.9.9')
self.assertEqual(response.status_code, 302)
response = self.client.get(reverse('rainboard:project', kwargs={'slug': project.slug}),
HTTP_X_FORWARDED_FOR='140.93.5.4')
self.assertEqual(response.status_code, 200)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment