Fix notifications on pr create (#6332)

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

View File

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

View File

@ -109,7 +109,7 @@ export async function sendEmailNotification (
try {
const sesURL = getMetadata(serverNotification.metadata.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
}
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))))
}
return await getCommonNotificationTxes(
const txes = await getCommonNotificationTxes(
control,
object,
data,
@ -240,6 +240,16 @@ export async function OnToDoCreate (tx: TxCUD<Doc>, control: TriggerControl): Pr
createTx.modifiedOn,
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,
pullRequestExternal.body
)
const op = this.client.apply(generateId())
let createdPullRequest: GithubPullRequest | undefined
await this.ctx.withLog(
'create pull request in platform',
{},
async () => {
await this.createPullRequest(
createdPullRequest = await this.createPullRequest(
op,
info,
accountGH,
{
@ -528,13 +533,17 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
{ url: pullRequestExternal.url }
)
const pullRequestObj = await this.client.findOne(github.class.GithubPullRequest, {
_id: info._id as unknown as Ref<GithubPullRequest>
})
const pullRequestObj =
createdPullRequest ??
(await this.client.findOne(github.class.GithubPullRequest, {
_id: info._id as unknown as Ref<GithubPullRequest>
}))
if (pullRequestObj !== undefined) {
await this.todoSync(pullRequestObj, pullRequestExternal, info, account)
await this.todoSync(op, pullRequestObj, pullRequestExternal, info, account)
}
await op.commit()
return {
needSync: '',
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> {
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 (
client: TxOperations,
pullRequest: Pick<
GithubPullRequest,
'_id' | 'identifier' | 'reviewers' | 'title' | 'state' | 'space' | '_class' | 'modifiedBy'
@ -628,11 +638,11 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
account: Ref<Account>
): Promise<void> {
// 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,
const removedTodos: GithubTodo[] = []
const removedTodoOps = await this.client.findAll<TxCollectionCUD<Doc, ToDo>>(
const removedTodoOps = await client.findAll<TxCollectionCUD<Doc, ToDo>>(
core.class.TxCollectionCUD,
{
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 mixinOps = await this.client.findAll<TxMixin<ToDo, GithubTodo>>(
const mixinOps = await client.findAll<TxMixin<ToDo, GithubTodo>>(
core.class.TxMixin,
{
objectId: { $in: todoIds }
@ -721,7 +731,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
(!hasPending && pendingOrDismissed.get(r) !== undefined)
) {
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) {
requestedIds.push(c)
// 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
@ -772,7 +782,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
if (todos.length === 0 && !requestedIds.includes(c)) {
requestedIds.push(c)
// 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 (
client: TxOperations,
pullRequest: Pick<GithubPullRequest, '_id' | 'identifier' | 'space' | '_class' | 'reviewers' | 'title' | 'state'>,
external: PullRequestExternalData,
todoUser: Ref<Person>,
account: Ref<Account>
): Promise<void> {
const latestTodo = await this.client.findOne(
const latestTodo = await client.findOne(
time.class.ToDo,
{
user: todoUser,
@ -813,7 +824,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
sort: { rank: SortingOrder.Ascending }
}
)
const todoId = await this.client.addCollection(
const todoId = await client.addCollection(
time.class.ProjectToDo,
time.space.ToDos,
pullRequest._id,
@ -833,7 +844,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
undefined,
account
)
await this.client.createMixin(
await client.createMixin(
todoId,
time.class.ToDo,
time.space.ToDos,
@ -847,6 +858,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
}
private async requestFix (
client: TxOperations,
pullRequest: Pick<
GithubPullRequest,
'_id' | 'identifier' | 'reviewers' | 'title' | 'space' | 'state' | 'space' | '_class'
@ -855,7 +867,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
todoUser: Ref<Person>,
account: Ref<Account>
): Promise<void> {
const latestTodo = await this.client.findOne(
const latestTodo = await client.findOne(
time.class.ToDo,
{
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.space.ToDos,
pullRequest._id,
@ -886,7 +898,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
undefined,
account
)
await this.client.createMixin(
await client.createMixin(
todoId,
time.class.ToDo,
time.space.ToDos,
@ -1124,6 +1136,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
}
private async createPullRequest (
client: TxOperations,
info: DocSyncInfo,
account: Ref<Account>,
pullRequestData: GithubPullRequestData & { status: Issue['status'] },
@ -1133,8 +1146,8 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
taskType: Ref<TaskType>,
repository: GithubIntegrationRepository,
isDescriptionLocked: boolean
): Promise<void> {
const lastOne = await this.client.findOne<Issue>(
): Promise<GithubPullRequest> {
const lastOne = await client.findOne<Issue>(
tracker.class.Issue,
{ space: prj._id },
{ 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>
await this.client.addCollection(
await client.addCollection(
github.class.GithubPullRequest,
info.space,
tracker.ids.NoParent,
@ -1187,7 +1200,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
new Date(pullRequestExternal.createdAt).getTime(),
account
)
await this.client.createMixin<Issue, GithubIssue>(
await client.createMixin<Issue, GithubIssue>(
prId,
github.class.GithubPullRequest,
info.space,
@ -1199,7 +1212,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
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(
github.string.PullRequestConnectedActivityInfo,
@ -1209,6 +1222,20 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
pullRequestExternal,
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 (