github.com/okex/exchain@v1.8.0/libs/tendermint/scripts/release_management/github-openpr.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Open a PR against the develop branch. --branch required.
     4  # Optimized for CircleCI
     5  
     6  import json
     7  import os
     8  import argparse
     9  import httplib
    10  from base64 import b64encode
    11  
    12  
    13  def request(org, repo, data):
    14    user_and_pass = b64encode(b"{0}:{1}".format(os.environ['GITHUB_USERNAME'], os.environ['GITHUB_TOKEN'])).decode("ascii")
    15    headers = {
    16      'User-Agent': 'tenderbot',
    17      'Accept': 'application/vnd.github.v3+json',
    18      'Authorization': 'Basic %s' % user_and_pass
    19    }
    20  
    21    conn = httplib.HTTPSConnection('api.github.com', timeout=5)
    22    conn.request('POST', '/repos/{0}/{1}/pulls'.format(org,repo), data, headers)
    23    response = conn.getresponse()
    24    if response.status < 200 or response.status > 299:
    25      print(response)
    26      conn.close()
    27      raise IOError(response.reason)
    28    responsedata = response.read()
    29    conn.close()
    30    return json.loads(responsedata)
    31  
    32  
    33  if __name__ == "__main__":
    34    parser = argparse.ArgumentParser()
    35    parser.add_argument("--org", default="tendermint", help="GitHub organization. Defaults to tendermint.")
    36    parser.add_argument("--repo", default="tendermint", help="GitHub repository. Defaults to tendermint.")
    37    parser.add_argument("--head", help="The name of the branch where your changes are implemented.", required=True)
    38    parser.add_argument("--base", help="The name of the branch you want the changes pulled into.", required=True)
    39    parser.add_argument("--title", default="Security release {0}".format(os.environ.get('CIRCLE_TAG')), help="The title of the pull request.")
    40    args = parser.parse_args()
    41  
    42    if not os.environ.has_key('GITHUB_USERNAME'):
    43      raise parser.error('GITHUB_USERNAME not set.')
    44  
    45    if not os.environ.has_key('GITHUB_TOKEN'):
    46      raise parser.error('GITHUB_TOKEN not set.')
    47  
    48    if os.environ.get('CIRCLE_TAG') is None:
    49      raise parser.error('CIRCLE_TAG not set.')
    50  
    51    result = request(args.org, args.repo, data=json.dumps({'title':"{0}".format(args.title),'head':"{0}".format(args.head),'base':"{0}".format(args.base),'body':"<Please fill in details.>"}))
    52    print(result['html_url'])