@@ -6,9 +6,10 @@ const { default: Ajv } = require('ajv')
6
6
const { ErrorWithCause } = require ( 'pony-cause' )
7
7
const { parse : yamlParse } = require ( 'yaml' )
8
8
9
+ const { socketYmlSchemaV1 } = require ( './lib/v1' )
10
+
9
11
/**
10
12
* @typedef SocketYmlGitHub
11
- * @property {boolean } [beta] beta opt in field
12
13
* @property {boolean } [enabled] enable/disable the Socket.dev GitHub app entirely
13
14
* @property {boolean } [projectReportsEnabled] enable/disable Github app project report checks
14
15
* @property {boolean } [pullRequestAlertsEnabled] enable/disable GitHub app pull request alert checks
@@ -31,22 +32,21 @@ const socketYmlSchema = {
31
32
projectIgnorePaths : {
32
33
type : 'array' ,
33
34
items : { type : 'string' } ,
34
- nullable : true ,
35
+ nullable : true
35
36
} ,
36
37
issueRules : {
37
38
type : 'object' ,
38
39
nullable : true ,
39
40
required : [ ] ,
40
- additionalProperties : { type : 'boolean' } ,
41
+ additionalProperties : { type : 'boolean' }
41
42
} ,
42
43
githubApp : {
43
44
type : 'object' ,
44
45
nullable : true ,
45
46
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 } ,
50
50
} ,
51
51
required : [ ] ,
52
52
additionalProperties : false ,
@@ -56,15 +56,27 @@ const socketYmlSchema = {
56
56
additionalProperties : false ,
57
57
}
58
58
59
- const ajv = new Ajv ( {
59
+ const ajvOptions = /** @type { const } */ ( {
60
60
allErrors : true ,
61
61
coerceTypes : 'array' ,
62
62
logger : false ,
63
+ useDefaults : true
64
+ } )
65
+
66
+ const ajv = new Ajv ( {
67
+ ...ajvOptions ,
63
68
removeAdditional : 'failing' ,
64
69
} )
65
70
66
71
const validate = ajv . compile ( socketYmlSchema )
67
72
73
+ // 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
74
+ const ajvV1 = new Ajv ( {
75
+ ...ajvOptions
76
+ } )
77
+
78
+ const validateV1 = ajvV1 . compile ( socketYmlSchemaV1 )
79
+
68
80
/**
69
81
* @param {string } filePath
70
82
* @returns {Promise<SocketYml|undefined> }
@@ -101,6 +113,13 @@ async function parseSocketConfig (fileContent) {
101
113
throw new ErrorWithCause ( 'Error when parsing socket.yml config' , { cause : err } )
102
114
}
103
115
116
+ if ( parsedContent && typeof parsedContent === 'object' && ! ( 'version' in parsedContent ) ) {
117
+ const parsedV1 = await parseV1SocketConfig ( parsedContent )
118
+ if ( parsedV1 ) {
119
+ return parsedV1
120
+ }
121
+ }
122
+
104
123
if ( ! validate ( parsedContent ) ) {
105
124
throw new SocketValidationError (
106
125
'Invalid config definition' ,
@@ -146,6 +165,29 @@ class SocketValidationError extends Error {
146
165
}
147
166
}
148
167
168
+ /**
169
+ * @param {object } parsedV1Content
170
+ * @returns {Promise<SocketYml | undefined> }
171
+ */
172
+ async function parseV1SocketConfig ( parsedV1Content ) {
173
+ if ( ! validateV1 ( parsedV1Content ) ) {
174
+ return
175
+ }
176
+
177
+ /** @type {SocketYml } */
178
+ const v2 = {
179
+ version : 2 ,
180
+ projectIgnorePaths : parsedV1Content ?. ignore ?? [ ] ,
181
+ issueRules : parsedV1Content ?. issues ?? { } ,
182
+ githubApp : {
183
+ enabled : parsedV1Content ?. enabled ,
184
+ pullRequestAlertsEnabled : parsedV1Content ?. pullRequestAlertsEnabled ,
185
+ projectReportsEnabled : parsedV1Content ?. projectReportsEnabled
186
+ }
187
+ }
188
+ return v2
189
+ }
190
+
149
191
module . exports = {
150
192
parseSocketConfig,
151
193
readSocketConfig,
0 commit comments