-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitalkAutoInit.java
More file actions
92 lines (84 loc) · 3.91 KB
/
GitalkAutoInit.java
File metadata and controls
92 lines (84 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import java.util.Arrays;
import java.util.HashMap;
public class GitalkAutoInit {
/**
* @param owner Gitalk 配置的 owner
* @param repo Gitalk 配置的 repo
* @param token 生成 token 地址 https://github.com/settings/tokens
* @param sitemapUrl
* @param clientId Gitalk 配置的 clientID
* @param clientSecret Gitalk 配置的 clientSecret
* @param indexPageName
*/
public static void initGitalk(String owner, String repo, String token, String sitemapUrl, String clientId, String clientSecret, String indexPageName) {
Digester md5 = new Digester(DigestAlgorithm.MD5);
String issueUrl = "https://api.github.com/repos/" + owner + "/" + repo + "/issues";
String xmlStr = HttpUtil.get(sitemapUrl, CharsetUtil.CHARSET_UTF_8);
// 解析 sitemap 文件
Document doc = XmlUtil.parseXml(xmlStr);
NodeList locs = XmlUtil.getNodeListByXPath("//urlset/url/loc", doc);
Console.log("Gitalk 初始化开始");
for (int i = 0; i < locs.getLength(); i++) {
String loc = locs.item(i).getTextContent();
String path = URLUtil.encode(URLUtil.getPath(loc));
String label = md5.digestHex(path);
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("client_id", clientId);
paramMap.put("client_secret", clientSecret);
paramMap.put("labels", "Gitalk," + label);
HttpResponse issueListResp = HttpRequest.get(issueUrl).form(paramMap).execute();
if (!issueListResp.isOk()) {
Console.error("查询仓库评论失败,返回[{}]", issueListResp.body());
return;
}
JSONArray issueList = JSONUtil.parseArray(HttpUtil.get(issueUrl, paramMap));
// issueList 为空表示还未初始化
if (CollUtil.isEmpty(issueList)) {
HashMap<String, Object> params = new HashMap<>();
params.put("body", loc);
params.put("labels", Arrays.asList("Gitalk", label));
String[] titles = path.split("/");
String title;
if (titles.length == 0) {
title = indexPageName;
} else {
title = URLUtil.decode(titles[titles.length - 1], "utf-8");
// 获取 issue title
title = title.substring(3).replace(".html", "");
}
params.put("title", title);
HttpResponse issueCreateResp = HttpRequest.post(issueUrl)
.header("Authorization", "token " + token)
.body(JSONUtil.toJsonStr(params))
.execute();
if (issueCreateResp.isOk()) {
Console.log("[{}]初始化成功", path);
} else {
Console.error("[{}]初始化失败,返回[{}]", path, issueCreateResp.body());
return;
}
} else {
Console.log("[{}]无需再次初始化", path);
}
}
Console.log("Gitalk 初始化结束");
}
public static void main(String[] args) {
GitalkAutoInit.initGitalk("Sdky", "MyBook", "fe52ce1234543210b7a203991ffde258865ea5ed",
"https://sdky.gitee.io/sitemap.xml", "b604e00b66666f971684", "85ee84c1a123d7994fccf2efd5ed951e1f41b676", "简介");
}
}