You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

  1. #!/usr/bin/env python
  2. # Upload a file to a GitHub draft release. --id and --file are required.
  3. # Optimized for CircleCI
  4. import json
  5. import os
  6. import re
  7. import argparse
  8. import mimetypes
  9. import httplib
  10. from base64 import b64encode
  11. def request(baseurl, path, mimetype, mimeencoding, data):
  12. user_and_pass = b64encode(b"{0}:{1}".format(os.environ['GITHUB_USERNAME'], os.environ['GITHUB_TOKEN'])).decode("ascii")
  13. headers = {
  14. 'User-Agent': 'tenderbot',
  15. 'Accept': 'application/vnd.github.v3.raw+json',
  16. 'Authorization': 'Basic %s' % user_and_pass,
  17. 'Content-Type': mimetype,
  18. 'Content-Encoding': mimeencoding
  19. }
  20. conn = httplib.HTTPSConnection(baseurl, timeout=5)
  21. conn.request('POST', path, data, headers)
  22. response = conn.getresponse()
  23. if response.status < 200 or response.status > 299:
  24. print(response)
  25. conn.close()
  26. raise IOError(response.reason)
  27. responsedata = response.read()
  28. conn.close()
  29. return json.loads(responsedata)
  30. if __name__ == "__main__":
  31. parser = argparse.ArgumentParser()
  32. parser.add_argument("--id", help="GitHub release ID", required=True, type=int)
  33. parser.add_argument("--file", default="/tmp/workspace/tendermint_{0}_{1}_{2}.zip".format(os.environ.get('CIRCLE_TAG'),os.environ.get('GOOS'),os.environ.get('GOARCH')), help="File to upload")
  34. parser.add_argument("--return-id-only", help="Return only the release ID after upload to GitHub.", action='store_true')
  35. args = parser.parse_args()
  36. if not os.environ.has_key('GITHUB_USERNAME'):
  37. raise parser.error('GITHUB_USERNAME not set.')
  38. if not os.environ.has_key('GITHUB_TOKEN'):
  39. raise parser.error('GITHUB_TOKEN not set.')
  40. mimetypes.init()
  41. filename = os.path.basename(args.file)
  42. mimetype,mimeencoding = mimetypes.guess_type(filename, strict=False)
  43. if mimetype is None:
  44. mimetype = 'application/zip'
  45. if mimeencoding is None:
  46. mimeencoding = 'utf8'
  47. with open(args.file,'rb') as f:
  48. asset = f.read()
  49. result = request('uploads.github.com', '/repos/tendermint/tendermint/releases/{0}/assets?name={1}'.format(args.id, filename), mimetype, mimeencoding, asset)
  50. if args.return_id_only:
  51. print(result['id'])
  52. else:
  53. print(result['browser_download_url'])