def create_repository( namespace, name, creating_user, visibility="private", repo_kind="image", description=None ): namespace_user = User.get(username=namespace) yesterday = datetime.now() - timedelta(days=1) with db_transaction(): repo = Repository.create( name=name, visibility=Repository.visibility.get_id(visibility), namespace_user=namespace_user, kind=Repository.kind.get_id(repo_kind), description=description, ) RepositoryActionCount.create(repository=repo, count=0, date=yesterday) RepositorySearchScore.create(repository=repo, score=0) # Note: We put the admin create permission under the transaction to ensure it is created. if creating_user and not creating_user.organization: admin = Role.get(name="admin") RepositoryPermission.create(user=creating_user, repository=repo, role=admin) # Apply default permissions (only occurs for repositories under organizations) if creating_user and not creating_user.organization and creating_user.username != namespace: permission.apply_default_permissions(repo, creating_user) return repo
def create_repository(namespace, name, creating_user, visibility="private", repo_kind="image", description=None): namespace_user = User.get(username=namespace) yesterday = datetime.now() - timedelta(days=1) try: with db_transaction(): # Check if the repository exists to avoid an IntegrityError if possible. existing = get_repository(namespace, name) if existing is not None: return None try: repo = Repository.create( name=name, visibility=Repository.visibility.get_id(visibility), namespace_user=namespace_user, kind=Repository.kind.get_id(repo_kind), description=description, ) except IntegrityError as ie: raise _RepositoryExistsException(ie) RepositoryActionCount.create(repository=repo, count=0, date=yesterday) RepositorySearchScore.create(repository=repo, score=0) # Note: We put the admin create permission under the transaction to ensure it is created. if creating_user and not creating_user.organization: admin = Role.get(name="admin") RepositoryPermission.create(user=creating_user, repository=repo, role=admin) except _RepositoryExistsException as ree: try: return Repository.get(namespace_user=namespace_user, name=name) except Repository.DoesNotExist: logger.error( "Got integrity error when trying to create repository %s/%s: %s", namespace, name, ree.internal_exception, ) return None # Apply default permissions (only occurs for repositories under organizations) if creating_user and not creating_user.organization and creating_user.username != namespace: permission.apply_default_permissions(repo, creating_user) return repo
def store_repository_action_count(repository, day, action_count): """ Stores the action count for a repository for a specific day. Returns False if the repository already has an entry for the specified day. """ try: RepositoryActionCount.create(repository=repository, date=day, count=action_count) return True except IntegrityError: logger.debug("Count already written for repository %s", repository.id) return False
def test_update_repository_score(bucket_sums, expected_score, initialized_db): # Create a new repository. repo = create_repository('devtable', 'somenewrepo', None, repo_kind='image') # Delete the RAC created in create_repository. RepositoryActionCount.delete().where( RepositoryActionCount.repository == repo).execute() # Add RAC rows for each of the buckets. for index, bucket in enumerate(SEARCH_BUCKETS): for day in range(0, bucket.days): RepositoryActionCount.create( repository=repo, count=(bucket_sums[index] / bucket.days * 1.0), date=date.today() - bucket.delta + timedelta(days=day)) assert update_repository_score(repo) assert RepositorySearchScore.get(repository=repo).score == expected_score