Skip to content

Commit 4688afd

Browse files
bjoernricksgreenbonebot
authored andcommitted
Change: Use TypeScript for Ticket commands
Ensure the behavior of both commands and define the expected parameters.
1 parent 2d838e0 commit 4688afd

8 files changed

Lines changed: 137 additions & 74 deletions

File tree

src/gmp/__tests__/gmp.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ describe('Gmp tests', () => {
263263
'targets',
264264
'task',
265265
'tasks',
266+
'ticket',
267+
'tickets',
266268
'timezones',
267269
'trashcan',
268270
'user',
@@ -276,8 +278,6 @@ describe('Gmp tests', () => {
276278
'license',
277279
'scanconfig',
278280
'scanconfigs',
279-
'ticket',
280-
'tickets',
281281
'tlscertificate',
282282
'tlscertificates',
283283
])('should expose command %s', name => {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
createEntityResponse,
1010
createHttp,
1111
} from 'gmp/commands/testing';
12-
import {TicketCommand} from 'gmp/commands/tickets';
12+
import TicketCommand from 'gmp/commands/ticket';
1313

1414
describe('TicketCommand tests', () => {
1515
test('should trim note when creating ticket', async () => {
@@ -19,7 +19,6 @@ describe('TicketCommand tests', () => {
1919
const resp = await cmd.create({
2020
resultId: 'r1',
2121
userId: 'u1',
22-
foo: 'bar',
2322
note: ' ',
2423
});
2524
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
@@ -42,7 +41,6 @@ describe('TicketCommand tests', () => {
4241
resultId: 'r1',
4342
userId: 'u1',
4443
note: 'foo',
45-
foo: 'bar',
4644
});
4745
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
4846
data: {

src/gmp/commands/__tests__/tickets.test.js renamed to src/gmp/commands/__tests__/tickets.test.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
import {describe, test, expect} from '@gsa/testing';
77
import {createHttp, createEntitiesResponse} from 'gmp/commands/testing';
8-
import {TicketsCommand} from 'gmp/commands/tickets';
8+
import TicketsCommand from 'gmp/commands/tickets';
99
import {ALL_FILTER} from 'gmp/models/filter';
10+
import Ticket from 'gmp/models/ticket';
1011

1112
describe('TicketsCommand tests', () => {
12-
test('should return all tickets', async () => {
13+
test('should fetch all tickets', async () => {
1314
const response = createEntitiesResponse('ticket', [
1415
{
1516
_id: '1',
@@ -32,7 +33,7 @@ describe('TicketsCommand tests', () => {
3233
expect(data.length).toEqual(2);
3334
});
3435

35-
test('should return tickets', async () => {
36+
test('should fetch tickets with default params', async () => {
3637
const response = createEntitiesResponse('ticket', [
3738
{
3839
_id: '1',
@@ -53,4 +54,29 @@ describe('TicketsCommand tests', () => {
5354
const {data} = resp;
5455
expect(data.length).toEqual(2);
5556
});
57+
58+
test('should fetch tickets with custom params', async () => {
59+
const response = createEntitiesResponse('ticket', [
60+
{
61+
_id: '1',
62+
status: 'open',
63+
},
64+
]);
65+
66+
const fakeHttp = createHttp(response);
67+
const cmd = new TicketsCommand(fakeHttp);
68+
const resp = await cmd.get({filter: "status='open'"});
69+
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
70+
args: {
71+
cmd: 'get_tickets',
72+
filter: "status='open'",
73+
},
74+
});
75+
expect(resp.data).toEqual([
76+
new Ticket({
77+
id: '1',
78+
status: 'open',
79+
}),
80+
]);
81+
});
5682
});

src/gmp/commands/ticket.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* SPDX-FileCopyrightText: 2024 Greenbone AG
2+
*
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import EntityCommand from 'gmp/commands/entity';
7+
import type Http from 'gmp/http/http';
8+
import {type Element} from 'gmp/models/model';
9+
import Ticket, {type TicketStatus, type TicketElement} from 'gmp/models/ticket';
10+
import {isDefined} from 'gmp/utils/identity';
11+
12+
interface CreateTicketArguments {
13+
resultId: string;
14+
userId: string;
15+
note?: string;
16+
}
17+
18+
interface SaveTicketArguments {
19+
id: string;
20+
openNote?: string;
21+
fixedNote?: string;
22+
closedNote?: string;
23+
status: TicketStatus;
24+
userId: string;
25+
}
26+
27+
const convertNote = (note?: string) => {
28+
if (!isDefined(note)) {
29+
return note;
30+
}
31+
32+
note = note.trim();
33+
34+
return note;
35+
};
36+
37+
class TicketCommand extends EntityCommand<Ticket, TicketElement> {
38+
constructor(http: Http) {
39+
super(http, 'ticket', Ticket);
40+
}
41+
42+
create({resultId, userId, note}: CreateTicketArguments) {
43+
return this.action({
44+
cmd: 'create_ticket',
45+
result_id: resultId,
46+
user_id: userId,
47+
note: convertNote(note),
48+
});
49+
}
50+
51+
save({
52+
id,
53+
openNote,
54+
fixedNote,
55+
closedNote,
56+
status,
57+
userId,
58+
}: SaveTicketArguments) {
59+
return this.action({
60+
cmd: 'save_ticket',
61+
id,
62+
open_note: convertNote(openNote),
63+
fixed_note: convertNote(fixedNote),
64+
closed_note: convertNote(closedNote),
65+
ticket_status: status,
66+
user_id: userId,
67+
});
68+
}
69+
70+
getElementFromRoot(root: Element): TicketElement {
71+
// @ts-expect-error
72+
return root.get_ticket.get_tickets_response.ticket;
73+
}
74+
}
75+
76+
export default TicketCommand;

src/gmp/commands/tickets.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/gmp/commands/tickets.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-FileCopyrightText: 2024 Greenbone AG
2+
*
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import EntitiesCommand from 'gmp/commands/entities';
7+
import type Http from 'gmp/http/http';
8+
import {type Element} from 'gmp/models/model';
9+
import Ticket from 'gmp/models/ticket';
10+
11+
class TicketsCommand extends EntitiesCommand<Ticket> {
12+
constructor(http: Http) {
13+
super(http, 'ticket', Ticket);
14+
}
15+
16+
getEntitiesResponse(root: Element): Element {
17+
// @ts-expect-error
18+
return root.get_tickets.get_tickets_response;
19+
}
20+
}
21+
22+
export default TicketsCommand;

src/gmp/gmp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import 'gmp/commands/audit-reports';
77
import 'gmp/commands/license';
88
import 'gmp/commands/scan-configs';
9-
import 'gmp/commands/tickets';
109
import 'gmp/commands/tls-certificates';
1110

1211
import {getCommands} from 'gmp/command';
@@ -90,6 +89,8 @@ import TargetCommand from 'gmp/commands/target';
9089
import TargetsCommand from 'gmp/commands/targets';
9190
import TaskCommand from 'gmp/commands/task';
9291
import TasksCommand from 'gmp/commands/tasks';
92+
import TicketCommand from 'gmp/commands/ticket';
93+
import TicketsCommand from 'gmp/commands/tickets';
9394
import TimezonesCommand from 'gmp/commands/timezones';
9495
import TrashCanCommand from 'gmp/commands/trashcan';
9596
import UserCommand from 'gmp/commands/user';
@@ -201,6 +202,8 @@ class Gmp {
201202
public readonly targets: TargetsCommand;
202203
public readonly task: TaskCommand;
203204
public readonly tasks: TasksCommand;
205+
public readonly ticket: TicketCommand;
206+
public readonly tickets: TicketsCommand;
204207
public readonly timezones: TimezonesCommand;
205208
public readonly trashcan: TrashCanCommand;
206209
public readonly user: UserCommand;
@@ -316,6 +319,8 @@ class Gmp {
316319
this.targets = new TargetsCommand(this.http);
317320
this.task = new TaskCommand(this.http);
318321
this.tasks = new TasksCommand(this.http);
322+
this.ticket = new TicketCommand(this.http);
323+
this.tickets = new TicketsCommand(this.http);
319324
this.timezones = new TimezonesCommand(this.http);
320325
this.trashcan = new TrashCanCommand(this.http);
321326
this.user = new UserCommand(this.http);

src/gmp/models/ticket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {isEmpty} from 'gmp/utils/string';
1212

1313
export type TicketStatus = keyof typeof TICKET_STATUS;
1414

15-
interface TicketElement extends ModelElement {
15+
export interface TicketElement extends ModelElement {
1616
assigned_to?: {
1717
user?: {
1818
_id?: string;

0 commit comments

Comments
 (0)