Skip to content

Commit

Permalink
Release v3 (#104)
Browse files Browse the repository at this point in the history
[BREAKING]
Use ant pattern matching to check for included files.
This might be a breaking change for some people, please check your included files in the settings if you encounter any issues.
E.g. the default inclusion pattern **openapi.json would now no longer match the path openapi/v1-openapi.json, please use **/*openapi.json instead.
  • Loading branch information
markbrockhoff authored Oct 4, 2023
1 parent 8b5bb5d commit 3bf1267
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# Check out current repository
- name: Fetch Sources
uses: actions/checkout@v3
uses: actions/checkout@v4

# Validate wrapper
- name: Gradle Wrapper Validation
Expand Down Expand Up @@ -148,7 +148,7 @@ jobs:

# Check out current repository
- name: Fetch Sources
uses: actions/checkout@v3
uses: actions/checkout@v4

# Remove old release drafts by using the curl request for the available releases with a draft flag
- name: Remove Old Release Drafts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

# Check out current repository
- name: Fetch Sources
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ github.event.release.tag_name }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-ui-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

# Check out current repository
- name: Fetch Sources
uses: actions/checkout@v3
uses: actions/checkout@v4

# Setup Java 17 environment for the next steps
- name: Setup Java
Expand Down
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

## [Unreleased]

### Added
- Improve check for files to lint on non unix operating systems

### Changed
- [BREAKING] Use [ant pattern matching](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html) to check for included files. This might be a breaking change for some people, please check your included files in the settings if you encounter any issues. E.g. the default inclusion pattern `**openapi.json` would now no longer match the path `openapi/v1-openapi.json`, please use `**/*openapi.json` instead.

## [2.1.3] - 2023-09-01

### Changed
- Update depenencies
- Update dependencies

## [2.1.2] - 2023-08-28

Expand Down Expand Up @@ -68,8 +74,7 @@
- BREAKING: Spectral no longer comes bundled with the plugin and needs to be installed additionally now
- BREAKING: Settings now only apply on a project level

[Unreleased]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.3...HEAD
[2.1.3]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.2...v2.1.3
[Unreleased]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.2...HEAD
[2.1.2]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.1...v2.1.2
[2.1.1]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.0...v2.1.1
[2.1.0]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.0.0...v2.1.0
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,25 @@ Examples:

- Link to a hosted ruleset: `https://raw.githubusercontent.com/SchwarzIT/api-linter-rules/main/spectral-api.yml`
- Local ruleset relative to Project base-path: `.spectral.json`
- Fully-qualified path: `/Users/mick/.spectral.yaml`
- Fully-qualified path: `/Users/user/.spectral.yaml`

### Configurable Included path patterns

Select the files that will be linted. By default, every file called "openapi.json", "openapi.yml" or "openapi.yaml"
within the Project root will be matched for linting by the plugin when it's opened.

You can adjust this in the settings under Preferences -> Tools -> Spectral -> Included path patterns. All paths are relative
to the project's root directory unless absolute.
You can adjust this in the settings under Preferences -> Tools -> Spectral -> Included path patterns. All paths are
relative to the project's root directory unless absolute.

Examples:

- `openapi.json`: Matches the file called "openapi.json" inside the root directory of the project
- `components/**.yaml`: Matches all files inside the project subdirectory "components" that end with ".yaml"
- `/Users/mick/code/**/openapi*.yaml`: Matches all YAML files within the absolute path "/Users/mick/code" that start with "openapi" and end with ".yaml"
- `components/**/*.yaml`: Matches all files inside the project subdirectory "components" that end with ".yaml"
- `/Users/user/code/**/openapi*.yaml`: Matches all YAML files within the absolute path "/Users/mick/code" that start
with "openapi" and end with ".yaml"

**Note:** Each file must also be recognised by the IDE as a JSON or YAML file - that is with a suitable File Type association.
**Note:** Each file must also be recognised by the IDE as a JSON or YAML file - that is with a suitable File Type
association.
If it is detected as a plain text (or any other type) it will be ignored.

<!-- Plugin description end -->
Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
// Gradle IntelliJ Plugin
id("org.jetbrains.intellij") version "1.15.0"
// Gradle Changelog Plugin
id("org.jetbrains.changelog") version "2.1.2"
id("org.jetbrains.changelog") version "2.2.0"
// Gradle Qodana Plugin
id("org.jetbrains.qodana") version "0.1.13"
// Gradle Kover Plugin
Expand Down Expand Up @@ -141,5 +141,7 @@ dependencies {
implementation(kotlin("stdlib"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.10")
implementation("org.springframework:spring-core:6.0.11")

testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin
pluginName=Spectral
pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin
# SemVer format -> https://semver.org
pluginVersion=2.1.3
pluginVersion=3.0.0
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild=222
#pluginUntilBuild=231.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiFile
import com.schwarzit.spectralIntellijPlugin.settings.ProjectSettingsState
import org.jetbrains.yaml.psi.YAMLFile
import java.nio.file.FileSystems
import org.springframework.util.AntPathMatcher
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths

Expand All @@ -31,7 +32,15 @@ class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile, Editor>, List<
val includedFiles = settings?.includedFiles?.lines() ?: emptyList()

try {
if (!isFileIncluded(file.project.basePath ?: "", file.virtualFile.toNioPath(), includedFiles)) return null
if (!isFileIncluded(
Paths.get(file.project.basePath ?: "/"),
file.virtualFile.toNioPath(),
includedFiles
)
) {
logger.trace("The given file ${file.virtualFile.toNioPath()} did not match any pattern defined in the settings")
return null
}
} catch (e: Throwable) {
logger.error("Failed to check if current file is included. Parameters: basePath: ${file.project.basePath}, path: ${file.virtualFile.toNioPath()}, includedFiles: $includedFiles")
return null
Expand All @@ -40,18 +49,23 @@ class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile, Editor>, List<
return Pair(file, editor)
}

fun isFileIncluded(basePath: String, path: Path, includedFiles: List<String>): Boolean {
var matcherPattern = "glob:{"
val iterator = includedFiles.iterator()
while (iterator.hasNext()) {
val pathPattern = iterator.next()
if (!Paths.get(pathPattern).isAbsolute) matcherPattern += "$basePath/"
matcherPattern += pathPattern
matcherPattern += if (iterator.hasNext()) "," else "}"
fun isFileIncluded(
basePath: Path,
path: Path,
includedFiles: List<String>,
separator: String = File.separator
): Boolean {
val pathMatcher = AntPathMatcher(separator)

return includedFiles.any { s ->
var globPattern = s
if (!Paths.get(s).isAbsolute) {
var base = basePath.toString()
if (!base.endsWith(separator)) base += separator
globPattern = base + s
}
return pathMatcher.match(globPattern, path.toString())
}
logger.trace(matcherPattern)
val fileMatcher = FileSystems.getDefault().getPathMatcher(matcherPattern)
return fileMatcher.matches(path)
}

override fun doAnnotate(info: Pair<PsiFile, Editor>): List<SpectralIssue> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class ProjectSettingsState : PersistentStateComponent<ProjectSettingsState> {

var ruleset: String = "https://raw.githubusercontent.com/SchwarzIT/api-linter-rules/main/spectral-api.yml"
var includedFiles: String = """
**openapi.json
**openapi.yml
**openapi.yaml
**/*openapi.json
**/*openapi.yml
**/*openapi.yaml
""".trimIndent()

override fun getState(): ProjectSettingsState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ class SpectralExternalAnnotatorTest {

@ParameterizedTest(name = "isFileIncluded {index} - {arguments}")
@MethodSource("provideIsFileIncludedTestParams")
fun isFileIncluded(basePath: String, path: String, includedFiles: List<String>, isIncluded: Boolean) {
fun isFileIncluded(
basePath: String,
path: String,
includedFiles: List<String>,
isIncluded: Boolean,
separator: String
) {
val spectralExternalAnnotator = SpectralExternalAnnotator()
val fileIncluded = spectralExternalAnnotator.isFileIncluded(basePath, Paths.get(path), includedFiles)
val fileIncluded =
spectralExternalAnnotator.isFileIncluded(Paths.get(basePath), Paths.get(path), includedFiles, separator)
Assertions.assertEquals(fileIncluded, isIncluded)
}

Expand All @@ -24,9 +31,21 @@ class SpectralExternalAnnotatorTest {
Arguments.of(
"/home/user/project",
"/home/user/project/src/openapi.json",
listOf("**openapi.json"),
true
)
listOf("**/openapi.json"),
true,
"/"
),
Arguments.of(
"C:\\Users\\Username\\Projekt\\",
"C:\\Users\\Username\\Projekt\\API_Name.yaml",
listOf("**.yaml"),
true,
"\\"
),
Arguments.of("/test", "/test/testing.test", listOf("*.test"), true, "/"),
Arguments.of("/", "/foo/bar/something/test.json", listOf("**/*.json"), true, "/"),
Arguments.of("/", "test.test", listOf("*.json", "*.yml"), false, "/"),
Arguments.of("/", "/test/openapi.json", listOf("**.json"), false, "/")
)
}
}
Expand Down

0 comments on commit 3bf1267

Please sign in to comment.