wiki历史支持push到远程仓库,防止丢失

This commit is contained in:
暮光:城中城
2020-09-07 21:07:30 +08:00
parent 7405d3d711
commit b05c2158de
20 changed files with 78 additions and 28 deletions

View File

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyplayer.doc.core.annotation.AuthMan;
import com.zyplayer.doc.core.exception.ConfirmException;
import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.core.json.ResponseJson;
import com.zyplayer.doc.data.config.security.DocUserDetails;
@@ -281,8 +282,12 @@ public class WikiPageController {
} else {
logger.warn("未开启elasticsearch服务建议开启");
}
// 提交历史版本记录
gitService.commitAndAddHistory(wikiPage.getId(), content);
try {
// 提交历史版本记录
gitService.commitAndAddHistory(wikiPage.getId(), content);
} catch (ConfirmException e) {
return DocResponseJson.warn(e.getMessage());
}
return DocResponseJson.ok(wikiPage);
}

View File

@@ -10,11 +10,13 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -36,8 +38,16 @@ import java.util.Set;
public class GitService {
private static Logger logger = LoggerFactory.getLogger(GitService.class);
@Value("${zyplayer.doc.wiki.git-file-path:''}")
@Value("${zyplayer.doc.wiki.git-file-path:}")
private String gitFilePath;
@Value("${zyplayer.doc.wiki.git-local-path:}")
private String gitLocalPath;
@Value("${zyplayer.doc.wiki.git-remote-url:}")
private String gitRemoteUrl;
@Value("${zyplayer.doc.wiki.git-remote-username:}")
private String gitRemoteUsername;
@Value("${zyplayer.doc.wiki.git-remote-password:}")
private String gitRemotePassword;
@Resource
WikiPageHistoryService wikiPageHistoryService;
@@ -50,7 +60,7 @@ public class GitService {
*/
public void commitAndAddHistory(Long pageId, String content) {
// 未配置git仓库地址不存历史
if (StringUtils.isBlank(gitFilePath)) {
if (StringUtils.isBlank(this.getGitLocalPath())) {
return;
}
String commitId = this.writeAndCommit(pageId, content);
@@ -78,11 +88,25 @@ public class GitService {
private synchronized String writeAndCommit(Long pageId, String content) {
try {
// 初始化git仓库
File dirFile = new File(this.getGitPath());
Git git = Git.init().setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
Git git;
File dirFile = new File(this.getDotGitPath());
if (!dirFile.exists()) {
if (StringUtils.isNotBlank(gitRemoteUrl)) {
// 远程仓库拉取
git = Git.cloneRepository().setURI(gitRemoteUrl)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitRemoteUsername, gitRemotePassword))
.setBranch("master").setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
} else {
// 创建本地仓库
git = Git.init().setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
}
} else {
// 打开本地仓库
git = new Git(new FileRepository(this.getDotGitPath()));
}
// 修改文件内容
FileUtil.writeString(content, this.getGitPagePath(pageId), "utf-8");
// git提交
// 没有改变的内容
if (git.status().call().isClean()) {
logger.info("no changed");
return null;
@@ -104,12 +128,16 @@ public class GitService {
}
}
RevCommit commit = git.commit().setMessage("commit").call();
// 存在远程地址则推送
if (StringUtils.isNotBlank(gitRemoteUrl)) {
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitRemoteUsername, gitRemotePassword)).call();
}
logger.info("commit id: {}", commit.getName());
return commit.getName();
} catch (Exception e) {
logger.error("git仓库提交失败", e);
throw new ConfirmException("创建历史记录失败:" + e.getMessage(), e);
}
return null;
}
/**
@@ -120,11 +148,11 @@ public class GitService {
* @return 页面内容
*/
public String getPageHistory(String objId, Long pageId) {
if (StringUtils.isBlank(gitFilePath)) {
if (StringUtils.isBlank(this.getGitLocalPath())) {
throw new ConfirmException("未配置WIKI历史版本的git目录");
}
try {
Git git = Git.open(new File(this.getGitPath()));
Git git = Git.open(new File(this.getDotGitPath()));
Repository repository = git.getRepository();
RevCommit revCommit = new RevWalk(repository).parseCommit(ObjectId.fromString(objId));
TreeWalk treeWalk = TreeWalk.forPath(repository, this.getGitPageFile(pageId), revCommit.getTree());
@@ -134,12 +162,16 @@ public class GitService {
return new String(loader.getBytes());
} catch (Exception e) {
logger.error("获取git文件内容失败", e);
return null;
throw new ConfirmException("获取历史版本数据失败:" + e.getMessage());
}
}
private String getGitPath() {
return gitFilePath + "/.git";
private String getGitLocalPath() {
return StringUtils.defaultIfBlank(gitLocalPath, gitFilePath);
}
private String getDotGitPath() {
return this.getGitLocalPath() + "/.git";
}
private String getGitPageFile(Long pageId) {
@@ -147,6 +179,6 @@ public class GitService {
}
private String getGitPagePath(Long pageId) {
return gitFilePath + "/" + this.getGitPageFile(pageId);
return this.getGitLocalPath() + "/" + this.getGitPageFile(pageId);
}
}