def reset_number_of_failures_to_zero(notification_id): """ This resets the number of failures for a repo notification to 0. """ RepositoryNotification.update(number_of_failures=0).where( RepositoryNotification.id == notification_id ).execute()
def create_repo_notification(repo, event_name, method_name, method_config, event_config, title=None): event = ExternalNotificationEvent.get(ExternalNotificationEvent.name == event_name) method = ExternalNotificationMethod.get(ExternalNotificationMethod.name == method_name) return RepositoryNotification.create(repository=repo, event=event, method=method, config_json=json.dumps(method_config), title=title, event_config_json=json.dumps(event_config))
def increment_notification_failure_count(uuid): """ This increments the number of failures by one. """ (RepositoryNotification.update( number_of_failures=RepositoryNotification.number_of_failures + 1).where(RepositoryNotification.uuid == uuid).execute())
def _base_get_notification(uuid): """ This is a base query for get statements. """ return (RepositoryNotification.select( RepositoryNotification, Repository, Namespace).join(Repository).join( Namespace, on=(Repository.namespace_user == Namespace.id)).where( RepositoryNotification.uuid == uuid))
def reset_notification_number_of_failures(namespace_name, repository_name, uuid): """ This resets the number of failures for a repo notification to 0 """ try: notification = RepositoryNotification.select().where(RepositoryNotification.uuid == uuid).get() if (notification.repository.namespace_user.username != namespace_name or notification.repository.name != repository_name): raise InvalidNotificationException('No repository notification found with uuid: %s' % uuid) reset_number_of_failures_to_zero(notification.id) return notification except RepositoryNotification.DoesNotExist: return None
def lookup_notifiable_tags_for_legacy_image(docker_image_id, storage_uuid, event_name): """ Yields any alive Tags found in repositories with an event with the given name registered and whose legacy Image has the given docker image ID and storage UUID. """ event = ExternalNotificationEvent.get(name=event_name) images = (Image.select().join(ImageStorage).where( Image.docker_image_id == docker_image_id, ImageStorage.uuid == storage_uuid)) for image in list(images): # Ensure the image is under a repository that supports the event. try: RepositoryNotification.get(repository=image.repository_id, event=event) except RepositoryNotification.DoesNotExist: continue # If found in a repository with the valid event, yield the tag(s) that contains the image. for tag in tags_containing_legacy_image(image): yield tag
def list_repo_notifications(namespace_name, repository_name, event_name=None): query = (RepositoryNotification.select( RepositoryNotification, Repository, Namespace).join(Repository).join( Namespace, on=(Repository.namespace_user == Namespace.id)).where( Namespace.username == namespace_name, Repository.name == repository_name)) if event_name: query = (query.switch(RepositoryNotification).join( ExternalNotificationEvent).where( ExternalNotificationEvent.name == event_name)) return query
def lookup_secscan_notification_severities(repository_id): """ Returns the configured security scanner notification severities for the repository or None if none. """ try: repo = Repository.get(id=repository_id) except Repository.DoesNotExist: return None event_kind = ExternalNotificationEvent.get(name="vulnerability_found") for event in RepositoryNotification.select().where( RepositoryNotification.repository == repository_id, RepositoryNotification.event == event_kind, ): severity = json.loads(event.event_config_json).get( "vulnerability", {}).get("priority") if severity: yield severity