Skip to content

Commit

Permalink
Added support for extensions. (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchicken committed Feb 8, 2024
1 parent e3cc7b6 commit 713ae51
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 121 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## [2.2.0] - unreleased
## [2.3.0] - 2024-02-07

### Added

- Support for extensions, they're just a straight YAML-to-JSON conversion.

## [2.2.0] - 2023-12-21

### Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ exports[`constants mappings 1`] = `
"v2.1": [Function],
"v2.2": [Function],
},
"extensions": {
"v2": [Function],
"v2.1": [Function],
"v2.2": [Function],
},
"integrations": {
"v2": [Function],
"v2.1": [Function],
Expand Down Expand Up @@ -114,5 +119,6 @@ exports[`constants schemaFields 1`] = `
"docs",
"repos",
"ci-pipeline-fingerprints",
"extensions",
]
`;
6 changes: 6 additions & 0 deletions __tests__/lib/__snapshots__/fieldMappings.test.cjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ exports[`constants mappings 1`] = `
"v2.1": [Function],
"v2.2": [Function],
},
"extensions": {
"v2": [Function],
"v2.1": [Function],
"v2.2": [Function],
},
"integrations": {
"v2": [Function],
"v2.1": [Function],
Expand Down Expand Up @@ -114,5 +119,6 @@ exports[`constants schemaFields 1`] = `
"docs",
"repos",
"ci-pipeline-fingerprints",
"extensions",
]
`;
34 changes: 34 additions & 0 deletions __tests__/lib/fieldMappings-schema.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,40 @@ opsgenie:
'Sorry, but the «repos» field is not avaiable in version v2.1 of the Datadog Service Catalog schema; this field is only available in version(s): v2',
},
},
{
version: 'v2.2',
field: 'extensions',
input: `
shopist.com/release-scheduler:
release-manager:
slack: "release-train-shopist"
schedule: "* * * * *"
env:
- name: "staging"
ci_pipeline: "//domains/examples/apps/hello-joe/config/k8s:release-staging"
branch: "hello-joe/staging"
schedule: "* * * * 1"
`,
value: {
extensions: {
'shopist.com/release-scheduler': {
'release-manager': {
slack: 'release-train-shopist',
schedule: '* * * * *',
env: [
{
name: 'staging',
ci_pipeline:
'//domains/examples/apps/hello-joe/config/k8s:release-staging',
branch: 'hello-joe/staging',
schedule: '* * * * 1',
},
],
},
},
},
},
},
])('$field:$version', ({ version, field, input, value }) => {
afterAll(() => {
core.setFailed.mockClear()
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inputs:

# Fields which are common to all schema versions
schema-version:
description: 'The version of the Datadog Service Catalog Schema. The default is v2. Valid values are `v2` and `v2.1`.'
description: 'The version of the Datadog Service Catalog Schema. For backwards-compatibility reasons, the default is v2. Valid values are `v2` and `v2.1`.'
required: false
default: 'v2'
service-name:
Expand All @@ -52,6 +52,9 @@ inputs:
integrations:
description: 'The integrations for the service'
required: false
extensions:
description: 'Any custom extensions you wish to use'
required: false

# Fields which are unique to v2
docs:
Expand Down
16 changes: 13 additions & 3 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30494,18 +30494,23 @@ var require_input_expander = __commonJS({
"lib/input-expander.cjs"(exports2, module2) {
var core2 = require_core();
var YAML = require_dist();
var FailedParse = Symbol("This will never match.");
var parseSafely = (x) => {
try {
return YAML.parse(x);
} catch (e) {
return Symbol("This will never match.");
return FailedParse;
}
};
var isArray = Array.isArray;
var isObject = (x) => x?.constructor === Object;
var isYamlScalarEquivalent = (x) => !x || ["string", "number", "boolean"].includes(typeof x);
var isJustAScalar = (x) => !x || x[0] === "@" || x === parseSafely(`${x}`);
var isJustAScalar = (x) => !x || x[0] === "@" || parseSafely(`${x}`) === FailedParse || x === parseSafely(`${x}`);
var deserializeNestedStrings = (input) => {
const out = _deserializeNestedStrings(input);
return out;
};
var _deserializeNestedStrings = (input) => {
if (isYamlScalarEquivalent(input)) {
return isJustAScalar(input) ? input : deserializeNestedStrings(parseSafely(input));
}
Expand Down Expand Up @@ -30552,6 +30557,7 @@ var require_fieldMappings = __commonJS({
var useSharedMappings = (versions, mapper) => Object.assign(...versions.map((x) => ({ [x]: mapper })));
var mapToUsing = (input, func) => (value) => func(input, value);
var passThru = (input, value) => ({ [input]: value });
var simpleYamlParse = (input, str) => ({ [input]: expandObjectInputs(str) });
var arrayYamlParse = (input, str) => ({
[input]: forceArray(expandObjectInputs(str))
});
Expand Down Expand Up @@ -30681,7 +30687,11 @@ var require_fieldMappings = __commonJS({
"v2.2"
]),
"v2.2": mapToUsing("ci-pipeline-fingerprints", arrayYamlParse)
}
},
extensions: useSharedMappings(
["v2", "v2.1", "v2.2"],
mapToUsing("extensions", simpleYamlParse)
)
};
Object.freeze(mappings);
var schemaFields = _.keys(mappings);
Expand Down
10 changes: 5 additions & 5 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const registerWithDataDog = async (apiKey, appKey, ddHost, configJsonStr) => {
core.debug(`JSON: ${configJsonStr}`)
// Prep the auth
const client = new HttpClient(
'nodejs - GitHub Actions - arcxp/datadog-service-catalog-metadata-provider',
'nodejs - GitHub Actions - arcxp/datadog-service-catalog-metadata-provider'
)

const response = await client.post(
Expand All @@ -27,15 +27,15 @@ const registerWithDataDog = async (apiKey, appKey, ddHost, 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}`,
`Failed to register service with DataDog. Status Code: ${statusCode} Body: ${body}`
)
}
}
Expand All @@ -51,7 +51,7 @@ const run = async (configs) => {

if (!apiKey || !appKey) {
return core.setFailed(
'Both `datadog-key` and `datadog-app-key` are required.',
'Both `datadog-key` and `datadog-app-key` are required.'
)
}

Expand All @@ -78,7 +78,7 @@ 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)}`,
`Inputs coming off of configs: ${JSON.stringify(configs, undefined, 2)}`
)

return configs
Expand Down
5 changes: 5 additions & 0 deletions lib/fieldMappings.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ const mappings = {
]),
'v2.2': mapToUsing('ci-pipeline-fingerprints', arrayYamlParse),
},

extensions: useSharedMappings(
['v2', 'v2.1', 'v2.2'],
mapToUsing('extensions', simpleYamlParse)
),
}
Object.freeze(mappings)

Expand Down
23 changes: 19 additions & 4 deletions lib/input-expander.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@
* @author Mike Stemle <themanchicken@duck.com>
*/

// This is my little debug function. Please don't remove.
// const ___ = (note, x) => {
// console.debug(note, x)
// return x
// }

const core = require('@actions/core')
const YAML = require('yaml')

/**
* As part of our recursive implementation, we need
* to engage in a little bit of type inference.
* @private
* @param {string} x - The input value.
* @returns {Object|Symbol} The parsed YAML object, or a Symbol.
*/
const FailedParse = Symbol('This will never match.')
const parseSafely = (x) => {
try {
return YAML.parse(x)
} catch (e) {
return Symbol('This will never match.')
return FailedParse
}
}

Expand Down Expand Up @@ -56,7 +62,11 @@ const isYamlScalarEquivalent = (x) =>
* @returns {boolean} Whether or not the input is a scalar.
* @private
**/
const isJustAScalar = (x) => !x || x[0] === '@' || x === parseSafely(`${x}`)
const isJustAScalar = (x) =>
!x ||
x[0] === '@' ||
parseSafely(`${x}`) === FailedParse ||
x === parseSafely(`${x}`)

/**
* This function deserializes YAML strings which contain
Expand All @@ -65,6 +75,11 @@ const isJustAScalar = (x) => !x || x[0] === '@' || x === parseSafely(`${x}`)
* @private
*/
const deserializeNestedStrings = (input) => {
const out = _deserializeNestedStrings(input)
return out
}

const _deserializeNestedStrings = (input) => {
if (isYamlScalarEquivalent(input)) {
return isJustAScalar(input)
? input
Expand All @@ -79,7 +94,7 @@ const deserializeNestedStrings = (input) => {
return Object.assign(
...Object.keys(input).map((x) => ({
[x]: deserializeNestedStrings(input[x]),
})),
}))
)
}
}
Expand Down
Loading

0 comments on commit 713ae51

Please sign in to comment.