我试图通过使用 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 的问题?
此错误可能表明您提供的 url 端点不可用。
我通过使用端点解决了这个问题:
或者如果是自托管的话
而不是:
或
希望这会有所帮助