-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.cjs
87 lines (74 loc) · 2.65 KB
/
index.cjs
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
const core = require('@actions/core')
const { HttpClient } = require('@actions/http-client')
const { DatadogPostGovernor } = require('./lib/governors')
const { inputsToRegistryDocument } = require('./lib/input-to-registry-document')
const { validateDatadogHostname } = require('./lib/input-validation')
const { fetchAndApplyOrgRules } = require('./lib/org-rules')
/**
* This function takes the config JSON string and registers the service with
* DataDog.
* @param {string} config - The config JSON string.
**/
const registerWithDataDog = async (apiKey, appKey, ddHost, configJsonStr) => {
DatadogPostGovernor.increment()
core.debug(`JSON: «${configJsonStr}»`)
// Prep the auth
const client = new HttpClient(
'nodejs - GitHub Actions - arcxp/datadog-service-catalog-metadata-provider',
)
const response = await client.post(
`https://${ddHost}/api/v2/services/definitions`,
configJsonStr,
{
'DD-API-KEY': apiKey,
'DD-APPLICATION-KEY': appKey,
'Content-Type': 'application/json',
},
)
const statusCode = response.message.statusCode
const body = await response.readBody()
core.debug(`Response status code: ${statusCode}, with body: ${body}`)
if (statusCode !== 200) {
core.setFailed(
`Failed to register service with DataDog. Status Code: ${statusCode} Body: ${body}`,
)
}
}
const run = async (configs) => {
try {
// Extract the API key for DataDog
const apiKey = core.getInput('datadog-key')
const appKey = core.getInput('datadog-app-key')
// Initialize the Post governor to help prevent excessive calls to Datadog
DatadogPostGovernor.init()
if (!apiKey || !appKey) {
return core.setFailed(
'Both `datadog-key` and `datadog-app-key` are required.',
)
}
// Fetch and verify the host.
const ddHost = validateDatadogHostname(core.getInput('datadog-hostname'))
// Verify the org config
if (await fetchAndApplyOrgRules(configs)) {
// Debug all of the info
core.debug(`All of the configs: ${JSON.stringify(configs, undefined, 2)}`)
// Register the service with DataDog
await registerWithDataDog(apiKey, appKey, ddHost, JSON.stringify(configs))
}
} catch (error) {
console.error(error)
core.setFailed(error.message)
}
}
// Grab the inputs and then run with them!
core.debug('STARTING THE PARSE')
Promise.resolve()
.then(() => inputsToRegistryDocument())
.then((configs) => {
core.debug(`Input schema version is «${core.getInput('schema-version')}»`)
core.debug(
`Inputs coming off of configs: ${JSON.stringify(configs, undefined, 2)}`,
)
return configs
})
.then((configs) => run(configs))