Warning, /iDDS/main/tools/git/submit-merge is written in an unsupported language. File is not indexed.
0001 #!/usr/bin/env python
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # You may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at
0006 # http://www.apache.org/licenses/LICENSE-2.0
0007 #
0008 # Authors:
0009 # - Wen Guan, <wen.guan@cern.ch>, 2019
0010
0011 import commands
0012 import sys
0013 import json
0014 import requests
0015
0016 # requests.packages.urllib3.disable_warnings()
0017
0018 project_url = "https://api.github.com/repos/wguanicedew/ess/pulls"
0019
0020 def submit_merge_request(token, data):
0021 r = requests.post(url='%s' % project_url,
0022 headers={"Content-Type": "application/json",
0023 "Authorization": "token %s" % token},
0024 data=json.dumps(data))
0025 return r
0026
0027
0028 root_git_dir = commands.getstatusoutput('git rev-parse --show-toplevel')[1]
0029
0030 # Load private_token
0031 print 'Loading private token ...',
0032 try:
0033 with open(root_git_dir + '/.githubkey', 'r') as f:
0034 private_token = f.readline().strip()
0035 print 'OK'
0036 except:
0037 print 'ERROR'
0038 print 'No github keyfile found at %s' % root_git_dir + '/.githubkey'
0039 sys.exit(-1)
0040
0041 # Check if current branch is not master or next
0042 print 'Checking if current branch is a patch/feature branch ...',
0043 current_branch = commands.getstatusoutput('git rev-parse --abbrev-ref HEAD')[1]
0044 commit_msg = current_branch
0045 for line in commands.getstatusoutput('git show')[1].splitlines():
0046 if line.strip().startswith('[PILOT-'):
0047 commit_msg = line.strip()
0048 if current_branch == 'master' or current_branch == 'dev':
0049 print 'ERROR'
0050 print 'You are currently on branch \'%s\'. Please change to a dev/patch branch.' % current_branch
0051 sys.exit(-1)
0052 if not current_branch.startswith('patch') and not current_branch.startswith('dev'):
0053 print 'ERROR'
0054 print 'You are currently on branch \'%s\'. This is not a dev/patch branch.' % current_branch
0055 sys.exit(-1)
0056 print 'OK'
0057
0058 # Push the branch to origin
0059 print 'Pushing the dev/patch branch to origin ...',
0060 op = commands.getstatusoutput('git push origin %s' % current_branch)
0061 if op[0] == 0:
0062 print 'OK'
0063 else:
0064 print 'ERROR'
0065 print op[1]
0066 sys.exit(-1)
0067
0068 # Check if there is already a merge request for this:
0069 print 'Checking if there already exists a merge request for this dev/patch ...',
0070 resp = requests.get(url='%s' % project_url,
0071 params={'state': 'open'})
0072 mr_list = json.loads(resp.text)
0073 for mr in mr_list:
0074 if current_branch in mr['head']['ref'] and mr['state'] == 'open':
0075 print 'ERROR'
0076 print 'There is already an open Merge Request for this branch.'
0077 sys.exit(-1)
0078 print 'OK'
0079
0080 # Create the Merge-requests
0081 if current_branch.startswith('patch'):
0082 print 'Submitting merge request against patch ...',
0083 result = submit_merge_request(token=private_token,
0084 data={'head': current_branch,
0085 'base': 'master',
0086 'title': commit_msg,
0087 'body': commit_msg})
0088 if result.status_code == 200 or result.status_code == 201:
0089 print 'OK'
0090 else:
0091 print 'ERROR'
0092 print result.content
0093
0094 # Submit against dev
0095 print 'Submitting merge request against dev ...',
0096 result = submit_merge_request(token=private_token,
0097 data={'head': current_branch,
0098 'base': 'dev',
0099 'title': commit_msg,
0100 'body': commit_msg})
0101 if result.status_code == 200 or result.status_code == 201:
0102 print 'OK'
0103 else:
0104 print 'ERROR'
0105 print result.content