PyGithub / PyGithub/PyGithub

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

Open
#2,972 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.8k
Forks
1.9k
Avg merge
17m
Merged PRs (30d)
2

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Repository.py around line 2495 and reproduce Repository.update_file with both string and bytes content. Verify that uploaded JSON is stored and displayed correctly in the repository, using the supplied patch and fixed example as references.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.