Skip to content
Snippets Groups Projects
Commit 0aa920a9 authored by Valentin Rigal's avatar Valentin Rigal Committed by Bastien Abadie
Browse files

Limit max GitRef and DataImport name fields from Gitlab provider

parent 9cb44bfb
No related branches found
No related tags found
1 merge request!1272Limit max GitRef and DataImport name fields from Gitlab provider
......@@ -69,7 +69,8 @@ class GitProvider(ABC):
ref.revision = revision
ref.save()
except GitRef.DoesNotExist:
repo.refs.create(name=name, type=type, revision=revision)
# Limit the name length to the max size of GitRef name field
repo.refs.create(name=name[:250], type=type, revision=revision)
def get_or_create_revision(self, repo, sha, save=True):
from arkindex.dataimport.models import Revision
......@@ -111,7 +112,8 @@ class GitProvider(ABC):
refs = ", ".join(rev.refs.values_list('name', flat=True))
if not refs:
refs = rev.hash
name = f"Import {refs} from {rev.repo.url}"
# Limit the name length to the max size of DataImport name field
name = f"Import {refs} from {rev.repo.url}"[:100]
if rev.repo.type == RepositoryType.Worker:
# Build revision workers in a new process
......
......@@ -583,7 +583,8 @@ class TestGitLabProvider(FixtureTestCase):
# Add 2 commits on 2 branches
self.gl_mock().projects.get.return_value.branches.list = lambda: [
Ref("master", {"id": "commit1", "message": "A commit on master", "author_email": "someone@teklia.com"}),
Ref("killer-feature", {"id": "commit2", "message": "My fancy feature", "author_email": "another@teklia.com"})
# Create a commit with a very long branch name
Ref("A" * 1000, {"id": "commit2", "message": "My fancy feature", "author_email": "another@teklia.com"})
]
# Add 1 new commit on a tag, and reuse a commit on another tag
......@@ -623,7 +624,8 @@ class TestGitLabProvider(FixtureTestCase):
('v0.1', GitRefType.Tag),
])
self.assertListEqual(list(commit2.refs.values_list('name', 'type').order_by('name')), [
('killer-feature', GitRefType.Branch),
# Name has been restricted to 250 chars due to GitRef model constraint
('A' * 250, GitRefType.Branch),
])
# We should now have 3 dataimport for these refs
......@@ -631,5 +633,6 @@ class TestGitLabProvider(FixtureTestCase):
self.assertListEqual(list(DataImport.objects.values_list('name', 'mode', 'revision__hash').order_by('revision__hash')), [
("Import v0.1 from http://gitlab/repo", DataImportMode.Repository, 'commit0'),
("Import master from http://gitlab/repo", DataImportMode.Repository, 'commit1'),
("Import killer-feature from http://gitlab/repo", DataImportMode.Repository, 'commit2'),
# Last import name has been limited to 100 chars due to DataImport model constraint
(f"Import {'A' * 93}", DataImportMode.Repository, 'commit2'),
])
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