-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautomation-AssignAnalystToIncident.yml
137 lines (130 loc) · 4.57 KB
/
automation-AssignAnalystToIncident.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
args:
- default: true
description: The optional list of roles we want to assign users from. Can accept
arrays or comma separated list. Leave empty to fetch all users.
name: roles
- auto: PREDEFINED
description: '(default: random) You can pick how to assign the owner - by random,
online, current, machine-learning, top-user or less-busy-user.'
name: assignBy
predefined:
- random
- machine-learning
- top-user
- less-busy-user
- online
- current
- description: When specified, the provided user will be assigned as the incident
owner (optional).
name: username
- description: When specified, the user of provided email will be assigned as the
incident owner (optional)
name: email
comment: |-
Assign analyst to incident.
By default, the analyst is picked randomly from the available users, according to the provided roles (if no roles provided, will fetch all users).
Otherwise, the analyst will be picked according to the 'assignBy' arguments.
machine-learning: DBot will calculated and decide who is the best analyst for the job.
top-user: The user that is most commonly owns this type of incident
less-busy-user: The less busy analyst will be picked to be the incident owner.
online: The analyst is picked randomly from all online analysts, according to the provided roles (if no roles provided, will fetch all users).
current: The user that executed the command
commonfields:
id: AssignAnalystToIncident
version: -1
enabled: true
name: AssignAnalystToIncident
runonce: false
script: |
if (args.email && args.username) {
throw 'Please provide either username or email';
}
var emailToAssign = args.email;
var userToAssign = args.username;
if (emailToAssign) {
userToAssign = '';
var res = executeCommand('getUsers',{});
if (res && res[0] && res[0].Contents) {
res[0].Contents.forEach(function(user) {
if (user.email === args.email) {
userToAssign = user.id;
}
});
}
if (!userToAssign) {
throw 'Cannot find user with email ' + args.email;
}
}
assignBy = args.assignBy || 'random';
function pickRandomUser(usersRes) {
var usersList = usersRes[0].Contents.map(function (u) { return u.username });
userToAssign = usersList[Math.floor(Math.random() * usersList.length)];
}
if (!userToAssign) {
switch(assignBy) {
case 'online':
var usersRes = executeCommand('getUsers', { roles: args.roles, online: true });
if (isError(usersRes[0])) {
return usersRes[0];
}
pickRandomUser(usersRes);
break;
case 'current':
var usersRes = executeCommand('getUsers', { current: true });
if (isError(usersRes[0])) {
return usersRes[0];
}
pickRandomUser(usersRes);
break;
case 'random':
var usersRes = executeCommand('getUsers', { roles: args.roles });
if (isError(usersRes[0])) {
return usersRes[0];
}
pickRandomUser(usersRes);
break;
default:
res = executeCommand("getOwnerSuggestion", {})[0].Contents;
switch (assignBy) {
case 'machine-learning':
userToAssign = res.ownerByMl;
break;
case 'top-user':
userToAssign = res.topOwner;
break;
case 'less-busy-user':
userToAssign = res.userLeastLoad;
break;
}
if (!userToAssign) {
var usersRes = executeCommand('getUsers', { roles: args.roles });
if (isError(usersRes[0])) {
return usersRes[0];
}
pickRandomUser(usersRes);
}
}
}
if (userToAssign) {
var res = executeCommand("setOwner", { owner: userToAssign });
if (!isError(res[0])) {
return 'User \'' + userToAssign + '\' assigned to be the incident owner.';
} else {
return {
ContentsFormat: formats.text,
Type: entryTypes.error,
Contents: 'Failed to assign user: \'' + userToAssign + '\', error: ' + res[0].Contents
};
}
} else {
return {
ContentsFormat: formats.text,
Type: entryTypes.error,
Contents: 'No user found.'
};
}
scripttarget: 0
system: true
tags:
- Utility
type: javascript