refactor: split integration route from feed route

This commit is contained in:
moonrailgun 2024-06-29 09:13:50 +08:00
parent 765cc41c06
commit a4c31fe2da
2 changed files with 150 additions and 140 deletions

View File

@ -5,137 +5,16 @@ import {
router,
workspaceOwnerProcedure,
workspaceProcedure,
} from '../trpc';
import { OPENAPI_TAG } from '../../utils/const';
} from '../../trpc';
import { OPENAPI_TAG } from '../../../utils/const';
import { OpenApiMeta } from 'trpc-openapi';
import { FeedChannelModelSchema, FeedEventModelSchema } from '../../prisma/zod';
import { prisma } from '../../model/_client';
import {
FeedChannelModelSchema,
FeedEventModelSchema,
} from '../../../prisma/zod';
import { prisma } from '../../../model/_client';
import _ from 'lodash';
import { subscribeEventBus } from '../../ws/shared';
export const feedIntegrationRouter = router({
github: publicProcedure
.meta(
buildFeedPublicOpenapi({
method: 'POST',
path: '/{channelId}/github',
})
)
.input(
z
.object({
channelId: z.string(),
})
.passthrough()
)
.output(z.string())
.mutation(async ({ input, ctx }) => {
const eventType = ctx.req.headers['x-github-event'];
const { channelId, ...data } = input;
const workspaceId = await prisma.feedChannel
.findFirst({
where: {
id: channelId,
},
select: {
workspaceId: true,
},
})
.then((res) => res?.workspaceId);
if (!workspaceId) {
return 'Not found';
}
if (eventType === 'push') {
const pusher = `${_.get(data, 'pusher.name')}<${_.get(data, 'pusher.email')}>`;
const commits = _.map(_.get(data, 'commits') as any[], 'id').join(', ');
const fullName = _.get(data, 'repository.full_name');
const ref = String(_.get(data, 'ref'));
const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'compare'));
const event = await prisma.feedEvent.create({
data: {
channelId: channelId,
eventName: eventType,
eventContent: `${pusher} push commit ${commits} to [${ref}] in ${fullName}`,
tags: [],
source: 'github',
senderId,
senderName,
important: false,
url,
},
});
subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event);
return 'ok';
} else if (eventType === 'star') {
const starCount = _.get(data, 'repository.stargazers_count');
const fullName = _.get(data, 'repository.full_name');
const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'compare'));
const event = await prisma.feedEvent.create({
data: {
channelId: channelId,
eventName: eventType,
eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`,
tags: [],
source: 'github',
senderId,
senderName,
important: false,
url,
},
});
subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event);
return 'ok';
} else if (eventType === 'issues') {
const action = _.get(data, 'action') as 'opened' | 'closed';
const starCount = _.get(data, 'repository.stargazers_count');
const fullName = _.get(data, 'repository.full_name');
const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'issue.url'));
const title = String(_.get(data, 'issue.title'));
let eventName = eventType;
let eventContent = '';
if (action === 'opened') {
eventName = 'open_issue';
eventContent = `${senderName} open issue [${title}] in repo [${fullName}]`;
} else if (action === 'closed') {
eventName = 'close_issue';
eventContent = `${senderName} close issue [${title}] in repo [${fullName}]`;
}
if (eventContent) {
const event = await prisma.feedEvent.create({
data: {
channelId: channelId,
eventName: eventName,
eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`,
tags: [],
source: 'github',
senderId,
senderName,
important: false,
url,
},
});
subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event);
return 'ok';
}
}
return 'not supported';
}),
});
import { buildFeedPublicOpenapi, feedIntegrationRouter } from './integration';
export const feedRouter = router({
channels: workspaceProcedure
@ -360,14 +239,3 @@ function buildFeedOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {
},
};
}
function buildFeedPublicOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {
return {
openapi: {
tags: [OPENAPI_TAG.FEED],
protect: false,
...meta,
path: `/feed${meta.path}`,
},
};
}

View File

@ -0,0 +1,142 @@
import { z } from 'zod';
import { OpenApiMetaInfo, publicProcedure, router } from '../../trpc';
import { prisma } from '../../../model/_client';
import _ from 'lodash';
import { subscribeEventBus } from '../../../ws/shared';
import { OpenApiMeta } from 'trpc-openapi';
import { OPENAPI_TAG } from '../../../utils/const';
export const feedIntegrationRouter = router({
github: publicProcedure
.meta(
buildFeedPublicOpenapi({
method: 'POST',
path: '/{channelId}/github',
})
)
.input(
z
.object({
channelId: z.string(),
})
.passthrough()
)
.output(z.string())
.mutation(async ({ input, ctx }) => {
const eventType = ctx.req.headers['x-github-event'];
const { channelId, ...data } = input;
const workspaceId = await prisma.feedChannel
.findFirst({
where: {
id: channelId,
},
select: {
workspaceId: true,
},
})
.then((res) => res?.workspaceId);
if (!workspaceId) {
return 'Not found';
}
if (eventType === 'push') {
const pusher = `${_.get(data, 'pusher.name')}<${_.get(data, 'pusher.email')}>`;
const commits = _.map(_.get(data, 'commits') as any[], 'id').join(', ');
const fullName = _.get(data, 'repository.full_name');
const ref = String(_.get(data, 'ref'));
const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'compare'));
const event = await prisma.feedEvent.create({
data: {
channelId: channelId,
eventName: eventType,
eventContent: `${pusher} push commit ${commits} to [${ref}] in ${fullName}`,
tags: [],
source: 'github',
senderId,
senderName,
important: false,
url,
},
});
subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event);
return 'ok';
} else if (eventType === 'star') {
const starCount = _.get(data, 'repository.stargazers_count');
const fullName = _.get(data, 'repository.full_name');
const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'compare'));
const event = await prisma.feedEvent.create({
data: {
channelId: channelId,
eventName: eventType,
eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`,
tags: [],
source: 'github',
senderId,
senderName,
important: false,
url,
},
});
subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event);
return 'ok';
} else if (eventType === 'issues') {
const action = _.get(data, 'action') as 'opened' | 'closed';
const starCount = _.get(data, 'repository.stargazers_count');
const fullName = _.get(data, 'repository.full_name');
const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'issue.url'));
const title = String(_.get(data, 'issue.title'));
let eventName = eventType;
let eventContent = '';
if (action === 'opened') {
eventName = 'open_issue';
eventContent = `${senderName} open issue [${title}] in repo [${fullName}]`;
} else if (action === 'closed') {
eventName = 'close_issue';
eventContent = `${senderName} close issue [${title}] in repo [${fullName}]`;
}
if (eventContent) {
const event = await prisma.feedEvent.create({
data: {
channelId: channelId,
eventName: eventName,
eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`,
tags: [],
source: 'github',
senderId,
senderName,
important: false,
url,
},
});
subscribeEventBus.emit('onReceiveFeedEvent', workspaceId, event);
return 'ok';
}
}
return 'not supported';
}),
});
export function buildFeedPublicOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {
return {
openapi: {
tags: [OPENAPI_TAG.FEED],
protect: false,
...meta,
path: `/feed${meta.path}`,
},
};
}