Skip to content

Commit 2969a77

Browse files
fix(rules): upload Automated Rules using form endpoint (#1790) (#1824)
(cherry picked from commit f83c546) Co-authored-by: Andrew Azores <[email protected]>
1 parent 47ba348 commit 2969a77

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/app/Shared/Services/Api.service.tsx

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,32 +228,43 @@ export class ApiService {
228228
onUploadProgress?: (progress: string | number) => void,
229229
abortSignal?: Observable<void>,
230230
): Observable<boolean> {
231+
const body = new window.FormData();
232+
Object.entries(rule).forEach((e) => {
233+
if (!e || !e[0] || !e[1]) {
234+
return;
235+
}
236+
if (e[0] === 'metadata') {
237+
const labels = {};
238+
e[1].labels.forEach((kv: KeyValue) => {
239+
labels[kv.key] = kv.value;
240+
});
241+
body.append(e[0], JSON.stringify({ labels }));
242+
} else {
243+
body.append(e[0], e[1]);
244+
}
245+
});
231246
window.onbeforeunload = (event: BeforeUnloadEvent) => event.preventDefault();
232-
return this.ctx
233-
.headers({
234-
'Content-Type': 'application/json',
235-
})
236-
.pipe(
237-
concatMap((headers) =>
238-
this.sendLegacyRequest('v4', 'rules', 'Rule Upload Failed', {
239-
method: 'POST',
240-
body: JSON.stringify(rule),
241-
headers,
242-
listeners: {
243-
onUploadProgress: (event) => {
244-
onUploadProgress && onUploadProgress(Math.floor((event.loaded * 100) / event.total));
245-
},
247+
return this.ctx.headers().pipe(
248+
concatMap((headers) =>
249+
this.sendLegacyRequest('v4', 'rules', 'Rule Upload Failed', {
250+
body,
251+
method: 'POST',
252+
headers,
253+
listeners: {
254+
onUploadProgress: (event) => {
255+
onUploadProgress && onUploadProgress(Math.floor((event.loaded * 100) / event.total));
246256
},
247-
abortSignal,
248-
}),
249-
),
250-
map((resp) => resp.ok),
251-
tap({
252-
next: () => (window.onbeforeunload = null),
253-
error: () => (window.onbeforeunload = null),
257+
},
258+
abortSignal,
254259
}),
255-
first(),
256-
);
260+
),
261+
map((resp) => resp.ok),
262+
tap({
263+
next: () => (window.onbeforeunload = null),
264+
error: () => (window.onbeforeunload = null),
265+
}),
266+
first(),
267+
);
257268
}
258269

259270
createRule(rule: Rule): Observable<boolean> {

0 commit comments

Comments
 (0)