def __init__(self, *args, **kw): super(PhabricatorService, self).__init__(*args, **kw) self.host = self.config.get("host", None) # These read login credentials from ~/.arcrc if self.host is not None: self.api = phabricator.Phabricator(host=self.host) else: self.api = phabricator.Phabricator() self.shown_user_phids = ( self.config.get("user_phids", None, aslist)) self.shown_project_phids = ( self.config.get("project_phids", None, aslist)) self.ignore_cc = self.config.get('ignore_cc', default=False, to_type=lambda x: x == "True") self.ignore_author = self.config.get('ignore_author', default=False, to_type=lambda x: x == "True") self.ignore_owner = self.config.get('ignore_owner', default=False, to_type=lambda x: x == "True") self.ignore_reviewers = self.config.get('ignore_reviewers', default=False, to_type=lambda x: x == "True")
def setUp(self): self.api = phabricator.Phabricator( username='******', certificate='test', host='http://localhost' ) self.api.certificate = CERTIFICATE
def test_maniphest_find(self, mock_connection): mock_obj = mock_connection.return_value = mock.Mock() mock_obj.getresponse.return_value = StringIO( RESPONSES['maniphest.find'] ) mock_obj.getresponse.return_value.status = 200 api = phabricator.Phabricator( username='******', certificate='test', host='http://localhost' ) api._conduit = True result = api.maniphest.find( ownerphids=['PHID-USER-5022a9389121884ab9db'] ) self.assertEqual(len(result), 1) # Test iteration self.assertIsInstance([x for x in result], list) # Test getattr self.assertEqual( result['PHID-TASK-4cgpskv6zzys6rp5rvrc']['status'], '3' )
def create_task(error_key): # Search for an existing task associated with this error key phabricator_domain = 'http://phabricator.khanacademy.org' phabctl = phabricator.Phabricator(host=phabricator_domain + '/api/') tasks = phabctl.maniphest.maniphest.query( projectPHIDs=["PHID-PROJ-wac5cp5twq6xcgubphie"]) for task_id in tasks.keys(): task = tasks[task_id] task_key = task["auxiliary"]["std:maniphest:khan:errorkey"] if task_key == error_key: return json.dumps({"status": "already_exists"}) err_def = error_parser.RedisErrorDef.get_by_key(error_key) title = "%s (%s): %s" % (error_parser.LEVELS[int( err_def.get("level"))], err_def.get("status"), err_def.get("message:title")) projectPHIDs = ["PHID-PROJ-wac5cp5twq6xcgubphie"] auxiliary = {"std:maniphest:khan:errorkey": error_key} res = phabctl.maniphest.createtask(title=title, projectPHIDs=projectPHIDs, auxiliary=auxiliary) return json.dumps({ "status": "ok", "url": "http://phabricator.khanacademy.org/%s" % res['objectName'] })
def setUp(self): self.api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost') self.api.certificate = "fdhcq3zsyijnm4h6gmh43zue5umsmng5t4dlwodvmiz4cnc6fl6f" + \ "zrvjbfg2ftktrcddan7b3xtgmfge2afbrh4uwam6pfxpq5dbkhbl" + \ "6mgaijdzpq5efw2ynlnjhoeqyh6dakl4yg346gbhabzkcxreu7hc" + \ "jhw6vo6wwa7ky2sjdk742khlgsakwtme6sr2dfkhlxxkcqw3jngy" + \ "rq5zj7m6m7hnscuzlzsviawnvg47pe7l4hxiexpbb5k456r"
def main(repo_rootdir, options): """New repositories will be placed under repo_rootdir/vcs_type/name.""" if options.verbose: print print 'START: %s' % time.ctime() phabricator_domain = 'https://phabricator.khanacademy.org' phabctl = phabricator.Phabricator(host=phabricator_domain + '/api/') (new_repos, deleted_repos, url_to_callsign_map) = ( _get_repos_to_add_and_delete(phabctl, options.verbose)) if options.verbose: print 'Adding %d new repositories to phabricator' % len(new_repos) num_failures = 0 # Since the order we go through the repos can affect the callsigns we # emit, sort the list of repos so this is always reproducible. for repo in sorted(new_repos): try: add_repository(phabctl, repo_rootdir, repo, url_to_callsign_map, options) except (phabricator.APIError, NameError), why: print >>sys.stderr, ('ERROR: Unable to add repository %s: %s' % (repo, why)) num_failures += 1
def clean(self): config = self.cleaned_data if not config.get('host'): raise forms.ValidationError( 'Missing required host configuration value') if not (config.get('token') or (config.get('username') and config.get('certificate'))): raise forms.ValidationError( 'Missing required authentication configuration value') projectPHIDs = config.get('projectPHIDs') if projectPHIDs: try: json.loads(projectPHIDs) except ValueError: raise forms.ValidationError( 'projectPHIDs field must be a valid JSON if present') api = phabricator.Phabricator( host=urlparse.urljoin(config['host'], 'api/'), username=config['username'], certificate=config['certificate'], token=config['token'], ) try: api.user.whoami() except phabricator.APIError, e: raise forms.ValidationError('%s %s' % (e.code, e.message))
def postComment(host=None, token=None, taskID=None, comment=None, patchForReview=False): data = [{'type': 'comment', 'value': comment}] if patchForReview: data.append({'type':'projects.add', 'value':['Patch-For-Review']}) logging.info('Posting comment to %s, data:\n%s' % (taskID, dumps(data, indent=4))) phab = phabricator.Phabricator(host=host, token=token) phab.maniphest.edit(objectIdentifier=taskID, transactions=data)
def get_api(self, project): return phabricator.Phabricator( host=urljoin(self.get_option('host', project), 'api/'), username=self.get_option('username', project), certificate=self.get_option('certificate', project), token=self.get_option('token', project), )
def validate_config(self, project, config, actor): projectPHIDs = config.get('projectPHIDs') if projectPHIDs: try: json.loads(projectPHIDs) except ValueError: raise PluginError( 'projectPHIDs field must be a valid JSON if present') if config.get('host') and ( (config.get('username') and config.get('certificate')) or config.get('token')): api = phabricator.Phabricator( host=urljoin(config['host'], 'api/'), username=config.get('username'), certificate=config.get('certificate'), token=config.get('token'), ) try: api.user.whoami() except phabricator.APIError as e: raise PluginError('%s %s' % (e.code, e.message)) except httplib.HTTPException as e: raise PluginError('Unable to reach Phabricator host: %s' % (e, )) except Exception as e: raise PluginError('Unhandled error from Phabricator: %s' % (e, )) return config
def test_classic_resources(self): api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost/api/') self.assertEqual(api.user.whoami.method, 'user') self.assertEqual(api.user.whoami.endpoint, 'whoami')
def __init__(self, *args, **kw): super(PhabricatorService, self).__init__(*args, **kw) self.host = self.config.get("host", None) # These read login credentials from ~/.arcrc if self.host is not None: self.api = phabricator.Phabricator(host=self.host) else: self.api = phabricator.Phabricator() self.shown_user_phids = ( self.config.get("user_phids", None, aslist)) self.shown_project_phids = ( self.config.get("project_phids", None, aslist))
def __init__(self, log_level=logging.DEBUG, sleep_delay=5): # IRC info # Read the configuration out of the .arcconfig file self.host = lugito.config.CONFIG['connectors']['irc']['host'] self.port = int(lugito.config.CONFIG['connectors']['irc']['port']) self.username = lugito.config.CONFIG['connectors']['irc']['username'] self.password = lugito.config.CONFIG['connectors']['irc']['password'] self.channel = lugito.config.CONFIG['connectors']['irc']['channel'] # Phabricator info self.phab = phabricator.Phabricator( host=lugito.config.CONFIG['phabricator']['host'], token=lugito.config.CONFIG['phabricator']['token'],) self.phab_host = self.phab.host.replace('api/', '') self.logger = logging.getLogger('lugito.connector.IRCConnector') # Add log level ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) self.logger.addHandler(ch) self.logger.setLevel(log_level) self.sleep_delay = sleep_delay
def validate_config(self, project, config, actor): projectPHIDs = config.get("projectPHIDs") if projectPHIDs: try: json.loads(projectPHIDs) except ValueError: raise PluginError( "projectPHIDs field must be a valid JSON if present") if config.get("host") and ( (config.get("username") and config.get("certificate")) or config.get("token")): api = phabricator.Phabricator( host=urljoin(config["host"], "api/"), username=config.get("username"), certificate=config.get("certificate"), token=config.get("token"), ) try: api.user.whoami() except phabricator.APIError as e: raise PluginError(f"{e.code} {e}") except HTTPException as e: raise PluginError(f"Unable to reach Phabricator host: {e}") except Exception as e: raise PluginError(f"Unhandled error from Phabricator: {e}") return config
def get_api(self, project): return phabricator.Phabricator( host=urljoin(self.get_option("host", project), "api/"), username=self.get_option("username", project), certificate=self.get_option("certificate", project), token=self.get_option("token", project), )
def phabricator(self): if self._phabricator: return self._phabricator if self.arcrc: try: with open(self.arcrc) as f: phabricator.ARCRC.update(json.load(f)) except FileNotFoundError: self.die("Failed to load a given arcrc file, %s" % self.arcrc) needs_credential = False try: host = self.phabricator_uri + "/api/" self._phabricator = phabricator.Phabricator(timeout=120, host=host) if not self.phabricator.token and not self.phabricator.certificate: needs_credential = True # FIXME, workaround # https://github.com/disqus/python-phabricator/issues/37 self._phabricator.differential.creatediff.api.interface[ "differential"]["creatediff"]["required"]["changes"] = dict except phabricator.ConfigurationError: needs_credential = True if needs_credential: if self.setup_login_certificate(): self.die("Try again now that the login certificate has been" " added") else: self.die("Please setup login certificate before trying again") return self._phabricator
def __init__(self, log_level=logging.DEBUG): """ Initialise """ self.phab = phabricator.Phabricator( host=lugito.config.CONFIG['phabricator']['host'], token=lugito.config.CONFIG['phabricator']['token'], ) self.HMAC = lugito.config.CONFIG['phabricator']['hooks'] self.host = lugito.config.CONFIG['phabricator']['host'] self.logger = logging.getLogger('lugito.lugito') # Add log level ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) self.logger.addHandler(ch) self.logger.setLevel(log_level) for key, val in self.HMAC.items(): self.HMAC[key] = bytes(u'%s' % val, 'utf-8')
def __init__(self, log_level=logging.DEBUG): # Launchpad info # Read the configuration out of the .lugitorc file self.package_names =\ lugito.config.CONFIG['phabricator']['package_names'] # Phabricator info self.phab = phabricator.Phabricator( host=lugito.config.CONFIG['phabricator']['host'], token=lugito.config.CONFIG['phabricator']['token'], ) # Jenkins info self.jenkins_site = lugito.config.CONFIG['connectors']['jenkins']\ ['site'] self.jenkins_trigger_url = lugito.config.CONFIG['connectors']\ ['jenkins']['template_url'] self.phab_host = lugito.config.CONFIG['phabricator']['host'].replace( 'api/', '') self.logger = logging.getLogger('lugito.connector.jenkins') self.jenkins = self.auth_jenkins() # Add log level ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) self.logger.addHandler(ch) self.logger.setLevel(log_level)
def __init__(self, host=None, username=None, cert=None): self.client = phabricator.Phabricator() if host is not None: self.client.host = host if username is not None: self.client.username = username if cert is not None: self.client.certificate = cert
def test_user_whoami(self, mock_connection): mock = mock_connection.return_value = Mock() mock.getresponse.return_value = StringIO(RESPONSES['user.whoami']) api = phabricator.Phabricator(token='test', host='http://localhost') api.conduit = True self.assertEqual('testaccount', api.user.whoami()['userName'])
def test_nested_resources(self): api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost/api/') self.assertEqual(api.diffusion.repository.edit.method, 'diffusion') self.assertEqual(api.diffusion.repository.edit.endpoint, 'repository.edit')
def test_connect(self, mock_connection): mock = mock_connection.return_value = Mock() mock.getresponse.return_value = StringIO(RESPONSES['conduit.connect']) api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost') api.connect() self.assertTrue('sessionKey' in api.conduit.keys()) self.assertTrue('connectionID' in api.conduit.keys())
def create_phabricator(bot_config_file: str, section: str = 'phabricator_bot', dry_run: bool = True) -> phabricator.Phabricator: """Initialize the Phabricator client from the bot config file. Examples: :: from wmflib.phabricator import create_phabricator phab_client = create_phabricator('/path/to/config.ini') phab_client.task_comment('T12345', 'Message') Arguments: bot_config_file (str): the path to the configuration file for the Phabricator bot, with the following structure:: [section_name] host = https://phabricator.example.com/api/ username = phab-bot token = api-12345 section (str, optional): the name of the section of the configuration file where to find the required parameters. dry_run (bool, optional): whether this is a DRY-RUN. Returns: wmflib.phabricator.Phabricator: a Phabricator instance. Raises: wmflib.phabricator.PhabricatorError: if unable to get all the required parameters from the bot configuration file, or to initialize the Phabricator client. """ parser = configparser.ConfigParser() parser.read(bot_config_file) required_options = ('host', 'username', 'token') params = {} try: for option in required_options: params[option] = parser.get(section, option) except configparser.NoSectionError as e: raise PhabricatorError( f'Unable to find section {section} in config file {bot_config_file}' ) from e except configparser.NoOptionError as e: raise PhabricatorError( f'Unable to find all required options {required_options} in section {section} of config ' f'file {bot_config_file}') from e try: client = phabricator.Phabricator(**params) except Exception as e: raise PhabricatorError( 'Unable to instantiate Phabricator client') from e return Phabricator(client, dry_run=dry_run)
def __init__(self, url: str, user_phid: str, token: str): self.url = url self.api_url = dirjoin(url, "api/") self.wiki_url = dirjoin(url, "w/") self.token = token self.user_phid = user_phid self.phab = phabricator.Phabricator(host=self.api_url, token=token, timeout=60)
def __init__(self, *args, **kw): super(PhabricatorService, self).__init__(*args, **kw) # These reads in login credentials from ~/.arcrc self.api = phabricator.Phabricator() self.shown_user_phids = ( self.config.get("user_phids", None, aslist)) self.shown_project_phids = ( self.config.get("project_phids", None, aslist))
def conduit_for_host_token(self, host, token): if (host, token) in self._conduits: return self._conduits[host, token] conduit = phabricator.Phabricator( host=host, token=token, ) conduit.update_interfaces() self._conduits[host, token] = conduit return conduit
def test_user_whoami(self, mock_connection): mock_obj = mock_connection.return_value = mock.Mock() mock_obj.getresponse.return_value = StringIO(RESPONSES['user.whoami']) mock_obj.getresponse.return_value.status = 200 api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost') api._conduit = True self.assertEqual(api.user.whoami()['userName'], 'testaccount')
def test_user_whoami(self): responses.add('POST', 'http://localhost/api/user.whoami', body=RESPONSES['user.whoami'], status=200) api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost/api/') api._conduit = True self.assertEqual(api.user.whoami()['userName'], 'testaccount')
def test_bad_status(self): responses.add('POST', 'http://localhost/api/conduit.connect', status=400) api = phabricator.Phabricator(username='******', certificate='test', host='http://localhost/api/') with self.assertRaises(requests.exceptions.HTTPError): api.user.whoami() assert len(responses.calls) == 1
def load_task(id): username = request.authorization.username password = request.authorization.password phab = phabricator.Phabricator(username = username, certificate = password, host = config.PHAB_API_URL) ret = phab.maniphest.query(ids = [ id.replace("T", "") ]) t = transform_task(ret.response.values()[0]) t = { "task": t } ret = make_response(json.dumps(t)) ret.headers["Content-Type"] = "text/json; charset=UTF-8" return ret