Skip to content
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

[7.x] [Cases] Migrate connector ID to references (#104221) #108266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions x-pack/plugins/cases/common/api/cases/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NumberFromString } from '../saved_object';
import { UserRT } from '../user';
import { CommentResponseRt } from './comment';
import { CasesStatusResponseRt, CaseStatusRt } from './status';
import { CaseConnectorRt, ESCaseConnector } from '../connectors';
import { CaseConnectorRt } from '../connectors';
import { SubCaseResponseRt } from './sub_case';

const BucketsAggs = rt.array(
Expand Down Expand Up @@ -87,24 +87,17 @@ const CaseBasicRt = rt.type({
owner: rt.string,
});

const CaseExternalServiceBasicRt = rt.type({
connector_id: rt.string,
export const CaseExternalServiceBasicRt = rt.type({
connector_id: rt.union([rt.string, rt.null]),
connector_name: rt.string,
external_id: rt.string,
external_title: rt.string,
external_url: rt.string,
pushed_at: rt.string,
pushed_by: UserRT,
});

const CaseFullExternalServiceRt = rt.union([
rt.intersection([
CaseExternalServiceBasicRt,
rt.type({
pushed_at: rt.string,
pushed_by: UserRT,
}),
]),
rt.null,
]);
const CaseFullExternalServiceRt = rt.union([CaseExternalServiceBasicRt, rt.null]);

export const CaseAttributesRt = rt.intersection([
CaseBasicRt,
Expand Down Expand Up @@ -326,11 +319,6 @@ export type CaseFullExternalService = rt.TypeOf<typeof CaseFullExternalServiceRt
export type CaseSettings = rt.TypeOf<typeof SettingsRt>;
export type ExternalServiceResponse = rt.TypeOf<typeof ExternalServiceResponseRt>;

export type ESCaseAttributes = Omit<CaseAttributes, 'connector'> & { connector: ESCaseConnector };
export type ESCasePatchRequest = Omit<CasePatchRequest, 'connector'> & {
connector?: ESCaseConnector;
};

export type AllTagsFindRequest = rt.TypeOf<typeof AllTagsFindRequestRt>;
export type AllReportersFindRequest = AllTagsFindRequest;

Expand Down
6 changes: 1 addition & 5 deletions x-pack/plugins/cases/common/api/cases/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as rt from 'io-ts';

import { UserRT } from '../user';
import { CaseConnectorRt, ConnectorMappingsRt, ESCaseConnector } from '../connectors';
import { CaseConnectorRt, ConnectorMappingsRt } from '../connectors';

// TODO: we will need to add this type rt.literal('close-by-third-party')
const ClosureTypeRT = rt.union([rt.literal('close-by-user'), rt.literal('close-by-pushing')]);
Expand Down Expand Up @@ -83,8 +83,4 @@ export type CasesConfigureAttributes = rt.TypeOf<typeof CaseConfigureAttributesR
export type CasesConfigureResponse = rt.TypeOf<typeof CaseConfigureResponseRt>;
export type CasesConfigurationsResponse = rt.TypeOf<typeof CaseConfigurationsResponseRt>;

export type ESCasesConfigureAttributes = Omit<CasesConfigureAttributes, 'connector'> & {
connector: ESCaseConnector;
};

export type GetConfigureFindRequest = rt.TypeOf<typeof GetConfigureFindRequestRt>;
15 changes: 2 additions & 13 deletions x-pack/plugins/cases/common/api/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const ConnectorNoneTypeFieldsRt = rt.type({
fields: rt.null,
});

export const noneConnectorId: string = 'none';

export const ConnectorTypeFieldsRt = rt.union([
ConnectorJiraTypeFieldsRt,
ConnectorNoneTypeFieldsRt,
Expand Down Expand Up @@ -102,16 +104,3 @@ export type ConnectorServiceNowSIRTypeFields = rt.TypeOf<typeof ConnectorService

// we need to change these types back and forth for storing in ES (arrays overwrite, objects merge)
export type ConnectorFields = rt.TypeOf<typeof ConnectorFieldsRt>;

export type ESConnectorFields = Array<{
key: string;
value: unknown;
}>;

export type ESCaseConnectorTypes = ConnectorTypes;
export interface ESCaseConnector {
id: string;
name: string;
type: ESCaseConnectorTypes;
fields: ESConnectorFields | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ export const getPushedServiceLabelTitle = (action: CaseUserActions, firstPush: b

export const getPushInfo = (
caseServices: CaseServices,
parsedValue: { connector_id: string; connector_name: string },
// a JSON parse failure will result in null for parsedValue
parsedValue: { connector_id: string | null; connector_name: string } | null,
index: number
) =>
parsedValue != null
parsedValue != null && parsedValue.connector_id != null
? {
firstPush: caseServices[parsedValue.connector_id]?.firstPushIndex === index,
parsedConnectorId: parsedValue.connector_id,
Expand Down
15 changes: 2 additions & 13 deletions x-pack/plugins/cases/server/client/cases/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ import {
MAX_TITLE_LENGTH,
} from '../../../common';
import { buildCaseUserActionItem } from '../../services/user_actions/helpers';
import { getConnectorFromConfiguration } from '../utils';

import { Operations } from '../../authorization';
import {
createCaseError,
flattenCaseSavedObject,
transformCaseConnectorToEsConnector,
transformNewCase,
} from '../../common';
import { createCaseError, flattenCaseSavedObject, transformNewCase } from '../../common';
import { CasesClientArgs } from '..';

/**
Expand All @@ -48,7 +42,6 @@ export const create = async (
const {
unsecuredSavedObjectsClient,
caseService,
caseConfigureService,
userActionService,
user,
logger,
Expand Down Expand Up @@ -90,10 +83,6 @@ export const create = async (
// eslint-disable-next-line @typescript-eslint/naming-convention
const { username, full_name, email } = user;
const createdDate = new Date().toISOString();
const myCaseConfigure = await caseConfigureService.find({
unsecuredSavedObjectsClient,
});
const caseConfigureConnector = getConnectorFromConfiguration(myCaseConfigure);

const newCase = await caseService.postNewCase({
unsecuredSavedObjectsClient,
Expand All @@ -103,7 +92,7 @@ export const create = async (
username,
full_name,
email,
connector: transformCaseConnectorToEsConnector(query.connector ?? caseConfigureConnector),
connector: query.connector,
}),
id: savedObjectID,
});
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/client/cases/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { SavedObject } from 'kibana/server';
import {
CaseResponseRt,
CaseResponse,
ESCaseAttributes,
User,
UsersRt,
AllTagsFindRequest,
Expand All @@ -27,6 +26,7 @@ import {
ENABLE_CASE_CONNECTOR,
CasesByAlertId,
CasesByAlertIdRt,
CaseAttributes,
} from '../../../common';
import { countAlertsForID, createCaseError, flattenCaseSavedObject } from '../../common';
import { CasesClientArgs } from '..';
Expand Down Expand Up @@ -171,7 +171,7 @@ export const get = async (
);
}

let theCase: SavedObject<ESCaseAttributes>;
let theCase: SavedObject<CaseAttributes>;
let subCaseIds: string[] = [];
if (ENABLE_CASE_CONNECTOR) {
const [caseInfo, subCasesForCaseId] = await Promise.all([
Expand Down
9 changes: 5 additions & 4 deletions x-pack/plugins/cases/server/client/cases/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
CaseResponse,
CaseStatuses,
ExternalServiceResponse,
ESCaseAttributes,
ESCasesConfigureAttributes,
CaseType,
ENABLE_CASE_CONNECTOR,
CasesConfigureAttributes,
CaseAttributes,
} from '../../../common';
import { buildCaseUserActionItem } from '../../services/user_actions/helpers';

Expand All @@ -33,8 +33,8 @@ import { casesConnectors } from '../../connectors';
* In the future we could allow push to close all the sub cases of a collection but that's not currently supported.
*/
function shouldCloseByPush(
configureSettings: SavedObjectsFindResponse<ESCasesConfigureAttributes>,
caseInfo: SavedObject<ESCaseAttributes>
configureSettings: SavedObjectsFindResponse<CasesConfigureAttributes>,
caseInfo: SavedObject<CaseAttributes>
): boolean {
return (
configureSettings.total > 0 &&
Expand Down Expand Up @@ -186,6 +186,7 @@ export const push = async (

const [updatedCase, updatedComments] = await Promise.all([
caseService.patchCase({
originalCase: myCase,
unsecuredSavedObjectsClient,
caseId,
updatedAttributes: {
Expand Down
Loading