Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHA not supplied while creating new file #1725

Closed
pizycki opened this issue Dec 8, 2017 · 4 comments
Closed

SHA not supplied while creating new file #1725

pizycki opened this issue Dec 8, 2017 · 4 comments

Comments

@pizycki
Copy link

pizycki commented Dec 8, 2017

I'm trying to create file with Octokit.NET inside my repository

let user = "pizycki"
let repo = "testo-repo"
let branch = "Testo_" + DateTime.UtcNow.ToString("hhmmss")
let newFilePath = "file.md"
let createFileReq = new CreateFileRequest("Create file", """
        # Hello Octokit!
        ## Me gusta
        """, branch)
client.Repository.Content.CreateFile(user, repo, newFilePath, createFileReq).Result

but receiving the following error

{"message":"Invalid request.\n\n\"sha\" wasn't supplied.","documentation_url":"https://developer.github.com/v3/repos/contents/#update-a-file"}

I checked with Fiddler the request sent to API and this is how it looks like

PUT /repos/pizycki/testo-repo/contents/file.md HTTP/1.1

{
  "content":"DQogICAgICAgICMgSGVsbG8gT2N0b2tpdCENCiAgICAgICAgIyMgTWUgZ3VzdGENCiAgICA=",
  "message":"Create file",
  "branch":"Testo_100809"
}

I checked this with API Documentation and what is happening, according to docu, is ok (yet odd). Looks like API awaits for SHA parameter to update the file, not create a new one.

I'd expect the file creation to use POST instead of PUT.

I've tried changing it in .NET code, but using POST method gives me 404.

@shiftkey
Copy link
Member

shiftkey commented Dec 9, 2017

@pizycki I think this might be related to the branch name - it looks like you're creating it from the current time, and if that doesn't exist the repository wouldn't know where to create the new commit from. What if you used a fixed branch name to confirm that the PUT is working as expected?

@pizycki
Copy link
Author

pizycki commented Dec 9, 2017

I create branch with that name just before attempting to create new file. You can check my program right here https://github.com/pizycki/DevoWeek/blob/octokit/DevoWeek/Program.fs#L39

Edit:

I found a bug in my code. I was trying to create a file that already exists on base branch.

The file creation works fine :)

Thanks for your reply and for Octokit, its great :)

@pizycki pizycki closed this as completed Dec 9, 2017
@shiftkey
Copy link
Member

@pizycki I'm glad you were able to get to the bottom of it!

@bharatkale007
Copy link

I was searching for the same, got below solution.
repo = g.get_repo(repo_name)
file_path = 'myfilepathtocommit'
fc = open(file_path, 'r').read()
file_content = repo.get_contents(file_path)
repo.update_file("electricity.py", "commit message", fc, file_content.sha)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants