Skip to content

Commit b0f875d

Browse files
authored
Return incident's url (#60617) (#60694)
1 parent 69b2082 commit b0f875d

File tree

10 files changed

+61
-13
lines changed

10 files changed

+61
-13
lines changed

x-pack/plugins/actions/server/builtin_action_types/servicenow/action_handlers.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ beforeAll(() => {
7878
incidentId: '123',
7979
number: 'INC01',
8080
pushedDate: '2020-03-10T12:24:20.000Z',
81+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
8182
}),
8283
updateIncident: jest.fn().mockResolvedValue({
8384
incidentId: '123',
8485
number: 'INC01',
8586
pushedDate: '2020-03-10T12:24:20.000Z',
87+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
8688
}),
8789
batchCreateComments: jest
8890
.fn()
@@ -107,6 +109,7 @@ describe('handleIncident', () => {
107109
incidentId: '123',
108110
number: 'INC01',
109111
pushedDate: '2020-03-10T12:24:20.000Z',
112+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
110113
comments: [
111114
{
112115
commentId: '456',
@@ -129,6 +132,7 @@ describe('handleIncident', () => {
129132
incidentId: '123',
130133
number: 'INC01',
131134
pushedDate: '2020-03-10T12:24:20.000Z',
135+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
132136
comments: [
133137
{
134138
commentId: '456',
@@ -161,6 +165,7 @@ describe('handleCreateIncident', () => {
161165
incidentId: '123',
162166
number: 'INC01',
163167
pushedDate: '2020-03-10T12:24:20.000Z',
168+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
164169
});
165170
});
166171

@@ -203,6 +208,7 @@ describe('handleCreateIncident', () => {
203208
incidentId: '123',
204209
number: 'INC01',
205210
pushedDate: '2020-03-10T12:24:20.000Z',
211+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
206212
comments: [
207213
{
208214
commentId: '456',
@@ -236,6 +242,7 @@ describe('handleUpdateIncident', () => {
236242
incidentId: '123',
237243
number: 'INC01',
238244
pushedDate: '2020-03-10T12:24:20.000Z',
245+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
239246
});
240247
});
241248

@@ -326,6 +333,7 @@ describe('handleUpdateIncident', () => {
326333
incidentId: '123',
327334
number: 'INC01',
328335
pushedDate: '2020-03-10T12:24:20.000Z',
336+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
329337
comments: [
330338
{
331339
commentId: '456',
@@ -383,8 +391,10 @@ describe('handleUpdateIncident: different action types', () => {
383391
incidentId: '123',
384392
number: 'INC01',
385393
pushedDate: '2020-03-10T12:24:20.000Z',
394+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
386395
});
387396
});
397+
388398
test('nothing & append', async () => {
389399
const { serviceNow } = new ServiceNowMock();
390400
finalMapping.set('title', {
@@ -426,8 +436,10 @@ describe('handleUpdateIncident: different action types', () => {
426436
incidentId: '123',
427437
number: 'INC01',
428438
pushedDate: '2020-03-10T12:24:20.000Z',
439+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
429440
});
430441
});
442+
431443
test('append & append', async () => {
432444
const { serviceNow } = new ServiceNowMock();
433445
finalMapping.set('title', {
@@ -471,8 +483,10 @@ describe('handleUpdateIncident: different action types', () => {
471483
incidentId: '123',
472484
number: 'INC01',
473485
pushedDate: '2020-03-10T12:24:20.000Z',
486+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
474487
});
475488
});
489+
476490
test('nothing & nothing', async () => {
477491
const { serviceNow } = new ServiceNowMock();
478492
finalMapping.set('title', {
@@ -511,8 +525,10 @@ describe('handleUpdateIncident: different action types', () => {
511525
incidentId: '123',
512526
number: 'INC01',
513527
pushedDate: '2020-03-10T12:24:20.000Z',
528+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
514529
});
515530
});
531+
516532
test('overwrite & nothing', async () => {
517533
const { serviceNow } = new ServiceNowMock();
518534
finalMapping.set('title', {
@@ -553,8 +569,10 @@ describe('handleUpdateIncident: different action types', () => {
553569
incidentId: '123',
554570
number: 'INC01',
555571
pushedDate: '2020-03-10T12:24:20.000Z',
572+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
556573
});
557574
});
575+
558576
test('overwrite & overwrite', async () => {
559577
const { serviceNow } = new ServiceNowMock();
560578
finalMapping.set('title', {
@@ -596,8 +614,10 @@ describe('handleUpdateIncident: different action types', () => {
596614
incidentId: '123',
597615
number: 'INC01',
598616
pushedDate: '2020-03-10T12:24:20.000Z',
617+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
599618
});
600619
});
620+
601621
test('nothing & overwrite', async () => {
602622
const { serviceNow } = new ServiceNowMock();
603623
finalMapping.set('title', {
@@ -638,8 +658,10 @@ describe('handleUpdateIncident: different action types', () => {
638658
incidentId: '123',
639659
number: 'INC01',
640660
pushedDate: '2020-03-10T12:24:20.000Z',
661+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
641662
});
642663
});
664+
643665
test('append & overwrite', async () => {
644666
const { serviceNow } = new ServiceNowMock();
645667
finalMapping.set('title', {
@@ -682,8 +704,10 @@ describe('handleUpdateIncident: different action types', () => {
682704
incidentId: '123',
683705
number: 'INC01',
684706
pushedDate: '2020-03-10T12:24:20.000Z',
707+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
685708
});
686709
});
710+
687711
test('append & nothing', async () => {
688712
const { serviceNow } = new ServiceNowMock();
689713
finalMapping.set('title', {
@@ -725,6 +749,7 @@ describe('handleUpdateIncident: different action types', () => {
725749
incidentId: '123',
726750
number: 'INC01',
727751
pushedDate: '2020-03-10T12:24:20.000Z',
752+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
728753
});
729754
});
730755
});

x-pack/plugins/actions/server/builtin_action_types/servicenow/action_handlers.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export const handleCreateIncident = async ({
4747
fields,
4848
});
4949

50-
const { incidentId, number, pushedDate } = await serviceNow.createIncident({
50+
const createdIncident = await serviceNow.createIncident({
5151
...incident,
5252
});
5353

54-
const res: HandlerResponse = { incidentId, number, pushedDate };
54+
const res: HandlerResponse = { ...createdIncident };
5555

5656
if (
5757
comments &&
@@ -61,7 +61,12 @@ export const handleCreateIncident = async ({
6161
) {
6262
comments = transformComments(comments, params, ['informationAdded']);
6363
res.comments = [
64-
...(await createComments(serviceNow, incidentId, mapping.get('comments').target, comments)),
64+
...(await createComments(
65+
serviceNow,
66+
res.incidentId,
67+
mapping.get('comments').target,
68+
comments
69+
)),
6570
];
6671
}
6772

@@ -88,11 +93,11 @@ export const handleUpdateIncident = async ({
8893
currentIncident,
8994
});
9095

91-
const { number, pushedDate } = await serviceNow.updateIncident(incidentId, {
96+
const updatedIncident = await serviceNow.updateIncident(incidentId, {
9297
...incident,
9398
});
9499

95-
const res: HandlerResponse = { incidentId, number, pushedDate };
100+
const res: HandlerResponse = { ...updatedIncident };
96101

97102
if (
98103
comments &&

x-pack/plugins/actions/server/builtin_action_types/servicenow/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,10 @@ describe('execute()', () => {
231231
services,
232232
};
233233

234+
handleIncidentMock.mockImplementation(() => incidentResponse);
235+
234236
const actionResponse = await actionType.executor(executorOptions);
235-
expect(actionResponse).toEqual({ actionId, status: 'ok' });
237+
expect(actionResponse).toEqual({ actionId, status: 'ok', data: incidentResponse });
236238
});
237239

238240
test('should throw an error when failed to update an incident', async () => {

x-pack/plugins/actions/server/builtin_action_types/servicenow/lib/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ export const API_VERSION = 'v2';
88
export const INCIDENT_URL = `api/now/${API_VERSION}/table/incident`;
99
export const USER_URL = `api/now/${API_VERSION}/table/sys_user?user_name=`;
1010
export const COMMENT_URL = `api/now/${API_VERSION}/table/incident`;
11+
12+
// Based on: https://docs.servicenow.com/bundle/orlando-platform-user-interface/page/use/navigation/reference/r_NavigatingByURLExamples.html
13+
export const VIEW_INCIDENT_URL = `nav_to.do?uri=incident.do?sys_id=`;

x-pack/plugins/actions/server/builtin_action_types/servicenow/lib/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('ServiceNow lib', () => {
9292
incidentId: '123',
9393
number: 'INC01',
9494
pushedDate: '2020-03-10T12:24:20.000Z',
95+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
9596
});
9697
});
9798

@@ -116,6 +117,7 @@ describe('ServiceNow lib', () => {
116117
incidentId: '123',
117118
number: 'INC01',
118119
pushedDate: '2020-03-10T12:24:20.000Z',
120+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
119121
});
120122
});
121123

x-pack/plugins/actions/server/builtin_action_types/servicenow/lib/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import axios, { AxiosInstance, Method, AxiosResponse } from 'axios';
88

9-
import { INCIDENT_URL, USER_URL, COMMENT_URL } from './constants';
9+
import { INCIDENT_URL, USER_URL, COMMENT_URL, VIEW_INCIDENT_URL } from './constants';
1010
import { Instance, Incident, IncidentResponse, UpdateIncident, CommentResponse } from './types';
1111
import { Comment } from '../types';
1212

@@ -72,6 +72,10 @@ class ServiceNow {
7272
return `[Action][ServiceNow]: ${msg}`;
7373
}
7474

75+
private _getIncidentViewURL(id: string) {
76+
return `${this.instance.url}/${VIEW_INCIDENT_URL}${id}`;
77+
}
78+
7579
async getUserID(): Promise<string> {
7680
try {
7781
const res = await this._request({ url: `${this.userUrl}${this.instance.username}` });
@@ -109,6 +113,7 @@ class ServiceNow {
109113
number: res.data.result.number,
110114
incidentId: res.data.result.sys_id,
111115
pushedDate: new Date(this._addTimeZoneToDate(res.data.result.sys_created_on)).toISOString(),
116+
url: this._getIncidentViewURL(res.data.result.sys_id),
112117
};
113118
} catch (error) {
114119
throw new Error(this._getErrorMessage(`Unable to create incident. Error: ${error.message}`));
@@ -126,6 +131,7 @@ class ServiceNow {
126131
number: res.data.result.number,
127132
incidentId: res.data.result.sys_id,
128133
pushedDate: new Date(this._addTimeZoneToDate(res.data.result.sys_updated_on)).toISOString(),
134+
url: this._getIncidentViewURL(res.data.result.sys_id),
129135
};
130136
} catch (error) {
131137
throw new Error(

x-pack/plugins/actions/server/builtin_action_types/servicenow/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface IncidentResponse {
2121
number: string;
2222
incidentId: string;
2323
pushedDate: string;
24+
url: string;
2425
}
2526

2627
export interface CommentResponse {

x-pack/plugins/actions/server/builtin_action_types/servicenow/mock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ const params: ExecutorParams = {
6969
const incidentResponse = {
7070
incidentId: 'c816f79cc0a8016401c5a33be04be441',
7171
number: 'INC0010001',
72+
pushedDate: '2020-03-13T08:34:53.450Z',
73+
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
7274
};
7375

7476
const userId = '2e9a0a5e2f79001016ab51172799b670';

x-pack/plugins/actions/server/builtin_action_types/servicenow/types.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './schema';
1717

1818
import { ServiceNow } from './lib';
19-
import { Incident } from './lib/types';
19+
import { Incident, IncidentResponse } from './lib/types';
2020

2121
// config definition
2222
export type ConfigType = TypeOf<typeof ConfigSchema>;
@@ -50,11 +50,8 @@ export type IncidentHandlerArguments = CreateHandlerArguments & {
5050
incidentId: string | null;
5151
};
5252

53-
export interface HandlerResponse {
54-
incidentId: string;
55-
number: string;
53+
export interface HandlerResponse extends IncidentResponse {
5654
comments?: SimpleComment[];
57-
pushedDate: string;
5855
}
5956

6057
export interface SimpleComment {

x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
294294
expect(result).to.eql({
295295
status: 'ok',
296296
actionId: simulatedActionId,
297-
data: { incidentId: '123', number: 'INC01', pushedDate: '2020-03-10T12:24:20.000Z' },
297+
data: {
298+
incidentId: '123',
299+
number: 'INC01',
300+
pushedDate: '2020-03-10T12:24:20.000Z',
301+
url: `${servicenowSimulatorURL}/nav_to.do?uri=incident.do?sys_id=123`,
302+
},
298303
});
299304
});
300305

0 commit comments

Comments
 (0)