2

Using version 1.0.0a4 of the github3.py library.

I am trying to create a new file in the repository on a new branch.

Assumption: gh is an authenticated object.

repo = gh.repository('User','Repo')
repo.create_file(path='NewFile',message='Commit Message',content='File Content', branch='NewBranch')

The call to create_file fails to create the 'NewFile' and the 'NewBranch'. If I do the following the file is created ok on the "Default" branch as I would expect.

repo.create_file(path='NewFile',message='Commit Message',content='File Content')

I was sure that the first form of the 'create_file' did in fact create the branch once, but I'm unsure why it isn't happening now. Is it not a fair expectation that the 'branch' would be created by this call? I haven't found another API that would create a branch.

Hipny
  • 739
  • 1
  • 11
  • 24
Matt
  • 21
  • 4
  • Since "The call to create_file fails to create the 'NewFile' and the 'NewBranch'" could you add the stack trace? – Hipny Sep 07 '17 at 14:39
  • @Hipny traceback? But, yes, anyway – Arount Sep 07 '17 at 15:46
  • The call itself doesn't fail, that seems to work just fine, but the actions expected don't happen. I don't seem to get much of a helpful error back from github3.py as well. – Matt Sep 07 '17 at 16:51
  • 1
    Ok, looks like the create_file doesn't support the creation of the branches. Needed to use the `create_ref` method and give it the new **reference** `refs/heads/new_branch` and then it was created. I can now use that new branch and create the new file there. – Matt Sep 07 '17 at 17:15

1 Answers1

0

Since 1.2.0 you can use Repository.create_branch_ref:

repo = gh.repository('User','Repo')
repo.create_branch_ref('NewBranch')
repo.create_file(path='NewFile',message='Commit Message',content='File Content', branch='NewBranch')

By default the branch will be forked off the default branch (usually main / master), but you can also supply a sha to specify the exact fork point.

ecatmur
  • 152,476
  • 27
  • 293
  • 366