Commit f4de9427 authored by Liang Ding's avatar Liang Ding

同步 GitHub solo-blog 仓库功能 #125

parent c076cf9f
......@@ -13,6 +13,7 @@ package org.b3log.solo.util;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -21,11 +22,13 @@ import org.b3log.solo.model.Common;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Base64;
/**
* GitHub utilities.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.2, Mar 17, 2020
* @version 1.1.0.0, May 20, 2020
* @since 3.0.0
*/
public final class GitHubs {
......@@ -63,6 +66,100 @@ public final class GitHubs {
}
}
private static boolean updateFile(final String pat, final String loginName, final String repoName, final String filePath, final byte[] content) {
final String fullRepoName = loginName + "/" + repoName;
try {
HttpResponse response = HttpRequest.get("https://api.github.com/repos/" + fullRepoName + "/git/trees/master").header("Authorization", "token " + pat).
connectionTimeout(3000).timeout(60000).header("User-Agent", Solos.USER_AGENT).send();
int statusCode = response.statusCode();
response.charset("UTF-8");
String responseBody = response.bodyText();
if (200 != statusCode && 409 != statusCode) {
LOGGER.log(Level.ERROR, "Get git tree of file [" + filePath + "] failed: " + responseBody);
return false;
}
final JSONObject body = new JSONObject().
put("message", ":memo: 更新博客").
put("content", Base64.getEncoder().encode(content));
if (200 == statusCode) {
final JSONObject responseData = new JSONObject(responseBody);
final JSONArray tree = responseData.optJSONArray("tree");
for (int i = 0; i < tree.length(); i++) {
final JSONObject file = tree.optJSONObject(i);
if (StringUtils.equals(filePath, file.optString("path"))) {
body.put("sha", file.optString("sha"));
break;
}
}
}
response = HttpRequest.put("https://api.github.com/repos/" + fullRepoName + "/contents/" + filePath).header("Authorization", "token " + pat).
connectionTimeout(3000).timeout(60000 * 2).header("User-Agent", Solos.USER_AGENT).body(body.toString()).send();
statusCode = response.statusCode();
response.charset("UTF-8");
responseBody = response.bodyText();
if (200 != statusCode && 201 != statusCode) {
LOGGER.log(Level.ERROR, "Updates repo [" + repoName + "] file [" + filePath + "] failed: " + responseBody);
return false;
}
return true;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Updates repo [" + repoName + "] file [" + filePath + "] failed: " + e.getMessage());
return false;
}
}
private static boolean createOrUpdateGitHubRepo(final String pat, final String loginName, final String repoName, final String repoDesc, final String repoHomepage) {
try {
final JSONObject body = new JSONObject().
put("name", repoName).
put("description", repoDesc).
put("homepage", repoHomepage).
put("has_wiki", false);
HttpResponse response = HttpRequest.post("https://api.github.com/user/repos").header("Authorization", "token " + pat).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).body(body.toString()).send();
int statusCode = response.statusCode();
response.charset("UTF-8");
String responseBody = response.bodyText();
if (201 != statusCode && 422 != statusCode) {
LOGGER.log(Level.ERROR, "Creates GitHub repo [" + repoName + "] failed: " + responseBody);
return false;
}
if (201 == statusCode) {
return true;
}
response = HttpRequest.patch("https://api.github.com/repos/" + loginName + "/" + repoName).header("Authorization", "token " + pat).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).body(body.toString()).send();
statusCode = response.statusCode();
responseBody = response.bodyText();
if (200 != statusCode) {
LOGGER.log(Level.ERROR, "Updates GitHub repo [" + repoName + "] failed: " + responseBody);
return false;
}
return true;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Creates or updates GitHub repo failed: " + e.getMessage());
return false;
}
}
private static JSONObject getGitHubUser(final String pat) {
try {
final HttpResponse response = HttpRequest.get("https://api.github.com/user").header("Authorization", "token " + pat).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send();
if (200 != response.statusCode()) {
return null;
}
response.charset("UTF-8");
return new JSONObject(response.bodyText());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets GitHub user info failed: " + e.getMessage());
return null;
}
}
/**
* Private constructor.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment