def update_repo_data(self, repo: Repository, event: Mapping[str, Any]) -> None: """ Given a webhook payload, update stored repo data if needed. Assumes a 'repository' key in event payload, with certain subkeys. Rework this if that stops being a safe assumption. XXX(meredith): In it's current state, this tends to cause a lot of IntegrityErrors when we try to update the repo. Those would need to be handled should we decided to add this back in. Keeping the method for now, even though it's not currently used. """ name_from_event = event["repository"]["full_name"] url_from_event = event["repository"]["html_url"] if (repo.name != name_from_event or repo.config.get("name") != name_from_event or repo.url != url_from_event): repo.update( name=name_from_event, url=url_from_event, config=dict(repo.config, name=name_from_event), )
def get(self, request): repo = Repository(name='getsentry/sentry') email = repo.generate_delete_fail_email( 'An internal server error occurred') return MailPreview( html_template=email.html_template, text_template=email.template, context=email.context, ).render(request)
def get(self, request): repo = Repository(name='getsentry/sentry') email = repo.generate_delete_fail_email( 'An internal server error occurred' ) return MailPreview( html_template=email.html_template, text_template=email.template, context=email.context, ).render(request)
def get(self, request): def mock_get_provider(self): return DummyRepositoryProvider("dummy") repo = Repository(name="getsentry/sentry", provider="dummy") repo.get_provider = types.MethodType(mock_get_provider, repo) email = repo.generate_delete_fail_email( "An internal server error occurred") return MailPreview(html_template=email.html_template, text_template=email.template, context=email.context).render(request)
def test_repository_external_slug(self): repo = Repository( name='MyFirstProject', url='https://mbittker.visualstudio.com/_git/MyFirstProject/', external_id=self.vsts_external_id) result = self.provider.repository_external_slug(repo) assert result == repo.external_id
def dispatch(self, request, organization, **kwargs): try: config = self.validate_config(organization, request.DATA) except Exception as e: return self.handle_api_error(e) try: result = self.create_repository( organization=organization, data=config, ) except IntegrationError as e: return Response( { 'errors': { '__all__': e.message }, }, status=400 ) try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, integration_id=result.get('integration_id'), ) except IntegrityError: # Try to delete webhook we just created try: repo = Repository( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, integration_id=result.get('integration_id'), ) self.delete_repository(repo) except IntegrationError: pass return Response( {'errors': {'__all__': 'A repository with that name already exists'}}, status=400, ) else: repo_linked.send_robust(repo=repo, user=request.user, sender=self.__class__) analytics.record( 'integration.repo.added', provider=self.id, id=result.get('integration_id'), organization_id=organization.id, ) return Response(serialize(repo, request.user), status=201)
def dispatch(self, request, organization, **kwargs): from sentry.integrations.exceptions import IntegrationError # Django 1.9 setup issue from sentry.models import Repository # Django 1.9 setup issue try: config = self.get_repository_data(organization, request.data) result = self.build_repository_config( organization=organization, data=config, ) except Exception as e: return self.handle_api_error(e) try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, integration_id=result.get('integration_id'), ) except IntegrityError: # Try to delete webhook we just created try: repo = Repository( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, integration_id=result.get('integration_id'), ) self.on_delete_repository(repo) except IntegrationError: pass return Response( { 'errors': { '__all__': 'A repository with that name already exists' } }, status=400, ) else: repo_linked.send_robust(repo=repo, user=request.user, sender=self.__class__) analytics.record( 'integration.repo.added', provider=self.id, id=result.get('integration_id'), organization_id=organization.id, ) from sentry.api.serializers import serialize # Django 1.9 setup issue return Response(serialize(repo, request.user), status=201)
def get(self, request): release = Release(version="abcdef") repo = Repository(name="repo_name") email = generate_fetch_commits_error_email( release, repo, "An internal server error occurred") return MailPreview(html_template=email.html_template, text_template=email.template, context=email.context).render(request)
def get(self, request: Request) -> Response: org = Organization(slug="myorg") release = Release(version="abcdef", organization=org) repo = Repository(name="repo_name") email = generate_fetch_commits_error_email( release, repo, "An internal server error occurred" ) return MailPreview( html_template=email.html_template, text_template=email.template, context=email.context ).render(request)
def dispatch(self, request, organization, **kwargs): try: config = self.get_repository_data(organization, request.data) result = self.build_repository_config(organization=organization, data=config) except Exception as e: return self.handle_api_error(e) try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result["name"], external_id=result.get("external_id"), url=result.get("url"), config=result.get("config") or {}, provider=self.id, integration_id=result.get("integration_id"), ) except IntegrityError: # Try to delete webhook we just created try: repo = Repository( organization_id=organization.id, name=result["name"], external_id=result.get("external_id"), url=result.get("url"), config=result.get("config") or {}, provider=self.id, integration_id=result.get("integration_id"), ) self.on_delete_repository(repo) except IntegrationError: pass return Response( { "errors": { "__all__": "A repository with that name already exists" } }, status=400) else: repo_linked.send_robust(repo=repo, user=request.user, sender=self.__class__) analytics.record( "integration.repo.added", provider=self.id, id=result.get("integration_id"), organization_id=organization.id, ) return Response(serialize(repo, request.user), status=201)
def dispatch(self, request, organization, **kwargs): try: config = self.get_repository_data(organization, request.data) result = self.build_repository_config(organization=organization, data=config) except Exception as e: return self.handle_api_error(e) repo_update_params = { "external_id": result.get("external_id"), "url": result.get("url"), "config": result.get("config") or {}, "provider": self.id, "integration_id": result.get("integration_id"), } # first check if there is a repository without an integration that matches repo = Repository.objects.filter(organization_id=organization.id, name=result["name"], integration_id=None).first() if repo: if self.logger: self.logger.info( "repository.update", extra={ "organization_id": organization.id, "repo_name": result["name"], "old_provider": repo.provider, }, ) # update from params for field_name, field_value in six.iteritems(repo_update_params): setattr(repo, field_name, field_value) # also update the status if it was in a bad state repo.status = ObjectStatus.VISIBLE repo.save() else: try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result["name"], **repo_update_params) except IntegrityError: # Try to delete webhook we just created try: repo = Repository(organization_id=organization.id, name=result["name"], **repo_update_params) self.on_delete_repository(repo) except IntegrationError: pass return Response( { "errors": { "__all__": "A repository with that name already exists" } }, status=400, ) else: repo_linked.send_robust(repo=repo, user=request.user, sender=self.__class__) analytics.record( "integration.repo.added", provider=self.id, id=result.get("integration_id"), organization_id=organization.id, ) return Response(serialize(repo, request.user), status=201)
def dispatch(self, request, organization, **kwargs): if self.needs_auth(request.user): # TODO(dcramer): this should be a 401 return Response( { 'error_type': 'auth', 'auth_url': reverse('socialauth_associate', args=[self.auth_provider]), }, status=400 ) try: fields = self.get_config() except Exception as e: return self.handle_api_error(e) if request.method == 'GET': return Response(fields) validator = ConfigValidator(fields, request.data) if not validator.is_valid(): return Response( { 'error_type': 'validation', 'errors': validator.errors, }, status=400 ) try: config = self.validate_config(organization, validator.result, actor=request.user) except Exception as e: return self.handle_api_error(e) try: result = self.create_repository( organization=organization, data=config, actor=request.user, ) except PluginError as e: logger.exception('repo.create-error') return Response( { 'errors': { '__all__': e.message }, }, status=400 ) try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, ) except IntegrityError: # Try to delete webhook we just created try: repo = Repository( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, ) self.delete_repository(repo, actor=request.user) except PluginError: pass return Response( {'errors': {'__all__': 'A repository with that name already exists'}}, status=400, ) else: repo_linked.send_robust(repo=repo, user=request.user, sender=self.__class__) return Response(serialize(repo, request.user), status=201)
def dispatch(self, request, organization, **kwargs): if self.needs_auth(request.user): # TODO(dcramer): this should be a 401 return Response( { "error_type": "auth", "auth_url": reverse("socialauth_associate", args=[self.auth_provider]), }, status=400, ) try: fields = self.get_config() except Exception as e: return self.handle_api_error(e) if request.method == "GET": return Response(fields) validator = ConfigValidator(fields, request.data) if not validator.is_valid(): return Response( { "error_type": "validation", "errors": validator.errors }, status=400) try: config = self.validate_config(organization, validator.result, actor=request.user) except Exception as e: return self.handle_api_error(e) try: result = self.create_repository(organization=organization, data=config, actor=request.user) except PluginError as e: logger.exception("repo.create-error") return Response({"errors": {"__all__": str(e)}}, status=400) try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result["name"], external_id=result.get("external_id"), url=result.get("url"), config=result.get("config") or {}, provider=self.id, ) except IntegrityError: # Try to delete webhook we just created try: repo = Repository( organization_id=organization.id, name=result["name"], external_id=result.get("external_id"), url=result.get("url"), config=result.get("config") or {}, provider=self.id, ) self.delete_repository(repo, actor=request.user) except PluginError: pass return Response( { "errors": { "__all__": "A repository with that name already exists" } }, status=400) else: repo_linked.send_robust(repo=repo, user=request.user, sender=self.__class__) return Response(serialize(repo, request.user), status=201)
def dispatch(self, request, organization, **kwargs): try: fields = self.get_config(organization) except Exception as e: return self.handle_api_error(e) if request.method == 'GET': return Response(fields) validator = ConfigValidator(fields, request.DATA) if not validator.is_valid(): return Response( { 'error_type': 'validation', 'errors': validator.errors, }, status=400 ) try: config = self.validate_config(organization, validator.result, actor=request.user) except Exception as e: return self.handle_api_error(e) try: result = self.create_repository( organization=organization, data=config, actor=request.user, ) except PluginError as e: return Response( { 'errors': { '__all__': e.message }, }, status=400 ) try: with transaction.atomic(): repo = Repository.objects.create( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, ) except IntegrityError: # Try to delete webhook we just created try: repo = Repository( organization_id=organization.id, name=result['name'], external_id=result.get('external_id'), url=result.get('url'), config=result.get('config') or {}, provider=self.id, ) self.delete_repository(repo, actor=request.user) except PluginError: pass return Response( {'errors': {'__all__': 'A repository with that name already exists'}}, status=400, ) return Response(serialize(repo, request.user), status=201)