Skip to content

Commit 86d45b6

Browse files
Update ConsistentVersionNumbers to handle YAML schema version replacements (#2500)
* Update ConsistentVersionNumbers to handle YAML schema version replacements * Remove YAML schema URL version update step from release workflow
1 parent c585db9 commit 86d45b6

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

.github/workflows/release-build.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ jobs:
6161
- uses: actions/checkout@v3
6262
with:
6363
ref: ${{ github.ref }}
64-
- name: Update $schema URL in YAML files
65-
run: |
66-
VERSION='${{ steps.get_version.outputs.VERSION }}'
67-
echo "Updating schema URLs in YAML files to v${VERSION}.json"
68-
find . \( -name "apis.yaml" -o -name "apis.yml" \) -print0 | while IFS= read -r -d '' file; do
69-
sed -i "s|\$schema=https://www\.membrane-api\.io/v[0-9\.]\+\.json|\$schema=https://www.membrane-api.io/v${VERSION}.json|g" "$file"
70-
done
7164
- id: install-secret-key
7265
name: Install gpg secret key
7366
run: |

distribution/src/test/java/com/predic8/membrane/examples/withoutinternet/env/ConsistentVersionNumbers.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@
3838
import javax.xml.xpath.XPathFactory;
3939
import java.io.File;
4040
import java.io.IOException;
41+
import java.nio.file.Files;
42+
import java.nio.file.Path;
4143
import java.util.List;
4244
import java.util.concurrent.atomic.AtomicReference;
4345
import java.util.function.Function;
4446
import java.util.regex.Matcher;
4547
import java.util.regex.Pattern;
48+
import java.util.stream.Stream;
4649

4750
import static com.predic8.membrane.examples.util.TestFileUtil.getFileContentAsLines;
4851
import static com.predic8.membrane.examples.util.TestFileUtil.writeLinesToFile;
@@ -69,6 +72,7 @@ public class ConsistentVersionNumbers {
6972
private static final Pattern CONSTANTS_VERSION_PATTERN = Pattern.compile("(\\s*String version = \")(\\d+.\\d+)(\";.*)");
7073
private static final Pattern RPM_SPEC_VERSION_PATTERN = Pattern.compile("(Version:\\s+)(\\S+)(.*)");
7174
private static final Pattern HELP_REFERENCE_VERSION_PATTERN = Pattern.compile("(path.replace\\(\"%VERSION%\", \")([^\"]*)(\"\\))");
75+
private static final Pattern YAML_SCHEMA_VERSION_PATTERN = Pattern.compile("(\\s*#\\s*yaml-language-server:\\s*\\$schema=https://www\\.membrane-api\\.io/v)(\\d+\\.\\d+(?:\\.\\d+)?)(\\.json\\b.*)");
7276

7377
@Test
7478
public void doit() throws Exception {
@@ -142,6 +146,8 @@ private static void transformRecursively(File baseDirectory, VersionTransformer
142146
handleHelpReference(new File(baseDirectory.getAbsolutePath(), "/annot/src/main/java/com/predic8/membrane/annot/generator/HelpReference.java"), versionTransformer);
143147
handleRpmSpec(new File(baseDirectory.getAbsolutePath(), "/membrane.spec"), versionTransformer);
144148
handleConstants(new File(baseDirectory.getAbsolutePath(), "core/src/main/java/com/predic8/membrane/core/Constants.java"), versionTransformer);
149+
150+
handleYamlSchemas(baseDirectory, versionTransformer);
145151
}
146152

147153
private static void handleConstants(File file, VersionTransformer versionTransformer) throws Exception {
@@ -277,4 +283,59 @@ public Semver withIncPatch() {
277283
private interface VersionTransformer {
278284
Semver map(File file, Semver old);
279285
}
286+
287+
private static void handleYamlSchemas(File baseDirectory, VersionTransformer versionTransformer) throws Exception {
288+
try (Stream<Path> s = Files.walk(baseDirectory.toPath())) {
289+
s.filter(Files::isRegularFile)
290+
.filter(p -> {
291+
String n = p.getFileName().toString();
292+
return n.endsWith(".yml") || n.endsWith(".yaml");
293+
})
294+
.filter(p -> !p.toString().contains(File.separator + "target" + File.separator))
295+
.forEach(p -> {
296+
try {
297+
handleYamlSchemaFile(p.toFile(), versionTransformer);
298+
} catch (Exception e) {
299+
throw new RuntimeException("in file " + p.toAbsolutePath(), e);
300+
}
301+
});
302+
}
303+
}
304+
305+
private static void handleYamlSchemaFile(File file, VersionTransformer versionTransformer) throws Exception {
306+
List<String> content = getFileContentAsLines(file);
307+
308+
boolean touched = false;
309+
for (int i = 0; i < content.size(); i++) {
310+
Matcher m = YAML_SCHEMA_VERSION_PATTERN.matcher(content.get(i));
311+
if (!m.find())
312+
continue;
313+
314+
touched = true;
315+
316+
Semver mapped = versionTransformer.map(file, new MembraneVersion(m.group(2)));
317+
318+
// Snapshot must NOT change YAML schema.
319+
if (!isFullRelease(mapped))
320+
continue;
321+
322+
content.set(i, m.replaceFirst(Matcher.quoteReplacement(m.group(1) + formatYamlSchemaVersion(mapped) + m.group(3))));
323+
}
324+
325+
if (touched) {
326+
writeLinesToFile(file, content);
327+
}
328+
}
329+
330+
private static boolean isFullRelease(Semver v) {
331+
return v.getValue().indexOf('-') < 0;
332+
}
333+
334+
private static String formatYamlSchemaVersion(Semver v) {
335+
if (v.getPatch() == null) {
336+
return "%d.%d".formatted(v.getMajor(), v.getMinor() == null ? 0 : v.getMinor());
337+
}
338+
return "%d.%d.%d".formatted(v.getMajor(), v.getMinor() == null ? 0 : v.getMinor(), v.getPatch());
339+
}
340+
280341
}

0 commit comments

Comments
 (0)