+def get_change(url, repo, branch, change_id):
+ """
+ Fetches a change from upstream repo
+ :param url: URL of upstream gerrit
+ :param repo: name of repo
+ :param branch: branch of repo
+ :param change_id: SHA change id
+ :return: change if found and not abandoned, closed, or merged
+ """
+ rest = GerritRestAPI(url=url)
+ change_path = "{}~{}~{}".format(quote_plus(repo), quote_plus(branch),
+ change_id)
+ change_str = "changes/{}?o=CURRENT_REVISION".format(change_path)
+ change = rest.get(change_str)
+ try:
+ assert change['status'] not in 'ABANDONED' 'CLOSED', \
+ 'Change {} is in {} state'.format(change_id, change['status'])
+ if change['status'] == 'MERGED':
+ logging.info('Change {} is merged, ignoring...'
+ .format(change_id))
+ return None
+ else:
+ return change
+
+ except KeyError:
+ logging.error('Failed to get valid change data structure from url '
+ '{}/{}, data returned: \n{}'
+ .format(change_id, change_str, change))
+ raise
+
+