Ejemplo n.º 1
0
def checksecuritypatch():
    apiaction = BaseUrl + '/a/changes/?q=status'
    try:
        auth = HTTPDigestAuth('h127301',
                              'f8ACkrUqSquzxGSun8awONIrtcD8UtvEM0AytcWz8w')
        rest = GerritRestAPI(url=BaseUrl, auth=auth)
        changes = rest.get(
            "/changes/?q=status:open+branch:hon660/nougat/master&o=CURRENT_REVISION&o=CURRENT_COMMIT"
        )
        if changes != None and len(changes) > 0:
            for item in changes:
                context_sub = item['subject']
                if context_sub != None and context_sub == GERRIT_CHANGE_SUBJECT:
                    email_info = item['revisions'][
                        item['current_revision']]['commit']['author']['email']
                    if email_info != None and "Honeywell.com" in email_info:
                        print "Honeywell patch"
                        return False
                    else:
                        print "email_info: %s" % email_info
                        commit = item['project'] + ' ' + str(
                            item['_number']) + '/' + str(item['revisions'][
                                item['current_revision']]['_number'])
                        commands = 'repo download ' + ' ' + commit
                        print '==================download code==============='
                        print commands
                        os.system(commands)
                        return True
    except Exception as err:
        print err
        return False
Ejemplo n.º 2
0
    def get_task_reviews(self, task):
        self.log.debug("Searching for reviews for task {0}".format(task))
        bug = task.bug
        gerrits = [
            'https://review.openstack.org/', 'https://review.fuel-infra.org/']
        reviews = []
        # Message number 0 is description
        is_description = True
        for msg in bug.messages:
            if is_description:
                is_description = False
                continue
            for g in gerrits:
                reviews += re.findall(g + '\d+', msg.content)
                long_reviews = re.findall(g + '#/c/\d+', msg.content)
                for u in long_reviews:
                    reviews += [u.replace('#/c/', '')]
        open_reviews = []
        for rev_url in set(reviews):
            [base_url, id] = rev_url.rsplit('/', 1)
            rest = GerritRestAPI(base_url)
            try:
                review = rest.get('/changes/{0}/detail'.format(id))
                if review['status'] == 'NEW':
                    status = []
                    if 'rejected' in review['labels']['Workflow']:
                        status.append('WIP')
                    if 'disliked' in review['labels']['Verified']:
                        status.append('FAIL')
                    open_reviews.append({'url': rev_url, 'status': status})
                    self.log.debug("Found open review {0}".format(rev_url))
            except Exception:
                pass

        return open_reviews
Ejemplo n.º 3
0
def post_add(changid,email):
    auth = HTTPBasicAuth('xiao.hu', '123456')
    rest = GerritRestAPI(url=gerrit_url)
    post_url=post
    #"reviewer": "*****@*****.**"
    test_data = {'reviewer':'*****@*****.**'}
    try: 
        change=rest.post(post_url, data=test_data)
        print change
    except RequestException as err:
        print err
Ejemplo n.º 4
0
def get_project_and_release(patch_num):
    """Returns downstream & upstream branch and project based on given

    upstream change.
    """
    rest = GerritRestAPI(url=US_GERRIT_URL, auth=None)
    change = rest.get("/changes/?q=change:%s" % patch_num)
    us_branch = change[0]['branch'].split('/')[1]
    project = change[0]['project'].split('/')[1]
    logging.info("Detected project: %s, branch: %s" % (project, us_branch))

    return project, "rhos-%s.0-patches" % RELEASE_MAP[us_branch], us_branch
Ejemplo n.º 5
0
def get_project_and_release(patch_num):
    """Returns downstream & upstream branch and project based on given

    upstream change.
    """
    rest = GerritRestAPI(url=US_GERRIT_URL, auth=None)
    change = rest.get("/changes/?q=change:%s" % patch_num)
    us_branch = change[0]['branch'].split('/')[1]
    project = change[0]['project'].split('/')[1]
    logging.info("Detected project: %s, branch: %s" % (project, us_branch))

    return project, "rhos-%s.0-patches" % RELEASE_MAP[us_branch], us_branch
Ejemplo n.º 6
0
 def __init__(self):
     uri = os.environ.get("GERRIT_URL")
     username = os.environ.get("GERRIT_USER")
     password = os.environ.get("GERRIT_PASS")
     if not uri or not username or not password:
         err_msg = "Set environment variables GERRIT_URL, GERRIT_USER, GERRIT_PASS"
         print(err_msg)
         print("WARNING: gerrit commands will be ignored")
         self.rest = None
     else:
         self.auth = HTTPDigestAuth(username, password)
         self.rest = GerritRestAPI(url=uri, auth=self.auth)
Ejemplo n.º 7
0
   def __init__(self, gerrit_host_alias):

      ssh_config = SSHConfig()
      user_ssh_config_file = os.path.expanduser("~/.ssh/config")
      if os.path.exists(user_ssh_config_file):
         with open(user_ssh_config_file) as f:
            ssh_config.parse(f)

      gerrit_host_name = ssh_config.lookup(gerrit_host_alias).get("hostname")

      auth = HTTPBasicAuthFromNetrc("https://{}/".format(gerrit_host_alias))

      self.gerrit_api_client = GerritRestAPI("https://{}/".format(gerrit_host_name), auth)
Ejemplo n.º 8
0
def _main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage)

    parser.add_option('-g', '--gerrit-url', dest='gerrit_url',
                      help='gerrit server url')
    parser.add_option('-b', '--basic-auth', dest='basic_auth',
                      action='store_true',
                      help='use basic auth instead of digest')
    parser.add_option('-u', '--username', dest='username',
                      help='username')
    parser.add_option('-p', '--password', dest='password',
                      help='password')
    parser.add_option('-n', '--netrc', dest='netrc',
                      action='store_true',
                      help='Use credentials from netrc')
    parser.add_option('-v', '--verbose', dest='verbose',
                      action='store_true',
                      help='enable verbose (debug) logging')

    (options, _args) = parser.parse_args()

    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=level)

    if not options.gerrit_url:
        parser.error("Must specify Gerrit URL with --gerrit-url")

    if options.username and options.password:
        if options.netrc:
            logging.warning("--netrc option ignored")
        if options.basic_auth:
            auth = HTTPBasicAuth(options.username, options.password)
        else:
            auth = HTTPDigestAuth(options.username, options.password)
    elif options.netrc:
        if options.basic_auth:
            auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
        else:
            auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
    else:
        auth = None

    rest = GerritRestAPI(url=options.gerrit_url, auth=auth)

    changes = rest.get("/changes/?q=owner:self%20status:open")
    logging.info("%d changes", len(changes))
    for change in changes:
        logging.info(change['change_id'])
Ejemplo n.º 9
0
def gerritChanges(status="open", branch="hon660/nougat/master"):
    apiaction = BaseUrl + '/a/changes/?q=status'
    try:
        auth = HTTPDigestAuth('h127301',
                              'f8ACkrUqSquzxGSun8awONIrtcD8UtvEM0AytcWz8w')
        rest = GerritRestAPI(url=BaseUrl, auth=auth)
        #searchStr = "/changes/?q=status:"+ status + "+branch:" + branch +"&o=CURRENT_REVISION&o=CURRENT_COMMIT"
        changes0 = rest.get("/changes/?q=status:" + status + "+branch:" +
                            branch + "+before:2017-06-01")
        changes1 = rest.get("/changes/?q=status:" + status + "+branch:" +
                            branch + "+after:2017-06-01+before:2017-07-01")
        changes2 = rest.get("/changes/?q=status:" + status + "+branch:" +
                            branch + "+after:2017-07-01+before:2017-08-01")
        changes3 = rest.get("/changes/?q=status:" + status + "+branch:" +
                            branch + "+after:2017-08-01+before:2017-09-01")
        changes4 = rest.get("/changes/?q=status:" + status + "+branch:" +
                            branch + "+after:2017-09-01+before:2017-10-01")
        changes5 = rest.get("/changes/?q=status:" + status + "+branch:" +
                            branch + "+after:2017-10-01")
        #changes = rest.get(searchStr)
        changes = changes0 + changes1 + changes2 + changes3 + changes4 + changes5
        return changes
    except Exception as err:
        print err
        return None
Ejemplo n.º 10
0
 def gerrit(self, url):
     if url not in self._gerrit:
         ssl_verify = True
         if 'review.openstack.org' not in url:
             ssl_verify = False
         self._gerrit[url] = GerritRestAPI(url=url, auth=None, verify=ssl_verify)
     return self._gerrit[url]
Ejemplo n.º 11
0
class GerritProcessor(object):
    def __init__(self):
        uri = os.environ.get("GERRIT_URL")
        username = os.environ.get("GERRIT_USER")
        password = os.environ.get("GERRIT_PASS")
        if not uri or not username or not password:
            err_msg = "Set environment variables GERRIT_URL, GERRIT_USER, GERRIT_PASS"
            print(err_msg)
            print("WARNING: gerrit commands will be ignored")
            self.rest = None
        else:
            self.auth = HTTPDigestAuth(username, password)
            self.rest = GerritRestAPI(url=uri, auth=self.auth)

    def process(self, msg):
        if not self.rest:
            return "gerrit commands ignored"
        supported = ['status open']
        if 'status open' in msg:
            gurl = ""
            # gurl = "q/status:open%20owner:self"
            print("URL:[{}]".format(gurl))
            changes = self.rest.get(gurl)
            return changes
        else:
            return "Supported gerrit messages {}".format(supported)
Ejemplo n.º 12
0
class GerritUsers:
    def __init__(self, users, url = 'https://review.openstack.org'):
        self.users = users
        self.rest = GerritRestAPI(url=url)

    def fixes(self, start_date, report_date, branch, cachedir = "~/.gerrit"):
        fixes = {}
        report_day = datetime.datetime.strptime(report_date, '%Y-%m-%d')
        last_sun = report_day - datetime.timedelta(days=report_day.weekday()) + \
                datetime.timedelta(days=6, weeks=-1)
        prelast_sun = last_sun - datetime.timedelta(weeks=1)

        projects = ['^stackforge/fuel-.*', '^stackforge/python-fuel.*']
        template = '/changes/?q=project:%s+owner:".*<*****@*****.**>"+message:"bug:+"'
        url = 'https://review.openstack.org/#/c/%s'

        for user in self.users:
            fixes[user] = {}
            fixes[user]['merged'] = []
            fixes[user]['merged_this_week'] = []
            fixes[user]['merged_last_week'] = []
            fixes[user]['open_this_week'] = []
            # Let's use something like cache to no overload gerrit API and improve performance
            cache_filename = "%s/%s_%s_%s_%s.grc" % (cachedir, user, report_date, start_date, branch)
            try:
                cache_file = open(cache_filename, 'rb')
                fixes[user] = pickle.load(cache_file)
                cache_file.close()
                continue
            except:
                pass

            for project in projects:
                try:
                    changes = self.rest.get(template % (project, user))
                except:
                    changes = []
                for change in changes:
                    if change['branch'] != branch or change['status'] == 'ABANDONED':
                        continue
                    if change['created'] > start_date and change['created'] < report_date:
                        if change['status'] == 'MERGED':
                            fixes[user]['merged'].append(url % change['_number'])
                            if change['updated'][:10] > str(last_sun)[:10]:
                                fixes[user]['merged_this_week'].append(url % change['_number'])
                            elif change['created'][:10] > str(prelast_sun)[:10]:
                                fixes[user]['merged_last_week'].append(url % change['_number'])
                        else:
                            if change['created'][:10] > str(last_sun)[:10]:
                                fixes[user]['open_this_week'].append(url % change['_number'])

            # Let's use something like cache to no overload gerrit API and improve performance
            try:
                cache_file = open(cache_filename, 'wb')
                pickle.dump(fixes[user], cache_file)
                cache_file.close()
            except:
                pass

        return fixes
Ejemplo n.º 13
0
 def __init__(self, users, gerrit = 'https://review.openstack.org',
         projects = ['^openstack/fuel-.*', '^openstack/python-fuel.*'],
         template = '/changes/?q=project:%s+owner:".*<*****@*****.**>"+message:"bug:+"',
         url = 'https://review.openstack.org/#/c/%s'):
     self.users = users
     self.rest = GerritRestAPI(url=gerrit)
     self.projects = projects
     self.template = template
     self.url = url
Ejemplo n.º 14
0
Archivo: views.py Proyecto: haobug/pycm
def update(request):
    output=""
    for emp in Employee.objects.order_by('email'):
        if not emp.team_id:
            continue
        output += '' + str('<h1>%s</h1>' % emp.name)
        for comm in Community.objects.order_by('name'):
            if not comm.enabled :
                continue
            output += '<h2>%s</h2>' % comm.name
            #rest = GerritRestAPI(url='http://review.cyanogenmod.org/')
            rest = GerritRestAPI(url=comm.review_base)
            output += "%s<br/>\n" % emp.email
            query_url ="/changes/?q=owner:"+emp.email+"+status:merged"
            output +=  query_url  + "<br/>\n" 
            changes=''
            try:
                #proxies = {
                #    "http": "http://127.0.0.1:8087",
                #    "https": "https://127.0.0.1:8087",
                #}
                #changes = rest.get(query_url, proxies=proxies)
                changes = rest.get(query_url)
            except HTTPError as he:
                output += '' + str(he.message)
            output += time.strftime('%Y-%m-%d %H:%M:%S') + "==> %d changes<br/>\n" % len(changes)
            for change in changes:
                subject = str(change['subject'])
                num =  str(change['_number'])
                date =  str(change['updated'])

                q = Contribution.objects.filter(employee_id=emp,review_id=num, community_id=comm)
                if len(q) < 1:
                    cont = Contribution(employee_id=emp,review_id=num, community_id=comm, merge_date=date)
                    cont.save()
                else:
                    continue
                output += "<br/>\n" 
                output += subject
                output += "<br/>\n" 
                output += comm.review_base + num
                output += "<br/>\n" 
                time.sleep(1)
    return HttpResponse(output)
Ejemplo n.º 15
0
class GerritClient(object):
    def __init__(self, gerrit_url, user=None, passwd=None):
        auth = None
        if user and passwd:
            auth = HTTPDigestAuth(user, passwd)

        self.api = GerritRestAPI(url='https://review.tizen.org/gerrit',  auth=auth)

    def get_branches(self, project_name):
        quoted_path = urllib.quote(project_name,safe='')
        branches = self.api.get("/projects/%s/branches/" % quoted_path)

        return [br['ref'].replace("refs/heads/", "") for br in branches]
Ejemplo n.º 16
0
class GerritServer:

   def __init__(self, gerrit_host_alias):

      ssh_config = SSHConfig()
      user_ssh_config_file = os.path.expanduser("~/.ssh/config")
      if os.path.exists(user_ssh_config_file):
         with open(user_ssh_config_file) as f:
            ssh_config.parse(f)

      gerrit_host_name = ssh_config.lookup(gerrit_host_alias).get("hostname")

      auth = HTTPBasicAuthFromNetrc("https://{}/".format(gerrit_host_alias))

      self.gerrit_api_client = GerritRestAPI("https://{}/".format(gerrit_host_name), auth)

   def set_score_labels(self, change_id, code_review_score, verified_score):
      labels = {}
      labels['Code-Review'] = code_review_score
      labels['Verified']    = verified_score

      review = GerritReview()
      review.add_labels(labels)
      self.gerrit_api_client.review(change_id, "current", review)
Ejemplo n.º 17
0
    def get_task_reviews(self, task):
        self.log.debug("Searching for reviews for task {0}".format(task))
        bug = task.bug
        gerrits = [
            'https://review.openstack.org/', 'https://review.fuel-infra.org/'
        ]
        reviews = []
        # Message number 0 is description
        is_description = True
        for msg in bug.messages:
            if is_description:
                is_description = False
                continue
            for g in gerrits:
                reviews += re.findall(g + '\d+', msg.content)
                long_reviews = re.findall(g + '#/c/\d+', msg.content)
                for u in long_reviews:
                    reviews += [u.replace('#/c/', '')]
        open_reviews = []
        for rev_url in set(reviews):
            [base_url, id] = rev_url.rsplit('/', 1)
            rest = GerritRestAPI(base_url)
            try:
                review = rest.get('/changes/{0}/detail'.format(id))
                if review['status'] == 'NEW':
                    status = []
                    if 'rejected' in review['labels']['Workflow']:
                        status.append('WIP')
                    if 'disliked' in review['labels']['Verified']:
                        status.append('FAIL')
                    open_reviews.append({'url': rev_url, 'status': status})
                    self.log.debug("Found open review {0}".format(rev_url))
            except Exception:
                pass

        return open_reviews
Ejemplo n.º 18
0
class GerritChangesFetcher(object):

    """Gerrit yields 500 changes at most"""
    MAX_BATCH_SIZE = 500

    def __init__(self, rest_url='https://gerrit.wikimedia.org/r',
                 batch_size=100):

        self.batch = int(batch_size)

        self.rest = GerritRestAPI('https://gerrit.wikimedia.org/r')

    def fetch_chunks(self, query={}):
        if not self._validate_batch_size(self.batch):
            raise Exception('Chunk size %s overflows Gerrit limit %s' % (
                            self.batch, self.MAX_BATCH_SIZE))

        search_operators = {'is': 'open',
                            'limit': str(self.batch),
                            }
        search_operators.update(query)

        sortkey = None
        while True:
            if sortkey is not None:
                search_operators['resume_sortkey'] = sortkey
            query = [':'.join(t) for t in search_operators.iteritems()]
            endpoint = '/changes/?o=LABELS&q=' + '%20'.join(query)
            ret = self.rest.get(endpoint)

            if not ret:
                return
            stderr("Retrieved chunk of %s changes\n" % len(ret))
            yield ret
            sortkey = ret[-1].get('_sortkey')

    def fetch_all(self, query={}):
        changes = []
        for chunk in self.fetch_chunks(query=query):
            changes.extend(chunk)
        return changes

    def _validate_batch_size(self, size):
        if size > self.MAX_BATCH_SIZE:
            stderr('Batch sizes should be less than 500 due to Gerrit '
                   'internal limit')
            return False
        return True
Ejemplo n.º 19
0
class GerritUsers:
    def __init__(self, users, gerrit = 'https://review.openstack.org',
            projects = ['^openstack/fuel-.*', '^openstack/python-fuel.*'],
            template = '/changes/?q=project:%s+owner:".*<*****@*****.**>"+message:"bug:+"',
            url = 'https://review.openstack.org/#/c/%s'):
        self.users = users
        self.rest = GerritRestAPI(url=gerrit)
        self.projects = projects
        self.template = template
        self.url = url

    def fixes(self, start_date, report_date, branch):
        fixes = {}
        last_sun = report_date - datetime.timedelta(days=report_date.weekday()) + \
                datetime.timedelta(days=6, weeks=-1)
        prelast_sun = last_sun - datetime.timedelta(weeks=1)

        for user in self.users:
            fixes[user] = {}
            fixes[user]['merged'] = []
            fixes[user]['merged_this_week'] = []
            fixes[user]['merged_last_week'] = []
            fixes[user]['open_this_week'] = []

            for project in self.projects:
                try:
                    changes = self.rest.get(self.template % (project, user), timeout=1)
                except:
                    changes = []
                for change in changes:
                    if not re.search(branch, change['branch']) or change['status'] == 'ABANDONED':
                        continue
                    if change['created'] > str(start_date) and change['created'] < str(report_date):
                        if change['status'] == 'MERGED':
                            fixes[user]['merged'].append(self.url % change['_number'])
                            if change['updated'][:10] > str(last_sun)[:10]:
                                fixes[user]['merged_this_week'].append(self.url % change['_number'])
                            elif change['created'][:10] > str(prelast_sun)[:10]:
                                fixes[user]['merged_last_week'].append(self.url % change['_number'])
                        else:
                            if change['created'][:10] > str(last_sun)[:10]:
                                fixes[user]['open_this_week'].append(self.url % change['_number'])

        return fixes
Ejemplo n.º 20
0
from pygerrit.rest import GerritRestAPI
import sys
from debian.changelog import change
from requests.exceptions import RequestException
rest = GerritRestAPI(url='http://review.cyanogenmod.org')
query_url = "/changes/?q=owner:%s+status:merged"


def tm_query(email):
    try:
        final_url = query_url % email
        change = rest.get(final_url)
        print email, len(change)
    except RequestException as err:
        print email, 0


if __name__ == '__main__':
    f = open('all.txt', 'r')
    lines = f.readlines()
    for line in lines:
        tm_query(line)
    f.close()
Ejemplo n.º 21
0
 def _get_rest_client(self):
     if not self._restclient:
         self._restclient = GerritRestAPI(url=self._gerrit_url)
     return self._restclient
Ejemplo n.º 22
0
url ={}
projects['https://review.openstack.org'] = ['^openstack/fuel-.*', '^openstack/python-fuel.*']
projects['https://review.fuel-infra.org'] = ['^.*']
url['https://review.openstack.org'] = 'https://review.openstack.org/#/c/%s'
url['https://review.fuel-infra.org'] = 'https://review.fuel-infra.org/#/c/%s'
branch = {}
branch['https://review.openstack.org'] = 'master'
branch['https://review.fuel-infra.org'] = '.*'

total_merged = 0
total_open = 0

for gerrit in gerrits:
    if args.debug:
        print "Checking %s gerrit" % gerrit
    rest = GerritRestAPI(url=gerrit)
    for project in projects[gerrit]:
        print "Getting changes for user"
        try:
            changes = rest.get(template % (project, user), timeout=1)
        except:
            changes = []
        for change in changes:
            if args.debug:
                print "checking %s review" % (url[gerrit] % change['_number'])
            if not re.search(branch[gerrit], change['branch']) or change['status'] == 'ABANDONED':
                continue
            if change['created'] > start_date and change['created'] < report_date:
                if change['status'] == 'MERGED':
                    print "Merged: %s" % (url[gerrit] % change['_number'])
                    total_merged += 1
Ejemplo n.º 23
0
class GerritUsers:
    def __init__(self, users, gerrit = 'https://review.openstack.org',
            projects = ['^openstack/fuel-.*', '^openstack/python-fuel.*'],
            template = '/changes/?q=project:%s+owner:".*<*****@*****.**>"+message:"bug:+"',
            url = 'https://review.openstack.org/#/c/%s'):
        self.users = users
        self.rest = GerritRestAPI(url=gerrit)
        self.projects = projects
        self.template = template
        self.url = url

    def fixes(self, start_date, report_date, branch, format="weekly"):
        fixes = {}
        last_sun = report_date - datetime.timedelta(days=report_date.weekday()) + \
                datetime.timedelta(days=6, weeks=-1)
        prelast_sun = last_sun - datetime.timedelta(weeks=1)

        for user in self.users:
            if format == "github":
                fixes[user] = []
            else:
                fixes[user] = {}
                fixes[user]['merged'] = []
                fixes[user]['merged_this_week'] = []
                fixes[user]['merged_last_week'] = []
                fixes[user]['open_this_week'] = []

            for project in self.projects:
                try:
                    changes = self.rest.get(self.template % (project, user), timeout=1)
                except:
                    changes = []
                for change in changes:
                    if not re.search(branch, change['branch']) or change['status'] == 'ABANDONED':
                        continue
                    if format == "github":
                        if change['created'] > str(start_date) and change['created'] < str(report_date):
                            if change['status'] == 'MERGED':
                                state = 'closed'
                            else:
                                state = 'open'
                            created_at = datetime.datetime.strptime(change['created'].split('.')[0], "%Y-%m-%d %H:%M:%S")
                            fixes[user].append({
                                'repository_url': change['project'],
                                'html_url': self.url % change['_number'],
                                'state': state,
                                'title': change['subject'],
                                'created_at': created_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
                                'user': {
                                    'login': user
                                 }
                            })
                    else:
                        if change['created'] > str(start_date) and change['created'] < str(report_date):
                            if change['status'] == 'MERGED':
                                fixes[user]['merged'].append(self.url % change['_number'])
                                if change['updated'][:10] > str(last_sun)[:10]:
                                    fixes[user]['merged_this_week'].append(self.url % change['_number'])
                                elif change['created'][:10] > str(prelast_sun)[:10]:
                                    fixes[user]['merged_last_week'].append(self.url % change['_number'])
                            else:
                                if change['created'][:10] > str(last_sun)[:10]:
                                    fixes[user]['open_this_week'].append(self.url % change['_number'])

        return fixes
Ejemplo n.º 24
0
# USAGE: python askgerrit.py 2>/dev/null | wc -l

#import os
#from requests.auth import HTTPDigestAuth
import requests
from pygerrit.rest import GerritRestAPI

from_time = "2016-09-16 16:40:00.000000"
match_word = 'revert'
token = 'xoxp-XXXXX-XXXXX'  # change XXX to ABC, SLACK TOKEN =>  https://api.slack.com/docs/oauth-test-tokens

#auth = HTTPDigestAuth('username', 'password')
rest = GerritRestAPI(url='http://review.openstack.org')
changes = rest.get(
    "/changes/?q=project:openstack/fuel-library branch:stable/mitaka")

for change in changes:
    if match_word in change['subject'].lower(
    ) and change['updated'] > from_time:
        ch = change['status'], change['updated'], change['subject']
        r = requests.get("https://slack.com/api/chat.postMessage?token=" +
                         token + "&channel=vmaliaev_notify&text=\
                         " + ', '.join(ch) +
                         "&pretty=1")  #Send message to SLACK

        print change['status'], change['updated'], change['subject']

####

#os.system("curl -k 'https://slack.com/api/chat.postMessage?token=xoxp-XXXXXXXXXXXXXXXXXXXXXXXXXXX&channel=vmaliaev_notify&text=hello&pretty=1'")
Ejemplo n.º 25
0
def importer(service, id, url, host, user, password, project, board, labels,
             list_name):
    config = configuration.get_config()
    trello_key = config['trello']['api_key']
    trello_token = config['trello']['access_token']
    if not board:
        if 'default_board' not in config['trello']:
            click.echo("No default_board exists in ~/filch.conf")
            click.echo("You must either set a default_board in ~/filch.conf "
                       "or use the --board_name option.")
            sys.exit(1)
        else:
            board = config['trello']['default_board']

    if service == 'gerrit':
        # default to upstream openstack
        # if host is present then use that
        # if url is present then use that
        gerrit_url = "https://review.openstack.org"
        if host:
            gerrit_url = config['gerrit'][host]['url']
        if url:
            gerrit_url = url

        gerrit_api = GerritRestAPI(url=gerrit_url, auth=None)
        for change_id in list(id):
            change = gerrit_api.get("/changes/%s" % change_id)
            cards.create_card(trello_key,
                              trello_token,
                              board,
                              change['subject'],
                              constants.GERRIT_CARD_DESC.format(**change),
                              card_labels=list(labels),
                              card_due="null",
                              list_name=list_name)
            click.echo('You have successfully imported "%s"' %
                       change['subject'])

    if service == 'blueprint':
        if not project:
            click.echo('To import a blueprint you must provide a project.')
            sys.exit(1)
        for bp_id in list(id):
            blueprint = utils.get_blueprint(project, bp_id)
            cards.create_card(
                trello_key,
                trello_token,
                board,
                blueprint['title'],
                constants.BLUEPRINT_CARD_DESC.format(**blueprint),
                card_labels=list(labels),
                card_due="null",
                list_name=list_name)
            click.echo('You have successfully imported "%s"' %
                       blueprint['title'])

    if service == 'bug':
        for bug_id in list(id):
            bug = utils.get_launchpad_bug(bug_id)
            cards.create_card(trello_key,
                              trello_token,
                              board,
                              bug['title'],
                              constants.BUG_CARD_DESC.format(**bug),
                              card_labels=list(labels),
                              card_due="null",
                              list_name=list_name)
            click.echo('You have successfully imported "%s"' % bug['title'])

    if service in ['bz', 'bugzilla']:
        if url:
            # also need user & password.  sslverify is optional
            if not user or not password:
                click.echo("If using a url for Bugzilla, you must also "
                           "provide a user and password.")
                sys.exit(1)

        # if host arg is not used, use first host from
        # configuration as default
        if not host:
            # check to ensure a host for bugzilla exists in config
            if len(config['bugzilla'].keys()) == 0:
                click.echo("No Bugzilla data configuration file.  Please "
                           "add configuration data or pass url, user and "
                           "password arguments.")
                sys.exit(1)
            else:
                host = config['bugzilla'].keys()[0]

        url = config['bugzilla'][host]['url']
        user = config['bugzilla'][host]['user']

        sslverify = config['bugzilla'][host].get('sslverify', True)

        for bz_id in list(id):
            try:
                bug = utils.get_bz(bz_id,
                                   url=url,
                                   user=user,
                                   password=password,
                                   sslverify=sslverify)

                if len(bug.comments) > 0:
                    bug.description = bug.comments[0]['text']

                cards.create_card(
                    trello_key,
                    trello_token,
                    board,
                    bug.summary,
                    constants.BZ_CARD_DESC.format(**bug.__dict__),
                    card_labels=list(labels),
                    card_due="null",
                    list_name=list_name)
                click.echo('You have successfully imported "%s"' % bug.summary)
            except Exception as err:
                click.echo(err)

    if service == 'debug':
        ids = list(id)
        print(ids)
Ejemplo n.º 26
0
	def get_build_url(self):
		gerrit_auth = HTTPDigestAuth('shangchunlei', '60gelr2RLEd8XZQgz+XqI2lRZBMSgQQt+g7V76Yf0A')
		rest = GerritRestAPI(url='http://10.3.3.15:8080', auth=gerrit_auth)
		detail=rest.get('/changes/'+self.change_id+'/detail')
		url=detail['messages'][1]['message'].split(" ")[-1]
		return url
Ejemplo n.º 27
0
gerrits = ['https://review.openstack.org', 'https://review.fuel-infra.org']
template = '/changes/?q=project:%s+owner:".*<*****@*****.**>"+message:"bug:+"'
projects = {}
url ={}
projects['https://review.openstack.org'] = ['^stackforge/fuel-.*', '^stackforge/python-fuel.*']
projects['https://review.fuel-infra.org'] = ['^.*']
url['https://review.openstack.org'] = 'https://review.openstack.org/#/c/%s'
url['https://review.fuel-infra.org'] = 'https://review.fuel-infra.org/#/c/%s'

total_merged = 0
total_open = 0

for gerrit in gerrits:
    if args.debug:
        print "Checking %s gerrit" % gerrit
    rest = GerritRestAPI(url=gerrit)
    for project in projects[gerrit]:
        changes = rest.get(template % (project, user))
        for change in changes:
            if args.debug:
                print "checking %s review" % (url[gerrit] % change['_number'])
            if change['branch'] != branch or change['status'] == 'ABANDONED':
                continue
            if change['created'] > start_date and change['created'] < report_date:
                if change['status'] == 'MERGED':
                    print "Merged: %s" % (url[gerrit] % change['_number'])
                    total_merged += 1
                else:
                    print "Open: %s" % (url[gerrit] % change['_number'])
                    total_open += 1
if script_url:
    meta['script_url'] = script_url
    ci_config += script_url + '\n'

# Add the test path if one was given
if test_path:
    meta['test_path'] = test_path

# If debug mode was requested, pass that via metadata
if debug_tests:
    meta['debug_tests'] = 'True'

# Pass the desired gerrit change request info via metadata
if change_req:
    # Retrieve the Gerrit ref info for the change request
    gerrit_server = GerritRestAPI(url='http://review.gluster.org')
    gerrit_request = '/changes/?q={0}&o=CURRENT_REVISION'.format(change_req)
    rev = gerrit_server.get(gerrit_request)
    ref = rev[0]['revisions'][rev[0]['revisions'].keys()[0]]['fetch']['http']['ref']

    # Pass the Gerrit Change Request # and the matching git reference
    meta['change_req'] = change_req
    meta['change_ref'] = ref

    # Ensure we have the matching git branch
    git_branch = rev[0]['branch']

# Pass the desired git branch name via metadata
meta['glusterfs_branch'] = git_branch

# Files to be injected info the new image
Ejemplo n.º 29
0
    def __init__(self, gerrit_url, user=None, passwd=None):
        auth = None
        if user and passwd:
            auth = HTTPDigestAuth(user, passwd)

        self.api = GerritRestAPI(url='https://review.tizen.org/gerrit',  auth=auth)
Ejemplo n.º 30
0
def _main():
    parser = optparse.OptionParser()
    parser.add_option('-g', '--gerrit-url', dest='gerrit_url',
                      metavar='URL',
                      default=None,
                      help='gerrit server URL')
    parser.add_option('-b', '--basic-auth', dest='basic_auth',
                      action='store_true',
                      help='use HTTP basic authentication instead of digest')
    parser.add_option('-n', '--dry-run', dest='dry_run',
                      action='store_true',
                      help='enable dry-run mode: show stale changes but do '
                           'not abandon them')
    parser.add_option('-a', '--age', dest='age',
                      metavar='AGE',
                      default="6months",
                      help='age of change since last update '
                           '(default: %default)')
    parser.add_option('-m', '--message', dest='message',
                      metavar='STRING', default=None,
                      help='Custom message to append to abandon message')
    parser.add_option('--branch', dest='branches', metavar='BRANCH_NAME',
                      default=[], action='append',
                      help='Abandon changes only on the given branch')
    parser.add_option('--exclude-branch', dest='exclude_branches',
                      metavar='BRANCH_NAME',
                      default=[],
                      action='append',
                      help='Do not abandon changes on given branch')
    parser.add_option('--project', dest='projects', metavar='PROJECT_NAME',
                      default=[], action='append',
                      help='Abandon changes only on the given project')
    parser.add_option('--exclude-project', dest='exclude_projects',
                      metavar='PROJECT_NAME',
                      default=[],
                      action='append',
                      help='Do not abandon changes on given project')
    parser.add_option('--owner', dest='owner',
                      metavar='USERNAME',
                      default=None,
                      action='store',
                      help='Only abandon changes owned by the given user')
    parser.add_option('-v', '--verbose', dest='verbose',
                      action='store_true',
                      help='enable verbose (debug) logging')

    (options, _args) = parser.parse_args()

    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=level)

    if not options.gerrit_url:
        logging.error("Gerrit URL is required")
        return 1

    pattern = re.compile(r"^([\d]+)(month[s]?|year[s]?|week[s]?)")
    match = pattern.match(options.age)
    if not match:
        logging.error("Invalid age: %s", options.age)
        return 1
    message = "Abandoning after %s %s or more of inactivity." % \
        (match.group(1), match.group(2))

    if options.basic_auth:
        auth_type = HTTPBasicAuthFromNetrc
    else:
        auth_type = HTTPDigestAuthFromNetrc

    try:
        auth = auth_type(url=options.gerrit_url)
        gerrit = GerritRestAPI(url=options.gerrit_url, auth=auth)
    except Exception as e:
        logging.error(e)
        return 1

    logging.info(message)
    try:
        stale_changes = []
        offset = 0
        step = 500
        query_terms = ["status:new", "age:%s" % options.age]
        if options.branches:
            query_terms += ["branch:%s" % b for b in options.branches]
        elif options.exclude_branches:
            query_terms += ["-branch:%s" % b for b in options.exclude_branches]
        if options.projects:
            query_terms += ["project:%s" % p for p in options.projects]
        elif options.exclude_projects:
            query_terms = ["-project:%s" % p for p in options.exclude_projects]
        if options.owner:
            query_terms += ["owner:%s" % options.owner]
        query = "%20".join(query_terms)
        while True:
            q = query + "&n=%d&S=%d" % (step, offset)
            logging.debug("Query: %s", q)
            url = "/changes/?q=" + q
            result = gerrit.get(url)
            logging.debug("%d changes", len(result))
            if not result:
                break
            stale_changes += result
            last = result[-1]
            if "_more_changes" in last:
                logging.debug("More...")
                offset += step
            else:
                break
    except Exception as e:
        logging.error(e)
        return 1

    abandoned = 0
    errors = 0
    abandon_message = message
    if options.message:
        abandon_message += "\n\n" + options.message
    for change in stale_changes:
        number = change["_number"]
        try:
            owner = change["owner"]["name"]
        except:
            owner = "Unknown"
        subject = change["subject"]
        if len(subject) > 70:
            subject = subject[:65] + " [...]"
        change_id = change["id"]
        logging.info("%s (%s): %s", number, owner, subject)
        if options.dry_run:
            continue

        try:
            gerrit.post("/changes/" + change_id + "/abandon",
                        data='{"message" : "%s"}' % abandon_message)
            abandoned += 1
        except Exception as e:
            errors += 1
            logging.error(e)
    logging.info("Total %d stale open changes", len(stale_changes))
    if not options.dry_run:
        logging.info("Abandoned %d changes. %d errors.", abandoned, errors)
Ejemplo n.º 31
0
 def __init__(self, users, url = 'https://review.openstack.org'):
     self.users = users
     self.rest = GerritRestAPI(url=url)
Ejemplo n.º 32
0
def getgerritresult():
    apiaction = BaseUrl + '/a/changes/?q=status'
    print apiaction
    # resdata=json.loads(urllib2.urlopen(apiaction).read())
    # response = requests.get(apiaction,auth=('h156930','6gTqZy4gsUyvzug6HJjT7i8i8fPGoSirFtIh2fnvSw'));
    try:
        auth = HTTPDigestAuth('h127301',
                              'f8ACkrUqSquzxGSun8awONIrtcD8UtvEM0AytcWz8w')
        print auth
        rest = GerritRestAPI(url=BaseUrl, auth=auth)
        changes = rest.get(
            "/changes/?q=status:open+branch:hon660/nougat/master&o=CURRENT_REVISION"
        )
        # print changes
        if changes != None and len(changes) > 0:
            for item in changes:
                print "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
                print item
                print "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
                context_sub = item['subject']
                if context_sub != None:
                    print("subject %s" % context_sub)
                    pat_report = re.compile(PATTERN_PRX)
                    match_report = pat_report.search(context_sub)
                    if match_report != None:
                        print("match_report %s" % match_report)
                        context_ID = match_report.group()
                        if context_ID == JIRA_ID:
                            print("context_ID %s" % context_ID)
                            context_change_id = item['change_id']
                            print context_ID
                            print item['project']
                            print item['_number']
                            COMMIT_INFO['commit'] = item['project'] + ' ' + str(
                                item['_number']) + '/' + str(item['revisions'][
                                    item['current_revision']]['_number'])
                            print COMMIT_INFO['commit']
                            print item['revisions'][
                                item['current_revision']]['_number']

                            # COMMIT_LIST.append(COMMIT_INFO['commit'])
                            need_append = True
                            for commit in COMMIT_LIST:
                                if commit == COMMIT_INFO['commit']:
                                    need_append = False
                                    break
                            if need_append:
                                index = 0
                                for commit in COMMIT_LIST:
                                    if commit.startswith(item['project'] +
                                                         ' ' +
                                                         str(item['_number'])):
                                        del COMMIT_LIST[index]
                                        print "del COMMIT_LIST"
                                    index += 1
                                COMMIT_LIST.append(COMMIT_INFO['commit'])

                            print COMMIT_LIST
                            print len(COMMIT_LIST)
                            # PATCH_NUM='1'
                            if len(COMMIT_LIST) == int(PATCH_NUM):
                                print '==================download code==============='
                                for commit in COMMIT_LIST:
                                    commands = 'repo download ' + ' ' + commit
                                    print commands
                                    os.system(commands)
                                return True
            return False
    except Exception as err:
        print err
        return False
Ejemplo n.º 33
0
    config = ConfigParser.ConfigParser()
    # Preserve Case in keys
    config.optionxform = str
    for config_file in args.files:
        if not os.path.exists(config_file):
            raise ValueError(
                'Failed to open/read "{file}"'.format(file=config_file))
        config.read(config_file)
except ImportError:
    args = None

gerrit_service = config.get('Header', 'gerrit')
#'https://review.openstack.org'
human_gerrit_service = config.get('Header', 'human_gerrit')
# "https://review.openstack.org/#/q/"
rest = GerritRestAPI(url=gerrit_service)

queries = config.items("Queries")
for output_file, query in queries:
    #HACK to exclude [DEFAULT] items
    if not output_file.startswith('_'):
        print("Processing '{0}' to File '{1}.json' ".format(
            query, output_file))

        # Don't cache the gerrit search query
        with requests_cache.disabled():
            changes = rest.get("/changes/?q=" + query)

        G = nx.DiGraph()
        counter = 0
Ejemplo n.º 34
0
    def __init__(self, rest_url='https://gerrit.wikimedia.org/r',
                 batch_size=100):

        self.batch = int(batch_size)

        self.rest = GerritRestAPI('https://gerrit.wikimedia.org/r')
Ejemplo n.º 35
0
def _main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage)

    parser.add_option('-g', '--gerrit-url', dest='gerrit_url',
                      help='gerrit server url')
    parser.add_option('-b', '--basic-auth', dest='basic_auth',
                      action='store_true',
                      help='use basic auth instead of digest')
    if _kerberos_support:
        parser.add_option('-k', '--kerberos-auth', dest='kerberos_auth',
                          action='store_true',
                          help='use kerberos auth')
    parser.add_option('-u', '--username', dest='username',
                      help='username')
    parser.add_option('-p', '--password', dest='password',
                      help='password')
    parser.add_option('-n', '--netrc', dest='netrc',
                      action='store_true',
                      help='Use credentials from netrc')
    parser.add_option('-v', '--verbose', dest='verbose',
                      action='store_true',
                      help='enable verbose (debug) logging')

    (options, _args) = parser.parse_args()

    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=level)

    if not options.gerrit_url:
        parser.error("Must specify Gerrit URL with --gerrit-url")

    if _kerberos_support and options.kerberos_auth:
        if options.username or options.password \
                or options.basic_auth or options.netrc:
            parser.error("--kerberos-auth may not be used together with "
                         "--username, --password, --basic-auth or --netrc")
        auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
    elif options.username and options.password:
        if options.netrc:
            logging.warning("--netrc option ignored")
        if options.basic_auth:
            auth = HTTPBasicAuth(options.username, options.password)
        else:
            auth = HTTPDigestAuth(options.username, options.password)
    elif options.netrc:
        if options.basic_auth:
            auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
        else:
            auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
    else:
        auth = None

    rest = GerritRestAPI(url=options.gerrit_url, auth=auth)

    try:
        changes = rest.get("/changes/?q=owner:self%20status:open")
        logging.info("%d changes", len(changes))
        for change in changes:
            logging.info(change['change_id'])
    except RequestException as err:
        logging.error("Error: %s", str(err))
Ejemplo n.º 36
0
def importer(service, id, url, host, user, password, project, board,
             labels, list_name):
    try:
        config = configuration.get_config()
    except Exception as err:
        click.echo(err)
        sys.exit(1)

    trello_key = config['trello']['api_key']
    trello_token = config['trello']['access_token']
    if not board:
        if 'default_board' not in config['trello']:
            click.echo("No default_board exists in ~/filch.conf")
            click.echo("You must either set a default_board in ~/filch.conf "
                       "or use the --board_name option.")
            sys.exit(1)
        else:
            board = config['trello']['default_board']

    trello_api = trelloclient.TrelloClient(
        api_key=config['trello']['api_key'],
        token=config['trello']['access_token']
    )

    board_obj = [b for b in trello_api.list_boards()
                 if b.name == board][0]

    # ensure labels being used are actually in the board
    card_labels = [label for label in board_obj.get_labels()
                      if label.name in list(labels)]

    # ensure list name exists in board
    board_list = [trello_list for trello_list in board_obj.open_lists()
                if trello_list.name == list_name][0]

    if service == 'gerrit':
        # default to upstream openstack
        # if host is present then use that
        # if url is present then use that
        gerrit_url = "https://review.openstack.org"
        if host:
            gerrit_url = config['gerrit'][host]['url']
        if url:
            gerrit_url = url

        gerrit_api = GerritRestAPI(url=gerrit_url, auth=None)
        for change_id in list(id):
            change = gerrit_api.get("/changes/%s" % change_id)
            cards.create_card(
                board_list,
                change['subject'],
                constants.GERRIT_CARD_DESC.format(**change),
                labels=card_labels,
                due="null",
            )
            click.echo(
                'You have successfully imported "%s"' % change['subject'])

    if service == 'blueprint':
        if not project:
            click.echo('To import a blueprint you must provide a project.')
            sys.exit(1)
        for bp_id in list(id):
            blueprint = utils.get_blueprint(project, bp_id)
            cards.create_card(
                board_list,
                blueprint['title'],
                constants.BLUEPRINT_CARD_DESC.format(**blueprint),
                labels=card_labels,
                due="null",
            )
            click.echo(
                'You have successfully imported "%s"' % blueprint['title'])

    if service == 'bug':
        for bug_id in list(id):
            bug = utils.get_launchpad_bug(bug_id)
            cards.create_card(
                board_list,
                bug['title'],
                constants.BUG_CARD_DESC.format(**bug),
                labels=card_labels,
                due="null",
            )
            click.echo(
                'You have successfully imported "%s"' % bug['title'])

    if service == 'story':
        for story_id in list(id):
            story = utils.get_storyboard_story(story_id)
            cards.create_card(
                board_list,
                story['title'],
                constants.STORY_CARD_DESC.format(**story),
                labels=card_labels,
                due="null",
            )
            click.echo(
                'You have successfully imported "%s"' % story['title'])

    if service in ['bz', 'bugzilla']:
        if url:
            # also need user & password.  sslverify is optional
            if not user or not password:
                click.echo("If using a url for Bugzilla, you must also "
                           "provide a user and password.")
                sys.exit(1)

        # if host arg is not used, use first host from
        # configuration as default
        if not host:
            # check to ensure a host for bugzilla exists in config
            if len(config['bugzilla'].keys()) == 0:
                click.echo("No Bugzilla data configuration file.  Please "
                           "add configuration data or pass url, user and "
                           "password arguments.")
                sys.exit(1)
            else:
                host = list(config['bugzilla'].keys())[0]

        url = config['bugzilla'][host]['url']
        user = config['bugzilla'][host]['user']

        sslverify = config['bugzilla'][host].get('sslverify', True)

        for bz_id in list(id):
            try:
                bug = utils.get_bz(bz_id, url=url, user=user, password=password,
                                   sslverify=sslverify)

                if len(bug.comments) > 0:
                    bug.description = bug.comments[0]['text']

                bug_card = cards.create_card(
                    board_list,
                    bug.summary,
                    constants.BZ_CARD_DESC.format(**bug.__dict__),
                    labels=card_labels,
                    due="null",
                )

                # adds comments to a card
                if len(bug.comments) > 1:
                    for comment in bug.comments[1:]:
                        bug_card.comment(constants.COMMENT_TEXT.format(
                            text=comment['text'],
                            author=comment['author'],
                            create_time=comment['creation_time'],
                            is_private=constants.COMMENT_PRIVACY[
                                comment['is_private']
                            ],
                        ))

                # adds external trackers to card
                if len(bug.external_bugs) > 0:
                    external_trackers = []
                    for ext_bug in bug.external_bugs:
                        external_trackers.append(
                            os.path.join(ext_bug['type']['url'],
                                         ext_bug['ext_bz_bug_id'])
                        )
                    bug_card.add_checklist(
                        'External Trackers',
                        external_trackers
                    )

                click.echo('You have successfully imported "%s"' % bug.summary)
            except Exception as err:
                click.echo(err)

    if service == 'debug':
        ids = list(id)
        print(ids)
Ejemplo n.º 37
0
def importer(service, id, url, host, user, password, project, board, labels,
             list_name):
    try:
        config = configuration.get_config()
    except Exception as err:
        click.echo(err)
        sys.exit(1)

    trello_key = config['trello']['api_key']
    trello_token = config['trello']['access_token']
    if not board:
        if 'default_board' not in config['trello']:
            click.echo("No default_board exists in ~/filch.conf")
            click.echo("You must either set a default_board in ~/filch.conf "
                       "or use the --board_name option.")
            sys.exit(1)
        else:
            board = config['trello']['default_board']

    trello_api = trelloclient.TrelloClient(
        api_key=config['trello']['api_key'],
        token=config['trello']['access_token'])

    board_obj = [b for b in trello_api.list_boards() if b.name == board][0]

    # ensure labels being used are actually in the board
    card_labels = [
        label for label in board_obj.get_labels() if label.name in list(labels)
    ]

    # ensure list name exists in board
    board_list = [
        trello_list for trello_list in board_obj.open_lists()
        if trello_list.name == list_name
    ][0]

    if service == 'gerrit':
        # default to upstream openstack
        # if host is present then use that
        # if url is present then use that
        gerrit_url = "https://review.openstack.org"
        if host:
            gerrit_url = config['gerrit'][host]['url']
        if url:
            gerrit_url = url

        gerrit_api = GerritRestAPI(url=gerrit_url, auth=None)
        for change_id in list(id):
            change = gerrit_api.get("/changes/%s" % change_id)
            cards.create_card(
                board_list,
                change['subject'],
                constants.GERRIT_CARD_DESC.format(**change),
                labels=card_labels,
                due="null",
            )
            click.echo('You have successfully imported "%s"' %
                       change['subject'])

    if service == 'blueprint':
        if not project:
            click.echo('To import a blueprint you must provide a project.')
            sys.exit(1)
        for bp_id in list(id):
            blueprint = utils.get_blueprint(project, bp_id)
            cards.create_card(
                board_list,
                blueprint['title'],
                constants.BLUEPRINT_CARD_DESC.format(**blueprint),
                labels=card_labels,
                due="null",
            )
            click.echo('You have successfully imported "%s"' %
                       blueprint['title'])

    if service == 'bug':
        for bug_id in list(id):
            bug = utils.get_launchpad_bug(bug_id)
            cards.create_card(
                board_list,
                bug['title'],
                constants.BUG_CARD_DESC.format(**bug),
                labels=card_labels,
                due="null",
            )
            click.echo('You have successfully imported "%s"' % bug['title'])

    if service == 'story':
        for story_id in list(id):
            story = utils.get_storyboard_story(story_id)
            cards.create_card(
                board_list,
                story['title'],
                constants.STORY_CARD_DESC.format(**story),
                labels=card_labels,
                due="null",
            )
            click.echo('You have successfully imported "%s"' % story['title'])

    if service in ['bz', 'bugzilla']:
        if url:
            # also need user & password.  sslverify is optional
            if not user or not password:
                click.echo("If using a url for Bugzilla, you must also "
                           "provide a user and password.")
                sys.exit(1)

        # if host arg is not used, use first host from
        # configuration as default
        if not host:
            # check to ensure a host for bugzilla exists in config
            if len(config['bugzilla'].keys()) == 0:
                click.echo("No Bugzilla data configuration file.  Please "
                           "add configuration data or pass url, user and "
                           "password arguments.")
                sys.exit(1)
            else:
                host = list(config['bugzilla'].keys())[0]

        url = config['bugzilla'][host]['url']
        user = config['bugzilla'][host]['user']

        sslverify = config['bugzilla'][host].get('sslverify', True)

        for bz_id in list(id):
            try:
                bug = utils.get_bz(bz_id,
                                   url=url,
                                   user=user,
                                   password=password,
                                   sslverify=sslverify)

                if len(bug.comments) > 0:
                    bug.description = bug.comments[0]['text']

                bug_card = cards.create_card(
                    board_list,
                    bug.summary,
                    constants.BZ_CARD_DESC.format(**bug.__dict__),
                    labels=card_labels,
                    due="null",
                )

                # adds comments to a card
                if len(bug.comments) > 1:
                    for comment in bug.comments[1:]:
                        bug_card.comment(
                            constants.COMMENT_TEXT.format(
                                text=comment['text'],
                                author=comment['author'],
                                create_time=comment['creation_time'],
                                is_private=constants.COMMENT_PRIVACY[
                                    comment['is_private']],
                            ))

                # adds external trackers to card
                if len(bug.external_bugs) > 0:
                    external_trackers = []
                    for ext_bug in bug.external_bugs:
                        external_trackers.append(
                            os.path.join(ext_bug['type']['url'],
                                         ext_bug['ext_bz_bug_id']))
                    bug_card.add_checklist('External Trackers',
                                           external_trackers)

                click.echo('You have successfully imported "%s"' % bug.summary)
            except Exception as err:
                click.echo(err)

    if service == 'debug':
        ids = list(id)
        print(ids)
Ejemplo n.º 38
0
if script_url:
    meta['script_url'] = script_url
    ci_config += script_url + '\n'

# Add the test path if one was given
if test_path:
    meta['test_path'] = test_path

# If debug mode was requested, pass that via metadata
if debug_tests:
    meta['debug_tests'] = 'True'

# Pass the desired gerrit change request info via metadata
if change_req:
    # Retrieve the Gerrit ref info for the change request
    gerrit_server = GerritRestAPI(url='http://review.gluster.org')
    gerrit_request = '/changes/?q={0}&o=CURRENT_REVISION'.format(change_req)
    rev = gerrit_server.get(gerrit_request)
    ref = rev[0]['revisions'][rev[0]['revisions'].keys()
                              [0]]['fetch']['http']['ref']

    # Pass the Gerrit Change Request # and the matching git reference
    meta['change_req'] = change_req
    meta['change_ref'] = ref

    # Ensure we have the matching git branch
    git_branch = rev[0]['branch']

# Pass the desired git branch name via metadata
meta['glusterfs_branch'] = git_branch
Ejemplo n.º 39
0
def _main():
    descr = 'Send request using Gerrit HTTP API'
    parser = argparse.ArgumentParser(
        description=descr,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-g',
                        '--gerrit-url',
                        dest='gerrit_url',
                        required=True,
                        help='gerrit server url')
    parser.add_argument('-b',
                        '--basic-auth',
                        dest='basic_auth',
                        action='store_true',
                        help='use basic auth instead of digest')
    if _kerberos_support:
        parser.add_argument('-k',
                            '--kerberos-auth',
                            dest='kerberos_auth',
                            action='store_true',
                            help='use kerberos auth')
    parser.add_argument('-u', '--username', dest='username', help='username')
    parser.add_argument('-p', '--password', dest='password', help='password')
    parser.add_argument('-n',
                        '--netrc',
                        dest='netrc',
                        action='store_true',
                        help='Use credentials from netrc')
    parser.add_argument('-v',
                        '--verbose',
                        dest='verbose',
                        action='store_true',
                        help='enable verbose (debug) logging')

    options = parser.parse_args()

    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=level)

    if _kerberos_support and options.kerberos_auth:
        if options.username or options.password \
                or options.basic_auth or options.netrc:
            parser.error("--kerberos-auth may not be used together with "
                         "--username, --password, --basic-auth or --netrc")
        auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
    elif options.username and options.password:
        if options.netrc:
            logging.warning("--netrc option ignored")
        if options.basic_auth:
            auth = HTTPBasicAuth(options.username, options.password)
        else:
            auth = HTTPDigestAuth(options.username, options.password)
    elif options.netrc:
        if options.basic_auth:
            auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
        else:
            auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
    else:
        auth = None

    rest = GerritRestAPI(url=options.gerrit_url, auth=auth)

    try:
        changes = rest.get("/changes/?q=owner:self%20status:open")
        logging.info("%d changes", len(changes))
        for change in changes:
            logging.info(change['change_id'])
    except RequestException as err:
        logging.error("Error: %s", str(err))
Ejemplo n.º 40
0
        ["Name", "Link", "Title", "Created", "Updated", "Mergeable"])

    ct.align["Name"] = "l"
    ct.align["Title"] = "l"
    ct.align["Link"] = "l"
    ct.align["Created"] = "l"
    ct.align["Updated"] = "l"
    ct.align["Mergeable"] = "l"

    for change in changes:
        if parse_gerrit_time(change['updated']) < cut_off_date:

            # rest.post("/changes/%s/abandon" % change['id'],
            #           data=MSG)

            ct.add_row(format_dict(change))

    print('Changes Abandoned in openstack/%s' % project)

    print(ct)


user = os.environ['GERRIT_USER']
pwd = os.environ['GERRIT_PWD']
auth = HTTPDigestAuth(user, pwd)

rest = GerritRestAPI(url='https://review.openstack.org', auth=auth)

for project in PROJECTS_LIST:
    process_project(project)
Ejemplo n.º 41
0
def _main():
    parser = optparse.OptionParser()
    parser.add_option('-g', '--gerrit-url', dest='gerrit_url',
                      metavar='URL',
                      default=None,
                      help='gerrit server URL')
    parser.add_option('-b', '--basic-auth', dest='basic_auth',
                      action='store_true',
                      help='use HTTP basic authentication instead of digest')
    parser.add_option('-n', '--dry-run', dest='dry_run',
                      action='store_true',
                      help='enable dry-run mode: show stale changes but do '
                           'not abandon them')
    parser.add_option('-a', '--age', dest='age',
                      metavar='AGE',
                      default="6months",
                      help='age of change since last update '
                           '(default: %default)')
    parser.add_option('-m', '--message', dest='message',
                      metavar='STRING', default=None,
                      help='Custom message to append to abandon message')
    parser.add_option('--exclude-branch', dest='exclude_branches',
                      metavar='BRANCH_NAME',
                      default=[],
                      action='append',
                      help='Do not abandon changes on given branch')
    parser.add_option('--exclude-project', dest='exclude_projects',
                      metavar='PROJECT_NAME',
                      default=[],
                      action='append',
                      help='Do not abandon changes on given project')
    parser.add_option('--owner', dest='owner',
                      metavar='USERNAME',
                      default=None,
                      action='store',
                      help='Only abandon changes owned by the given user')
    parser.add_option('-v', '--verbose', dest='verbose',
                      action='store_true',
                      help='enable verbose (debug) logging')

    (options, _args) = parser.parse_args()

    level = logging.DEBUG if options.verbose else logging.INFO
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=level)

    if not options.gerrit_url:
        logging.error("Gerrit URL is required")
        return 1

    pattern = re.compile(r"^([\d]+)(month[s]?|year[s]?|week[s]?)")
    match = pattern.match(options.age)
    if not match:
        logging.error("Invalid age: %s", options.age)
        return 1
    message = "Abandoning after %s %s or more of inactivity." % \
        (match.group(1), match.group(2))

    if options.basic_auth:
        auth_type = HTTPBasicAuthFromNetrc
    else:
        auth_type = HTTPDigestAuthFromNetrc

    try:
        auth = auth_type(url=options.gerrit_url)
        gerrit = GerritRestAPI(url=options.gerrit_url, auth=auth)
    except Exception as e:
        logging.error(e)
        return 1

    logging.info(message)
    try:
        stale_changes = []
        offset = 0
        step = 500
        query_terms = ["status:new", "age:%s" % options.age] + \
                      ["-branch:%s" % b for b in options.exclude_branches] + \
                      ["-project:%s" % p for p in options.exclude_projects]
        if options.owner:
            query_terms += ["owner:%s" % options.owner]
        query = "%20".join(query_terms)
        while True:
            q = query + "&n=%d&S=%d" % (step, offset)
            logging.debug("Query: %s", q)
            url = "/changes/?q=" + q
            result = gerrit.get(url)
            logging.debug("%d changes", len(result))
            if not result:
                break
            stale_changes += result
            last = result[-1]
            if "_more_changes" in last:
                logging.debug("More...")
                offset += step
            else:
                break
    except Exception as e:
        logging.error(e)
        return 1

    abandoned = 0
    errors = 0
    abandon_message = message
    if options.message:
        abandon_message += "\n\n" + options.message
    for change in stale_changes:
        number = change["_number"]
        try:
            owner = change["owner"]["name"]
        except:
            owner = "Unknown"
        subject = change["subject"]
        if len(subject) > 70:
            subject = subject[:65] + " [...]"
        change_id = change["id"]
        logging.info("%s (%s): %s", number, owner, subject)
        if options.dry_run:
            continue

        try:
            gerrit.post("/changes/" + change_id + "/abandon",
                        data='{"message" : "%s"}' % abandon_message)
            abandoned += 1
        except Exception as e:
            errors += 1
            logging.error(e)
    logging.info("Total %d stale open changes", len(stale_changes))
    if not options.dry_run:
        logging.info("Abandoned %d changes. %d errors.", abandoned, errors)