Skip to content

Commit 0bdfbaf

Browse files
authored
Docs: get release version from GitHub release (#136)
* add version from GitHub to asciidoc * add update version to the target execution
1 parent e6ea97a commit 0bdfbaf

File tree

7 files changed

+189
-10
lines changed

7 files changed

+189
-10
lines changed

commons/doc-maven-plugin/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* information: "Portions copyright [year] [name of copyright owner]".
1414
*
1515
* Copyright 2012-2015 ForgeRock AS.
16-
* Portions copyright 2024 3A Systems LLC.
16+
* Portions copyright 2024-2025 3A Systems LLC.
1717
-->
1818
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1919
<modelVersion>4.0.0</modelVersion>
@@ -163,6 +163,10 @@
163163
<groupId>org.asciidoctor</groupId>
164164
<artifactId>asciidoctorj</artifactId>
165165
</dependency>
166+
<dependency>
167+
<groupId>org.apache.httpcomponents</groupId>
168+
<artifactId>httpclient</artifactId>
169+
</dependency>
166170

167171
<!-- Runtime -->
168172

commons/doc-maven-plugin/src/main/java/org/openidentityplatform/doc/maven/AsciidocPreProcessMojo.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111
* Header, with the fields enclosed by brackets [] replaced by your own identifying
1212
* information: "Portions copyright [year] [name of copyright owner]".
1313
*
14-
* Copyright 2024 3A Systems LLC.
14+
* Copyright 2024-2025 3A Systems LLC.
1515
*/
1616

1717
package org.openidentityplatform.doc.maven;
1818

1919

2020
import org.apache.commons.io.FileUtils;
2121
import org.apache.commons.io.FilenameUtils;
22+
import org.apache.http.client.methods.CloseableHttpResponse;
23+
import org.apache.http.client.methods.HttpGet;
24+
import org.apache.http.impl.client.CloseableHttpClient;
25+
import org.apache.http.impl.client.HttpClients;
26+
import org.apache.http.util.EntityUtils;
2227
import org.apache.maven.plugin.MojoExecutionException;
2328
import org.apache.maven.plugin.MojoFailureException;
2429
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -28,13 +33,16 @@
2833
import java.io.IOException;
2934
import java.io.InputStream;
3035
import java.net.URL;
36+
import java.nio.charset.StandardCharsets;
3137
import java.nio.file.FileSystem;
3238
import java.nio.file.FileSystems;
3339
import java.nio.file.Files;
3440
import java.nio.file.Path;
3541
import java.nio.file.StandardCopyOption;
3642
import java.util.Collections;
3743
import java.util.List;
44+
import java.util.regex.Matcher;
45+
import java.util.regex.Pattern;
3846
import java.util.stream.Collectors;
3947
import java.util.stream.Stream;
4048

@@ -67,6 +75,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6775
throw new MojoExecutionException("error copying partials content", e);
6876
}
6977

78+
copyDocsDirectories();
79+
updateVersionAttributes();
80+
}
81+
82+
protected void copyDocsDirectories() throws MojoExecutionException {
7083
for(File docDir : getAsciidocSourceDirectory().listFiles()) {
7184
String document = FilenameUtils.getBaseName(docDir.toString());
7285

@@ -84,6 +97,46 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8497
}
8598
}
8699

100+
protected void updateVersionAttributes() {
101+
String version;
102+
String url = String.format("https://api.github.com/repos/OpenIdentityPlatform/%s/releases/latest", this.projectName);
103+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
104+
HttpGet request = new HttpGet(url);
105+
try (CloseableHttpResponse response = httpClient.execute(request)) {
106+
String responseBody = EntityUtils.toString(response.getEntity());
107+
String pattern = "\"name\"\\s*:\\s*\"([^\"]+)\"";
108+
Pattern regex = Pattern.compile(pattern);
109+
Matcher matcher = regex.matcher(responseBody);
110+
111+
if (!matcher.find()) {
112+
return;
113+
}
114+
version = matcher.group(1);
115+
setVersion(version);
116+
}
117+
} catch (Exception e) {
118+
getLog().warn("error occurred while getting version", e);
119+
}
120+
}
121+
122+
protected void setVersion(String version) throws IOException {
123+
String versionShort = version.replaceAll("(\\.\\d+)(?!.*\\d)", "");
124+
for(File docDir : getSourceOutputDirectory().listFiles()) {
125+
System.out.println(docDir);
126+
for(File docFile : docDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".adoc"))) {
127+
System.out.println(docFile);
128+
String adoc = FileUtils.readFileToString(docFile, StandardCharsets.UTF_8);
129+
String versionRegex = String.format("(:%s-version:)\\s*(.*)\\b", this.projectName.toLowerCase());
130+
adoc = adoc.replaceAll(versionRegex, "$1 " + version);
131+
132+
String versionShortRegex = String.format("(:%s-version-short:)\\s*(.*)\\b", this.projectName);
133+
adoc = adoc.replaceAll(versionShortRegex, "$1 " + versionShort);
134+
135+
FileUtils.writeStringToFile(docFile, adoc, StandardCharsets.UTF_8);
136+
}
137+
}
138+
}
139+
87140
private void copyResourcesFolder(String resFolder, File outputDir) throws Exception {
88141
List<Path> files;
89142
URL partials = getClass().getResource(resFolder);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* The contents of this file are subject to the terms of the Common Development and
3+
* Distribution License (the License). You may not use this file except in compliance with the
4+
* License.
5+
*
6+
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7+
* specific language governing permission and limitations under the License.
8+
*
9+
* When distributing Covered Software, include this CDDL Header Notice in each file and include
10+
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11+
* Header, with the fields enclosed by brackets [] replaced by your own identifying
12+
* information: "Portions copyright [year] [name of copyright owner]".
13+
*
14+
* Copyright 2025 3A Systems LLC.
15+
*/
16+
17+
package org.openidentityplatform.doc.maven;
18+
19+
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import java.io.File;
25+
import java.nio.file.Files;
26+
import java.util.Map;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
public class AsciidocPreProcessMojoTest extends AbstractMojoTestCase {
31+
@Before
32+
public void setUp() throws Exception {
33+
super.setUp();
34+
}
35+
36+
@After
37+
public void tearDown() throws Exception {
38+
super.tearDown();
39+
}
40+
41+
@Test
42+
public void testExecute() throws Exception {
43+
File pom = getTestFile("src/test/resources/antora/pom.xml");
44+
assertThat(pom).isNotNull();
45+
46+
AsciidocPreProcessMojo asciidocPreProcessMojo = (AsciidocPreProcessMojo) lookupMojo("asciidoc-pre-process", pom);
47+
assertThat(asciidocPreProcessMojo).isNotNull();
48+
this.configureMojo(asciidocPreProcessMojo, "doc-maven-plugin", pom);
49+
Map<String, Object> params = this.getVariablesAndValuesFromObject(asciidocPreProcessMojo);
50+
System.out.println(params);
51+
52+
Files.createDirectories(asciidocPreProcessMojo.getSourceOutputPartialsDirectory().toPath());
53+
asciidocPreProcessMojo.copyDocsDirectories();
54+
55+
asciidocPreProcessMojo.updateVersionAttributes();
56+
}
57+
58+
}

commons/doc-maven-plugin/src/test/resources/antora/pom.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* information: "Portions copyright [year] [name of copyright owner]".
1414
*
1515
* Copyright 2014 ForgeRock AS
16-
* Portions copyright 2024 3A Systems LLC.
16+
* Portions copyright 2024-2025 3A Systems LLC.
1717
-->
1818
<project xmlns="http://maven.apache.org/POM/4.0.0"
1919
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -32,24 +32,25 @@
3232
<artifactId>doc-maven-plugin</artifactId>
3333

3434
<configuration>
35-
<projectName>Test</projectName>
35+
<projectName>commons</projectName>
3636
<projectVersion>1.0.0-SNAPSHOT</projectVersion>
3737
<releaseVersion>1.0.0</releaseVersion>
38+
<asciidocSourceDirectory>${basedir}/src/test/resources/asciidoc</asciidocSourceDirectory>
39+
<buildDirectory>${basedir}/target/asciidoc-test</buildDirectory>
40+
<documents>
41+
<document>reference</document>
42+
<document>admin-guide</document>
43+
</documents>
3844
</configuration>
3945

4046
<executions>
4147
<execution>
4248
<id>antora-doc-build</id>
4349
<phase>site</phase>
4450
<goals>
51+
<goal>asciidoc-pre-process</goal>
4552
<goal>antora</goal>
4653
</goals>
47-
<configuration>
48-
<documents>
49-
<document>gateway-guide</document>
50-
<document>admin-guide</document>
51-
</documents>
52-
</configuration>
5354
</execution>
5455
</executions>
5556
</plugin>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
////
2+
The contents of this file are subject to the terms of the Common Development and
3+
Distribution License (the License). You may not use this file except in compliance with the
4+
License.
5+
6+
You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7+
specific language governing permission and limitations under the License.
8+
9+
When distributing Covered Software, include this CDDL Header Notice in each file and include
10+
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11+
Header, with the fields enclosed by brackets [] replaced by your own identifying
12+
information: "Portions copyright [year] [name of copyright owner]".
13+
Copyright 2025 3A Systems LLC.
14+
////
15+
16+
= Administration Guide
17+
:doctype: book
18+
:commons-version: 0.1.1
19+
:commons-version-short: 0.1
20+
21+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
////
2+
The contents of this file are subject to the terms of the Common Development and
3+
Distribution License (the License). You may not use this file except in compliance with the
4+
License.
5+
6+
You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7+
specific language governing permission and limitations under the License.
8+
9+
When distributing Covered Software, include this CDDL Header Notice in each file and include
10+
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11+
Header, with the fields enclosed by brackets [] replaced by your own identifying
12+
information: "Portions copyright [year] [name of copyright owner]".
13+
Copyright 2025 3A Systems LLC.
14+
////
15+
16+
:commons-version: 0.1.1
17+
18+
== Chapter 2
19+
20+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
////
2+
The contents of this file are subject to the terms of the Common Development and
3+
Distribution License (the License). You may not use this file except in compliance with the
4+
License.
5+
6+
You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7+
specific language governing permission and limitations under the License.
8+
9+
When distributing Covered Software, include this CDDL Header Notice in each file and include
10+
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11+
Header, with the fields enclosed by brackets [] replaced by your own identifying
12+
information: "Portions copyright [year] [name of copyright owner]".
13+
Copyright 2025 3A Systems LLC.
14+
////
15+
16+
= Reference
17+
:doctype: book
18+
:commons-version: 0.1.1
19+
20+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
21+
22+
include::./ch02.adoc[]

0 commit comments

Comments
 (0)