-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cases] Split attachment types for versioned http apis #155440
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { User } from './user'; | ||
|
||
interface AttachmentCommonPersistedAttributes { | ||
created_at: string; | ||
created_by: User; | ||
owner: string; | ||
pushed_at: string | null; | ||
pushed_by: User | null; | ||
updated_at: string | null; | ||
updated_by: User | null; | ||
} | ||
|
||
export interface AttachmentRequestAttributes { | ||
type: string; | ||
alertId?: string | string[]; | ||
index?: string | string[]; | ||
rule?: Record<string, unknown>; | ||
comment?: string; | ||
actions?: Record<string, unknown>; | ||
externalReferenceMetadata?: Record<string, unknown> | null; | ||
externalReferenceAttachmentTypeId?: string; | ||
externalReferenceStorage?: { | ||
type: string; | ||
soType?: string; | ||
}; | ||
persistableStateAttachmentState?: Record<string, unknown>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, yeah I was getting confused between that and the |
||
} | ||
|
||
export type AttachmentPersistedAttributes = AttachmentRequestAttributes & | ||
AttachmentCommonPersistedAttributes; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export interface User { | ||
email: undefined | null | string; | ||
full_name: undefined | null | string; | ||
username: undefined | null | string; | ||
profile_uid?: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ import { | |
getLensVisualizations, | ||
} from '../../common/utils/markdown_plugins/utils'; | ||
import { dedupAssignees } from '../client/cases/utils'; | ||
import type { AttachmentRequestAttributes } from './types/attachments'; | ||
|
||
/** | ||
* Default sort field for querying saved objects. | ||
|
@@ -258,7 +259,7 @@ export const isCommentRequestTypeAlert = ( | |
* A type narrowing function for external reference so attachments. | ||
*/ | ||
export const isCommentRequestTypeExternalReferenceSO = ( | ||
context: Partial<CommentRequest> | ||
context: Partial<AttachmentRequestAttributes> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we do the same for the other utility functions? Even if we don't get TS errors now it will protects us from mistakes in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was a little unsure about these functions because most of them are used in the clients and not the service layer. Maybe we should eventually move the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok. If they are used in the client's layer then it is fine. Moving |
||
): context is CommentRequestExternalReferenceSOType => { | ||
return ( | ||
context.type === CommentType.externalReference && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,16 @@ | |
|
||
import type { | ||
SavedObject, | ||
SavedObjectReference, | ||
SavedObjectsBulkResponse, | ||
SavedObjectsBulkUpdateResponse, | ||
SavedObjectsFindResponse, | ||
SavedObjectsUpdateOptions, | ||
SavedObjectsUpdateResponse, | ||
} from '@kbn/core/server'; | ||
|
||
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import type { | ||
CommentAttributes as AttachmentAttributes, | ||
CommentAttributesWithoutRefs as AttachmentAttributesWithoutRefs, | ||
CommentPatchAttributes as AttachmentPatchAttributes, | ||
} from '../../../common/api'; | ||
import { CommentType } from '../../../common/api'; | ||
import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../../common/constants'; | ||
|
@@ -32,50 +29,19 @@ import { | |
injectAttachmentSOAttributesFromRefsForPatch, | ||
} from '../so_references'; | ||
import type { SavedObjectFindOptionsKueryNode } from '../../common/types'; | ||
import type { IndexRefresh } from '../types'; | ||
import type { AttachedToCaseArgs, ServiceContext } from './types'; | ||
import type { | ||
AlertsAttachedToCaseArgs, | ||
AttachmentsAttachedToCaseArgs, | ||
BulkCreateAttachments, | ||
BulkUpdateAttachmentArgs, | ||
CountActionsAttachedToCaseArgs, | ||
CreateAttachmentArgs, | ||
DeleteAttachmentArgs, | ||
ServiceContext, | ||
UpdateAttachmentArgs, | ||
} from './types'; | ||
import { AttachmentGetter } from './operations/get'; | ||
|
||
type AlertsAttachedToCaseArgs = AttachedToCaseArgs; | ||
|
||
interface AttachmentsAttachedToCaseArgs extends AttachedToCaseArgs { | ||
attachmentType: CommentType; | ||
aggregations: Record<string, estypes.AggregationsAggregationContainer>; | ||
} | ||
|
||
interface CountActionsAttachedToCaseArgs extends AttachedToCaseArgs { | ||
aggregations: Record<string, estypes.AggregationsAggregationContainer>; | ||
} | ||
|
||
interface DeleteAttachmentArgs extends IndexRefresh { | ||
attachmentIds: string[]; | ||
} | ||
|
||
interface CreateAttachmentArgs extends IndexRefresh { | ||
attributes: AttachmentAttributes; | ||
references: SavedObjectReference[]; | ||
id: string; | ||
} | ||
|
||
interface BulkCreateAttachments extends IndexRefresh { | ||
attachments: Array<{ | ||
attributes: AttachmentAttributes; | ||
references: SavedObjectReference[]; | ||
id: string; | ||
}>; | ||
} | ||
|
||
interface UpdateArgs { | ||
attachmentId: string; | ||
updatedAttributes: AttachmentPatchAttributes; | ||
options?: Omit<SavedObjectsUpdateOptions<AttachmentAttributes>, 'upsert'>; | ||
} | ||
|
||
export type UpdateAttachmentArgs = UpdateArgs; | ||
|
||
interface BulkUpdateAttachmentArgs extends IndexRefresh { | ||
comments: UpdateArgs[]; | ||
} | ||
import type { AttachmentPersistedAttributes } from '../../common/types/attachments'; | ||
|
||
export class AttachmentService { | ||
private readonly _getter: AttachmentGetter; | ||
|
@@ -207,7 +173,7 @@ export class AttachmentService { | |
); | ||
|
||
const attachment = | ||
await this.context.unsecuredSavedObjectsClient.create<AttachmentAttributesWithoutRefs>( | ||
await this.context.unsecuredSavedObjectsClient.create<AttachmentPersistedAttributes>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤦♂️ don't know why I missed all that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like |
||
CASE_COMMENT_SAVED_OBJECT, | ||
extractedAttributes, | ||
{ | ||
|
@@ -234,7 +200,7 @@ export class AttachmentService { | |
try { | ||
this.context.log.debug(`Attempting to bulk create attachments`); | ||
const res = | ||
await this.context.unsecuredSavedObjectsClient.bulkCreate<AttachmentAttributesWithoutRefs>( | ||
await this.context.unsecuredSavedObjectsClient.bulkCreate<AttachmentPersistedAttributes>( | ||
attachments.map((attachment) => { | ||
const { attributes: extractedAttributes, references: extractedReferences } = | ||
extractAttachmentSORefsFromAttributes( | ||
|
@@ -380,7 +346,7 @@ export class AttachmentService { | |
try { | ||
this.context.log.debug(`Attempting to find comments`); | ||
const res = | ||
await this.context.unsecuredSavedObjectsClient.find<AttachmentAttributesWithoutRefs>({ | ||
await this.context.unsecuredSavedObjectsClient.find<AttachmentPersistedAttributes>({ | ||
sortField: defaultSortField, | ||
...options, | ||
type: CASE_COMMENT_SAVED_OBJECT, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import type { SavedObject } from '@kbn/core/server'; | ||
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import { FILE_SO_TYPE } from '@kbn/files-plugin/common'; | ||
import type { AttachmentPersistedAttributes } from '../../../common/types/attachments'; | ||
import { | ||
CASE_COMMENT_SAVED_OBJECT, | ||
CASE_SAVED_OBJECT, | ||
|
@@ -19,13 +20,13 @@ import type { | |
AttachmentTotals, | ||
AttributesTypeAlerts, | ||
CommentAttributes as AttachmentAttributes, | ||
CommentAttributesWithoutRefs as AttachmentAttributesWithoutRefs, | ||
CommentAttributes, | ||
} from '../../../../common/api'; | ||
import { CommentType } from '../../../../common/api'; | ||
import type { | ||
AttachedToCaseArgs, | ||
AlertIdsAggsResult, | ||
BulkOptionalAttributes, | ||
GetAllAlertsAttachToCaseArgs, | ||
GetAttachmentArgs, | ||
ServiceContext, | ||
} from '../types'; | ||
|
@@ -37,16 +38,6 @@ import { partitionByCaseAssociation } from '../../../common/partitioning'; | |
import type { AttachmentSavedObject } from '../../../common/types'; | ||
import { getCaseReferenceId } from '../../../common/references'; | ||
|
||
type GetAllAlertsAttachToCaseArgs = AttachedToCaseArgs; | ||
|
||
interface AlertIdsAggsResult { | ||
alertIds: { | ||
buckets: Array<{ | ||
key: string; | ||
}>; | ||
}; | ||
} | ||
|
||
export class AttachmentGetter { | ||
constructor(private readonly context: ServiceContext) {} | ||
|
||
|
@@ -59,7 +50,7 @@ export class AttachmentGetter { | |
); | ||
|
||
const response = | ||
await this.context.unsecuredSavedObjectsClient.bulkGet<AttachmentAttributesWithoutRefs>( | ||
await this.context.unsecuredSavedObjectsClient.bulkGet<AttachmentPersistedAttributes>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, I think we should add the new type to all SO methods of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the ones I didn't add it to here are intentionally wanting |
||
attachmentIds.map((id) => ({ id, type: CASE_COMMENT_SAVED_OBJECT })) | ||
); | ||
|
||
|
@@ -133,18 +124,24 @@ export class AttachmentGetter { | |
const combinedFilter = combineFilters([alertsFilter, filter]); | ||
|
||
const finder = | ||
this.context.unsecuredSavedObjectsClient.createPointInTimeFinder<AttributesTypeAlerts>({ | ||
type: CASE_COMMENT_SAVED_OBJECT, | ||
hasReference: { type: CASE_SAVED_OBJECT, id: caseId }, | ||
sortField: 'created_at', | ||
sortOrder: 'asc', | ||
filter: combinedFilter, | ||
perPage: MAX_DOCS_PER_PAGE, | ||
}); | ||
this.context.unsecuredSavedObjectsClient.createPointInTimeFinder<AttachmentPersistedAttributes>( | ||
{ | ||
type: CASE_COMMENT_SAVED_OBJECT, | ||
hasReference: { type: CASE_SAVED_OBJECT, id: caseId }, | ||
sortField: 'created_at', | ||
sortOrder: 'asc', | ||
filter: combinedFilter, | ||
perPage: MAX_DOCS_PER_PAGE, | ||
} | ||
); | ||
|
||
let result: Array<SavedObject<AttributesTypeAlerts>> = []; | ||
for await (const userActionSavedObject of finder.find()) { | ||
result = result.concat(userActionSavedObject.saved_objects); | ||
result = result.concat( | ||
// We need a cast here because to limited attachment type conflicts with the expected result even though they | ||
// should be the same | ||
userActionSavedObject.saved_objects as unknown as Array<SavedObject<AttributesTypeAlerts>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just calling this out. Do you see a better way around this? I believe we skip transforming this because alerts don't have fields that need to be migrated (the rule.id should have been but we had that bugfix). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I cannot think of another way of doing it. |
||
); | ||
} | ||
|
||
return result; | ||
|
@@ -197,11 +194,10 @@ export class AttachmentGetter { | |
}: GetAttachmentArgs): Promise<SavedObject<AttachmentAttributes>> { | ||
try { | ||
this.context.log.debug(`Attempting to GET attachment ${attachmentId}`); | ||
const res = | ||
await this.context.unsecuredSavedObjectsClient.get<AttachmentAttributesWithoutRefs>( | ||
CASE_COMMENT_SAVED_OBJECT, | ||
attachmentId | ||
); | ||
const res = await this.context.unsecuredSavedObjectsClient.get<AttachmentPersistedAttributes>( | ||
CASE_COMMENT_SAVED_OBJECT, | ||
attachmentId | ||
); | ||
|
||
return injectAttachmentSOAttributesFromRefs( | ||
res, | ||
|
@@ -324,7 +320,7 @@ export class AttachmentGetter { | |
* to retrieve them all. | ||
*/ | ||
const finder = | ||
this.context.unsecuredSavedObjectsClient.createPointInTimeFinder<AttachmentAttributesWithoutRefs>( | ||
this.context.unsecuredSavedObjectsClient.createPointInTimeFinder<AttachmentPersistedAttributes>( | ||
{ | ||
type: CASE_COMMENT_SAVED_OBJECT, | ||
hasReference: references, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the child fields of this field be represented as well? It doesn't seem to affect type errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be more explicit on this and define them. Same for the
rule
.