def get_experiment_repo_info(experiment): """Returns information required to create a build for an experiment.""" project_name = experiment.project.name experiment_spec = experiment.specification if experiment_spec.build.git: # We need to fetch the repo first repo, is_created = ExternalRepo.objects.get_or_create( project=experiment.project, git_url=experiment_spec.build.git) if not is_created: # If the repo already exist, we just need to refetch it git.fetch(git_url=repo.git_url, repo_path=repo.path) if not experiment.code_reference.commit: # Update experiment commit if not set already code_reference, _ = CodeReference.objects.get_or_create( repo=repo, commit=repo.last_commit[0]) experiment.code_reference = code_reference experiment.save() repo_path = repo.path repo_name = repo.name else: repo_path = experiment.project.repo.path repo_name = project_name image_name = '{}/{}'.format(settings.REGISTRY_HOST, repo_name) image_tag = experiment.code_reference.commit if not image_tag: raise Repo.DoesNotExist return { 'repo_path': repo_path, 'image_name': image_name, 'image_tag': image_tag }
def _get_external_repo_code_reference(repo: 'ExternalRepo', commit: str = None ) -> Optional['CodeReference']: from libs.repos import git def get_or_create(ref): code_references = CodeReference.objects.filter(external_repo=repo, git_url=repo.git_url, commit=ref) if code_references.exists(): return code_references.last() return CodeReference.objects.create(external_repo=repo, git_url=repo.git_url, commit=ref) # Fetch latest git.fetch(git_url=repo.git_clone_url, repo_path=repo.path) if commit: return get_or_create(ref=commit) # If no commit is provided we get the last commit, and save new ref if not found try: last_commit = repo.last_commit except ValueError: return None return get_or_create(ref=last_commit[0])
def get_experiment_repo_info(experiment): """Returns information required to create a build for an experiment.""" project_name = experiment.project.name experiment_spec = experiment.specification if experiment_spec.build.git: # We need to fetch the repo first repo, is_created = ExternalRepo.objects.get_or_create(project=experiment.project, git_url=experiment_spec.build.git) if not is_created: # If the repo already exist, we just need to refetch it git.fetch(git_url=repo.git_url, repo_path=repo.path) if not experiment.code_reference.commit: # Update experiment commit if not set already code_reference, _ = CodeReference.objects.get_or_create(repo=repo, commit=repo.last_commit[0]) experiment.code_reference = code_reference experiment.save() repo_path = repo.path repo_name = repo.name else: repo_path = experiment.project.repo.path repo_name = project_name image_name = '{}/{}'.format(settings.REGISTRY_HOST, repo_name) image_tag = experiment.code_reference.commit if not image_tag: raise Repo.DoesNotExist return { 'repo_path': repo_path, 'image_name': image_name, 'image_tag': image_tag }
def get_job_repo_info(project, job): project_name = project.name job_spec = job.specification if job_spec.build.git: # We need to fetch the repo first repo, is_created = ExternalRepo.objects.get_or_create( project=project, git_url=job_spec.build.git) if not is_created: # If the repo already exist, we just need to refetch it git.fetch(git_url=repo.git_url, repo_path=repo.path) repo_path = repo.path repo_name = repo.name last_commit = repo.last_commit else: repo_path = project.repo.path last_commit = project.repo.last_commit repo_name = project_name image_name = '{}/{}'.format(settings.REGISTRY_HOST, repo_name) if not last_commit: raise Repo.DoesNotExist image_tag = last_commit[0] return { 'repo_path': repo_path, 'image_name': image_name, 'image_tag': image_tag }
def test_external_repo_creation_and_deletion(self): repo_path = '{}/{}/{}/{}'.format(settings.REPOS_MOUNT_PATH, self.project.user.username, self.project.name, 'empty') self.assertFalse(os.path.exists(repo_path)) git_url = 'https://github.com/polyaxon/empty.git' # Create repo repo = ExternalRepo(project=self.project, git_url=git_url) repo.save() assert repo.path == repo_path assert repo.name == 'empty' self.assertTrue(os.path.exists(repo_path)) git_file_path = '{}/.git'.format(repo_path) self.assertTrue(os.path.exists(git_file_path)) # Test fetch works correctly git.fetch(repo.git_url, repo.path) # Test query model works assert repo == ExternalRepo.objects.get(project=self.project, git_url=git_url) # Check last commit assert repo.last_commit is None # Delete repo repo.delete() self.assertFalse(os.path.exists(repo_path))