def review(self, label=None, vote=1, message=''): print_err("Posting {} review for change: {}".format( " {}: {}".format(label, vote) if label else '', self)) auth = HTTPDigestAuthFromNetrc(url=GERRIT_URL) rest = GerritRestAPI(url=GERRIT_URL, auth=auth, verify=GERRIT_VERIFY) rev = GerritReview() rev.set_message(message) if label: rev.add_labels({label: vote}) rest.review(self.id, self.patchset, rev)
def review(self, label=None, vote=1, message=''): print("Posting {} review for change: {}".format( " {}: {}".format(label, vote) if label else '', self)) auth = HTTPDigestAuthFromNetrc(url=GERRIT_URL) rest = GerritRestAPI(url=GERRIT_URL, auth=auth, verify=GERRIT_VERIFY) rev = GerritReview() rev.set_message(message) if label: rev.add_labels({label: vote}) rest.review(self.id, self.patchset, rev)
class RestAPI: def __init__(self, credentialsFile, gerritUrl): # Get Login Authentication information # This expects a file that contains only a Gerrit user's # <Username> <HTTP Password> # Currently, this is found on Gerrit, select: # -> Your username dropdown # -> Settings # -> HTTP Password scriptPath = os.path.dirname(os.path.abspath(__file__)) authFilepath = os.path.expanduser(scriptPath + "/" + credentialsFile) if os.path.isfile(authFilepath) == False: print("Error: No authentication file named " + credentialsFile + " found") vprint(scriptPath) quit() with open(authFilepath, 'r') as loginFile: line = loginFile.readline() login = line.split() if len(login) < 2: print("Error: Insufficient login credentials") quit() user = login[0] password = login[1] auth = HTTPBasicAuth(user, password) self.rest = GerritRestAPI(url=gerritUrl, auth=auth) # Wrapper for GerritRestAPI's GET method def get(self, query): result = self.rest.get(query, headers={'Content-Type': 'application/json'}) return result # Wrapper for GerritRestAPI's review method def review(self, changeID, revision, review): result = self.rest.review(changeID, revision, review) return result
class Gerrit(object): def __init__(self, url, use_internal=False): auth = AuthFromNetrc(url, use_internal) self.rest = GerritRestAPI(url=url, auth=auth) self.url = url self.change_options = [ 'CURRENT_REVISION', 'MESSAGES', 'DETAILED_LABELS', 'DETAILED_ACCOUNTS', 'COMMIT_FOOTERS' ] def get_change(self, change_id, rev_num=None): options = self.change_options if rev_num != None: options += ['ALL_REVISIONS'] uri = '/changes/{}?o={}'.format(change_id, '&o='.join(options)) rest = self.rest.get(uri) c = GerritChange(self.url, rest) # The modifications to change here shouldn't be relied upon, but rolling # back to a previous revision is useful for testing. So we'll do our best # to act like the requested revision is the current_revision and hope # nothing downstream of us gets too confused if rev_num != None: uri = '/changes/{}/revisions/{}/commit'.format(change_id, rev_num) rest = self.rest.get(uri) for r in c.revisions: if int(r.number) != int(rev_num): continue r.commit_message = rest['message'] c.subject = rest['subject'] c.current_revision = r return c def get_related_changes(self, change): uri = '/changes/{}/revisions/current/related'.format(change.id) changes = [] for c in self.rest.get(uri)['changes']: changes.append(self.get_change(c['change_id'])) return changes def query_changes(self, status=None, message=None, after=None, age_days=None, change_id=None, change_num=None, project=None): query = [] if message: query.append('message:"{}"'.format(urllib.parse.quote(message))) if status: query.append('status:{}'.format(status)) if after: query.append('after:"{}"'.format(after.isoformat())) if age_days: query.append('age:{}d'.format(age_days)) if change_id: query.append('change:{}'.format(change_id)) if change_num: query.append('change:{}'.format(change_num)) if project: query.append('project:{}'.format(project)) uri = '/changes/?q={}&o={}'.format('+'.join(query), '&o='.join(self.change_options)) changes = [] for c in self.rest.get(uri): changes.append(GerritChange(self.url, c)) return changes def get_patch(self, change): uri = '/changes/{}/revisions/{}/patch'.format( change.id, change.current_revision.id) return self.rest.get(uri) def get_messages(self, change): uri = '/changes/{}/messages'.format(change.id) return self.rest.get(uri) def remove_reviewer(self, change): uri = '/changes/{}/reviewers/self/delete'.format(change.id) options = { 'notify': 'NONE', } try: self.rest.post(uri, data=options) return True except requests.exceptions.HTTPError as e: return False def review(self, change, tag, message, notify_owner, vote_code_review=None, vote_verified=None, vote_cq_ready=None, inline_comments=None): review = { 'tag': tag, 'message': message, 'notify': 'OWNER' if notify_owner else 'NONE', 'omit_duplicate_comments': True, } labels = {} if vote_code_review != None: labels['Code-Review'] = vote_code_review if vote_verified != None: labels['Verified'] = vote_verified if vote_cq_ready != None: labels['Commit-Queue'] = vote_cq_ready if labels: review['labels'] = labels if inline_comments: review['comments'] = inline_comments #pprint.PrettyPrinter(indent=4).pprint(review) #pprint.PrettyPrinter(indent=4).pprint(json.dumps(review)) return self.rest.review(change.id, change.current_revision.id, json.dumps(review))
class Gerrit(object): def __init__(self, url): auth = HTTPBasicAuthFromNetrc(url=url) self.rest = GerritRestAPI(url=url, auth=auth) self.url = url self.change_options = [ 'CURRENT_REVISION', 'MESSAGES', 'DETAILED_LABELS', 'DETAILED_ACCOUNTS', 'COMMIT_FOOTERS' ] def get_change(self, change_id): uri = '/changes/{}?o={}'.format(change_id, '&o='.join(self.change_options)) rest = self.rest.get(uri) return GerritChange(self.url, rest) def get_related_changes(self, change): uri = '/changes/{}/revisions/current/related'.format(change.id) changes = [] for c in self.rest.get(uri)['changes']: changes.append(self.get_change(c['change_id'])) return changes def query_changes(self, status=None, message=None, after=None, age_days=None, change_id=None, change_num=None, project=None): query = [] if message: query.append('message:"{}"'.format(urllib.parse.quote(message))) if status: query.append('status:{}'.format(status)) if after: query.append('after:"{}"'.format(after.isoformat())) if age_days: query.append('age:{}d'.format(age_days)) if change_id: query.append('change:{}'.format(change_id)) if change_num: query.append('change:{}'.format(change_num)) if project: query.append('project:{}'.format(project)) uri = '/changes/?q={}&o={}'.format('+'.join(query), '&o='.join(self.change_options)) changes = [] for c in self.rest.get(uri): changes.append(GerritChange(self.url, c)) return changes def get_patch(self, change): uri = '/changes/{}/revisions/{}/patch'.format( change.id, change.current_revision.id) return self.rest.get(uri) def get_messages(self, change): uri = '/changes/{}/messages'.format(change.id) return self.rest.get(uri) def review(self, change, tag, message, notify_owner, vote_code_review=None, vote_verified=None, vote_cq_ready=None, vote_trybot_ready=None): review = { 'tag': tag, 'message': message, 'notify': 'OWNER' if notify_owner else 'NONE', } labels = {} if vote_code_review != None: labels['Code-Review'] = vote_code_review if vote_verified != None: labels['Verified'] = vote_verified if vote_cq_ready != None: labels['Commit-Queue'] = vote_cq_ready if vote_trybot_ready != None: labels['Trybot-Ready'] = vote_trybot_ready if labels: review['labels'] = labels #pprint.PrettyPrinter(indent=4).pprint(review) return self.rest.review(change.id, change.current_revision.id, json.dumps(review))