From: Aric Gardner Date: Tue, 19 Mar 2019 19:00:50 +0000 (-0400) Subject: Add lftools infofile check-votes X-Git-Tag: v0.22.0^0 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F62%2F14962%2F10;p=releng%2Flftools.git Add lftools infofile check-votes Usage: lftools infofile check-votes [OPTIONS] INFO_FILE GERRIT_URL CHANGE_NUMBER $ lftools infofile check-votes ~/lf/allrepos/pharos/INFO.yaml \ https://gerrit.opnfv.org/gerrit/ 67269 Number of Committers: 11 Committers have voted: ['mrichomme', 'trev', 'jose.lausuch', 'fdegir', 'Julien-zte', 'agardner', 'bramwelt', 'AlexandruAvadanii', 'gherrero'] 9 Committers that have not voted: ['jmorgan1', 'wenjing'] 2 majority vote reached $ echo $? 0 (in this case exit 0 rather than 1) Problems: 1) Someone will have to comment recheck when majority is reached for the auto merge from jenkins. 2) Non-Interactive Users will need submit. (and possibly +2? (unless we expect one of the votes to be a +2) ISSUE:RELENG-975 Change-Id: I83df2e20c3ae61681de3c0fbe5c90aaa1d873a8f Signed-off-by: Aric Gardner --- diff --git a/.coafile b/.coafile index ab5bc5f0..6d0a517e 100644 --- a/.coafile +++ b/.coafile @@ -47,7 +47,8 @@ known_third_party_imports = six, shade, xdg, - yaml + yaml, + pygerrit2 pydocstyle_ignore = D203, D213, D301 max_line_length = 120 diff --git a/docs/commands/index.rst b/docs/commands/index.rst index 5881a996..9947d04a 100644 --- a/docs/commands/index.rst +++ b/docs/commands/index.rst @@ -14,6 +14,7 @@ It supports the following commands: deploy dco gerrit + infofile lfidapi license nexus diff --git a/docs/commands/infofile.rst b/docs/commands/infofile.rst new file mode 100644 index 00000000..85c2d70f --- /dev/null +++ b/docs/commands/infofile.rst @@ -0,0 +1,23 @@ +****** +Schema +****** + +.. program-output:: lftools infofile --help + +Commands +======== + +check-votes +----------- + + .. program-output:: lftools infofile check-votes --help + +get-committers +-------------- + + .. program-output:: lftools infofile get-committers --help + +sync-committers +--------------- + + .. program-output:: lftools infofile sync-committers --help diff --git a/lftools/cli/infofile.py b/lftools/cli/infofile.py index 7d5be7e1..c12c25bb 100644 --- a/lftools/cli/infofile.py +++ b/lftools/cli/infofile.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # SPDX-License-Identifier: EPL-1.0 ############################################################################## -# Copyright (c) 2018 The Linux Foundation and others. +# Copyright (c) 2019 The Linux Foundation and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 @@ -10,10 +10,16 @@ ############################################################################## """Script to insert missing values from ldap into a projects INFO.yaml.""" +import logging +import sys + import click +from pygerrit2 import GerritRestAPI import ruamel.yaml import yaml +log = logging.getLogger(__name__) + @click.group() @click.pass_context @@ -122,5 +128,82 @@ def sync_committers(ctx, id, info_file, ldap_file, repo): readfile(info_data, ldap_data, id) +@click.command(name='check-votes') +@click.argument('info_file') +@click.argument('gerrit_url') +@click.argument('change_number') +@click.option('--tsc', type=str, required=False, + help='path to TSC INFO file') +@click.pass_context +def check_votes(ctx, info_file, gerrit_url, change_number, tsc): + """Check votes on an INFO.yaml change. + + Check for Majority of votes on a gerrit patchset + which changes an INFO.yaml file. + """ + def main(ctx, info_file, gerrit_url, change_number, tsc, majority_of_committers): + """Function so we can iterate into TSC members after commiter vote has happend.""" + with open(info_file) as file: + try: + info_data = yaml.safe_load(file) + except yaml.YAMLError as exc: + log.error(exc) + + committer_info = info_data['committers'] + + info_committers = [] + for count, item in enumerate(committer_info): + committer = committer_info[count]['id'] + info_committers.append(committer) + + rest = GerritRestAPI(url=gerrit_url) + changes = rest.get("changes/{}/reviewers".format(change_number)) + + info_change = [] + for change in changes: + line = (change['username'], change['approvals']['Code-Review']) + + if '+1' in line[1] or '+2' in line[1]: + info_change.append(change['username']) + + have_not_voted = [item for item in info_committers if item not in info_change] + have_not_voted_length = (len(have_not_voted)) + + have_voted = [item for item in info_committers if item in info_change] + have_voted_length = (len(have_voted)) + + log.info("Number of Committers:") + log.info(len(info_committers)) + committer_lenght = (len(info_committers)) + + log.info("Committers that have voted:") + log.info(have_voted) + log.info(have_voted_length) + log.info("Committers that have not voted:") + log.info(have_not_voted) + log.info(have_not_voted_length) + + if (have_voted_length != 0): + majority = (committer_lenght / have_voted_length) + + if (majority == 1): + log.info("Majority committer vote reached") + if (tsc): + log.info("Need majority of tsc") + info_file = tsc + majority_of_committers += 1 + if majority_of_committers == 2: + log.info("TSC majority reached auto merging commit") + else: + main(ctx, info_file, gerrit_url, change_number, tsc, majority_of_committers) + else: + log.info("majority not yet reached") + sys.exit(1) + + majority_of_committers = 0 + main(ctx, info_file, gerrit_url, change_number, tsc, majority_of_committers) + + infofile.add_command(get_committers) infofile.add_command(sync_committers) +infofile.add_command(check_votes) diff --git a/releasenotes/notes/infofile-2116cc444a88945e.yaml b/releasenotes/notes/infofile-2116cc444a88945e.yaml new file mode 100644 index 00000000..89edebe5 --- /dev/null +++ b/releasenotes/notes/infofile-2116cc444a88945e.yaml @@ -0,0 +1,17 @@ +--- +features: + - | + check-votes + + Usage: lftools infofile check-votes [OPTIONS] INFO_FILE GERRIT_URL + + .. code-block:: none + + Commands: + Check for Majority of votes on a gerrit patchset that changes + an INFO.yaml file. + + .. code-block:: none + + Options: + --help Show this message and exit. diff --git a/requirements.txt b/requirements.txt index dd180e8e..b746327e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ glob2 # Needed for Python < 3.5 recursive glob support deb-pkg-tools~=5.2 defusedxml # Needed due to tox complains on parseString not safe jsonschema~=2.6.0 -requests~=2.18.0 +requests>=2.20.0 rpmfile~=0.1.4 ruamel.yaml setuptools>=36.5.0 @@ -17,6 +17,7 @@ httplib2 email_validator oauth2client pyyaml +pygerrit2 # workarounds to prevent upstream from breaking us netifaces==0.10.5