簡體   English   中英

python gitlab project.files.create 給出了缺失的屬性:file_path 錯誤

[英]python gitlab project.files.create gives Missing attributes: file_path error

當我想通過 api 將文件添加到我的存儲庫時,我總是以相同的錯誤告終。 缺少屬性:file_path。 我的文件路徑是 test_api_upload.txt。 當我嘗試完整路徑時。 例如:/myproject/test_api_upload.txt 我得到同樣的錯誤。

python-gitlab 版本

pip freeze |grep gitlab
python-gitlab==1.3.0

代碼

import gitlab
# private token or personal token authentication
gl = gitlab.Gitlab('https://gitlab.company.com', private_token='MYTOKEN', api_version=4)
gl.auth()

project = gl.projects.get('/my/project')
items = project.repository_tree() 
(works fine all items can be printed)

f = project.files.create({'test_api_upload.txt': 'test_api_upload',
                          'branch': 'master',
                          'content': 'file content',
                          'commit_message': 'Create testfile'})

輸出

Traceback (most recent call last):
  File "/home/toon/.PyCharm2017.2/config/scratches/scratch_5.py", line 50, in <module>
    'commit_message': 'Create testfile'})
  File "/home/toon/.local/lib/python2.7/site-packages/gitlab/cli.py", line 42, in wrapped_f
    return f(*args, **kwargs)
  File "/home/toon/.local/lib/python2.7/site-packages/gitlab/exceptions.py", line 251, in wrapped_f
    return f(*args, **kwargs)
  File "/home/toon/.local/lib/python2.7/site-packages/gitlab/v4/objects.py", line 1872, in create
    self._check_missing_create_attrs(data)
  File "/home/toon/.local/lib/python2.7/site-packages/gitlab/mixins.py", line 142, in _check_missing_create_attrs
    raise AttributeError("Missing attributes: %s" % ", ".join(missing))
AttributeError: Missing attributes: file_path

Process finished with exit code 1

剛剛意識到我在原始請求中更改了密鑰而不是 file_path 的值。 但是通過查看它,我發現文檔缺少一些屬性。 我提出了拉取請求。 https://github.com/python-gitlab/python-gitlab/pull/483

f = project.files.create({'file_path': 'testfile.txt',
                          'branch_name': 'master',
                          'content': file_content,
                          'author_email': 'test@example.com',
                          'author_name': 'yourname',
                          'encoding': 'text',
                          'commit_message': 'Create testfile'})

我對 'file_path: testfile' 元組感到困惑。 添加了 .txt 以進行澄清。

您將嘗試這種方式:

import sys, os 
import gitlab

file_content = open(os.getcwd() + 'testfile.txt').read()
f = project.files.create({
                           'file_path': 'testfile.txt',
                           'branch': 'master',
                           'content': file_content,
                           'author_email': 'test@example.com',
                           'author_name': 'yourname',
                           'commit_message': 'Create testfile'
                         })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM