Skip to content

Commit

Permalink
Got integrations and application in.
Browse files Browse the repository at this point in the history
- Also added some safeties
  • Loading branch information
manchicken committed Jun 19, 2023
1 parent 1516f22 commit 77781c1
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 4 deletions.
2 changes: 2 additions & 0 deletions __mocks__/@actions/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ core.getInput = jest.fn().mockImplementation((name) => {
return inputs[name]
})

core.setFailed = jest.fn()

module.exports = core
99 changes: 99 additions & 0 deletions __tests__/lib/fieldMappings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @fileoverview This test covers all of the field mappings across versions.
**/

const core = require('@actions/core')
const { mappings, mapField } = require('../../lib/fieldMappings')

describe.each([
Expand Down Expand Up @@ -166,8 +167,94 @@ describe.each([
],
},
},
{
version: 'v2',
field: 'integrations',
input: `
pagerduty: "https://my-org.pagerduty.com/service-directory/PMyService"
`,
value: {
integrations: {
pagerduty: 'https://my-org.pagerduty.com/service-directory/PMyService',
},
},
},
{
version: 'v2',
field: 'integrations',
input: `
opsgenie:
service-url: "https://www.opsgenies.com/service/123e4567-e89b-12d3-a456-426614174000"
region: US
`,
value: {
integrations: {
opsgenie: {
'service-url':
'https://www.opsgenies.com/service/123e4567-e89b-12d3-a456-426614174000',
region: 'US',
},
},
},
},
{
version: 'v2.1',
field: 'integrations',
input: `
pagerduty:
service-url: "https://my-org.pagerduty.com/service-directory/PMyService"
`,
value: {
integrations: {
pagerduty: {
'service-url':
'https://my-org.pagerduty.com/service-directory/PMyService',
},
},
},
},
{
version: 'v2.1',
field: 'integrations',
input: `
opsgenie:
service-url: "https://www.opsgenies.com/service/123e4567-e89b-12d3-a456-426614174000"
region: US
`,
value: {
integrations: {
opsgenie: {
'service-url':
'https://www.opsgenies.com/service/123e4567-e89b-12d3-a456-426614174000',
region: 'US',
},
},
},
},
{
version: 'v2',
field: 'application',
input: 'app-name',
value: {
error:
'Sorry, but the «application» field is not avaiable in version v2 of the Datadog Service Catalog schema; this field is only available in version(s): v2.1',
},
},
{
version: 'v2.1',
field: 'application',
input: 'app-name',
value: { application: 'app-name' },
},
])('$field:$version', ({ version, field, input, value }) => {
beforeEach(() => {
core.setFailed.mockClear()
})
test('mapping', () => {
if (value?.error) {
expect(mapField(field, version)(input)).toBeUndefined()
return
}
console.debug(
JSON.stringify(
{ result: mapField(field, version)(input), value },
Expand All @@ -177,4 +264,16 @@ describe.each([
)
expect(mapField(field, version)(input)).toEqual(value)
})

test('failures', () => {
if (!value?.error) {
expect(core.setFailed).not.toHaveBeenCalled()
return
}

console.debug(`Field: ${field} - Version: ${version}`, mappings.application)
mapField(field, version)(input)
expect(core.setFailed).toHaveBeenCalledTimes(1)
expect(core.setFailed).toHaveBeenLastCalledWith(value.error)
})
})
32 changes: 28 additions & 4 deletions lib/fieldMappings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const core = require('@actions/core')
const _ = require('lodash')
const {
expandObjectInputs,
Expand All @@ -14,11 +15,20 @@ const passThru = (input, value) => ({ [input]: value })
const skipField = (_input, _value) => ({})
const simpleYamlParse = (input, str) => ({ [input]: expandObjectInputs(str) })

const versionCompatibilityError =
(field, chosenVersion, validVersions) => (_input) =>
core.setFailed(
`Sorry, but the «${field}» field is not avaiable in version ${chosenVersion} of the Datadog Service Catalog schema; this field is only available in version(s): ${validVersions.join(
',',
)}`,
)

/**
* This is the list of mappings which tracks which fields map to different versions in different ways.
* - Keyed by the GitHub Actions input name (action.yml)
* - Values are objects keyed with version tags
* - Values of those objects are the function which maps the input value to the registry document value.
* TODO: Add warnings for when folks try to use the wrong schema versions.
**/
const mappings = {
'schema-version': useSharedMappings(
Expand Down Expand Up @@ -65,13 +75,22 @@ const mappings = {
'v2.1': (input) => ({ links: forceArray(expandObjectInputs(input)) }),
},

integrations: {},
integrations: useSharedMappings(
['v2', 'v2.1'],
mapToUsing('integrations', simpleYamlParse),
),
// ({
// integrations: forceObject(expandObjectInputs(input)),
// })),

docs: {},

repo: {},
repos: {},

application: {},
application: {
v2: versionCompatibilityError('application', 'v2', ['v2.1']),
'v2.1': mapToUsing('application', passThru),
},

description: {},

Expand All @@ -83,10 +102,15 @@ const mappings = {

'slack-support-channel': {},

// TODO: Add convenience fields for opsgenie and pagerduty URLs.

repo: {},
}
const mapField = (field, version) => (input) =>
mappings?.[field]?.[version](input)
(
mappings?.[field]?.[version] ??
((_) => core.setFailed(`Unknown field: ${field}`))
)(input)

module.exports = {
mappings,
Expand Down

0 comments on commit 77781c1

Please sign in to comment.