Skip to content

Commit 2aa4482

Browse files
committed
Extend the repository configuration syntax
Reviewed-by: ehelin
1 parent a0e7eda commit 2aa4482

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

bot/src/main/java/org/openjdk/skara/bot/BotConfiguration.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import org.openjdk.skara.census.Census;
2626
import org.openjdk.skara.host.HostedRepository;
2727
import org.openjdk.skara.json.JSONObject;
28+
import org.openjdk.skara.vcs.Branch;
2829

2930
import java.nio.file.Path;
3031
import java.util.Optional;
3132

3233
public interface BotConfiguration {
33-
3434
/**
3535
* Folder that WorkItems may use to store permanent data.
3636
* @return
@@ -44,6 +44,14 @@ public interface BotConfiguration {
4444
*/
4545
HostedRepository repository(String name);
4646

47+
/**
48+
* Retrieves the ref name that optionally follows the configuration-specific repository name.
49+
* If not configured, returns the name of the VCS default branch.
50+
* @param name
51+
* @return
52+
*/
53+
String repositoryRef(String name);
54+
4755
/**
4856
* Additional bot-specific configuration.
4957
* @return

bot/src/main/java/org/openjdk/skara/bot/BotRunnerConfiguration.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.openjdk.skara.host.*;
2727
import org.openjdk.skara.host.network.URIBuilder;
2828
import org.openjdk.skara.json.*;
29-
import org.openjdk.skara.vcs.Repository;
29+
import org.openjdk.skara.vcs.*;
3030

3131
import java.io.*;
3232
import java.net.URI;
@@ -115,11 +115,39 @@ private Map<String, HostedRepository> parseRepositories(JSONObject config) throw
115115
return ret;
116116
}
117117

118-
private HostedRepository getRepository(String name) throws ConfigurationError {
119-
if (!repositories.containsKey(name)) {
120-
throw new ConfigurationError("Repository " + name + " is not defined!");
118+
private static class RepositoryEntry {
119+
HostedRepository repository;
120+
String ref;
121+
}
122+
123+
private RepositoryEntry parseRepositoryEntry(String entry) throws ConfigurationError {
124+
var ret = new RepositoryEntry();
125+
var refSeparatorIndex = entry.indexOf(':');
126+
if (refSeparatorIndex >= 0) {
127+
ret.ref = entry.substring(refSeparatorIndex + 1);
128+
entry = entry.substring(0, refSeparatorIndex);
129+
}
130+
var hostSeparatorIndex = entry.indexOf('/');
131+
if (hostSeparatorIndex >= 0) {
132+
var hostName = entry.substring(0, hostSeparatorIndex);
133+
var host = hosts.get(hostName);
134+
if (!hosts.containsKey(hostName)) {
135+
throw new ConfigurationError("Repository entry " + entry + " uses undefined host '" + hostName + "'");
136+
}
137+
var repositoryName = entry.substring(hostSeparatorIndex + 1);
138+
ret.repository = host.getRepository(repositoryName);
139+
} else {
140+
if (!repositories.containsKey(entry)) {
141+
throw new ConfigurationError("Repository " + entry + " is not defined!");
142+
}
143+
ret.repository = repositories.get(entry);
144+
}
145+
146+
if (ret.ref == null) {
147+
ret.ref = ret.repository.getRepositoryType() == VCS.GIT ? "master" : "default";
121148
}
122-
return repositories.get(name);
149+
150+
return ret;
123151
}
124152

125153
public static BotRunnerConfiguration parse(JSONObject config, Path cwd) throws ConfigurationError {
@@ -131,7 +159,6 @@ public static BotRunnerConfiguration parse(JSONObject config) throws Configurati
131159
}
132160

133161
public BotConfiguration perBotConfiguration(String botName) throws ConfigurationError {
134-
135162
if (!config.contains(botName)) {
136163
throw new ConfigurationError("No configuration for bot name: " + botName);
137164
}
@@ -152,7 +179,18 @@ public Path storageFolder() {
152179
@Override
153180
public HostedRepository repository(String name) {
154181
try {
155-
return getRepository(name);
182+
var entry = parseRepositoryEntry(name);
183+
return entry.repository;
184+
} catch (ConfigurationError configurationError) {
185+
throw new RuntimeException("Couldn't find repository with name: " + name, configurationError);
186+
}
187+
}
188+
189+
@Override
190+
public String repositoryRef(String name) {
191+
try {
192+
var entry = parseRepositoryEntry(name);
193+
return entry.ref;
156194
} catch (ConfigurationError configurationError) {
157195
throw new RuntimeException("Couldn't find repository with name: " + name, configurationError);
158196
}

bot/src/test/java/org/openjdk/skara/bot/BotRunnerConfigurationTests.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
package org.openjdk.skara.bot;
2424

2525
import java.nio.file.Path;
26+
2627
import org.junit.jupiter.api.Test;
27-
import org.openjdk.skara.json.JSON;
2828

29-
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import org.openjdk.skara.json.*;
3030

31-
public class BotRunnerConfigurationTests {
31+
import static org.junit.jupiter.api.Assertions.*;
3232

33+
class BotRunnerConfigurationTests {
3334
@Test
3435
void storageFolder() throws ConfigurationError {
3536
var input = JSON.object().put("storage", JSON.object().put("path", "/x"))
@@ -39,4 +40,28 @@ void storageFolder() throws ConfigurationError {
3940

4041
assertEquals(Path.of("/x/xbot"), botCfg.storageFolder());
4142
}
43+
44+
@Test
45+
void parseHost() throws ConfigurationError {
46+
var input = JSON.object()
47+
.put("xbot",
48+
JSON.object().put("repository", "test/x/y"));
49+
var cfg = BotRunnerConfiguration.parse(input);
50+
var botCfg = cfg.perBotConfiguration("xbot");
51+
52+
var error = assertThrows(RuntimeException.class, () -> botCfg.repository("test/x/y"));
53+
assertEquals("Repository entry test/x/y uses undefined host 'test'", error.getCause().getMessage());
54+
}
55+
56+
@Test
57+
void parseRef() throws ConfigurationError {
58+
var input = JSON.object()
59+
.put("xbot",
60+
JSON.object().put("repository", "test/x/y:z"));
61+
var cfg = BotRunnerConfiguration.parse(input);
62+
var botCfg = cfg.perBotConfiguration("xbot");
63+
64+
var error = assertThrows(RuntimeException.class, () -> botCfg.repositoryRef("test/x/y:z"));
65+
assertEquals("Repository entry test/x/y uses undefined host 'test'", error.getCause().getMessage());
66+
}
4267
}

0 commit comments

Comments
 (0)