Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arkindex
Backend
Commits
cabba7ae
Commit
cabba7ae
authored
4 years ago
by
Bastien Abadie
Browse files
Options
Downloads
Plain Diff
Merge branch 'webhook-error-email' into 'master'
Send an email to user if Git webhook fails Closes
#690
See merge request
!1297
parents
002782b2
d9d68a47
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1297
Send an email to user if Git webhook fails
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
arkindex/dataimport/api.py
+28
-1
28 additions, 1 deletion
arkindex/dataimport/api.py
arkindex/dataimport/tests/test_providers.py
+29
-0
29 additions, 0 deletions
arkindex/dataimport/tests/test_providers.py
arkindex/templates/webhook_error.html
+13
-0
13 additions, 0 deletions
arkindex/templates/webhook_error.html
with
70 additions
and
1 deletion
arkindex/dataimport/api.py
+
28
−
1
View file @
cabba7ae
...
...
@@ -2,10 +2,12 @@ import logging
from
uuid
import
UUID
from
django.conf
import
settings
from
django.core.mail
import
send_mail
from
django.db
import
transaction
from
django.db.models
import
CharField
,
Count
,
F
,
Max
,
Q
,
Value
from
django.db.models.functions
import
Cast
,
Concat
,
Greatest
,
NullIf
from
django.shortcuts
import
get_object_or_404
from
django.template.loader
import
render_to_string
from
django.utils.functional
import
cached_property
from
drf_spectacular.utils
import
(
OpenApiExample
,
...
...
@@ -519,7 +521,32 @@ class GitRepositoryImportHook(APIView):
repo
=
get_object_or_404
(
Repository
,
id
=
pk
)
if
not
repo
.
enabled
:
raise
PermissionDenied
(
detail
=
'
No credentials available for this repository.
'
)
repo
.
provider_class
(
credentials
=
repo
.
credentials
).
handle_webhook
(
repo
,
request
)
try
:
repo
.
provider_class
(
credentials
=
repo
.
credentials
).
handle_webhook
(
repo
,
request
)
except
Exception
as
e
:
user
=
repo
.
credentials
.
user
# Notifying the user by mail that there was an error during webhook handling
sent
=
send_mail
(
subject
=
'
An error occurred during Git hook handling
'
,
message
=
render_to_string
(
'
webhook_error.html
'
,
context
=
{
'
user
'
:
user
,
'
url
'
:
repo
.
url
,
'
error
'
:
e
.
args
[
0
],
},
request
=
request
,
),
from_email
=
None
,
recipient_list
=
[
user
.
email
],
fail_silently
=
True
,
)
if
sent
==
0
:
logger
.
error
(
f
'
Failed to send webhook error email to
{
user
.
email
}
'
)
raise
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
...
...
This diff is collapsed.
Click to expand it.
arkindex/dataimport/tests/test_providers.py
+
29
−
0
View file @
cabba7ae
from
unittest.mock
import
patch
from
django.core
import
mail
from
django.urls
import
reverse
from
rest_framework
import
status
...
...
@@ -7,6 +8,20 @@ from arkindex.dataimport.models import RepositoryType
from
arkindex.dataimport.providers
import
GitLabProvider
,
GitProvider
from
arkindex.project.tests
import
FixtureAPITestCase
MAIL_CONTENT
=
"""
Hello Test user,
An error occurred while processing a new push on your repository http://gitlab/repo.
Error: Error during handle_webhook
No process will be launched to import your new revision.
Please, try again with another push or contact your system administrator.
--
Arkindex
"""
class
TestProviders
(
FixtureAPITestCase
):
...
...
@@ -41,6 +56,19 @@ class TestProviders(FixtureAPITestCase):
response
=
self
.
client
.
post
(
reverse
(
'
api:import-hook
'
,
kwargs
=
{
'
pk
'
:
self
.
repo
.
id
}))
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
self
.
assertDictEqual
(
response
.
json
(),
{
'
detail
'
:
'
No credentials available for this repository.
'
})
self
.
assertEqual
(
len
(
mail
.
outbox
),
0
)
@patch
(
'
arkindex.dataimport.api.Repository.provider_class
'
)
def
test_webhook_handle_error
(
self
,
provider_class
):
provider_class
.
return_value
.
handle_webhook
.
side_effect
=
Exception
(
'
Error during handle_webhook
'
)
self
.
client
.
force_login
(
self
.
user
)
with
self
.
assertRaisesRegex
(
Exception
,
'
Error during handle_webhook
'
):
self
.
client
.
post
(
reverse
(
'
api:import-hook
'
,
kwargs
=
{
'
pk
'
:
self
.
repo
.
id
}))
self
.
assertTrue
(
provider_class
.
return_value
.
handle_webhook
.
called
)
self
.
assertEqual
(
len
(
mail
.
outbox
),
1
)
self
.
assertEqual
(
mail
.
outbox
[
0
].
to
,
[
'
user@user.fr
'
])
self
.
assertEqual
(
mail
.
outbox
[
0
].
subject
,
'
An error occurred during Git hook handling
'
)
self
.
assertEqual
(
mail
.
outbox
[
0
].
body
,
MAIL_CONTENT
)
@patch
(
'
arkindex.dataimport.api.Repository.provider_class
'
)
def
test_webhook
(
self
,
provider_class
):
...
...
@@ -48,6 +76,7 @@ class TestProviders(FixtureAPITestCase):
response
=
self
.
client
.
post
(
reverse
(
'
api:import-hook
'
,
kwargs
=
{
'
pk
'
:
self
.
repo
.
id
}))
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
self
.
assertTrue
(
provider_class
.
return_value
.
handle_webhook
.
called
)
self
.
assertEqual
(
len
(
mail
.
outbox
),
0
)
@patch
(
'
arkindex.dataimport.models.DataImport.start
'
)
def
test_start_imports_workers
(
self
,
start_di_mock
):
...
...
This diff is collapsed.
Click to expand it.
arkindex/templates/webhook_error.html
0 → 100644
+
13
−
0
View file @
cabba7ae
{% autoescape off %}
Hello {{ user.display_name }},
An error occurred while processing a new push on your repository {{ url }}.
Error: {{ error }}
No process will be launched to import your new revision.
Please, try again with another push or contact your system administrator.
--
Arkindex
{% endautoescape %}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment