Attempted to initialize RESTObject with a non-dictionary value when adding file to GitLab project using python-gitlab

300 Views Asked by At

I am trying to create GitLab projects through GitLab CI pipeline by importing the GitLab export tar.gz file using python-gitlab library. The new project gets created but does not get initialized with the configuration in the GitLab export file. Instead, I get "Attempted to initialize RESTObject with a non-dictionary value" error. As an alternative, I tried creating a blank project and initialize it with the files that I need, such as Readme.md and .gitlab-ci.yml, but getting the same error still.

First I tried creating a new project by importing GitLab export file from the source project, where I am running this code in a CI container:

# Read file from the repository where this CI runs
with open('scripts/new_export.tar.gz', 'rb') as f:
                import_file_content = f.read()

# Create project by importing the above gitlab export file
create_project = gl.projects.import_project(
                import_file_content,
                path=project_name,
                namespace_id=gitlab_namespace_id
                )

I confirmed that this creates the project, but does not apply the gitlab export file (import_file_content). Instead, it throws the error "Attempted to initialize RESTObject with a non-dictionary value. This likely indicates an incorrect or malformed server response.".

Then, I thought of creating a blank project and uploading the files I need instead of using gitlab export file to do all that:

# Create new blank project
create_project = gl.projects.create({'name': project_name, 'namespace_id': gitlab_namespace_id})

# Get the project ID for subsequent calls
gitlab_project_id = create_project.id

# Use the new project's ID for subsequent calls
new_project = gl.projects.get(gitlab_project_id)

I confirmed that this created the new GitLab project as well. Then,

# Read the readme.md file from the source repository where this CI runs
readmeFile = open("assets/README.md", "rt").read()

# Create a file in the new GitLab project
new_project.files.create({'file_path': 'README.md',
                          'branch': 'main',
                          'content': readmeFile,
                          'author_email': '[email protected]',
                          'author_name': 'yourname',
                          'commit_message': 'Create testfile'})

Still, I am getting the same error I got earlier "Attempted to initialize RESTObject with a non-dictionary value. This likely indicates an incorrect or malformed server response.".

Am I doing something wrong or is it an issue with GitLab?

0

There are 0 best solutions below