feat(Wiki历史版本):wiki历史版本列表及历史版本详情

This commit is contained in:
wanjia 2025-05-13 10:46:07 +08:00
parent 5dae889f13
commit 6f7d969ce9
5 changed files with 108 additions and 14 deletions

View File

@ -1,13 +1,11 @@
package com.microservices.wiki.contoller.wiki; package com.microservices.wiki.contoller.wiki;
import com.microservices.common.core.constant.HttpStatus;
import com.microservices.common.core.exception.ServiceException; import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.web.domain.AjaxResult; import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.wiki.domain.sys.User; import com.microservices.wiki.domain.sys.User;
import com.microservices.wiki.domain.wiki.Wiki; import com.microservices.wiki.domain.wiki.Wiki;
import com.microservices.wiki.domain.wiki.vo.WikiAllVo; import com.microservices.wiki.domain.wiki.vo.*;
import com.microservices.wiki.domain.wiki.vo.WikiContent;
import com.microservices.wiki.domain.wiki.vo.WikiDeleteVo;
import com.microservices.wiki.domain.wiki.vo.WikiPageVo;
import com.microservices.wiki.refactoring.response.HttpClientResult; import com.microservices.wiki.refactoring.response.HttpClientResult;
import com.microservices.wiki.refactoring.utils.BeanCopyUtils; import com.microservices.wiki.refactoring.utils.BeanCopyUtils;
import com.microservices.wiki.refactoring.utils.FastJsonUtils; import com.microservices.wiki.refactoring.utils.FastJsonUtils;
@ -36,6 +34,9 @@ public class WikiController {
@Value("${gitea-wiki-api-base-url}") @Value("${gitea-wiki-api-base-url}")
private String giteaApiBaseUrl; private String giteaApiBaseUrl;
@Value("${gitea-hat-wiki-api-base-url}")
private String giteaHatApiBaseUrl;
@Value("${gitea-wiki-token}") @Value("${gitea-wiki-token}")
private String token; private String token;
@ -138,6 +139,44 @@ public class WikiController {
return new AjaxResult(httpClientResult.getCode(), "", httpClientResult.getContent()); return new AjaxResult(httpClientResult.getCode(), "", httpClientResult.getContent());
} }
@ApiOperation("getWikiRevisions")
@GetMapping("/getWikiRevisions")
public AjaxResult getWikiRevisions(
HttpServletRequest request,
@ApiParam(value = "owner of the repo", required = true) @RequestParam(defaultValue = "") String owner,
@ApiParam(value = "name of the repo", required = true) @RequestParam(defaultValue = "") String repo,
@ApiParam(value = "name of the wikipage", required = true) @RequestParam(defaultValue = "") String pageName,
@ApiParam(value = "id of the project", required = true) @RequestParam(defaultValue = "") Integer projectId) throws Exception {
String giteaToken = wikiService.getGiteaToken(request, projectId);
//未获取有效giteaToken时赋值内置giteaToken
giteaToken = giteaToken == null ? token : giteaToken;
WikiRevisionsVo wikiRevisionsVo = wikiService.getWikiRevisions(owner, repo, pageName, giteaApiDomain+giteaApiBaseUrl, giteaToken);
return new AjaxResult(HttpStatus.SUCCESS,"操作成功", wikiRevisionsVo);
}
@ApiOperation("getPreviousWiki")
@GetMapping("/getPreviousWiki")
public AjaxResult getPreviousWiki(
HttpServletRequest request,
@ApiParam(value = "owner of the repo", required = true) @RequestParam(defaultValue = "") String owner,
@ApiParam(value = "name of the repo", required = true) @RequestParam(defaultValue = "") String repo,
@ApiParam(value = "name of the wikipage", required = true) @RequestParam(defaultValue = "") String pageName,
@ApiParam(value = "id of the project", required = true) @RequestParam(defaultValue = "") Integer projectId,
@ApiParam(value = "id of the project", required = true) @RequestParam(defaultValue = "") String sha) throws Exception {
String giteaToken = wikiService.getGiteaToken(request, projectId);
//未获取有效giteaToken时赋值内置giteaToken
giteaToken = giteaToken == null ? token : giteaToken;
HttpClientResult httpClientResult = wikiService.getPreviousWiki(owner, repo, pageName, sha, giteaApiDomain+giteaHatApiBaseUrl, giteaToken);
if (httpClientResult == null) {
throw new ServiceException("获取wiki历史版本数据异常");
}
if (httpClientResult.getCode() != 200) {
return new AjaxResult(httpClientResult.getCode(),"操作失败", httpClientResult.getContent());
}
WikiContent wikiContent = getWikiContent(httpClientResult);
return new AjaxResult(httpClientResult.getCode(), "操作成功", wikiContent);
}
@ -150,7 +189,10 @@ public class WikiController {
WikiContent wikiContent = FastJsonUtils.toBean(httpClientResult.getContent(), WikiContent.class); WikiContent wikiContent = FastJsonUtils.toBean(httpClientResult.getContent(), WikiContent.class);
if (wikiContent != null && wikiContent.getLast_commit() != null if (wikiContent != null && wikiContent.getLast_commit() != null
&& wikiContent.getLast_commit().getAuthor() != null) { && wikiContent.getLast_commit().getAuthor() != null) {
wikiContent.setImage_url(userService.getRemoteUserLogoByUserLogin(wikiContent.getLast_commit().getAuthor().getName())); String image_url = userService.getRemoteUserLogoByUserLogin(wikiContent.getLast_commit().getCommiter().getName());
wikiContent.setImage_url(image_url);
wikiContent.getLast_commit().getCommiter().setImage_url(image_url);
wikiContent.getLast_commit().getAuthor().setImage_url(image_url);
User commitUser = userService.getUserByLogin(wikiContent.getLast_commit().getAuthor().getName()); User commitUser = userService.getUserByLogin(wikiContent.getLast_commit().getAuthor().getName());
if(commitUser != null){ if(commitUser != null){
wikiContent.setUserName(StringUtils.isEmpty(commitUser.getNickname()) ? commitUser.getLogin() : commitUser.getNickname()); wikiContent.setUserName(StringUtils.isEmpty(commitUser.getNickname()) ? commitUser.getLogin() : commitUser.getNickname());

View File

@ -12,4 +12,6 @@ public class AuthorMessage {
private Date date; private Date date;
private String email; private String email;
private String image_url;
} }

View File

@ -9,4 +9,6 @@ public class CommitMessage {
private String message; private String message;
private AuthorMessage author; private AuthorMessage author;
private AuthorMessage commiter;
} }

View File

@ -0,0 +1,12 @@
package com.microservices.wiki.domain.wiki.vo;
import lombok.Data;
import java.util.List;
@Data
public class WikiRevisionsVo {
private List<CommitMessage> commits;
private Integer count;
}

View File

@ -1,11 +1,13 @@
package com.microservices.wiki.service.wiki; package com.microservices.wiki.service.wiki;
import cn.hutool.core.util.ZipUtil; import cn.hutool.core.util.ZipUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf; import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig; import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param; import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
import com.microservices.common.core.exception.ServiceException; import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.wiki.domain.projects.Projects; import com.microservices.wiki.domain.projects.Projects;
import com.microservices.wiki.domain.sys.UserVo; import com.microservices.wiki.domain.sys.UserVo;
import com.microservices.wiki.domain.wiki.Wiki; import com.microservices.wiki.domain.wiki.Wiki;
@ -13,6 +15,7 @@ import com.microservices.wiki.domain.wiki.WikiContentSimple;
import com.microservices.wiki.domain.wiki.WikiPage; import com.microservices.wiki.domain.wiki.WikiPage;
import com.microservices.wiki.domain.wiki.vo.WikiCloneLinkVo; import com.microservices.wiki.domain.wiki.vo.WikiCloneLinkVo;
import com.microservices.wiki.domain.wiki.vo.WikiPageVo; import com.microservices.wiki.domain.wiki.vo.WikiPageVo;
import com.microservices.wiki.domain.wiki.vo.WikiRevisionsVo;
import com.microservices.wiki.domain.wiki.vo.WikiVo; import com.microservices.wiki.domain.wiki.vo.WikiVo;
import com.microservices.wiki.mapper.members.MembersMapper; import com.microservices.wiki.mapper.members.MembersMapper;
import com.microservices.wiki.mapper.projects.ProjectsMapper; import com.microservices.wiki.mapper.projects.ProjectsMapper;
@ -82,9 +85,9 @@ public class WikiService {
urlFormat += "?access_token=" + giteaToken; urlFormat += "?access_token=" + giteaToken;
} }
String completeUrl = giteaApiDomain + urlFormat; String completeUrl = giteaApiDomain + urlFormat;
logger.info("completeUrl:" + completeUrl); logger.info("getWikiPages completeUrl:{}", completeUrl);
HttpClientResult httpClientResult = HttpClientUtils.doGet(completeUrl, null); HttpClientResult httpClientResult = HttpClientUtils.doGet(completeUrl, null);
logger.info("httpClientResultCode:" + httpClientResult.getCode()); logger.info("httpClientResultCode:{}", httpClientResult.getCode());
return httpClientResult; return httpClientResult;
} }
@ -106,7 +109,7 @@ public class WikiService {
urlFormat += "?access_token=" + giteaToken; urlFormat += "?access_token=" + giteaToken;
} }
String completeUrl = URL + urlFormat; String completeUrl = URL + urlFormat;
logger.info("completeUrl: " + completeUrl); logger.info("getWiki completeUrl: {}", completeUrl);
return HttpClientUtils.doGet(completeUrl, null, null); return HttpClientUtils.doGet(completeUrl, null, null);
} }
@ -117,7 +120,7 @@ public class WikiService {
Map<String, String> params; Map<String, String> params;
WikiVo wikiVo = BeanCopyUtils.beanPropertiesCopy(wiki, WikiVo.class); WikiVo wikiVo = BeanCopyUtils.beanPropertiesCopy(wiki, WikiVo.class);
params = ConvertBean.convertToStringMap(wikiVo); params = ConvertBean.convertToStringMap(wikiVo);
logger.info("completeUrl: " + URL + urlFormat); logger.info("completeUrl: {}{}", URL, urlFormat);
String completeUrl = URL + urlFormat; String completeUrl = URL + urlFormat;
return HttpClientUtils.doPost(completeUrl, null, params); return HttpClientUtils.doPost(completeUrl, null, params);
} }
@ -129,7 +132,7 @@ public class WikiService {
String pageName = wiki.getTitle(); String pageName = wiki.getTitle();
String urlFormat = String.format("/repos/%s/%s/wiki/page/%s?access_token=%s", owner, repo, pageName, giteaToken); String urlFormat = String.format("/repos/%s/%s/wiki/page/%s?access_token=%s", owner, repo, pageName, giteaToken);
String completeUrl = URL + urlFormat; String completeUrl = URL + urlFormat;
logger.info("delete completeUrl " + completeUrl); logger.info("delete completeUrl {}", completeUrl);
return HttpClientUtils.doDelete(completeUrl); return HttpClientUtils.doDelete(completeUrl);
} }
@ -140,10 +143,43 @@ public class WikiService {
String urlFormat = String.format("/repos/%s/%s/wiki/page/%s?access_token=%s", owner, repo, pageName, giteaToken); String urlFormat = String.format("/repos/%s/%s/wiki/page/%s?access_token=%s", owner, repo, pageName, giteaToken);
WikiVo wikiVo = BeanCopyUtils.beanPropertiesCopy(wiki, WikiVo.class); WikiVo wikiVo = BeanCopyUtils.beanPropertiesCopy(wiki, WikiVo.class);
String completeUrl = URL + urlFormat; String completeUrl = URL + urlFormat;
logger.info("completeUrl: " + completeUrl); logger.info("completeUrl: {}", completeUrl);
return HttpClientUtils.doPatch(completeUrl, ConvertBean.convertToStringMap(wikiVo)); return HttpClientUtils.doPatch(completeUrl, ConvertBean.convertToStringMap(wikiVo));
} }
public WikiRevisionsVo getWikiRevisions(String owner, String repo, String pageName, String URL, String giteaToken) throws Exception {
String urlFormat = String.format("/repos/%s/%s/wiki/revisions/%s", owner, repo, pageName);
if (giteaToken != null) {
urlFormat += "?access_token=" + giteaToken;
}
String completeUrl = URL + urlFormat;
logger.info("getWikiRevisions completeUrl: {}", completeUrl);
HttpClientResult httpClientResult = HttpClientUtils.doGet(completeUrl, null, null);
if (httpClientResult == null) {
throw new ServiceException("获取wiki历史版本列表数据异常");
}
if (httpClientResult.getCode() != 200) {
throw new ServiceException(httpClientResult.getCode(),"操作失败", httpClientResult.getContent());
}
WikiRevisionsVo wikiRevisionsVo = JSON.parseObject(httpClientResult.getContent(), WikiRevisionsVo.class);
wikiRevisionsVo.getCommits().forEach(commit -> {
commit.getCommiter().setImage_url(userService.getRemoteUserLogoByUserLogin(commit.getCommiter().getName()));
commit.getAuthor().setImage_url(userService.getRemoteUserLogoByUserLogin(commit.getAuthor().getName()));
});
return wikiRevisionsVo;
}
public HttpClientResult getPreviousWiki(String owner, String repo, String pageName, String sha, String URL, String giteaToken) throws Exception {
String urlFormat = String.format("/repos/%s/%s/wiki/revisions/%s/%s", owner, repo, pageName, sha);
if (giteaToken != null) {
urlFormat += "?access_token=" + giteaToken;
}
String completeUrl = URL + urlFormat;
logger.info("getPreviousWiki completeUrl: {}", completeUrl);
return HttpClientUtils.doGet(completeUrl, null, null);
}
public String StringToURLEncode(String s) throws UnsupportedEncodingException { public String StringToURLEncode(String s) throws UnsupportedEncodingException {
s = java.net.URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "-").replaceAll("\\*", "%2A"); s = java.net.URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "-").replaceAll("\\*", "%2A");
@ -224,7 +260,7 @@ public class WikiService {
if (httpClientResult1.getCode() == 200) { if (httpClientResult1.getCode() == 200) {
WikiContentSimple wikiContentSimple = FastJsonUtils.toBean(httpClientResult1.getContent(), WikiContentSimple.class); WikiContentSimple wikiContentSimple = FastJsonUtils.toBean(httpClientResult1.getContent(), WikiContentSimple.class);
fileUtils.writeStringToFile(wikiContentSimple.getContent_base64(), wikiContentSimple.getTitle() + ".md", dirPath); fileUtils.writeStringToFile(wikiContentSimple.getContent_base64(), wikiContentSimple.getTitle() + ".md", dirPath);
logger.info("wikiContent" + wikiContentSimple.getSimple_content()); logger.info("wikiContent:{}", wikiContentSimple.getSimple_content());
} }
} }
downLoadZip(response, repoName, dirPath); downLoadZip(response, repoName, dirPath);
@ -309,7 +345,7 @@ public class WikiService {
org.apache.commons.io.FileUtils.forceDelete(new File(completeDestPath)); org.apache.commons.io.FileUtils.forceDelete(new File(completeDestPath));
org.apache.commons.io.FileUtils.forceDelete(new File(dirPath)); org.apache.commons.io.FileUtils.forceDelete(new File(dirPath));
} catch (IOException e) { } catch (IOException e) {
logger.warn("delete scratch zip file fail: " + completeDestPath); logger.warn("delete scratch zip file fail: {}", completeDestPath);
} }
} }
@ -332,7 +368,7 @@ public class WikiService {
try { try {
org.apache.commons.io.FileUtils.forceDelete(new File(filePath)); org.apache.commons.io.FileUtils.forceDelete(new File(filePath));
} catch (IOException e) { } catch (IOException e) {
logger.warn("delete scratch file fail: " + filePath); logger.warn("delete scratch file fail: {}", filePath);
} }
} }
} }