Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone give a python requests example of uploading a release asset in github?

url = 'https://github.abc.defcom/api/v3/repos/abc/def/releases/401/assets?name=foo.sh'
r = requests.post(url, headers={'Content-Type':'application/binary'}, data=open('sometext.txt','r'), auth=('user','password'))

This is giving me

>>> r.text
u'{"message":"Not Found","documentation_url":"https://developer.github.com/enterprise/2.4/v3"}'

Where am I going wrong?

like image 800
Ankur Agarwal Avatar asked Jul 01 '16 20:07

Ankur Agarwal


People also ask

How do I get the latest release from GitHub?

On GitHub.com, navigate to the main page of the repository. To the right of the list of files, click Releases. To copy a unique URL to your clipboard, find the release you want to link to, right click the title, and copy the URL. Alternatively, right click Latest Release and copy the URL to share it.

What is Target_commitish?

target_commitish. string. Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists.

What is GitHub assets?

GitHub - symfony/asset: The Asset component manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files.


1 Answers

So I'll preface this with the advice that if you use a library it's as easy as:

from github3 import GitHubEnterprise

gh = GitHubEnterprise(token=my_token)
repository = gh.repository('abc', 'def')
release = repository.release(id=401)
asset = release.upload_asset(content_type='application/binary', name='foo.sh', asset=open('sometext.txt', 'rb'))

With that in mind, I'll also preface this with "application/binary" is not a real media type (see: https://www.iana.org/assignments/media-types/media-types.xhtml)

Next, if you read the documentation, you'll notice that GitHub requires clients that have real SNI (Server Name Indication), so depending on your version of Python, you may also have to install pyOpenSSL, pyasn1, and ndg-httpsclient from PyPI.

I'm not sure what the URL looks like for enterprise instances, but for public GitHub, it looks like:

https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets?name=foo.sh

So you're going to have that as url, plus you're going to want your auth credentials (in your case you seem to want to use basic auth). Then you're going to want a valid media-type in the headers, e.g.,

headers = {'Content-Type': 'text/plain'}

And your call would look pretty much exactly correct:

requests.post(url, headers=headers, data=open('file.txt', 'rb'), auth=(username, password))

To get the correct url, you should do:

release = requests.get(release_url, auth=(username, password))
upload_url = release.json().get('upload_url')

Note this is a URITemplate. You'll need to remove the templating or use a library like uritemplate.py to parse it and use it to build your URL for you.

One last reminder, github3.py (the library in the original example) takes care of all of this for you.

like image 147
Ian Stapleton Cordasco Avatar answered Sep 23 '22 01:09

Ian Stapleton Cordasco