Fix notifications on pr create ()

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina 2024-08-14 15:08:52 +04:00 committed by GitHub
parent 4ec2d56303
commit 25b890940e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 26 deletions
models/server-time/src
server-plugins
gmail-resources/src
time-resources/src
services/github/pod-github/src/sync

View File

@ -61,7 +61,8 @@ export function createModel (builder: Builder): void {
txMatch: { txMatch: {
_class: core.class.TxCollectionCUD, _class: core.class.TxCollectionCUD,
'tx._class': core.class.TxCreateDoc 'tx._class': core.class.TxCreateDoc
} },
isAsync: true
}) })
builder.createDoc(serverCore.class.Trigger, core.space.Model, { builder.createDoc(serverCore.class.Trigger, core.space.Model, {

View File

@ -109,7 +109,7 @@ export async function sendEmailNotification (
try { try {
const sesURL = getMetadata(serverNotification.metadata.SesUrl) const sesURL = getMetadata(serverNotification.metadata.SesUrl)
if (sesURL === undefined || sesURL === '') { if (sesURL === undefined || sesURL === '') {
ctx.error('Please provide email service url to enable email confirmations.') ctx.error('Please provide email service url to enable email notifications.')
return return
} }
await fetch(concatLink(sesURL, '/send'), { await fetch(concatLink(sesURL, '/send'), {

View File

@ -228,7 +228,7 @@ export async function OnToDoCreate (tx: TxCUD<Doc>, control: TriggerControl): Pr
messageHtml: jsonToMarkup(nodeDoc(nodeParagraph(nodeText(todo.title)))) messageHtml: jsonToMarkup(nodeDoc(nodeParagraph(nodeText(todo.title))))
} }
return await getCommonNotificationTxes( const txes = await getCommonNotificationTxes(
control, control,
object, object,
data, data,
@ -240,6 +240,16 @@ export async function OnToDoCreate (tx: TxCUD<Doc>, control: TriggerControl): Pr
createTx.modifiedOn, createTx.modifiedOn,
notifyResult notifyResult
) )
await control.apply(txes)
const ids = txes.map((it) => it._id)
control.operationContext.derived.targets.notifications = (it) => {
if (ids.includes(it._id)) {
return [receiverInfo.account.email]
}
}
return []
} }
/** /**

View File

@ -506,11 +506,16 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
container.container, container.container,
pullRequestExternal.body pullRequestExternal.body
) )
const op = this.client.apply(generateId())
let createdPullRequest: GithubPullRequest | undefined
await this.ctx.withLog( await this.ctx.withLog(
'create pull request in platform', 'create pull request in platform',
{}, {},
async () => { async () => {
await this.createPullRequest( createdPullRequest = await this.createPullRequest(
op,
info, info,
accountGH, accountGH,
{ {
@ -528,13 +533,17 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
{ url: pullRequestExternal.url } { url: pullRequestExternal.url }
) )
const pullRequestObj = await this.client.findOne(github.class.GithubPullRequest, { const pullRequestObj =
_id: info._id as unknown as Ref<GithubPullRequest> createdPullRequest ??
}) (await this.client.findOne(github.class.GithubPullRequest, {
_id: info._id as unknown as Ref<GithubPullRequest>
}))
if (pullRequestObj !== undefined) { if (pullRequestObj !== undefined) {
await this.todoSync(pullRequestObj, pullRequestExternal, info, account) await this.todoSync(op, pullRequestObj, pullRequestExternal, info, account)
} }
await op.commit()
return { return {
needSync: '', needSync: '',
external: pullRequestExternal, external: pullRequestExternal,
@ -615,10 +624,11 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
async afterSync (existing: Issue, account: Ref<Account>, issueExternal: any, info: DocSyncInfo): Promise<void> { async afterSync (existing: Issue, account: Ref<Account>, issueExternal: any, info: DocSyncInfo): Promise<void> {
const pullRequest = existing as GithubPullRequest const pullRequest = existing as GithubPullRequest
await this.todoSync(pullRequest, issueExternal as PullRequestExternalData, info, account) await this.todoSync(this.client, pullRequest, issueExternal as PullRequestExternalData, info, account)
} }
async todoSync ( async todoSync (
client: TxOperations,
pullRequest: Pick< pullRequest: Pick<
GithubPullRequest, GithubPullRequest,
'_id' | 'identifier' | 'reviewers' | 'title' | 'state' | 'space' | '_class' | 'modifiedBy' '_id' | 'identifier' | 'reviewers' | 'title' | 'state' | 'space' | '_class' | 'modifiedBy'
@ -628,11 +638,11 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
account: Ref<Account> account: Ref<Account>
): Promise<void> { ): Promise<void> {
// Find all todo's related to PR. // Find all todo's related to PR.
const allTodos = await this.client.findAll<GithubTodo>(github.mixin.GithubTodo, { attachedTo: pullRequest._id }) const allTodos = await client.findAll<GithubTodo>(github.mixin.GithubTodo, { attachedTo: pullRequest._id })
// We also need to track deleted Todos, // We also need to track deleted Todos,
const removedTodos: GithubTodo[] = [] const removedTodos: GithubTodo[] = []
const removedTodoOps = await this.client.findAll<TxCollectionCUD<Doc, ToDo>>( const removedTodoOps = await client.findAll<TxCollectionCUD<Doc, ToDo>>(
core.class.TxCollectionCUD, core.class.TxCollectionCUD,
{ {
objectId: pullRequest._id, objectId: pullRequest._id,
@ -644,7 +654,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
const todoIds = removedTodoOps.filter((it) => it.tx._class === core.class.TxCreateDoc).map((it) => it.tx.objectId) const todoIds = removedTodoOps.filter((it) => it.tx._class === core.class.TxCreateDoc).map((it) => it.tx.objectId)
const mixinOps = await this.client.findAll<TxMixin<ToDo, GithubTodo>>( const mixinOps = await client.findAll<TxMixin<ToDo, GithubTodo>>(
core.class.TxMixin, core.class.TxMixin,
{ {
objectId: { $in: todoIds } objectId: { $in: todoIds }
@ -721,7 +731,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
(!hasPending && pendingOrDismissed.get(r) !== undefined) (!hasPending && pendingOrDismissed.get(r) !== undefined)
) { ) {
if (todos.length === 0) { if (todos.length === 0) {
await this.requestReview(pullRequest, external, r, account) await this.requestReview(client, pullRequest, external, r, account)
} }
} }
} }
@ -754,7 +764,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
if (todos.length === 0) { if (todos.length === 0) {
requestedIds.push(c) requestedIds.push(c)
// We do not have todos's create one to solve issue. // We do not have todos's create one to solve issue.
await this.requestFix(pullRequest, external, c, account) await this.requestFix(client, pullRequest, external, c, account)
} }
} }
break break
@ -772,7 +782,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
if (todos.length === 0 && !requestedIds.includes(c)) { if (todos.length === 0 && !requestedIds.includes(c)) {
requestedIds.push(c) requestedIds.push(c)
// We do not have todos's create one to solve issue. // We do not have todos's create one to solve issue.
await this.requestFix(pullRequest, external, c, account) await this.requestFix(client, pullRequest, external, c, account)
} }
} }
} }
@ -798,12 +808,13 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
} }
private async requestReview ( private async requestReview (
client: TxOperations,
pullRequest: Pick<GithubPullRequest, '_id' | 'identifier' | 'space' | '_class' | 'reviewers' | 'title' | 'state'>, pullRequest: Pick<GithubPullRequest, '_id' | 'identifier' | 'space' | '_class' | 'reviewers' | 'title' | 'state'>,
external: PullRequestExternalData, external: PullRequestExternalData,
todoUser: Ref<Person>, todoUser: Ref<Person>,
account: Ref<Account> account: Ref<Account>
): Promise<void> { ): Promise<void> {
const latestTodo = await this.client.findOne( const latestTodo = await client.findOne(
time.class.ToDo, time.class.ToDo,
{ {
user: todoUser, user: todoUser,
@ -813,7 +824,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
sort: { rank: SortingOrder.Ascending } sort: { rank: SortingOrder.Ascending }
} }
) )
const todoId = await this.client.addCollection( const todoId = await client.addCollection(
time.class.ProjectToDo, time.class.ProjectToDo,
time.space.ToDos, time.space.ToDos,
pullRequest._id, pullRequest._id,
@ -833,7 +844,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
undefined, undefined,
account account
) )
await this.client.createMixin( await client.createMixin(
todoId, todoId,
time.class.ToDo, time.class.ToDo,
time.space.ToDos, time.space.ToDos,
@ -847,6 +858,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
} }
private async requestFix ( private async requestFix (
client: TxOperations,
pullRequest: Pick< pullRequest: Pick<
GithubPullRequest, GithubPullRequest,
'_id' | 'identifier' | 'reviewers' | 'title' | 'space' | 'state' | 'space' | '_class' '_id' | 'identifier' | 'reviewers' | 'title' | 'space' | 'state' | 'space' | '_class'
@ -855,7 +867,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
todoUser: Ref<Person>, todoUser: Ref<Person>,
account: Ref<Account> account: Ref<Account>
): Promise<void> { ): Promise<void> {
const latestTodo = await this.client.findOne( const latestTodo = await client.findOne(
time.class.ToDo, time.class.ToDo,
{ {
user: todoUser, user: todoUser,
@ -866,7 +878,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
} }
) )
const todoId = await this.client.addCollection( const todoId = await client.addCollection(
time.class.ProjectToDo, time.class.ProjectToDo,
time.space.ToDos, time.space.ToDos,
pullRequest._id, pullRequest._id,
@ -886,7 +898,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
undefined, undefined,
account account
) )
await this.client.createMixin( await client.createMixin(
todoId, todoId,
time.class.ToDo, time.class.ToDo,
time.space.ToDos, time.space.ToDos,
@ -1124,6 +1136,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
} }
private async createPullRequest ( private async createPullRequest (
client: TxOperations,
info: DocSyncInfo, info: DocSyncInfo,
account: Ref<Account>, account: Ref<Account>,
pullRequestData: GithubPullRequestData & { status: Issue['status'] }, pullRequestData: GithubPullRequestData & { status: Issue['status'] },
@ -1133,8 +1146,8 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
taskType: Ref<TaskType>, taskType: Ref<TaskType>,
repository: GithubIntegrationRepository, repository: GithubIntegrationRepository,
isDescriptionLocked: boolean isDescriptionLocked: boolean
): Promise<void> { ): Promise<GithubPullRequest> {
const lastOne = await this.client.findOne<Issue>( const lastOne = await client.findOne<Issue>(
tracker.class.Issue, tracker.class.Issue,
{ space: prj._id }, { space: prj._id },
{ sort: { rank: SortingOrder.Descending }, limit: 1 } { sort: { rank: SortingOrder.Descending }, limit: 1 }
@ -1176,7 +1189,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
} }
const prId = info._id as unknown as Ref<GithubPullRequest> const prId = info._id as unknown as Ref<GithubPullRequest>
await this.client.addCollection( await client.addCollection(
github.class.GithubPullRequest, github.class.GithubPullRequest,
info.space, info.space,
tracker.ids.NoParent, tracker.ids.NoParent,
@ -1187,7 +1200,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
new Date(pullRequestExternal.createdAt).getTime(), new Date(pullRequestExternal.createdAt).getTime(),
account account
) )
await this.client.createMixin<Issue, GithubIssue>( await client.createMixin<Issue, GithubIssue>(
prId, prId,
github.class.GithubPullRequest, github.class.GithubPullRequest,
info.space, info.space,
@ -1199,7 +1212,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
descriptionLocked: isDescriptionLocked descriptionLocked: isDescriptionLocked
} }
) )
await this.client.createMixin<Issue, Issue>(prId, github.mixin.GithubIssue, prj._id, prj.mixinClass, {}) await client.createMixin<Issue, Issue>(prId, github.mixin.GithubIssue, prj._id, prj.mixinClass, {})
await this.addConnectToMessage( await this.addConnectToMessage(
github.string.PullRequestConnectedActivityInfo, github.string.PullRequestConnectedActivityInfo,
@ -1209,6 +1222,20 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
pullRequestExternal, pullRequestExternal,
repository repository
) )
return {
...value,
_id: prId,
_class: github.class.GithubPullRequest,
space: info.space as any,
attachedTo: tracker.ids.NoParent,
attachedToClass: tracker.class.Issue,
collection: 'subIssues',
modifiedOn: new Date(pullRequestExternal.createdAt).getTime(),
modifiedBy: account,
createdOn: new Date(pullRequestExternal.createdAt).getTime(),
createdBy: account
}
} }
async externalSync ( async externalSync (