使用 python-gitlab 将文件添加到 GitLab 项目时尝试使用非字典值初始化 RESTObject

问题描述 投票:0回答:1

我试图通过使用 python-gitlab 库导入 GitLab 导出 tar.gz 文件,通过 GitLab CI 管道创建 GitLab 项目。新项目已创建,但未使用 GitLab 导出文件中的配置进行初始化。相反,我收到“

Attempted to initialize RESTObject with a non-dictionary value"
”错误。 作为替代方案,我尝试创建一个空白项目并使用我需要的文件(例如 Readme.md 和 .gitlab-ci.yml)对其进行初始化,但仍然出现相同的错误。

首先,我尝试通过从源项目导入 GitLab 导出文件来创建一个新项目,我在 CI 容器中运行此代码:

# 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
                )

我确认这会创建项目,但不会应用 gitlab 导出文件(import_file_content)。相反,它会抛出错误

"Attempted to initialize RESTObject with a non-dictionary value. This likely indicates an incorrect or malformed server response."

然后,我想到创建一个空白项目并上传我需要的文件,而不是使用 gitlab 导出文件来完成所有这些工作:

# 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)

我确认这也创建了新的 GitLab 项目。然后,

# 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'})

尽管如此,我还是遇到了与之前相同的错误

"Attempted to initialize RESTObject with a non-dictionary value. This likely indicates an incorrect or malformed server response."

我做错了什么还是 GitLab 的问题?

python gitlab gitlab-ci gitlab-ci-runner python-gitlab
1个回答
0
投票

此错误可能表明您提供的 url 端点不可用。

我通过使用端点解决了这个问题:

https://gitlab.com/api/v4/

或者如果是自托管的话

https://myCompagny.example.com/

而不是:

https://gitlab.com/api/v4/somethingElse/etc

https://myCompagny.example.com/somethingElse/etc

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.