def validate_config_field(self, project, name, value, actor=None): """ ``` if name == 'foo' and value != 'bar': raise PluginError('foo must be bar') return value ``` """ for config in self.get_config(project=project): if config['name'] != name: continue if value is None: if config.get('required'): raise PluginError('Field is required') return value if isinstance(value, six.string_types): value = value.strip() # TODO(dcramer): probably should do something with default # validations here, though many things will end up bring string # based if not value and config.get('required'): raise PluginError('Field is required') for validator in DEFAULT_VALIDATORS.get(config['type'], ()): value = validator(project=project, value=value, actor=actor) for validator in config.get('validators', ()): value = validator(value, project=project, actor=actor) return value return value
def validate_field(self, name, value): """ ``` if name == 'foo' and value != 'bar': raise PluginError('foo must be bar') return value ``` """ field = self.config[name] if value is None: if field.get("required"): raise PluginError("Field is required") return value if isinstance(value, six.string_types): value = value.strip() # TODO(dcramer): probably should do something with default # validations here, though many things will end up bring string # based if not value and field.get("required"): raise PluginError("Field is required") for validator in DEFAULT_VALIDATORS.get(field["type"], ()): value = validator(value=value) for validator in field.get("validators", ()): value = validator(value=value, **self.context) return value
def URLValidator(value, **kwargs): from sentry.http import is_valid_url # Django 1.9 setup issue if not value.startswith(('http://', 'https://')): raise PluginError('Not a valid URL.') if not is_valid_url(value): raise PluginError('Not a valid URL.') return value
def link_auth(self, user, organization, data): integration_id = data['integration_id'] try: integration = Integration.objects.get( provider=self.auth_provider, id=integration_id, ) except Integration.DoesNotExist: raise PluginError('Invalid integration id') # check that user actually has access to add allowed_gh_installations = set(self.get_installations(user)) if int(integration.external_id) not in allowed_gh_installations: raise PluginError('You do not have access to that integration') integration.add_organization(organization.id) for repo in self.get_repositories(integration): # TODO(jess): figure out way to migrate from github --> github apps Repository.objects.create_or_update( organization_id=organization.id, name=repo['name'], external_id=repo['external_id'], provider='github_apps', values={ 'integration_id': integration.id, 'url': repo['url'], 'config': repo['config'], }, )
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 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 link_auth(self, user, organization, data): integration_id = data["integration_id"] try: integration = Integration.objects.get(provider=self.auth_provider, id=integration_id) except Integration.DoesNotExist: raise PluginError("Invalid integration id") # check that user actually has access to add allowed_gh_installations = set(self.get_installations(user)) if int(integration.external_id) not in allowed_gh_installations: raise PluginError("You do not have access to that integration") integration.add_organization(organization) for repo in self.get_repositories(integration): # TODO(jess): figure out way to migrate from github --> github apps Repository.objects.create_or_update( organization_id=organization.id, name=repo["name"], external_id=repo["external_id"], provider="github_apps", values={ "integration_id": integration.id, "url": repo["url"], "config": repo["config"], }, )
def raise_error(self, exc): if isinstance(exc, ApiError): raise PluginError(self.message_from_error(exc)) elif isinstance(exc, PluginError): raise else: self.logger.exception(six.text_type(exc)) raise PluginError(self.message_from_error(exc))
def raise_error(self, exc): if isinstance(exc, ApiUnauthorized): raise InvalidIdentity(self.message_from_error(exc)) elif isinstance(exc, ApiError): raise PluginError(self.message_from_error(exc)) elif isinstance(exc, PluginError): raise else: self.logger.exception(six.text_type(exc)) raise PluginError(self.message_from_error(exc))
def validate_config(self, project, config, actor): super(YouTrackPlugin, self).validate_config(project, config, actor) errors = self.config_form.client_errors for key, message in errors.iteritems(): if key in ['url', 'username', 'pasword']: self.reset_options(project=project) raise PluginError(message) for error, message in errors.iteritems(): raise PluginError(message) return config
def validate_urls(value, **kwargs): output = [] for url in value.split('\n'): url = url.strip() if not url: continue if not url.startswith(('http://', 'https://')): raise PluginError('Not a valid URL.') if not is_valid_url(url): raise PluginError('Not a valid URL.') output.append(url) return '\n'.join(output)
def raise_error(self, exc, identity=None): if isinstance(exc, ApiUnauthorized): raise InvalidIdentity(self.message_from_error(exc), identity=identity).with_traceback( sys.exc_info()[2] ) elif isinstance(exc, ApiError): raise PluginError(self.message_from_error(exc)).with_traceback(sys.exc_info()[2]) elif isinstance(exc, PluginError): raise else: self.logger.exception(str(exc)) raise PluginError(self.message_from_error(exc)).with_traceback(sys.exc_info()[2])
def validate_urls4(value, **kwargs): output = [] for url in value.split("\n"): url = url.strip() if not url: continue if not url.startswith(("http://", "https://")): raise PluginError("Not a valid URL.") if not is_valid_url(url): raise PluginError("Not a valid URL.") output.append(url) return "\n".join(output)
def raise_error(self, exc, identity=None): if isinstance(exc, ApiUnauthorized): six.reraise( InvalidIdentity, InvalidIdentity(self.message_from_error(exc), identity=identity), sys.exc_info()[2], ) elif isinstance(exc, ApiError): six.reraise(PluginError, PluginError(self.message_from_error(exc)), sys.exc_info()[2]) elif isinstance(exc, PluginError): raise else: self.logger.exception(six.text_type(exc)) six.reraise(PluginError, PluginError(self.message_from_error(exc)), sys.exc_info()[2])
def create_issue(self, request, group, form_data, **kwargs): api = self.get_api(group.project) try: data = api.maniphest.createtask( title=str(form_data["title"]), description=str(form_data["description"]), ownerPHID=form_data.get("assignee"), projectPHIDs=form_data.get("tags"), ) except phabricator.APIError as e: raise PluginError(f"{e.code} {e}") except HTTPException as e: raise PluginError("Unable to reach Phabricator host: %s" % e) return data["id"]
def create_issue(self, request, group, form_data, **kwargs): api = self.get_api(group.project) try: data = api.maniphest.createtask( title=form_data['title'].encode('utf-8'), description=form_data['description'].encode('utf-8'), ownerPHID=form_data.get('assignee'), projectPHIDs=form_data.get('tags'), ) 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.message,)) return data['id']
def raise_error(self, exc): if isinstance(exc, ApiUnauthorized): raise PluginError(ERR_UNAUTHORIZED) elif isinstance(exc, ApiError): raise PluginError('Error Communicating with GitLab (HTTP %s): %s' % ( exc.code, exc.json.get('message', 'unknown error') if exc.json else 'unknown error', )) elif isinstance(exc, PluginError): raise else: self.logger.exception(six.text_type(exc)) raise PluginError(ERR_INTERNAL)
def validate_urls(value, **kwargs): from sentry.http import is_valid_url # Django 1.9 setup issue urls = split_urls(value) if any((not u.startswith(('http://', 'https://')) or not is_valid_url(u)) for u in urls): raise PluginError('Not a valid URL.') return '\n'.join(urls)
def notify_users(self, group, event, fail_silently=False, **kwargs): if not self.is_configured(group.project): return level = event.get_tag("level") if level in ("info", "debug"): message_type = "INFO" if level == "warning": message_type = "WARNING" else: message_type = "CRITICAL" client = self.get_client(group.project) try: response = client.trigger_incident( message_type=message_type, entity_id=group.id, entity_display_name=event.title, state_message=self.build_description(event), timestamp=int(event.datetime.strftime("%s")), issue_url=group.get_absolute_url(), issue_id=group.id, project_id=group.project.id ) except ApiError as e: message = "Could not communicate with victorops. Got %s" % e raise PluginError(message) assert response["result"] == "success"
def notify_users(self, group, event, fail_silently=False, **kwargs): if not self.is_configured(group.project): return level = event.get_tag('level') if level in ('info', 'debug'): message_type = 'INFO' if level == 'warning': message_type = 'WARNING' else: message_type = 'CRITICAL' client = self.get_client(group.project) try: response = client.trigger_incident( message_type=message_type, entity_id=group.id, entity_display_name=event.title, state_message=self.build_description(event), timestamp=int(event.datetime.strftime('%s')), issue_url=group.get_absolute_url(), ) except ApiError as e: message = 'Could not communicate with victorops. Got %s' % e raise PluginError(message) assert response['result'] == 'success'
def validate_config(self, project, config, actor): if int(config["priority"]) == 2 and config["retry"] < 30: retry = str(config["retry"]) self.logger.exception( str(f"Retry not 30 or higher. It is {retry}.")) raise PluginError(f"Retry must be 30 or higher. It is {retry}.") return config
def test_fetch_error_random_exception(self, mock_compare_commits): self.login_as(user=self.user) org = self.create_organization(owner=self.user, name="baz") repo = Repository.objects.create(name="example", provider="dummy", organization_id=org.id) release = Release.objects.create(organization_id=org.id, version="abcabcabc") commit = Commit.objects.create(organization_id=org.id, repository_id=repo.id, key="a" * 40) ReleaseHeadCommit.objects.create( organization_id=org.id, repository_id=repo.id, release=release, commit=commit ) refs = [{"repository": repo.name, "commit": "b" * 40}] release2 = Release.objects.create(organization_id=org.id, version="12345678") UserSocialAuth.objects.create(user=self.user, provider="dummy") mock_compare_commits.side_effect = PluginError("You can read me") with self.tasks(): fetch_commits( release_id=release2.id, user_id=self.user.id, refs=refs, previous_release_id=release.id, ) msg = mail.outbox[-1] assert msg.subject == "Unable to Fetch Commits" assert msg.to == [self.user.email] assert "You can read me" in msg.body
def validate_config(self, project, config, actor): """Validate available config""" try: config['space'] = config['space'] except ValueError as exc: self.logger.exception(six.text_type(exc)) raise PluginError('Invalid space value') return config
def validate_config(self, project, config, actor): super(TrelloCard, self).validate_config(project, config, actor) errors = self.client_errors if errors: self.reset_options(project=project) self.client_errors = [] raise PluginError(errors[0]) return config
def validate_config(self, project, config, actor): if int(config["priority"]) == 2 and config["retry"] < 30: retry = six.binary_type(config["retry"]) self.logger.exception( six.text_type( "Retry not 30 or higher. It is {}.".format(retry))) raise PluginError( "Retry must be 30 or higher. It is {}.".format(retry)) return config
def test_fetch_error_random_exception(self, mock_compare_commits): self.login_as(user=self.user) org = self.create_organization(owner=self.user, name='baz') repo = Repository.objects.create( name='example', provider='dummy', organization_id=org.id, ) release = Release.objects.create( organization_id=org.id, version='abcabcabc', ) commit = Commit.objects.create( organization_id=org.id, repository_id=repo.id, key='a' * 40, ) ReleaseHeadCommit.objects.create( organization_id=org.id, repository_id=repo.id, release=release, commit=commit, ) refs = [{ 'repository': repo.name, 'commit': 'b' * 40, }] release2 = Release.objects.create( organization_id=org.id, version='12345678', ) UserSocialAuth.objects.create( user=self.user, provider='dummy', ) mock_compare_commits.side_effect = PluginError('You can read me') with self.tasks(): fetch_commits( release_id=release2.id, user_id=self.user.id, refs=refs, previous_release_id=release.id, ) msg = mail.outbox[-1] assert msg.subject == 'Unable to Fetch Commits' assert msg.to == [self.user.email] assert 'You can read me' in msg.body
def get_token(self, api_origin, api_secret, corp_id): if (not self.access_token) or self.access_token['expires'] < datetime.now(): data = self.request_token(api_origin, api_secret, corp_id) if data['errcode'] != 0: raise PluginError("invalid wechat token response: %s" % data) self.access_token = { 'token': data['access_token'], 'expires': datetime.now() + timedelta(seconds = data['expires_in']) } return self.access_token['token']
def validate_config(self, project, config, actor): try: config["project"] = int(config["project"]) except ValueError as exc: self.logger.exception(six.text_type(exc)) raise PluginError( "Invalid Project ID. " "Project IDs are numbers-only, and can be found on the Project's page" ) return config
def validate_config(self, project, config, actor=None): sessionstack_client = SessionStackClient( account_email=config.get('account_email'), api_token=config.get('api_token'), website_id=config.get('website_id'), api_url=config.get('api_url'), player_url=config.get('player_url')) try: sessionstack_client.validate_api_access() except UnauthorizedError: raise PluginError(UNAUTHORIZED_ERROR) except InvalidApiUrlError: raise PluginError(INVALID_API_URL_ERROR) except InvalidWebsiteIdError: raise PluginError(INVALID_WEBSITE_ID_ERROR) except Exception: raise PluginError(UNEXPECTED_ERROR) return config
def validate_config(self, project, config, actor): super().validate_config(project, config, actor) self.client_errors = [] for field in self.fields: if field["name"] in ["project_id", "tracker_id", "default_priority"]: if not config[field["name"]]: self.logger.exception(six.text_type("{} required.".format(field["name"]))) self.client_errors.append(field["name"]) if self.client_errors: raise PluginError(", ".join(self.client_errors) + " required.") return config