PyGithub / PyGithub/PyGithub

Upload of content using github.Repository.update_file produces garbled content

オープン
#2,972 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
7.8k
フォーク
1.9k
平均マージ
17分
マージ済み PR(30日)
2

説明

Problem Introduction

When trying to upload a json stringified dict to a repository resulting content in the repository is garbled.

Example calling code with explanation

As per Repository.py inputs can either be bytes or a string that is ready to be encoded and therefore transmitted to a repository.

        content_to_transmit = json.dumps(obj)
        try:
            repo = self.github_instance.get_repo(f"{self.org_name}/{self.repo_name}")
            file_path = f"{container_name}/{self.object_files[container_name]}"
            # file_contents = repo.get_contents(file_path, ref=ref, sha=sha)
            write_response = repo.update_file(
                file_path, 
                f"Update object [{self.object_files[container_name]}]", 
                content=content_to_transmit,
                sha=sha, 
                branch=ref
            )
            return [
                True, 
                {
                    "status_msg": f"wrote object [{self.object_files[container_name]}] to container [{container_name}]",
                    "status_code": 200 
                },
                write_response
            ]
        except Exception as e:
            print(e)
            return [
                False, 
                {
                    "status_code":f"unable to write object [{self.object_files[container_name]}] to container [{container_name}] due to [{str(e)}]",
                    "status_msg": 503
                }, 
                str(e)
            ]

When this is called with just the string this error is returned 'bytes' object has no attribute 'encode' which caused me to explore github.Repository.update_file.

Problem resolution

As I inspected the file I found that the if block on line 2495 was improperly indented.

Version in main branch
if not isinstance(content, bytes):
            content = content.encode("utf-8")
content = b64encode(content).decode("utf-8")

As I looked at the code I realized that even if bytes or string was supplied the output would not be correct; therefore, I indented the if block and tried the call again.

Verified fixed version
if not isinstance(content, bytes):
           content = content.encode("utf-8")
           content = b64encode(content).decode("utf-8")

With that changed I tried the call again and was able to find the content in the repository to be correctly received, stored as json, and viewable in the repository. I've provided the original file, the fixed file and a patch file for review.

PyGithub.zip

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Repository.py の 2495 行付近から始め、string と bytes の両方のコンテンツで Repository.update_file を再現します。提供されたパッチと修正済みの例を参照として使用し、アップロードされた JSON がリポジトリに正しく保存され、表示されることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api
issue の種類
バグ
難易度
1/5
見積もり時間
1時間未満
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。