Skip to content

Commit

Permalink
fix: Fail on duplicate patternId on patterns.json or description.json (
Browse files Browse the repository at this point in the history
…#152)

* fix: Fail on duplicate patternId on patterns.json or description.json

* Update JsonTests.scala

- minor changes (irrelevant to code logic)

* reorganize code

* reorganize code

* remove duplicate import; rerun fmt;

---------

Co-authored-by: Vítor Pires <[email protected]>
Co-authored-by: Vítor Pires <[email protected]>
  • Loading branch information
3 people authored Feb 26, 2024
1 parent 262427b commit c6d7c1b
Showing 1 changed file with 68 additions and 36 deletions.
104 changes: 68 additions & 36 deletions src/main/scala/codacy/plugins/test/JsonTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package codacy.plugins.test

import codacy.utils.CollectionHelper
import com.codacy.plugins.api.PatternDescription
import com.codacy.plugins.api.results.{Category, Pattern, Tool}
import com.codacy.plugins.api.results.{Category, Pattern}
import com.codacy.plugins.results.traits.DockerToolDocumentation
import com.codacy.plugins.runners.IDocker
import com.codacy.plugins.utils.BinaryDockerHelper
Expand Down Expand Up @@ -39,9 +39,6 @@ object JsonTests extends ITest {
}
}

val onlyAllowedCategories =
dockerToolDocumentation.toolSpecification.map(validatePatternCategory).getOrElse(true)

val noMismatchBetweenPatternsAndDescription =
(dockerToolDocumentation.toolSpecification, dockerToolDocumentation.patternDescriptions) match {
case (Some(tool), Some(descriptions)) =>
Expand All @@ -55,21 +52,12 @@ object JsonTests extends ITest {
generateUniquePatternSignature(description.patternId.value, parameters)
}).fastDiff

val duplicateDescriptions = descriptions.groupBy(_.patternId).filter { case (_, v) => v.size > 1 }
if (duplicateDescriptions.nonEmpty) {
error(s"""
|Some patterns were duplicated in /docs/description/description.json
|
| * ${duplicateDescriptions.map { case (patternId, _) => patternId }.mkString(",")}
""".stripMargin)
}

if (diffResult.newObjects.nonEmpty) {
error(s"""
|Some patterns were only found in /docs/patterns.json
|Confirm that all the patterns and parameters present in /docs/patterns.json are also present in /docs/description/description.json
|
| * ${diffResult.newObjects.map(_.patternId).mkString(",")}
| * ${diffResult.newObjects.map(_.patternId).mkString(", ")}
""".stripMargin)
}

Expand All @@ -78,51 +66,95 @@ object JsonTests extends ITest {
|Some patterns were only found in /docs/description/description.json
|Confirm that all the patterns and parameters present in /docs/description/description.json are also present in /docs/patterns.json
|
| * ${diffResult.deletedObjects.map(_.patternId).mkString(",")}
| * ${diffResult.deletedObjects.map(_.patternId).mkString(", ")}
""".stripMargin)
}

ignoreDescriptions ||
(diffResult.newObjects.isEmpty && diffResult.deletedObjects.isEmpty)

case _ => ignoreDescriptions
}

val noInvalidDescriptions =
(dockerToolDocumentation.patternDescriptions) match {
case Some(descriptions) =>
val titlesAboveLimit = descriptions.filter(_.title.length > 255)
if (titlesAboveLimit.nonEmpty) {
error(s"""
|Some titles are too big in /docs/description/description.json
|The max size of a title is 255 characters
|
| * ${titlesAboveLimit.map(_.patternId).mkString(", ")}
|Some titles are too big in /docs/description/description.json
|The max size of a title is 255 characters
|
| * ${titlesAboveLimit.map(_.patternId).mkString(", ")}
""".stripMargin)
}

val descriptionsAboveLimit = descriptions.filter(_.description.getOrElse("").length > 500)
if (descriptionsAboveLimit.nonEmpty) {
error(s"""
|Some descriptions are too big in /docs/description/description.json
|The max size of a description is 500 characters
|Some descriptions are too big in /docs/description/description.json
|The max size of a description is 500 characters
|
| * ${descriptionsAboveLimit.map(_.patternId).mkString(", ")}
""".stripMargin)
}

ignoreDescriptions ||
(titlesAboveLimit.isEmpty && descriptionsAboveLimit.isEmpty)

case _ => ignoreDescriptions
}

val noDuplicatePatterns =
(dockerToolDocumentation.toolSpecification, dockerToolDocumentation.patternDescriptions) match {
case (Some(tool), Some(descriptions)) =>
val duplicatePatterns = tool.patterns.groupBy(_.patternId).filter { case (_, v) => v.size > 1 }
if (duplicatePatterns.nonEmpty) {
error(s"""
|Some patterns are duplicated in /docs/patterns.json
|
| * ${descriptionsAboveLimit.map(_.patternId).mkString(", ")}
| * ${duplicatePatterns.map { case (patternId, _) => patternId }.mkString(", ")}
""".stripMargin)
}

val duplicateDescriptions = descriptions.groupBy(_.patternId).filter { case (_, v) => v.size > 1 }
if (duplicateDescriptions.nonEmpty) {
error(s"""
|Some patterns are duplicated in /docs/description/description.json
|
| * ${duplicateDescriptions.map { case (patternId, _) => patternId }.mkString(", ")}
""".stripMargin)
}

ignoreDescriptions ||
(diffResult.newObjects.isEmpty && diffResult.deletedObjects.isEmpty)
(duplicatePatterns.isEmpty && duplicateDescriptions.isEmpty)

case _ => ignoreDescriptions
}

onlyAllowedCategories && noMismatchBetweenPatternsAndDescription
}
val noInvalidPatternCategories =
dockerToolDocumentation.toolSpecification match {
case Some(tool) =>
val invalidCategories =
tool.patterns.filterNot(pattern => Category.allowedCategories.contains(pattern.category))
if (invalidCategories.nonEmpty) {
error(s"""
|Some patterns have invalid categories in /docs/patterns.json
|The allowed categories are: ${Category.allowedCategories.mkString(", ")}
|
| * ${invalidCategories.map(pattern => s"""${pattern.patternId}:${pattern.category}""").mkString(", ")}
""".stripMargin)
}

invalidCategories.isEmpty

// TS-682: Validate disabled categories aren't used
private def validatePatternCategory(toolSpec: Tool.Specification): Boolean = {
toolSpec.patterns.forall(pattern => {
val isAllowed = Category.allowedCategories.contains(pattern.category)
if (!isAllowed) {
error(s"""
| Disabled category "${pattern.category}" used by pattern "${pattern.patternId}".
| Allowed categories are: ${Category.allowedCategories.mkString(",")}.
""".stripMargin)
case _ => ignoreDescriptions
}
isAllowed
})

noMismatchBetweenPatternsAndDescription &&
noInvalidDescriptions &&
noDuplicatePatterns &&
noInvalidPatternCategories
}

private def readDockerToolDocumentation(dockerImage: DockerImage) = {
Expand Down

0 comments on commit c6d7c1b

Please sign in to comment.