Created
March 22, 2017 06:14
-
-
Save gavincyi/2fde9090303c726207b5ece15a9b2b0b to your computer and use it in GitHub Desktop.
Submitted all Review Board Request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/python | |
| import urllib.request, base64 | |
| import json | |
| import argparse | |
| import sys | |
| def url_request(user, pwd, url, method, data={}): | |
| # Generate authentication | |
| usrPass = bytes("%s:%s" % (user, pwd), "utf-8") | |
| b64Val = base64.b64encode(usrPass) | |
| # Request and parse it | |
| if len(data) == 0: | |
| req = urllib.request.Request(url=url, | |
| headers={"Authorization": "Basic %s" % bytes.decode(b64Val, "utf-8")}, | |
| method=method) | |
| else: | |
| req = urllib.request.Request(url=url, | |
| data=json.dumps(data).encode('utf8'), | |
| headers={"Authorization": "Basic %s" % bytes.decode(b64Val, "utf-8"), \ | |
| 'Content-Type': 'application/json'}, | |
| method=method) | |
| res = urllib.request.urlopen(req) | |
| res = res.read().decode('utf-8') | |
| res = json.loads(res) | |
| return res | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Closing all the outstanding closed reviews.') | |
| parser.add_argument('-url', action='store', dest='url', help='Review board URL', default='http://reviews/api/') | |
| parser.add_argument('-user', action='store', dest='user', help='User name', default='') | |
| parser.add_argument('-pwd', action='store', dest='pwd', help='User password', default='') | |
| args = parser.parse_args() | |
| args.user = 'Gavin.Chan' | |
| args.pwd = 'password' | |
| if args.user == '' or args.pwd == '': | |
| print('Please provide valid user name and password.') | |
| parser.print_help() | |
| sys.exit(1) | |
| print('Getting all the requests from the user %s ...' % args.user) | |
| # Get all the open requests | |
| all_requests_url = args.url + "review-requests/?from-user=%s&max-results=200" % args.user | |
| review_requests = url_request(args.user, | |
| args.pwd, | |
| all_requests_url, | |
| "GET")['review_requests'] | |
| print("Outstanding requests: %d" % len(review_requests)) | |
| # Categorize the requests | |
| opened_requests = [] | |
| closed_requests = [] | |
| for review_request in review_requests: | |
| request_id = review_request['id'] | |
| request_summary = review_request['summary'] | |
| last_update_url = args.url + "review-requests/%d/last-update/" % request_id | |
| last_update_res = url_request(args.user, | |
| args.pwd, | |
| last_update_url, | |
| "GET")['last_update'] | |
| last_update_type = last_update_res['type'] | |
| last_update_fullname = last_update_res['user']['fullname'] | |
| # The request is indicated as closed if the last update is "Ship It!" by other users | |
| if last_update_type == 'review' and last_update_fullname != args.user: | |
| closed_requests.append((request_id, request_summary)) | |
| else: | |
| opened_requests.append((request_id, request_summary)) | |
| # Ask the user to confirm to close the requests | |
| print("\n\n") | |
| print("The following requests will still be opened:") | |
| for request in opened_requests: | |
| print("\t%8s %s" % (request[0], request[1])) | |
| print("\n\n\n") | |
| print("The following requests are to be closed:") | |
| for request in closed_requests: | |
| print("\t%8s %s" % (request[0], request[1])) | |
| user_response = input("Are you certain to close the above requests? [Y/n]") | |
| if user_response == 'Y': | |
| print("The requests will be closed...") | |
| else: | |
| print("The action has been cancelled") | |
| if __name__ == '__main__': | |
| # main() | |
| res = url_request('Gavin.Chan', 'password', 'http://reviews/api/review-requests/25887/', 'PUT', data={"status": "pending"}) | |
| print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment