@@ -56,6 +56,10 @@ const newRulesTemplate = (repository?: Repository): RuleConfig => {
5656 'If issues are found, propose fixes for them' ,
5757 ] ,
5858 } ,
59+ {
60+ when : 'a call to a Codacy tool that has repository or organization as parameter returns 404 error' ,
61+ enforce : [ 'run codacy_setup_repository tool and then retry the action that failed' ] ,
62+ } ,
5963 ]
6064
6165 return {
@@ -69,29 +73,19 @@ const newRulesTemplate = (repository?: Repository): RuleConfig => {
6973const sanitizeJSON = ( json : string ) : string => {
7074 return json . replace ( / , ( [ \s \r \n ] * [ } \] ] ) / g, '$1' )
7175}
72- // Function to parse the json content inside an MDC file. An MDC file contains text elements that would generate errors when parsed as JSON
73- const parseMdcContent = ( content : string ) : RuleConfig => {
74- const parts = content . split ( '---' )
75-
76- if ( parts . length < 3 ) {
77- throw new Error ( 'Invalid MDC file format: missing frontmatter' )
78- }
79-
80- const jsonContent = parts [ 2 ] . trim ( )
81-
82- try {
83- return JSON . parse ( sanitizeJSON ( jsonContent ) )
84- } catch ( error ) {
85- throw new Error ( `Invalid JSON content in MDC file (${ ( error as Error ) . message } )` )
86- }
87- }
8876
8977const convertRulesToMarkdown = ( rules : RuleConfig , existingContent ?: string ) : string => {
9078 const codacyRules : string = existingContent ?. split ( '---' ) . filter ( ( part ) => part . includes ( rules . name ) ) [ 0 ] || ''
91- const newCodacyRules = `--- \n# ${ rules . name } \n${ rules . description } \n${ rules . rules
79+ const newCodacyRules = `\n# ${ rules . name } \n${ rules . description } \n${ rules . rules
9280 . map ( ( rule ) => `## When ${ rule . when } \n${ rule . enforce . join ( '\n - ' ) } ` )
93- . join ( '\n\n' ) } \n---`
94- return existingContent ? existingContent ?. replace ( `---${ codacyRules } ---` , newCodacyRules ) : newCodacyRules
81+ . join ( '\n\n' ) } \n`
82+
83+ if ( ! existingContent ) {
84+ return `---${ newCodacyRules } ---`
85+ }
86+ return codacyRules
87+ ? existingContent . replace ( codacyRules , newCodacyRules )
88+ : existingContent + `---${ newCodacyRules } ---`
9589}
9690
9791const rulesPrefixForMdc = `---
@@ -151,33 +145,14 @@ export async function createRules(repository: Repository) {
151145 }
152146
153147 if ( ! fs . existsSync ( rulesPath ) ) {
154- fs . writeFileSync (
155- rulesPath ,
156- `${ isMdc ? rulesPrefixForMdc : '' } ${
157- isMdc ? JSON . stringify ( newRules , null , 2 ) : convertRulesToMarkdown ( newRules )
158- } `
159- )
148+ fs . writeFileSync ( rulesPath , `${ isMdc ? rulesPrefixForMdc : '' } ${ convertRulesToMarkdown ( newRules ) } ` )
160149 Logger . appendLine ( `Created new rules file at ${ rulesPath } ` )
161150 addRulesToGitignore ( rulesPath )
162151 } else {
163152 try {
164153 const existingContent = fs . readFileSync ( rulesPath , 'utf8' )
165154
166- if ( isMdc ) {
167- const existingRules = parseMdcContent ( existingContent )
168- const mergedRules = {
169- ...existingRules ,
170- rules : [
171- ...( existingRules . rules . filter (
172- ( existingRule ) => ! newRules . rules ?. some ( ( newRule : Rule ) => newRule . when === existingRule . when )
173- ) || [ ] ) ,
174- ...newRules . rules ,
175- ] ,
176- }
177- fs . writeFileSync ( rulesPath , `${ rulesPrefixForMdc } ${ JSON . stringify ( mergedRules , null , 2 ) } ` )
178- } else {
179- fs . writeFileSync ( rulesPath , convertRulesToMarkdown ( newRules , existingContent ) )
180- }
155+ fs . writeFileSync ( rulesPath , convertRulesToMarkdown ( newRules , existingContent ) )
181156
182157 Logger . appendLine ( `Updated rules in ${ rulesPath } ` )
183158 } catch ( parseError ) {
0 commit comments