def post(self, request, *args, **kwargs): obj = self.get_object() if obj.has_notebook: commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit: # Commit changes git.commit(obj.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(obj.repo.path) celery_app.send_task(SchedulerCeleryTasks.PROJECTS_NOTEBOOK_STOP, kwargs={ 'project_name': obj.unique_name, 'project_uuid': obj.uuid.hex, 'notebook_job_name': obj.notebook.unique_name, 'notebook_job_uuid': obj.notebook.uuid.hex, 'update_status': True }) auditor.record(event_type=NOTEBOOK_STOPPED_TRIGGERED, instance=obj.notebook, target='project', actor_id=self.request.user.id, countdown=1) elif obj.notebook and obj.notebook.is_running: obj.notebook.set_status(status=ExperimentLifeCycle.STOPPED, message='Notebook was stopped') return Response(status=status.HTTP_200_OK)
def test_trigger(self): assert Experiment.objects.count() == 0 assert ExperimentGroup.objects.count() == 0 assert Job.objects.count() == 0 assert BuildJob.objects.count() == 0 # No repo assert ci.trigger(self.project) is False assert Experiment.objects.count() == 0 assert ExperimentGroup.objects.count() == 0 assert Job.objects.count() == 0 assert BuildJob.objects.count() == 0 # New code repo = RepoFactory(project=self.project) open('{}/foo'.format(repo.path), 'w') git.commit(repo.path, '*****@*****.**', 'username') assert ci.trigger(self.project) is False assert Experiment.objects.count() == 0 assert ExperimentGroup.objects.count() == 0 assert Job.objects.count() == 0 assert BuildJob.objects.count() == 0 # New file shutil.copy(os.path.abspath('tests/fixtures_static/polyaxonfile.yml'), '{}/polyaxonfile.yml'.format(repo.path)) git.commit(repo.path, '*****@*****.**', 'username') assert ci.trigger(self.project) is True assert Experiment.objects.count() == 1 assert ExperimentGroup.objects.count() == 0 assert Job.objects.count() == 0 assert BuildJob.objects.count() == 0
def test_sync_get_latest_commit_internal_repo(self): assert self.ci.code_reference is None # No repo assert ci.sync(self.project) is False assert self.ci.code_reference is None # Repo but no commits repo = RepoFactory(project=self.project) assert ci.sync(self.project) is False assert self.ci.code_reference is None # Put file and commit open('{}/foo'.format(repo.path), 'w') git.commit(repo.path, '*****@*****.**', 'username') assert ci.sync(self.project) is True assert self.ci.code_reference is not None last_code_ref = self.ci.code_reference # Resync without change does not create new code ref assert ci.sync(self.project) is False assert self.ci.code_reference == last_code_ref # Add new commit open('{}/foo2'.format(repo.path), 'w') git.commit(repo.path, '*****@*****.**', 'username') assert ci.sync(self.project) is True assert self.ci.code_reference is not None assert self.ci.code_reference != last_code_ref
def handle_code(self, request): commit = request.data.get('commit') commit = to_bool(commit) if commit is not None else True if commit and conf.get(NOTEBOOKS_MOUNT_CODE) and self.project.has_repo: # Commit changes git.commit(self.project.repo.path, request.user.email, request.user.username) else: # Reset changes git.undo(self.project.repo.path)
def handle_new_files(user_id, repo_id, tar_file_name): if not tarfile.is_tarfile(tar_file_name): raise ValueError('Received wrong file format.') User = get_user_model() # noqa try: user = User.objects.get(id=user_id) except User.DoesNotExist: _logger.warning('User with id `%s` does not exist anymore.', user_id) return try: repo = Repo.objects.get(id=repo_id) # Checkout to master git.checkout_commit(repo.path) except User.DoesNotExist: _logger.warning('Repo with id `%s` does not exist anymore.', repo_id) return # Destination files new_repo_path = repo.get_tmp_tar_path() # clean the current path from all files path_files = os.listdir(repo.path) for member in path_files: if member == '.git': continue member = os.path.join(repo.path, member) if os.path.isfile(member): os.remove(member) else: delete_path(member) # Move the tar inside the repo path shutil.move(tar_file_name, new_repo_path) # Untar the file with tarfile.open(new_repo_path) as tar: tar.extractall(repo.path) # Delete the current tar os.remove(new_repo_path) # Get the git repo if not git.get_status(repo.path): return # commit changes git.commit(repo.path, user.email, user.username) auditor.record(event_type=REPO_NEW_COMMIT, instance=repo, actor_id=user.id, actor_name=user.username)
def handle_new_files(user_id, repo_id, tar_file_name): if not tarfile.is_tarfile(tar_file_name): raise ValueError('Received wrong file format.') User = get_user_model() # noqa try: user = User.objects.get(id=user_id) except User.DoesNotExist: logger.warning('User with id `%s` does not exist anymore.', user_id) return try: repo = Repo.objects.get(id=repo_id) # Checkout to master git.checkout_commit(repo.path) except User.DoesNotExist: logger.warning('Repo with id `%s` does not exist anymore.', repo_id) return # Destination files new_repo_path = repo.get_tmp_tar_path() # clean the current path from all files path_files = os.listdir(repo.path) for member in path_files: if member == '.git': continue member = os.path.join(repo.path, member) if os.path.isfile(member): os.remove(member) else: delete_path(member) # Move the tar inside the repo path shutil.move(tar_file_name, new_repo_path) # Untar the file with tarfile.open(new_repo_path) as tar: tar.extractall(repo.path) # Delete the current tar os.remove(new_repo_path) # Get the git repo if not git.get_status(repo.path): return # commit changes git.commit(repo.path, user.email, user.username) auditor.record(event_type=REPO_NEW_COMMIT, instance=repo, actor_id=user.id)
def test_checkout_commit_and_master(self): git_url = 'https://github.com/polyaxon/empty.git' # Create repo repo = ExternalRepo(project=self.project, git_url=git_url) repo.save() # Check last commit assert len(repo.last_commit) == 2 # Add new file file_path = os.path.join(repo.path, 'file1.dat') open(file_path, 'w+') assert git.get_status(repo.path) is not None git.commit(repo.path, '*****@*****.**', 'username') # Check last commit commit1 = repo.last_commit[0] assert commit1 is not None # Add new file file_path = os.path.join(repo.path, 'file2.dat') open(file_path, 'w+') assert git.get_status(repo.path) is not None git.commit(repo.path, '*****@*****.**', 'username') # Check last commit commit2 = repo.last_commit[0] assert commit2 is not None # Commits are different assert commit1 != commit2 # Checkout to commit1 git.checkout_commit(repo_path=repo.path, commit=commit1) assert repo.last_commit[0] == commit1 # Checkout to master git.checkout_commit(repo_path=repo.path) assert repo.last_commit[0] == commit2
def test_sync_works(self): assert self.model_class.objects.count() == 0 response = self.auth_client.post(self.set_url, data={'git_url': 'https://github.com/polyaxon/empty.git'}) assert response.status_code == status.HTTP_201_CREATED assert self.model_class.objects.count() == 1 repo = self.model_class.objects.last() commit = git.get_last_commit(repo_path=repo.path) # Enable CI response = self.auth_client.post(self.enable_url) assert response.status_code == status.HTTP_201_CREATED # Adding a new commit open('{}/foo'.format(repo.path), 'w') git.commit(repo.path, '*****@*****.**', 'username') assert git.get_last_commit(repo_path=repo.path) != commit # Sync must remove that commit response = self.auth_client.post(self.url) assert response.status_code == status.HTTP_200_OK assert git.get_last_commit(repo_path=repo.path) == commit
def test_sync_get_latest_commit_external_repo(self): assert self.ci.code_reference is None # No repo assert ci.sync(self.project) is False assert self.ci.code_reference is None # Repo repo = ExternalRepoFactory( project=self.project, git_url='https://github.com/polyaxon/empty.git') assert ci.sync(self.project) is True assert self.ci.code_reference is not None last_code_ref = self.ci.code_reference # Resync without change does not create new code ref assert ci.sync(self.project) is False assert self.ci.code_reference == last_code_ref # Creating file manually does not work open('{}/foo'.format(repo.path), 'w') git.commit(repo.path, '*****@*****.**', 'username') assert ci.sync(self.project) is False assert self.ci.code_reference is not None assert self.ci.code_reference == last_code_ref