Skip to content
Snippets Groups Projects
Commit 811e2416 authored by Eva Bardou's avatar Eva Bardou Committed by Erwan Rouchet
Browse files

Handle branch deletion during GitLab webhook

parent 4039015f
No related branches found
No related tags found
1 merge request!1342Handle branch deletion during GitLab webhook
......@@ -394,7 +394,16 @@ class GitLabProvider(GitProvider):
return
if not sha:
raise ValidationError('Missing checkout SHA')
# If there isn't any SHA it means that a branch was deleted
ref = request.data.get('ref')
if not ref:
raise ValidationError('Missing branch reference')
# Delete existing branch
branch_name = ref[11:] if ref.startswith('refs/heads/') else ref
repo.refs.filter(name=branch_name, type=GitRefType.Branch).delete()
return
# Already took care of this event
if repo.revisions.filter(hash=sha).exists():
......
......@@ -484,14 +484,6 @@ class TestGitLabProvider(FixtureTestCase):
self.assertFalse(rev.exists())
self.assertFalse(repo_imports.exists())
# Missing SHA
request_mock.data['object_kind'] = 'push'
del request_mock.data['checkout_sha']
with self.assertRaises(ValidationError):
glp.handle_webhook(self.repo, request_mock)
self.assertFalse(rev.exists())
self.assertFalse(repo_imports.exists())
# Breaking change: a list!
request_mock.data = [request_mock.data]
with self.assertRaises(ValidationError):
......@@ -499,6 +491,39 @@ class TestGitLabProvider(FixtureTestCase):
self.assertFalse(rev.exists())
self.assertFalse(repo_imports.exists())
def test_handle_webhook_delete_branch(self):
"""
Test GitLabProvider properly handles a branch deletion
"""
rev = Revision(
repo=self.repo,
hash='1',
message='commit message',
author='bob',
)
rev.save()
self.assertTrue(self.repo.revisions.filter(hash='1').exists())
repo_imports = DataImport.objects.filter(revision__repo_id=str(self.repo.id))
glp = GitLabProvider(url='http://aaa', credentials=self.creds)
glp.update_or_create_ref(self.repo, rev, 'test', GitRefType.Branch)
self.assertEqual(len(self.repo.refs.all()), 1)
request_mock = MagicMock()
request_mock.META = {
'HTTP_X_GITLAB_EVENT': 'Push Hook',
'HTTP_X_GITLAB_TOKEN': 'hook-token',
}
request_mock.data = {
'object_kind': 'push',
'ref': 'refs/heads/test',
'commits': []
}
glp.handle_webhook(self.repo, request_mock)
self.assertTrue(self.repo.revisions.filter(hash='1').exists())
self.assertEqual(len(self.repo.refs.all()), 0)
self.assertFalse(repo_imports.exists())
def test_retrieve_repo_type(self):
"""
Gitlab provider allow to retrieve a project type
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment