Skip to content

Commit 362aebe

Browse files
iocanelgastaldi
andauthored
Allow repository creation in the Gitea DevService (#156)
* feat: allow repository creation in Gitea * Optional repositories --------- Co-authored-by: George Gastaldi <[email protected]>
1 parent 360c730 commit 362aebe

File tree

8 files changed

+108
-7
lines changed

8 files changed

+108
-7
lines changed

deployment/src/main/java/io/quarkus/jgit/deployment/GiteaContainer.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import static org.testcontainers.containers.wait.strategy.Wait.forListeningPorts;
44

55
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
69

710
import org.jboss.logging.Logger;
811
import org.testcontainers.containers.GenericContainer;
@@ -15,10 +18,10 @@ class GiteaContainer extends GenericContainer<GiteaContainer> {
1518
* Logger which will be used to capture container STDOUT and STDERR.
1619
*/
1720
private static final Logger log = Logger.getLogger(GiteaContainer.class);
18-
1921
private static final int HTTP_PORT = 3000;
2022

2123
private JGitBuildTimeConfig.DevService devServiceConfig;
24+
private List<String> repositories = new ArrayList<>();
2225

2326
GiteaContainer(JGitBuildTimeConfig.DevService devServiceConfig) {
2427
super("gitea/gitea:latest-rootless");
@@ -41,6 +44,9 @@ protected void containerIsStarted(InspectContainerResponse containerInfo, boolea
4144
if (!reused) {
4245
try {
4346
createAdminUser();
47+
for (String repository : devServiceConfig.repositories().orElse(Collections.emptyList())) {
48+
createRepository(this, repository);
49+
}
4450
} catch (IOException | InterruptedException e) {
4551
throw new RuntimeException("Failed to create admin user", e);
4652
}
@@ -70,11 +76,46 @@ private void createAdminUser() throws IOException, InterruptedException {
7076
}
7177
}
7278

79+
private void createRepository(GiteaContainer giteaContainer, String repository)
80+
throws UnsupportedOperationException, IOException, InterruptedException {
81+
String httpUrl = "http://localhost:" + GiteaContainer.HTTP_PORT;
82+
String data = """
83+
{"name":"%s", "private":false, "auto_init":true, "readme":"Default"}
84+
"""
85+
.formatted(repository);
86+
87+
String[] cmd = {
88+
"/usr/bin/curl",
89+
"-X",
90+
"POST",
91+
"--user",
92+
devServiceConfig.adminUsername() + ":" + devServiceConfig.adminPassword(),
93+
"-H",
94+
"Content-Type: application/json",
95+
"-d",
96+
data,
97+
httpUrl + "/api/v1/user/repos"
98+
};
99+
100+
log.debug(String.join(" ", cmd));
101+
ExecResult execResult = giteaContainer.execInContainer(cmd);
102+
log.info(execResult.getStdout());
103+
if (execResult.getExitCode() != 0) {
104+
throw new RuntimeException("Failed to create repository: " + repository + ":" + execResult.getStderr());
105+
}
106+
repositories.add(repository);
107+
log.info("Created repository: " + repository);
108+
}
109+
73110
public String getHttpUrl() {
74111
return "http://" + getHost() + ":" + getHttpPort();
75112
}
76113

77114
public int getHttpPort() {
78115
return getMappedPort(HTTP_PORT);
79116
}
117+
118+
public List<String> getRepositories() {
119+
return repositories;
120+
}
80121
}

deployment/src/main/java/io/quarkus/jgit/deployment/GiteaDevServiceInfoBuildItem.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkus.jgit.deployment;
22

3+
import java.util.List;
4+
35
import io.quarkus.builder.item.SimpleBuildItem;
46

57
/**
@@ -11,12 +13,15 @@ public final class GiteaDevServiceInfoBuildItem extends SimpleBuildItem {
1113
private final int httpPort;
1214
private final String adminUsername;
1315
private final String adminPassword;
16+
private final List<String> repositories;
1417

15-
public GiteaDevServiceInfoBuildItem(String host, int httpPort, String adminUsername, String adminPassword) {
18+
public GiteaDevServiceInfoBuildItem(String host, int httpPort, String adminUsername, String adminPassword,
19+
List<String> repositories) {
1620
this.host = host;
1721
this.httpPort = httpPort;
1822
this.adminUsername = adminUsername;
1923
this.adminPassword = adminPassword;
24+
this.repositories = repositories;
2025
}
2126

2227
public int httpPort() {
@@ -35,4 +40,7 @@ public String adminPassword() {
3540
return adminPassword;
3641
}
3742

43+
public List<String> repositories() {
44+
return repositories;
45+
}
3846
}

deployment/src/main/java/io/quarkus/jgit/deployment/JGitBuildTimeConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkus.jgit.deployment;
22

3+
import java.util.List;
4+
import java.util.Optional;
35
import java.util.OptionalInt;
46

57
import io.quarkus.runtime.annotations.ConfigPhase;
@@ -46,6 +48,12 @@ interface DevService {
4648
@WithDefault("quarkus")
4749
String adminPassword();
4850

51+
/**
52+
* Repositories to be created when the Dev Service starts.
53+
*/
54+
@WithDefault("${quarkus.application.name}")
55+
Optional<List<String>> repositories();
56+
4957
/**
5058
* Should the container be reused?
5159
*/

deployment/src/main/java/io/quarkus/jgit/deployment/JGitDevServicesProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ DevServicesResultBuildItem createContainer(JGitBuildTimeConfig config,
4141
gitServer.getHost(),
4242
gitServer.getHttpPort(),
4343
config.devservices().adminUsername(),
44-
config.devservices().adminPassword()));
44+
config.devservices().adminPassword(),
45+
gitServer.getRepositories()));
4546
return devService.toBuildItem();
4647
}
4748

docs/modules/ROOT/pages/includes/quarkus-jgit.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ endif::add-copy-button-to-env-var[]
9393
|string
9494
|`quarkus`
9595

96+
a|icon:lock[title=Fixed at build time] [[quarkus-jgit_quarkus-jgit-devservices-repositories]] [.property-path]##link:#quarkus-jgit_quarkus-jgit-devservices-repositories[`quarkus.jgit.devservices.repositories`]##
97+
98+
[.description]
99+
--
100+
Repositories to be created when the Dev Service starts.
101+
102+
103+
ifdef::add-copy-button-to-env-var[]
104+
Environment variable: env_var_with_copy_button:+++QUARKUS_JGIT_DEVSERVICES_REPOSITORIES+++[]
105+
endif::add-copy-button-to-env-var[]
106+
ifndef::add-copy-button-to-env-var[]
107+
Environment variable: `+++QUARKUS_JGIT_DEVSERVICES_REPOSITORIES+++`
108+
endif::add-copy-button-to-env-var[]
109+
--
110+
|list of string
111+
|`${quarkus.application.name}`
112+
96113
a|icon:lock[title=Fixed at build time] [[quarkus-jgit_quarkus-jgit-devservices-reuse]] [.property-path]##link:#quarkus-jgit_quarkus-jgit-devservices-reuse[`quarkus.jgit.devservices.reuse`]##
97114

98115
[.description]

docs/modules/ROOT/pages/includes/quarkus-jgit_quarkus.jgit.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ endif::add-copy-button-to-env-var[]
9393
|string
9494
|`quarkus`
9595

96+
a|icon:lock[title=Fixed at build time] [[quarkus-jgit_quarkus-jgit-devservices-repositories]] [.property-path]##link:#quarkus-jgit_quarkus-jgit-devservices-repositories[`quarkus.jgit.devservices.repositories`]##
97+
98+
[.description]
99+
--
100+
Repositories to be created when the Dev Service starts.
101+
102+
103+
ifdef::add-copy-button-to-env-var[]
104+
Environment variable: env_var_with_copy_button:+++QUARKUS_JGIT_DEVSERVICES_REPOSITORIES+++[]
105+
endif::add-copy-button-to-env-var[]
106+
ifndef::add-copy-button-to-env-var[]
107+
Environment variable: `+++QUARKUS_JGIT_DEVSERVICES_REPOSITORIES+++`
108+
endif::add-copy-button-to-env-var[]
109+
--
110+
|list of string
111+
|`${quarkus.application.name}`
112+
96113
a|icon:lock[title=Fixed at build time] [[quarkus-jgit_quarkus-jgit-devservices-reuse]] [.property-path]##link:#quarkus-jgit_quarkus-jgit-devservices-reuse[`quarkus.jgit.devservices.reuse`]##
97114

98115
[.description]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
quarkus.native.resources.includes=repos/**
22
quarkus.jgit.devservices.enabled=true
33
quarkus.jgit.devservices.http-port=8089
4-
quarkus.jgit.devservices.show-logs=true
4+
quarkus.jgit.devservices.show-logs=false
5+
quarkus.jgit.devservices.repositories=${quarkus.application.name},extra-repo1,extra-repo2

integration-tests/src/test/java/io/quarkus/it/jgit/DevServiceTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.junit.jupiter.api.BeforeAll;
1717
import org.junit.jupiter.api.Test;
1818
import org.junit.jupiter.api.io.TempDir;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
1921

2022
import io.gitea.ApiClient;
2123
import io.gitea.Configuration;
@@ -63,10 +65,16 @@ public void serviceRunning() {
6365
.statusCode(200);
6466
}
6567

66-
@Test
67-
public void shouldCloneFromDevService(@TempDir Path tempDir) throws Exception {
68+
@ParameterizedTest
69+
@ValueSource(strings = {
70+
"test-repo", // The repository created in the @BeforeAll method
71+
"quarkus-jgit-integration-tests", //The project repository created by the Dev Service
72+
"extra-repo1", // An extra repository created by the Dev Service
73+
"extra-repo2" // An other extra repository created by the Dev Service
74+
})
75+
public void shouldCloneFromDevService(String repositoryName, @TempDir Path tempDir) throws Exception {
6876
try (Git git = Git.cloneRepository().setDirectory(tempDir.toFile())
69-
.setURI(config.devservices().httpUrl().get() + "/quarkus/test-repo.git").call()) {
77+
.setURI(config.devservices().httpUrl().get() + "/quarkus/" + repositoryName + ".git").call()) {
7078
assertThat(tempDir.resolve("README.md")).isRegularFile();
7179
assertThat(git.log().call()).extracting(RevCommit::getFullMessage).map(String::trim).contains("Initial commit");
7280
}

0 commit comments

Comments
 (0)