Skip to content

Commit

Permalink
fix: only create createHookTask if hook enable (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Aug 26, 2022
1 parent 3ed5269 commit 4e8700c
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions app/core/event/ChangesStream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EggAppConfig } from 'egg';
import { Event, Inject } from '@eggjs/tegg';
import {
PACKAGE_UNPUBLISHED,
Expand All @@ -24,6 +25,13 @@ class ChangesStreamEvent {
@Inject()
protected readonly taskService: TaskService;

@Inject()
protected readonly config: EggAppConfig;

protected get hookEnable() {
return this.config.hookEnable;
}

protected async addChange(type: string, fullname: string, data: object): Promise<Change> {
const change = Change.create({
type,
Expand All @@ -39,53 +47,65 @@ class ChangesStreamEvent {
export class PackageUnpublished extends ChangesStreamEvent {
async handle(fullname: string) {
const change = await this.addChange(PACKAGE_UNPUBLISHED, fullname, {});
const task = Task.createCreateHookTask(HookEvent.createUnpublishEvent(fullname, change.changeId));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createUnpublishEvent(fullname, change.changeId));
await this.taskService.createTask(task, true);
}
}
}

@Event(PACKAGE_VERSION_ADDED)
export class PackageVersionAdded extends ChangesStreamEvent {
async handle(fullname: string, version: string, tag?: string) {
const change = await this.addChange(PACKAGE_VERSION_ADDED, fullname, { version });
const task = Task.createCreateHookTask(HookEvent.createPublishEvent(fullname, change.changeId, version, tag));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createPublishEvent(fullname, change.changeId, version, tag));
await this.taskService.createTask(task, true);
}
}
}

@Event(PACKAGE_VERSION_REMOVED)
export class PackageVersionRemoved extends ChangesStreamEvent {
async handle(fullname: string, version: string, tag?: string) {
const change = await this.addChange(PACKAGE_VERSION_REMOVED, fullname, { version });
const task = Task.createCreateHookTask(HookEvent.createUnpublishEvent(fullname, change.changeId, version, tag));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createUnpublishEvent(fullname, change.changeId, version, tag));
await this.taskService.createTask(task, true);
}
}
}

@Event(PACKAGE_TAG_ADDED)
export class PackageTagAdded extends ChangesStreamEvent {
async handle(fullname: string, tag: string) {
const change = await this.addChange(PACKAGE_TAG_ADDED, fullname, { tag });
const task = Task.createCreateHookTask(HookEvent.createDistTagEvent(fullname, change.changeId, tag));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createDistTagEvent(fullname, change.changeId, tag));
await this.taskService.createTask(task, true);
}
}
}

@Event(PACKAGE_TAG_CHANGED)
export class PackageTagChanged extends ChangesStreamEvent {
async handle(fullname: string, tag: string) {
const change = await this.addChange(PACKAGE_TAG_CHANGED, fullname, { tag });
const task = Task.createCreateHookTask(HookEvent.createDistTagEvent(fullname, change.changeId, tag));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createDistTagEvent(fullname, change.changeId, tag));
await this.taskService.createTask(task, true);
}
}
}

@Event(PACKAGE_TAG_REMOVED)
export class PackageTagRemoved extends ChangesStreamEvent {
async handle(fullname: string, tag: string) {
const change = await this.addChange(PACKAGE_TAG_REMOVED, fullname, { tag });
const task = Task.createCreateHookTask(HookEvent.createDistTagRmEvent(fullname, change.changeId, tag));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createDistTagRmEvent(fullname, change.changeId, tag));
await this.taskService.createTask(task, true);
}
}
}

Expand All @@ -94,9 +114,11 @@ export class PackageMaintainerChanged extends ChangesStreamEvent {
async handle(fullname: string, maintainers: User[]) {
const change = await this.addChange(PACKAGE_MAINTAINER_CHANGED, fullname, {});
// TODO 应该比较差值,而不是全量推送
for (const maintainer of maintainers) {
const task = Task.createCreateHookTask(HookEvent.createOwnerEvent(fullname, change.changeId, maintainer.name));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
for (const maintainer of maintainers) {
const task = Task.createCreateHookTask(HookEvent.createOwnerEvent(fullname, change.changeId, maintainer.name));
await this.taskService.createTask(task, true);
}
}
}
}
Expand All @@ -105,8 +127,10 @@ export class PackageMaintainerChanged extends ChangesStreamEvent {
export class PackageMaintainerRemoved extends ChangesStreamEvent {
async handle(fullname: string, maintainer: string) {
const change = await this.addChange(PACKAGE_MAINTAINER_REMOVED, fullname, { maintainer });
const task = Task.createCreateHookTask(HookEvent.createOwnerRmEvent(fullname, change.changeId, maintainer));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
const task = Task.createCreateHookTask(HookEvent.createOwnerRmEvent(fullname, change.changeId, maintainer));
await this.taskService.createTask(task, true);
}
}
}

Expand All @@ -115,9 +139,11 @@ export class PackageMetaChanged extends ChangesStreamEvent {
async handle(fullname: string, meta: PackageMetaChange) {
const change = await this.addChange(PACKAGE_META_CHANGED, fullname, { ...meta });
const { deprecateds } = meta;
for (const deprecated of deprecateds || []) {
const task = Task.createCreateHookTask(HookEvent.createDeprecatedEvent(fullname, change.changeId, deprecated.version));
await this.taskService.createTask(task, true);
if (this.hookEnable) {
for (const deprecated of deprecateds || []) {
const task = Task.createCreateHookTask(HookEvent.createDeprecatedEvent(fullname, change.changeId, deprecated.version));
await this.taskService.createTask(task, true);
}
}
}
}

0 comments on commit 4e8700c

Please sign in to comment.