mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	hack: external events
This commit is contained in:
		
							parent
							
								
									fa9d38fc22
								
							
						
					
					
						commit
						419a8c51fe
					
				@ -85,12 +85,12 @@ export interface IEventTable {
 | 
				
			|||||||
const TABLE = 'events';
 | 
					const TABLE = 'events';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EventStore implements IEventStore {
 | 
					class EventStore implements IEventStore {
 | 
				
			||||||
    private db: Db;
 | 
					    protected db: Db;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // only one shared event emitter should exist across all event store instances
 | 
					    // only one shared event emitter should exist across all event store instances
 | 
				
			||||||
    private eventEmitter: EventEmitter = sharedEventEmitter;
 | 
					    private eventEmitter: EventEmitter = sharedEventEmitter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private logger: Logger;
 | 
					    protected logger: Logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // a new DB has to be injected per transaction
 | 
					    // a new DB has to be injected per transaction
 | 
				
			||||||
    constructor(db: Db, getLogger: LogProvider) {
 | 
					    constructor(db: Db, getLogger: LogProvider) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,5 @@
 | 
				
			|||||||
import { FromSchema } from 'json-schema-to-ts';
 | 
					import { FromSchema } from 'json-schema-to-ts';
 | 
				
			||||||
import { tagSchema } from './tag-schema';
 | 
					import { tagSchema } from './tag-schema';
 | 
				
			||||||
import { IEventTypes } from '../../types';
 | 
					 | 
				
			||||||
import { variantSchema } from './variant-schema';
 | 
					import { variantSchema } from './variant-schema';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const eventDataSchema = {
 | 
					const eventDataSchema = {
 | 
				
			||||||
@ -44,7 +43,6 @@ export const eventSchema = {
 | 
				
			|||||||
            type: 'string',
 | 
					            type: 'string',
 | 
				
			||||||
            description:
 | 
					            description:
 | 
				
			||||||
                'What [type](https://docs.getunleash.io/reference/api/legacy/unleash/admin/events#event-type-description) of event this is',
 | 
					                'What [type](https://docs.getunleash.io/reference/api/legacy/unleash/admin/events#event-type-description) of event this is',
 | 
				
			||||||
            enum: IEventTypes,
 | 
					 | 
				
			||||||
            example: 'feature-created',
 | 
					            example: 'feature-created',
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        createdBy: {
 | 
					        createdBy: {
 | 
				
			||||||
 | 
				
			|||||||
@ -10,7 +10,7 @@ import { ITag } from '../types';
 | 
				
			|||||||
export default class EventService {
 | 
					export default class EventService {
 | 
				
			||||||
    private logger: Logger;
 | 
					    private logger: Logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private eventStore: IEventStore;
 | 
					    protected eventStore: IEventStore;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private featureTagStore: IFeatureTagStore;
 | 
					    private featureTagStore: IFeatureTagStore;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -51,7 +51,7 @@ export default class EventService {
 | 
				
			|||||||
        return this.eventStore.on(eventName, listener);
 | 
					        return this.eventStore.on(eventName, listener);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private async enhanceEventsWithTags(
 | 
					    protected async enhanceEventsWithTags(
 | 
				
			||||||
        events: IBaseEvent[],
 | 
					        events: IBaseEvent[],
 | 
				
			||||||
    ): Promise<IBaseEvent[]> {
 | 
					    ): Promise<IBaseEvent[]> {
 | 
				
			||||||
        const featureNamesSet = new Set<string>();
 | 
					        const featureNamesSet = new Set<string>();
 | 
				
			||||||
 | 
				
			|||||||
@ -173,6 +173,14 @@ export const BANNER_CREATED = 'banner-created' as const;
 | 
				
			|||||||
export const BANNER_UPDATED = 'banner-updated' as const;
 | 
					export const BANNER_UPDATED = 'banner-updated' as const;
 | 
				
			||||||
export const BANNER_DELETED = 'banner-deleted' as const;
 | 
					export const BANNER_DELETED = 'banner-deleted' as const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const EVENT_WEBHOOK_CREATED = 'event-webhook-created' as const;
 | 
				
			||||||
 | 
					export const EVENT_WEBHOOK_UPDATED = 'event-webhook-updated' as const;
 | 
				
			||||||
 | 
					export const EVENT_WEBHOOK_DELETED = 'event-webhook-deleted' as const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const EVENT_ACTION_CREATED = 'event-action-created' as const;
 | 
				
			||||||
 | 
					export const EVENT_ACTION_UPDATED = 'event-action-updated' as const;
 | 
				
			||||||
 | 
					export const EVENT_ACTION_DELETED = 'event-action-deleted' as const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const IEventTypes = [
 | 
					export const IEventTypes = [
 | 
				
			||||||
    APPLICATION_CREATED,
 | 
					    APPLICATION_CREATED,
 | 
				
			||||||
    FEATURE_CREATED,
 | 
					    FEATURE_CREATED,
 | 
				
			||||||
@ -306,6 +314,12 @@ export const IEventTypes = [
 | 
				
			|||||||
    PROJECT_ENVIRONMENT_ADDED,
 | 
					    PROJECT_ENVIRONMENT_ADDED,
 | 
				
			||||||
    PROJECT_ENVIRONMENT_REMOVED,
 | 
					    PROJECT_ENVIRONMENT_REMOVED,
 | 
				
			||||||
    DEFAULT_STRATEGY_UPDATED,
 | 
					    DEFAULT_STRATEGY_UPDATED,
 | 
				
			||||||
 | 
					    EVENT_WEBHOOK_CREATED,
 | 
				
			||||||
 | 
					    EVENT_WEBHOOK_UPDATED,
 | 
				
			||||||
 | 
					    EVENT_WEBHOOK_DELETED,
 | 
				
			||||||
 | 
					    EVENT_ACTION_CREATED,
 | 
				
			||||||
 | 
					    EVENT_ACTION_UPDATED,
 | 
				
			||||||
 | 
					    EVENT_ACTION_DELETED,
 | 
				
			||||||
] as const;
 | 
					] as const;
 | 
				
			||||||
export type IEventType = (typeof IEventTypes)[number];
 | 
					export type IEventType = (typeof IEventTypes)[number];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								src/migrations/20231205122703-external-events.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/migrations/20231205122703-external-events.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					exports.up = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					        ALTER table events
 | 
				
			||||||
 | 
					                ADD COLUMN IF NOT EXISTS is_external boolean DEFAULT false
 | 
				
			||||||
 | 
					        `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.down = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					        ALTER table events
 | 
				
			||||||
 | 
					                DROP COLUMN is_external
 | 
				
			||||||
 | 
					        `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
							
								
								
									
										27
									
								
								src/migrations/20231205155322-event-webhooks.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/migrations/20231205155322-event-webhooks.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.up = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					        CREATE TABLE IF NOT EXISTS event_webhooks
 | 
				
			||||||
 | 
					            (
 | 
				
			||||||
 | 
					                id SERIAL PRIMARY KEY NOT NULL,
 | 
				
			||||||
 | 
					                enabled BOOLEAN DEFAULT true NOT NULL,
 | 
				
			||||||
 | 
					                name TEXT NOT NULL,
 | 
				
			||||||
 | 
					                event TEXT NOT NULL,
 | 
				
			||||||
 | 
					                url TEXT NOT NULL,
 | 
				
			||||||
 | 
					                created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					        `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.down = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					        DROP TABLE IF EXISTS event_webhooks;
 | 
				
			||||||
 | 
					        `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
							
								
								
									
										25
									
								
								src/migrations/20231205155331-event-actions.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/migrations/20231205155331-event-actions.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.up = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					        CREATE TABLE IF NOT EXISTS event_actions
 | 
				
			||||||
 | 
					            (
 | 
				
			||||||
 | 
					                id SERIAL PRIMARY KEY NOT NULL,
 | 
				
			||||||
 | 
					                event TEXT NOT NULL,
 | 
				
			||||||
 | 
					                action TEXT NOT NULL,
 | 
				
			||||||
 | 
					                parameters JSONB NOT NULL DEFAULT '{}'::jsonb
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					        `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.down = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					        DROP TABLE IF EXISTS event_actions;
 | 
				
			||||||
 | 
					        `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
							
								
								
									
										2
									
								
								src/test/fixtures/fake-event-store.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								src/test/fixtures/fake-event-store.ts
									
									
									
									
										vendored
									
									
								
							@ -8,7 +8,7 @@ import EventEmitter from 'events';
 | 
				
			|||||||
class FakeEventStore implements IEventStore {
 | 
					class FakeEventStore implements IEventStore {
 | 
				
			||||||
    events: IEvent[];
 | 
					    events: IEvent[];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private eventEmitter: EventEmitter = sharedEventEmitter;
 | 
					    protected eventEmitter: EventEmitter = sharedEventEmitter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructor() {
 | 
					    constructor() {
 | 
				
			||||||
        this.eventEmitter.setMaxListeners(0);
 | 
					        this.eventEmitter.setMaxListeners(0);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user