-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from typerefinery-ai/develop
Develop
- Loading branch information
Showing
512 changed files
with
33,623 additions
and
5,006 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...n/backend/src/main/java/ai/typerefinery/websight/actions/spaces/ExportSpaceWebAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...n/backend/src/main/java/ai/typerefinery/websight/actions/spaces/ImportSpaceWebAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ation/backend/src/main/java/ai/typerefinery/websight/actions/spaces/SpaceConfigModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
application/backend/src/main/java/ai/typerefinery/websight/actions/spaces/SpaceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
142 changes: 142 additions & 0 deletions
142
...end/src/main/java/ai/typerefinery/websight/actions/spaces/rest/ExportSpaceRestAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
Oops, something went wrong.