This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snow.js
58 lines (49 loc) · 1.43 KB
/
snow.js
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
import * as core from "@actions/core";
import fetch from "node-fetch";
const request = async (config, method, { headers, body, params, path }) => {
const options = {
method,
headers: {
Accept: "application/json",
Authorization: `Basic ${config.basic}`,
...headers,
},
};
if (body) {
if (typeof body === "object") {
options.body = JSON.stringify(body);
options.headers["Content-Type"] = "application/json";
} else {
options.body = body;
}
}
let url = `${config.serviceNowUrl}${path}`;
if (params) {
url = `${url}?${new URLSearchParams(params)}`;
}
core.debug(`Making request to ${path}`);
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(
`Non-2xx response code from ServiceNow (${path}) [${
response.status
}]: ${await response.text()}`
);
}
return response.json();
};
export const newClient = (inputs) => {
const { serviceNowUsername, serviceNowPassword, serviceNowUrl } = inputs;
core.setSecret(serviceNowPassword);
core.info(`Using ServiceNow instance: ${serviceNowUrl}`);
const basic = Buffer.from(
`${serviceNowUsername}:${serviceNowPassword}`
).toString("base64");
core.setSecret(basic);
const config = { basic, serviceNowUrl };
return {
get: request.bind(null, config, "GET"),
patch: request.bind(null, config, "PATCH"),
post: request.bind(null, config, "POST"),
};
};