Skip to content

Commit 749a06c

Browse files
committed
Add support for V1 mapping
Add a v1 definition and transformation step Update index.js Co-authored-by: Pelle Wessman <[email protected]> Signed-off-by: Bret Comnes <[email protected]> Remove beta field Update index.js Co-authored-by: Pelle Wessman <[email protected]> Signed-off-by: Bret Comnes <[email protected]> Update index.js Co-authored-by: Pelle Wessman <[email protected]> Signed-off-by: Bret Comnes <[email protected]> Update test/parse.spec.js Co-authored-by: Pelle Wessman <[email protected]> Signed-off-by: Bret Comnes <[email protected]> Feedback Options
1 parent 82061a5 commit 749a06c

File tree

7 files changed

+113
-13
lines changed

7 files changed

+113
-13
lines changed

index.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const { default: Ajv } = require('ajv')
66
const { ErrorWithCause } = require('pony-cause')
77
const { parse: yamlParse } = require('yaml')
88

9+
const { socketTmlSchemaV1 } = require('./lib/v1')
10+
911
/**
1012
* @typedef SocketYmlGitHub
11-
* @property {boolean} [beta] beta opt in field
1213
* @property {boolean} [enabled] enable/disable the Socket.dev GitHub app entirely
1314
* @property {boolean} [projectReportsEnabled] enable/disable Github app project report checks
1415
* @property {boolean} [pullRequestAlertsEnabled] enable/disable GitHub app pull request alert checks
@@ -31,22 +32,21 @@ const socketYmlSchema = {
3132
projectIgnorePaths: {
3233
type: 'array',
3334
items: { type: 'string' },
34-
nullable: true,
35+
nullable: true
3536
},
3637
issueRules: {
3738
type: 'object',
3839
nullable: true,
3940
required: [],
40-
additionalProperties: { type: 'boolean' },
41+
additionalProperties: { type: 'boolean' }
4142
},
4243
githubApp: {
4344
type: 'object',
4445
nullable: true,
4546
properties: {
46-
beta: { type: 'boolean', nullable: true },
47-
enabled: { type: 'boolean', nullable: true },
48-
projectReportsEnabled: { type: 'boolean', nullable: true },
49-
pullRequestAlertsEnabled: { type: 'boolean', nullable: true },
47+
enabled: { type: 'boolean', nullable: true, default: true },
48+
projectReportsEnabled: { type: 'boolean', nullable: true, default: true },
49+
pullRequestAlertsEnabled: { type: 'boolean', nullable: true, default: true },
5050
},
5151
required: [],
5252
additionalProperties: false,
@@ -56,14 +56,24 @@ const socketYmlSchema = {
5656
additionalProperties: false,
5757
}
5858

59-
const ajv = new Ajv({
59+
const ajvOptions = /** @type {const} */ ({
6060
allErrors: true,
6161
coerceTypes: 'array',
6262
logger: false,
63+
useDefaults: true
64+
})
65+
const ajv = new Ajv({
66+
...ajvOptions,
6367
removeAdditional: 'failing',
6468
})
6569

6670
const validate = ajv.compile(socketYmlSchema)
71+
// We want to be strict and fail rather than removeAdditional when we parse a possible v1 config – only fallback to it when it actually matches well
72+
const ajvV1 = new Ajv({
73+
...ajvOptions
74+
})
75+
76+
const validateV1 = ajvV1.compile(socketTmlSchemaV1)
6777

6878
/**
6979
* @param {string} filePath
@@ -101,6 +111,13 @@ async function parseSocketConfig (fileContent) {
101111
throw new ErrorWithCause('Error when parsing socket.yml config', { cause: err })
102112
}
103113

114+
if (parsedContent && typeof parsedContent === 'object' && !('version' in parsedContent)) {
115+
const parsedV1 = await parseV1SocketConfig(parsedContent)
116+
if (parsedV1) {
117+
return parsedV1
118+
}
119+
}
120+
104121
if (!validate(parsedContent)) {
105122
throw new SocketValidationError(
106123
'Invalid config definition',
@@ -146,6 +163,29 @@ class SocketValidationError extends Error {
146163
}
147164
}
148165

166+
/**
167+
* @param {object} parsedV1Content
168+
* @returns {Promise<SocketYml | undefined>}
169+
*/
170+
async function parseV1SocketConfig (parsedV1Content) {
171+
if (!validateV1(parsedV1Content)) {
172+
return
173+
}
174+
175+
/** @type {SocketYml} */
176+
const v2 = {
177+
version: 2,
178+
projectIgnorePaths: parsedV1Content?.ignore ?? [],
179+
issueRules: parsedV1Content?.issues ?? {},
180+
githubApp: {
181+
enabled: parsedV1Content?.enabled,
182+
pullRequestAlertsEnabled: parsedV1Content?.pullRequestAlertsEnabled,
183+
projectReportsEnabled: parsedV1Content?.projectReportsEnabled
184+
}
185+
}
186+
return v2
187+
}
188+
149189
module.exports = {
150190
parseSocketConfig,
151191
readSocketConfig,

lib/v1.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @typedef SocketYmlV1
3+
* @property {string[]} [ignore]
4+
* @property {{ [issueName: string]: boolean }} [issues]
5+
* @property {boolean} [beta] unused v1 option
6+
* @property {boolean} [enabled] enable/disable the Socket.dev GitHub app entirely
7+
* @property {boolean} [projectReportsEnabled] enable/disable Github app project report checks
8+
* @property {boolean} [pullRequestAlertsEnabled] enable/disable GitHub app pull request alert checks
9+
*/
10+
11+
/** @type {import('ajv').JSONSchemaType<SocketYmlV1>} */
12+
const socketTmlSchemaV1 = {
13+
$schema: 'http://json-schema.org/draft-07/schema#',
14+
type: 'object',
15+
properties: {
16+
ignore: {
17+
type: 'array',
18+
items: { type: 'string' },
19+
nullable: true,
20+
},
21+
issues: {
22+
type: 'object',
23+
nullable: true,
24+
required: [],
25+
additionalProperties: { type: 'boolean' },
26+
},
27+
beta: { type: 'boolean', nullable: true, default: true },
28+
enabled: { type: 'boolean', nullable: true, default: true },
29+
projectReportsEnabled: { type: 'boolean', nullable: true, default: true },
30+
pullRequestAlertsEnabled: { type: 'boolean', nullable: true, default: true },
31+
},
32+
minProperties: 1,
33+
additionalProperties: false,
34+
}
35+
36+
module.exports = {
37+
socketTmlSchemaV1
38+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"index.js",
2222
"index.d.ts",
2323
"index.d.ts.map",
24-
"schema.json"
24+
"schema.json",
25+
"lib/*"
2526
],
2627
"scripts": {
2728
"build:0": "run-s clean",

test/parse.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ const {
1414
chai.use(chaiAsPromised)
1515
chai.should()
1616

17-
describe('readSocketConfig()', () => {
17+
describe('parseSocketConfig()', () => {
1818
it('should read and parse socket.yml', async () => {
1919
const fileContent = await readFile(path.resolve(__dirname, 'sample.yml'), 'utf8')
2020

2121
await parseSocketConfig(fileContent).should.eventually.become({
2222
'githubApp': {
23-
'beta': false,
2423
'enabled': true,
2524
'projectReportsEnabled': true,
2625
'pullRequestAlertsEnabled': true,
@@ -36,6 +35,24 @@ describe('readSocketConfig()', () => {
3635
})
3736
})
3837

38+
it('should read and parse socket.yml v1', async () => {
39+
const fileContent = await readFile(path.resolve(__dirname, 'sample-v1.yml'), 'utf8')
40+
41+
await parseSocketConfig(fileContent).should.eventually.become({
42+
'githubApp': {
43+
'enabled': true,
44+
'projectReportsEnabled': false,
45+
'pullRequestAlertsEnabled': true,
46+
},
47+
'issueRules': {},
48+
'projectIgnorePaths': [
49+
'foo',
50+
'bar',
51+
],
52+
'version': 2,
53+
})
54+
})
55+
3956
it('should throw on invalid document structure', async () => {
4057
await parseSocketConfig(`
4158
projectIgnorePaths: true

test/read.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('readSocketConfig()', () => {
1616
it('should read and parse socket.yml', async () => {
1717
await readSocketConfig(path.resolve(__dirname, 'sample.yml')).should.eventually.become({
1818
'githubApp': {
19-
'beta': false,
2019
'enabled': true,
2120
'projectReportsEnabled': true,
2221
'pullRequestAlertsEnabled': true,

test/sample-v1.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ignore:
2+
- foo
3+
- bar
4+
projectReportsEnabled: false
5+
beta: true

test/sample.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2
22

33
projectIgnorePaths:
44
- workspaces/test*
5-
- '!workspaces/test-framework'
5+
- "!workspaces/test-framework"
66

77
issueRules:
88
unresolvedRequire: false

0 commit comments

Comments
 (0)