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] Return incident's url (#60617) #60694

Merged
merged 1 commit into from
Mar 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ beforeAll(() => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
}),
updateIncident: jest.fn().mockResolvedValue({
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
}),
batchCreateComments: jest
.fn()
Expand All @@ -107,6 +109,7 @@ describe('handleIncident', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
comments: [
{
commentId: '456',
Expand All @@ -129,6 +132,7 @@ describe('handleIncident', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
comments: [
{
commentId: '456',
Expand Down Expand Up @@ -161,6 +165,7 @@ describe('handleCreateIncident', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

Expand Down Expand Up @@ -203,6 +208,7 @@ describe('handleCreateIncident', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
comments: [
{
commentId: '456',
Expand Down Expand Up @@ -236,6 +242,7 @@ describe('handleUpdateIncident', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

Expand Down Expand Up @@ -326,6 +333,7 @@ describe('handleUpdateIncident', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
comments: [
{
commentId: '456',
Expand Down Expand Up @@ -383,8 +391,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('nothing & append', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -426,8 +436,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('append & append', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -471,8 +483,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('nothing & nothing', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -511,8 +525,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('overwrite & nothing', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -553,8 +569,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('overwrite & overwrite', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -596,8 +614,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('nothing & overwrite', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -638,8 +658,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('append & overwrite', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -682,8 +704,10 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

test('append & nothing', async () => {
const { serviceNow } = new ServiceNowMock();
finalMapping.set('title', {
Expand Down Expand Up @@ -725,6 +749,7 @@ describe('handleUpdateIncident: different action types', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export const handleCreateIncident = async ({
fields,
});

const { incidentId, number, pushedDate } = await serviceNow.createIncident({
const createdIncident = await serviceNow.createIncident({
...incident,
});

const res: HandlerResponse = { incidentId, number, pushedDate };
const res: HandlerResponse = { ...createdIncident };

if (
comments &&
Expand All @@ -61,7 +61,12 @@ export const handleCreateIncident = async ({
) {
comments = transformComments(comments, params, ['informationAdded']);
res.comments = [
...(await createComments(serviceNow, incidentId, mapping.get('comments').target, comments)),
...(await createComments(
serviceNow,
res.incidentId,
mapping.get('comments').target,
comments
)),
];
}

Expand All @@ -88,11 +93,11 @@ export const handleUpdateIncident = async ({
currentIncident,
});

const { number, pushedDate } = await serviceNow.updateIncident(incidentId, {
const updatedIncident = await serviceNow.updateIncident(incidentId, {
...incident,
});

const res: HandlerResponse = { incidentId, number, pushedDate };
const res: HandlerResponse = { ...updatedIncident };

if (
comments &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ describe('execute()', () => {
services,
};

handleIncidentMock.mockImplementation(() => incidentResponse);

const actionResponse = await actionType.executor(executorOptions);
expect(actionResponse).toEqual({ actionId, status: 'ok' });
expect(actionResponse).toEqual({ actionId, status: 'ok', data: incidentResponse });
});

test('should throw an error when failed to update an incident', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export const API_VERSION = 'v2';
export const INCIDENT_URL = `api/now/${API_VERSION}/table/incident`;
export const USER_URL = `api/now/${API_VERSION}/table/sys_user?user_name=`;
export const COMMENT_URL = `api/now/${API_VERSION}/table/incident`;

// Based on: https://docs.servicenow.com/bundle/orlando-platform-user-interface/page/use/navigation/reference/r_NavigatingByURLExamples.html
export const VIEW_INCIDENT_URL = `nav_to.do?uri=incident.do?sys_id=`;
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('ServiceNow lib', () => {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
});
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

import { INCIDENT_URL, USER_URL, COMMENT_URL } from './constants';
import { INCIDENT_URL, USER_URL, COMMENT_URL, VIEW_INCIDENT_URL } from './constants';
import { Instance, Incident, IncidentResponse, UpdateIncident, CommentResponse } from './types';
import { Comment } from '../types';

Expand Down Expand Up @@ -72,6 +72,10 @@ class ServiceNow {
return `[Action][ServiceNow]: ${msg}`;
}

private _getIncidentViewURL(id: string) {
return `${this.instance.url}/${VIEW_INCIDENT_URL}${id}`;
}

async getUserID(): Promise<string> {
try {
const res = await this._request({ url: `${this.userUrl}${this.instance.username}` });
Expand Down Expand Up @@ -109,6 +113,7 @@ class ServiceNow {
number: res.data.result.number,
incidentId: res.data.result.sys_id,
pushedDate: new Date(this._addTimeZoneToDate(res.data.result.sys_created_on)).toISOString(),
url: this._getIncidentViewURL(res.data.result.sys_id),
};
} catch (error) {
throw new Error(this._getErrorMessage(`Unable to create incident. Error: ${error.message}`));
Expand All @@ -126,6 +131,7 @@ class ServiceNow {
number: res.data.result.number,
incidentId: res.data.result.sys_id,
pushedDate: new Date(this._addTimeZoneToDate(res.data.result.sys_updated_on)).toISOString(),
url: this._getIncidentViewURL(res.data.result.sys_id),
};
} catch (error) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IncidentResponse {
number: string;
incidentId: string;
pushedDate: string;
url: string;
}

export interface CommentResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const params: ExecutorParams = {
const incidentResponse = {
incidentId: 'c816f79cc0a8016401c5a33be04be441',
number: 'INC0010001',
pushedDate: '2020-03-13T08:34:53.450Z',
url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123',
};

const userId = '2e9a0a5e2f79001016ab51172799b670';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './schema';

import { ServiceNow } from './lib';
import { Incident } from './lib/types';
import { Incident, IncidentResponse } from './lib/types';

// config definition
export type ConfigType = TypeOf<typeof ConfigSchema>;
Expand Down Expand Up @@ -50,11 +50,8 @@ export type IncidentHandlerArguments = CreateHandlerArguments & {
incidentId: string | null;
};

export interface HandlerResponse {
incidentId: string;
number: string;
export interface HandlerResponse extends IncidentResponse {
comments?: SimpleComment[];
pushedDate: string;
}

export interface SimpleComment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
expect(result).to.eql({
status: 'ok',
actionId: simulatedActionId,
data: { incidentId: '123', number: 'INC01', pushedDate: '2020-03-10T12:24:20.000Z' },
data: {
incidentId: '123',
number: 'INC01',
pushedDate: '2020-03-10T12:24:20.000Z',
url: `${servicenowSimulatorURL}/nav_to.do?uri=incident.do?sys_id=123`,
},
});
});

Expand Down