Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.condation.cms.api.eventbus.events;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.eventbus.Event;

/**
*
* @author t.marx
*/
public record RepoCheckoutEvent(String repo) implements Event {

}
5 changes: 4 additions & 1 deletion cms-git/src/main/java/com/condation/cms/git/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import lombok.Getter;
import lombok.Setter;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -46,5 +47,7 @@ public static Config load (final Path file) throws IOException {
@Setter
List<Repo> repos;


public Optional<Repo> find (String name) {
return repos.stream().filter(repo -> repo.getName().equals(name)).findFirst();
}
}
2 changes: 0 additions & 2 deletions cms-git/src/main/java/com/condation/cms/git/GitScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@
public class GitScheduler {

private final Scheduler scheduler;
private final TaskRunner taskRunner;


public void schedule(final Repo repo) {
JobDataMap data = new JobDataMap();
data.put("repo", repo);
data.put("taskRunner", taskRunner);
JobDetail jobDetail = JobBuilder
.newJob(UpdateRepoJob.class)
.withIdentity(repo.getName(), "update-repo")
Expand Down
35 changes: 24 additions & 11 deletions cms-git/src/main/java/com/condation/cms/git/RepositoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import com.condation.cms.git.tasks.CloneTask;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -40,22 +40,24 @@ public class RepositoryManager {
private final Scheduler scheduler;

Config config;
TaskRunner taskRunner;

GitScheduler gitScheduler;

public void init(final Path configFile) throws IOException {
public void init(final Path configFile) throws Exception {
if (!Files.exists(configFile)) {
log.info("no repository configuration found");
return;
}
config = Config.load(configFile);
taskRunner = new TaskRunner();
gitScheduler = new GitScheduler(scheduler, taskRunner);
gitScheduler = new GitScheduler(scheduler);

if (config.getRepos() != null) {
log.debug("initial clone repositories");
for (var repo : config.getRepos()) {
log.debug("clone {}", repo.getName());
var result = taskRunner.execute(new CloneTask(repo));
var result = new CloneTask(repo).call();
try {
log.debug("result : {} ", result.get());
log.debug("result : {} ", result);
log.debug("schedule repo");
gitScheduler.schedule(repo);
} catch (Exception ex) {
Expand All @@ -65,9 +67,20 @@ public void init(final Path configFile) throws IOException {

}
}

public void close() throws IOException {
taskRunner.executor.shutdown();

public void updateRepo (String name) {
log.debug("try updating git repo: {}", name);
if (config == null) {
log.warn("config not loaded");
return;
}
var repo = config.find(name);
if (repo.isEmpty()) {
log.warn("repository {} not found", name);
return;
}
log.debug("updating git repo: {}", name);
new UpdateRepoJob().execute(repo.get());
log.debug("get repo {} updated", name);
}

}
25 changes: 13 additions & 12 deletions cms-git/src/main/java/com/condation/cms/git/UpdateRepoJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/


import com.condation.cms.git.tasks.FetchTask;
import com.condation.cms.git.tasks.MergeTask;
import com.condation.cms.git.tasks.ResetTask;
Expand All @@ -37,24 +35,27 @@
*/
@Slf4j
public class UpdateRepoJob implements Job {

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {

Repo repo = (Repo) context.getJobDetail().getJobDataMap().get("repo");
TaskRunner taskRunner = (TaskRunner) context.getJobDetail().getJobDataMap().get("taskRunner");


execute(repo);
}

protected void execute(Repo repo) {
try {
var fetch = taskRunner.execute(new FetchTask(repo));
if (fetch.get()) {
var fetch = new FetchTask(repo).call();

if (fetch) {
log.debug("fetch {} done", repo.getName());
var merge = taskRunner.execute(new MergeTask(repo));
if (merge.get()) {
var merge = new MergeTask(repo).call();
if (merge) {
log.debug("{} merged", repo.getName());
} else {
log.error("merge {} error", repo.getName());
var reset = taskRunner.execute(new ResetTask(repo));
var reset = new ResetTask(repo).call();
log.debug("reset {}", repo.getName());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.condation.cms.git.Repo;
import com.condation.cms.git.Task;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.condation.cms.git.Repo;
import com.condation.cms.git.Task;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.RequiredArgsConstructor;
Expand Down
23 changes: 9 additions & 14 deletions cms-git/src/test/java/com/condation/cms/git/FlowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
*/


import com.condation.cms.git.TaskRunner;
import com.condation.cms.git.Config;
import com.condation.cms.git.tasks.CloneTask;
import com.condation.cms.git.tasks.FetchTask;
import com.condation.cms.git.tasks.MergeTask;
import com.condation.cms.git.tasks.ResetTask;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -41,28 +38,26 @@
*/
public class FlowTest {

TaskRunner runner = new TaskRunner();

@Test
void flow_test () throws IOException, InterruptedException, ExecutionException {
void flow_test () throws Exception {
var config = Config.load(Path.of("git.yaml"));

Future<Boolean> clone = runner.execute(new CloneTask(config.getRepos().get(0)));
Boolean clone = new CloneTask(config.getRepos().get(0)).call();

if (clone.get()) {
if (clone) {
System.out.println("clone done");

var fetch = runner.execute(new FetchTask(config.getRepos().get(0)));
var fetch = new FetchTask(config.getRepos().get(0)).call();

if (fetch.get()) {
if (fetch) {
System.out.println("fetch done");
var merge = runner.execute(new MergeTask(config.getRepos().get(0)));
if (merge.get()) {
var merge = new MergeTask(config.getRepos().get(0)).call();
if (merge) {
System.out.println("merged");
} else {
System.out.println("merge error");
var reset = runner.execute(new ResetTask(config.getRepos().get(0)));
System.out.println("reset " + reset.get());
var reset = new ResetTask(config.getRepos().get(0)).call();
System.out.println("reset " + reset);
}
} else {
System.out.println("fetch error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
*/


import com.condation.cms.git.GitScheduler;
import com.condation.cms.git.TaskRunner;
import com.condation.cms.git.Config;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
Expand All @@ -44,7 +41,6 @@
public class GitSchedulerTest {

static GitScheduler gitScheduler;
static TaskRunner runner = new TaskRunner();

static Scheduler scheduler;

Expand All @@ -55,12 +51,11 @@ static void setup () throws Exception {
scheduler = schedulerFactory.getScheduler();
scheduler.start();

gitScheduler = new GitScheduler(scheduler, runner);
gitScheduler = new GitScheduler(scheduler);
}
@AfterAll
static void shutdown () throws Exception {
scheduler.shutdown();
runner.executor.shutdown();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author t.marx
*/
@CommandLine.Command(name = "", subcommands = {
ServerCommand.class, HostCommands.class, ExtensionCommands.class, ModuleCommands.class, ThemeCommands.class})
ServerCommand.class, HostCommands.class, ExtensionCommands.class, ModuleCommands.class, ThemeCommands.class, RepoCommands.class})
@Slf4j
public class CLICommand implements Runnable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.condation.cms.git;
package com.condation.cms.cli.commands;

/*-
* #%L
* cms-git
* cms-server
* %%
* Copyright (C) 2023 - 2024 CondationCMS
* %%
Expand All @@ -23,19 +23,24 @@
*/


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.condation.cms.cli.commands.repo.Checkout;
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

/**
*
* @author t.marx
*/
public class TaskRunner {

ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();

public Future<Boolean> execute (Task<Boolean> task) {
return executor.submit(task);
@CommandLine.Command(
name = "repo",
subcommands = {
Checkout.class
})
@Slf4j
public class RepoCommands implements Runnable {

@Override
public void run() {
System.out.println("Subcommand needed: 'checkout'");
}
}
Loading