Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo-1
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo-1
Commits
7268a935
Unverified
Commit
7268a935
authored
May 21, 2020
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
同步 GitHub solo-blog 仓库功能 #125
parent
f4de9427
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
8 deletions
+34
-8
src/main/java/org/b3log/solo/util/GitHubs.java
src/main/java/org/b3log/solo/util/GitHubs.java
+34
-8
No files found.
src/main/java/org/b3log/solo/util/GitHubs.java
View file @
7268a935
...
...
@@ -66,11 +66,21 @@ public final class GitHubs {
}
}
private
static
boolean
updateFile
(
final
String
pat
,
final
String
loginName
,
final
String
repoName
,
final
String
filePath
,
final
byte
[]
content
)
{
/**
* Updates a file by the specified personal access token, GitHub login name, repo name, file path and file content.
*
* @param pat the specified personal access token
* @param loginName the specified GitHub login name
* @param repoName the specified repo name
* @param filePath the specfiied file name
* @param content the specified file content
* @return {@code true} if ok, returns {@code false} if failed
*/
public
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
(
3
000
).
timeout
(
60000
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
send
();
connectionTimeout
(
7
000
).
timeout
(
60000
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
send
();
int
statusCode
=
response
.
statusCode
();
response
.
charset
(
"UTF-8"
);
String
responseBody
=
response
.
bodyText
();
...
...
@@ -95,7 +105,7 @@ public final class GitHubs {
}
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
();
connectionTimeout
(
7000
).
timeout
(
60000
*
2
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
bodyText
(
body
.
toString
()).
send
();
statusCode
=
response
.
statusCode
();
response
.
charset
(
"UTF-8"
);
responseBody
=
response
.
bodyText
();
...
...
@@ -110,7 +120,17 @@ public final class GitHubs {
}
}
private
static
boolean
createOrUpdateGitHubRepo
(
final
String
pat
,
final
String
loginName
,
final
String
repoName
,
final
String
repoDesc
,
final
String
repoHomepage
)
{
/**
* Creates or updates a GitHub repository by the specified personal access token, GitHub login name, repo name repo desc and repo homepage.
*
* @param pat the specified personal access token
* @param loginName the specified GitHub login name
* @param repoName the specified repo name
* @param repoDesc the specified repo desc
* @param repoHomepage the specified repo homepage
* @return {@code true} if ok, returns {@code false} if failed
*/
public
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
).
...
...
@@ -118,7 +138,7 @@ public final class GitHubs {
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
();
connectionTimeout
(
7000
).
timeout
(
30000
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
bodyText
(
body
.
toString
()).
send
();
int
statusCode
=
response
.
statusCode
();
response
.
charset
(
"UTF-8"
);
String
responseBody
=
response
.
bodyText
();
...
...
@@ -131,7 +151,7 @@ public final class GitHubs {
}
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
();
connectionTimeout
(
7000
).
timeout
(
30000
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
bodyText
(
body
.
toString
()).
send
();
statusCode
=
response
.
statusCode
();
responseBody
=
response
.
bodyText
();
if
(
200
!=
statusCode
)
{
...
...
@@ -145,10 +165,16 @@ public final class GitHubs {
}
}
private
static
JSONObject
getGitHubUser
(
final
String
pat
)
{
/**
* Gets GitHub user by the specified personal access token.
*
* @param pat the specified personal access token
* @return GitHub user, returns {@code null} if failed
*/
public
static
JSONObject
getGitHubUser
(
final
String
pat
)
{
try
{
final
HttpResponse
response
=
HttpRequest
.
get
(
"https://api.github.com/user"
).
header
(
"Authorization"
,
"token "
+
pat
).
connectionTimeout
(
3000
).
timeout
(
7
000
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
send
();
connectionTimeout
(
7000
).
timeout
(
30
000
).
header
(
"User-Agent"
,
Solos
.
USER_AGENT
).
send
();
if
(
200
!=
response
.
statusCode
())
{
return
null
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment