Skip to content

Commit

Permalink
Merge pull request #381 from typerefinery-ai/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wildone authored Oct 10, 2024
2 parents e167e76 + 2213eca commit badb74d
Show file tree
Hide file tree
Showing 512 changed files with 33,623 additions and 5,006 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"configurations": [
{
"type": "java",
"name": "Debug (Attach)",
"name": "Debug (Attach 8115)",
"projectName": "websight",
"request": "attach",
"projectName": "CMS",
"hostName": "localhost",
"port": "8115"
}
Expand Down
1 change: 1 addition & 0 deletions application/backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<commonmark.version>0.20.0</commonmark.version>
<jgit.version>6.6.1.202309021850-r</jgit.version>
<slf4j.version>2.0.7</slf4j.version>
<log4j.version>2.20.0</log4j.version>
<JavaEWAH.version>1.2.3</JavaEWAH.version>
</properties>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ai.typerefinery.websight.actions.spaces;

import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import pl.ds.websight.ui.framework.actions.service.Condition;
import pl.ds.websight.ui.framework.actions.service.WebAction;
import pl.ds.websight.ui.framework.actions.service.WebActionConfig;
import pl.ds.websight.ui.framework.actions.service.conditions.HasPrivilegesCondition;

@Component
public class ExportSpaceWebAction implements WebAction {
private static final WebActionConfig CONFIG = WebActionConfig.Builder.newWebActionConfig("/apps/typerefinery/components/actions/spaces/exportspace.js")
.forAllModules()
.forTypes(new String[] { "ws:Space" }).forViewTypes(new String[] { "row" })
.withCondition((Condition)new HasPrivilegesCondition(new String[] { "ws:publish" })).withRanking(600)
.build();

@NotNull
public WebActionConfig getConfig() {
return CONFIG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ai.typerefinery.websight.actions.spaces;

import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import pl.ds.websight.ui.framework.actions.service.Condition;
import pl.ds.websight.ui.framework.actions.service.WebAction;
import pl.ds.websight.ui.framework.actions.service.WebActionConfig;
import pl.ds.websight.ui.framework.actions.service.conditions.HasPrivilegesCondition;

@Component
public class ImportSpaceWebAction implements WebAction {
private static final WebActionConfig CONFIG = WebActionConfig.Builder.newWebActionConfig("/apps/typerefinery/components/actions/spaces/importspace.js")
.forAllModules()
.forTypes(new String[] { "ws:Space" }).forViewTypes(new String[] { "row" })
.withCondition((Condition)new HasPrivilegesCondition(new String[] { "ws:publish" })).withRanking(600)
.build();

@NotNull
public WebActionConfig getConfig() {
return CONFIG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ai.typerefinery.websight.actions.spaces;

import java.net.URI;

import org.apache.commons.lang3.StringUtils;

public class SpaceConfigModel {

public static final String PAGES_SPACE_RESOURCE_TYPE = "typerefinery/components/structure/pagesspace";
public static final String ASSEPS_SPACE_RESOURCE_TYPE = "typerefinery/components/structure/assetsspace";

public static final String DEFAULT_GIT_USER_NAME = "typerefinery";
public static final String DEFAULT_GIT_USER_EMAIL = "deploy@typerefinery.ai";

public static final String DEFAULT_LOCAL_PATH = "content";
public static final String DEFAULT_LOCAL_CONTENT_ROOT_PATH = "contentroot";
public static final String DEFAULT_GIT_CONTENT_ROOT_ENV_VAR = "GIT_CONTENT_ROOT";
public static final String DEFAULT_PACKAGE_GROUP = "space-backup";
public static final String DEFAULT_PACKAGE_PREFIX = "space-backup-";
public static final String DEFAULT_PACKAGE_VERSION = "1.0.0";
public static final String DEFAULT_GIT_PROVIDER = "github";

public String repositoryUrl;
public String token;
public String branch;
public String branchContent;
public String configUsername = DEFAULT_GIT_USER_NAME;
public String configEmail = DEFAULT_GIT_USER_EMAIL;

public String getGitProvider() {
if (StringUtils.isEmpty(repositoryUrl)) {
return DEFAULT_GIT_PROVIDER;
}
URI repositoryUri = URI.create(repositoryUrl);

return repositoryUri.getHost();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ai.typerefinery.websight.actions.spaces;

import java.util.Map;

import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.typerefinery.websight.actions.spaces.rest.PublishSpaceRestAction;
import pl.ds.websight.pages.core.api.Page;
import pl.ds.websight.rest.framework.RestActionResult;

public class SpaceUtil {

private static final Logger LOG = LoggerFactory.getLogger(PublishSpaceRestAction.class);

public static SpaceConfigModel getSpaceConfig(Resource space) {
SpaceConfigModel spaceConfig = new SpaceConfigModel();
try {
Resource pagesRoot = space.getChild("pages");
// quick fail if pagesRoot is null
if (pagesRoot == null) {
LOG.error("No pages to deploy.");
return spaceConfig;
}

Resource adminChild = pagesRoot.getChild("_admin");
// quick fail if adminChild is null
if (adminChild == null) {
LOG.error("No admin page with config to deploy.");
return spaceConfig;
}

Page adminChildPage = adminChild.adaptTo(Page.class);
Map<String, Object> adminChildProperties = adminChildPage.getContentProperties();
if (adminChildProperties == null) {
LOG.error("Admin config page does not have any properties.");
return spaceConfig;
}
// ValueMap adminChildValueMap = adminChild.getValueMap(

spaceConfig.repositoryUrl = adminChildProperties.containsKey("deployGithubRepositoryUrl")
? adminChildProperties.get("deployGithubRepositoryUrl").toString()
: "";
spaceConfig.branch = adminChildProperties.containsKey("deployGithubBranch")
? adminChildProperties.get("deployGithubBranch").toString()
: "";
spaceConfig.token = adminChildProperties.containsKey("deployGithubToken")
? adminChildProperties.get("deployGithubToken").toString()
: "";
spaceConfig.configUsername = adminChildProperties.containsKey("deployGithubUserName")
? adminChildProperties.get("deployGithubUserName").toString()
: "";

spaceConfig.configEmail = adminChildProperties.containsKey("deployGithubUserEmail")
? adminChildProperties.get("deployGithubUserEmail").toString()
: "";
spaceConfig.branchContent = adminChildProperties.containsKey("contentGithubBranch")
? adminChildProperties.get("contentGithubBranch").toString()
: "";

} catch (Exception e) {
LOG.error("Error getting space config.", e);
}
return spaceConfig;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package ai.typerefinery.websight.actions.spaces.rest;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.vault.packaging.Packaging;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.event.jobs.JobManager;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ai.typerefinery.websight.actions.spaces.SpaceConfigModel;
import ai.typerefinery.websight.actions.spaces.SpaceUtil;
import ai.typerefinery.websight.repository.RepositoryUtil;
import ai.typerefinery.websight.utils.git.GitConfig;
import ai.typerefinery.websight.utils.git.GitUtil;
import pl.ds.websight.rest.framework.RestAction;
import pl.ds.websight.rest.framework.RestActionResult;
import pl.ds.websight.rest.framework.annotations.PrimaryTypes;
import pl.ds.websight.rest.framework.annotations.SlingAction;
import static ai.typerefinery.websight.actions.spaces.SpaceConfigModel.*;

@SlingAction
@PrimaryTypes({ "ws:PagesSpace" })
@Component
public class ExportSpaceRestAction extends AbstractSpacesRestAction<SpacesRestModel, Void>
implements RestAction<SpacesRestModel, Void> {

private static final Logger LOG = LoggerFactory.getLogger(ExportSpaceRestAction.class);

@Reference
private Packaging packaging;

@Reference
private JobManager jobManager;

RestActionResult<Void> execute(SpacesRestModel model, List<Resource> resources) {

// publish pages and assets recursively
boolean isCommit = model.getCommit();

// this would only run on one space
for (Resource resource : resources) {

try {

// get space config
SpaceConfigModel spaceConfig = SpaceUtil.getSpaceConfig(resource);

if (StringUtils.isNotBlank(spaceConfig.repositoryUrl)) {

// get environment variable GIT_CONTENT_ROOT
String docrootPath = System.getenv(DEFAULT_GIT_CONTENT_ROOT_ENV_VAR);
if (StringUtils.isBlank(docrootPath)) {
docrootPath = DEFAULT_LOCAL_CONTENT_ROOT_PATH;
}

// create new folder docroot/export/githuib/<space-name>
Path contentSpacePath = Paths.get(docrootPath,DEFAULT_LOCAL_PATH, spaceConfig.getGitProvider(), resource.getName());
contentSpacePath.toFile().mkdirs();

GitConfig gitConfig = null;
// clone git repo to docroot/export/githuib/<space-name>
try {
// create git config object
gitConfig = new GitConfig(
spaceConfig.repositoryUrl,
contentSpacePath,
spaceConfig.branchContent,
spaceConfig.token,
spaceConfig.configEmail,
spaceConfig.configUsername
);

// get git object
GitUtil.initializeGit(gitConfig, true, true);

// get latest from git remote
GitUtil.pull(gitConfig, null);

} catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return RestActionResult.failure("Failed", MessageFormat.format("Error while exporting to github, {}", ex.getMessage()));
}

// export pages to docroot/export/githuib/<space-name>
RepositoryUtil.jcrExport(resource.getResourceResolver(), contentSpacePath.toString(), resource.getPath(), true);

Path jcrRootToCommit = Paths.get(contentSpacePath.toString(), "jcr_root");

if (isCommit && jcrRootToCommit.toFile().exists()) {

try {

// add all files to git
GitUtil.createCommit(gitConfig, ".", "Exported from " + resource.getPath());

// push to git remote
GitUtil.push(gitConfig);

} catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return RestActionResult.failure("Failed", MessageFormat.format("Error while exporting to github, {}", ex.getMessage()));
}
} else {
LOG.info("Not committing to github.");
}

} else {
LOG.error("Repository URL is blank, not exporting to github.");
return RestActionResult.failure("Failed", "Please configure space with Git config.");
}

return RestActionResult.success("Success", "Space exported successfully.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return RestActionResult.failure("Failed", "Error exporting space.");
}

}

return RestActionResult.failure("Failed", "Could not export space.");
}

private RestActionResult<Void> getSuccessResult(int assetsCount) {
String message = "Publishing requested";
String messageDetails = String.format("Publishing requested successfully for %d %s",
new Object[] { Integer.valueOf(assetsCount),
(assetsCount == 1) ? "asset" : "assets" });
return RestActionResult.success(message, messageDetails);
}

String getFailureMessage(SpacesRestModel model) {
return "Error while requesting publishing";
}
}
Loading

0 comments on commit badb74d

Please sign in to comment.