Skip to content

Commit e63a7de

Browse files
committed
Added detection of latest release if no properties file exists for Python plugin, fallback is default branch
1 parent 457564f commit e63a7de

File tree

5 files changed

+46
-42
lines changed

5 files changed

+46
-42
lines changed

src/main/java/edu/kit/datamanager/mappingservice/plugins/AbstractPythonMappingPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public AbstractPythonMappingPlugin(String pluginName, String repositoryUrl) {
109109
minPython = properties.getProperty("min.python");
110110
}
111111
} else {
112-
System.err.println("Properties file not found!");
113-
tag = "unavailable";
112+
LOGGER.info("Properties file not found. Using latest version.");
113+
tag = "latest";
114114
}
115115

116116
if (System.getProperty("os.name").startsWith("Windows")) {

src/main/java/edu/kit/datamanager/mappingservice/util/FileUtil.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ public static void removeFile(Path tempFile) {
199199
* application/octet-stream is returned as default.
200200
*
201201
* @param file Path to file
202-
*
203202
* @return The mime type of application/octet-stream as fallback.
204203
*/
205204
public static String getMimeType(Path file) {
@@ -220,7 +219,6 @@ public static String getMimeType(Path file) {
220219
* application/octet-stream is returned as default.
221220
*
222221
* @param mimeType The mime type as string.
223-
*
224222
* @return The extension if it could be determined by mime type or 'bin'
225223
* otherwise.
226224
*/
@@ -243,7 +241,7 @@ public static String getExtensionForMimeType(String mimeType) {
243241
/**
244242
* Guess the extension of the file from the first bytes using Apache Tika
245243
*
246-
* @param filename The name of the file to support mime type detection.
244+
* @param filename The name of the file to support mime type detection.
247245
* @param fewKilobytesOfFile First few kilobytes of the file.
248246
* @return Estimated extension. e.g. '.xml'
249247
*/
@@ -287,42 +285,50 @@ private static String guessFileExtension(String filename, byte[] fewKilobytesOfF
287285
* 'mapping-service.codeLocation' obtained from ApplicationProperties.
288286
*
289287
* @param repositoryUrl the url of the repository to clone
290-
* @param branch the branch to clone
291-
* @param targetFolder the target folder
288+
* @param branch the branch to clone
289+
* @param targetFolder the target folder
292290
* @return the path to the cloned repository
293291
*/
294292
public static Path cloneGitRepository(String repositoryUrl, String branch, String targetFolder) {
295293
File target = new File(targetFolder);
296294
if (target.exists()) {
297-
Git g = null;
298-
try {
299-
g = Git.open(target);
295+
try (Git g = Git.open(target)) {
300296
LOGGER.trace("Repository already exists at {}. Active branch is: {}", target, g.getRepository().getBranch());
301297
} catch (IOException e) {
302298
String message = String.format("Folder '%s' already exists but contains not Git repository.", target);
303299
LOGGER.error(message, e);
304300
throw new MappingServiceException("Failed to prepare plugin. Plugin code destination already exists but is empty.");
305-
} finally {
306-
if (g != null) {
307-
g.getRepository().close();
308-
}
309301
}
310302
} else {
311-
if(!target.mkdirs()){
303+
if (!target.mkdirs()) {
312304
LOGGER.warn("Failed to create target directory {}. Assuming it already exists.", target);
313305
}
314306

315307
LOGGER.info("Cloning branch '{}' of repository '{}' to '{}'", branch, repositoryUrl, target.getPath());
316308
Git g = null;
317309
try {
318-
g = Git.cloneRepository().setURI(repositoryUrl).setBranch(branch).setDirectory(target).call();
310+
if ("latest".equals(branch)) {
311+
LOGGER.trace("Detected 'latest' branch. Checking out default branch.");
312+
g = Git.cloneRepository().setURI(repositoryUrl).setDirectory(target).call();
313+
LOGGER.trace("Determining 'latest' tag.");
314+
String tag = g.describe().setTags(true).setAbbrev(0).call();
315+
if (tag == null || tag.isEmpty()) {
316+
LOGGER.debug("No tags found. Using default branch directly.");
317+
} else {
318+
LOGGER.trace("Latest tag {} found. Checking out tag.", tag);
319+
g.checkout().setName(tag).call();
320+
}
321+
} else {
322+
g = Git.cloneRepository().setURI(repositoryUrl).setBranch(branch).setDirectory(target).call();
323+
}
319324
LOGGER.trace("Repository successfully cloned to {}.", target);
320325
} catch (JGitInternalException | GitAPIException e) {
321326
LOGGER.error("Error cloning git repository '{}' to '{}'!", repositoryUrl, target, e);
322327
throw new MappingServiceException("Failed to prepare plugin. Plugin code destination not accessible.");
323328
} finally {
324329
if (g != null) {
325330
g.getRepository().close();
331+
g.close();
326332
}
327333
}
328334
}

src/test/java/edu/kit/datamanager/mappingservice/plugins/PluginLoaderTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ class PluginLoaderTest {
3636

3737
@Autowired
3838
private PluginManager pluginManager;
39-
39+
4040
@Autowired
4141
private PluginLoader pluginLoader;
4242

4343
@Autowired
4444
private ApplicationProperties applicationProperties;
4545

46-
46+
private final String INOUTPLUGIN_VERSION = "2.0.0";
47+
private final String INOUTPLUGIN_NAME = "InOutPlugin";
48+
private final String INOUTPLUGIN_ID = INOUTPLUGIN_NAME + "_" + INOUTPLUGIN_VERSION;
49+
50+
4751
@BeforeEach
4852
void setUp() throws Exception {
4953
//not needed as plugins are part of the service now
@@ -64,21 +68,21 @@ void valid() {
6468
} catch (Exception e) {
6569
fail(e);
6670
}
67-
71+
6872
try {
69-
assertEquals("InOutPlugin_1.1.2", plugins.get("InOutPlugin_1.1.2").id());
70-
assertEquals("InOutPlugin", plugins.get("InOutPlugin_1.1.2").name());
71-
assertEquals("Simple plugin for testing just returning the input file.", plugins.get("InOutPlugin_1.1.2").description());
72-
assertEquals("1.1.2", plugins.get("InOutPlugin_1.1.2").version());
73-
assertEquals("https://github.com/kit-data-manager/mapping-service", plugins.get("InOutPlugin_1.1.2").uri());
74-
assertEquals("application/*", plugins.get("InOutPlugin_1.1.2").inputTypes()[0]);
75-
assertEquals("application/*", plugins.get("InOutPlugin_1.1.2").outputTypes()[0]);
76-
plugins.get("InOutPlugin_1.1.2").setup(applicationProperties);
73+
assertEquals(INOUTPLUGIN_ID, plugins.get(INOUTPLUGIN_ID).id());
74+
assertEquals(INOUTPLUGIN_NAME, plugins.get(INOUTPLUGIN_ID).name());
75+
assertEquals("Simple plugin for testing just returning the input file.", plugins.get(INOUTPLUGIN_ID).description());
76+
assertEquals(INOUTPLUGIN_VERSION, plugins.get(INOUTPLUGIN_ID).version());
77+
assertEquals("https://github.com/kit-data-manager/mapping-service", plugins.get(INOUTPLUGIN_ID).uri());
78+
assertEquals("application/*", plugins.get(INOUTPLUGIN_ID).inputTypes()[0]);
79+
assertEquals("application/*", plugins.get(INOUTPLUGIN_ID).outputTypes()[0]);
80+
plugins.get(INOUTPLUGIN_ID).setup(applicationProperties);
7781
File inputFile = new File("/tmp/inputFile");
7882
if (!inputFile.exists()) {
7983
Assertions.assertTrue(inputFile.createNewFile());
8084
}
81-
assertEquals(MappingPluginState.SUCCESS().getState(), plugins.get("InOutPlugin_1.1.2").mapFile(new File("schema").toPath(), inputFile.toPath(), new File("output").toPath()).getState());
85+
assertEquals(MappingPluginState.SUCCESS().getState(), plugins.get(INOUTPLUGIN_ID).mapFile(new File("schema").toPath(), inputFile.toPath(), new File("output").toPath()).getState());
8286
} catch (Exception e) {
8387
fail(e);
8488
}

src/test/java/edu/kit/datamanager/mappingservice/plugins/PluginManagerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class PluginManagerTest {
3838
@Autowired
3939
private ApplicationProperties applicationProperties;
4040

41+
private final String INOUTPLUGIN_ID = "InOutPlugin_2.0.0";
42+
4143
@BeforeEach
4244
void setup() throws Exception {
4345
/*try {
@@ -119,7 +121,7 @@ void mapFile() {
119121
if (!inputFile.exists()) {
120122
assertTrue(inputFile.createNewFile());
121123
}
122-
pluginManager.mapFile("InOutPlugin_1.1.2", new File("mapping-schema").toPath(), inputFile.toPath(), outputFile.toPath());
124+
pluginManager.mapFile(INOUTPLUGIN_ID, new File("mapping-schema").toPath(), inputFile.toPath(), outputFile.toPath());
123125
assertTrue(outputFile.exists());
124126
assertTrue(inputFile.delete());
125127
assertTrue(outputFile.delete());

src/test/java/edu/kit/datamanager/mappingservice/rest/impl/MappingExecutionControllerTest.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public class MappingExecutionControllerTest {
6262
private final static String TEMP_DIR_4_ALL = "/tmp/mapping-service/";
6363
private final static String TEMP_DIR_4_MAPPING = TEMP_DIR_4_ALL + "mapping/";
6464
private static final String MAPPING_ID = "my_dc";
65-
private static final String MAPPING_TYPE = "InOutPlugin_1.1.2";
65+
private static final String MAPPING_TYPE = "InOutPlugin_2.0.0";
6666
private static final String MAPPING_URL = "/api/v1/mappingExecution/" + MAPPING_ID;
67-
private static final String MAPPING_TITLE = "TITEL";
67+
private static final String MAPPING_TITLE = "TITLE";
6868
private static final String MAPPING_DESCRIPTION = "DESCRIPTION";
6969

7070
@Autowired
@@ -128,22 +128,14 @@ void setUp(RestDocumentationContextProvider restDocumentation) throws Exception
128128
ex.printStackTrace();
129129
}
130130

131-
try {
131+
//skip as currently no external plugins are shipped with mapping service
132+
/*try {
132133
FileUtils.copyDirectory(Path.of("./plugins").toFile(), Path.of(applicationProperties.getPluginLocation().toURI()).toFile());
133134
} catch (IOException ex) {
134135
ex.printStackTrace();
135-
}
136+
}*/
136137

137138
pluginManager.reloadPlugins();
138-
/* this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
139-
.apply(documentationConfiguration(restDocumentation)
140-
.uris().withPort(8095)
141-
.and().operationPreprocessors()
142-
.withRequestDefaults(prettyPrint())
143-
.withResponseDefaults(Preprocessors.removeHeaders("X-Content-Type-Options", "X-XSS-Protection", "X-Frame-Options"), prettyPrint()))
144-
.alwaysDo(document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())))
145-
.build();
146-
*/
147139
createMapping();
148140
}
149141

0 commit comments

Comments
 (0)