Esempio n. 1
0
 def test_basic_auth_from_netrc(self):
     """Test that the HTTP basic auth is taken from netrc."""
     with patch("pygerrit2.rest.auth._get_netrc_auth") as mock_netrc:
         mock_netrc.return_value = ("netrcuser", "netrcpass")
         auth = HTTPBasicAuthFromNetrc(url="http://review.example.com")
         assert auth.username == "netrcuser"
         assert auth.password == "netrcpass"
Esempio n. 2
0
 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'
     ]
Esempio n. 3
0
    def review(self, label=None, vote=1, message=''):
        print_err("Posting {} review for change: {}".format(
            " {}: {}".format(label, vote) if label else '', self))
        auth = HTTPBasicAuthFromNetrc(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)
Esempio n. 4
0
 def _authenticate(self):
     username = password = None
     if self.options.netrc:
         auth = HTTPBasicAuthFromNetrc(url=self.options.url)
         username = auth.username
         password = auth.password
     if not username:
         username = os.environ.get("USERNAME")
     if not password:
         password = os.environ.get("PASSWORD")
     while not username:
         username = input("user: "******"password: ")
     auth = HTTPBasicAuth(username, password)
     return auth
Esempio n. 5
0
 def test_basic_auth_from_netrc_fails(self):
     """Test that an exception is raised when credentials are not found."""
     with self.assertRaises(ValueError) as exc:
         HTTPBasicAuthFromNetrc(url="http://review.example.com")
     assert str(exc.exception) == "netrc missing or no credentials found in netrc"
Esempio n. 6
0
 def merge(self):
     auth = HTTPBasicAuthFromNetrc(url=GERRIT_URL)
     rest = GerritRestAPI(url=GERRIT_URL, auth=auth, verify=GERRIT_VERIFY)
     url_path = '/changes/{}/submit'.format(self.id)
     rest.post(url_path)
Esempio n. 7
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="(deprecated) use basic auth instead of digest",
    )
    parser.add_argument(
        "-d",
        "--digest-auth",
        dest="digest_auth",
        action="store_true",
        help="use digest auth instead of basic",
    )
    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.digest_auth:
            auth = HTTPDigestAuth(options.username, options.password)
        else:
            auth = HTTPBasicAuth(options.username, options.password)
    elif options.netrc:
        if options.digest_auth:
            auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
        else:
            auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
    else:
        auth = None

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

    try:
        query = ["status:open"]
        if auth:
            query += ["owner:self"]
        else:
            query += ["limit:10"]
        changes = rest.get("/changes/?q=%s" % "%20".join(query))
        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))
Esempio n. 8
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='(deprecated) use basic auth instead of digest')
    parser.add_argument('-d',
                        '--digest-auth',
                        dest='digest_auth',
                        action='store_true',
                        help='use digest auth instead of basic')
    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.digest_auth:
            auth = HTTPDigestAuth(options.username, options.password)
        else:
            auth = HTTPBasicAuth(options.username, options.password)
    elif options.netrc:
        if options.digest_auth:
            auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
        else:
            auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
    else:
        auth = None

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

    try:
        query = ["status:open"]
        if auth:
            query += ["owner:self"]
        else:
            query += ["limit:10"]
        changes = rest.get("/changes/?q=%s" % "%20".join(query))
        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))