-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolo.go
210 lines (184 loc) · 6.33 KB
/
solo.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Octocat - B3log 的 GitHub 仓库操作服务
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package main
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/parnurzeal/gorequest"
)
func pushRepos(c *gin.Context) {
result := gulu.Ret.NewResult()
result.Code = -1
ak := c.PostForm("ak")
user := user(ak)
if nil == user {
result.Msg = "get user failed"
c.JSON(http.StatusOK, result)
return
}
user["ak"] = ak
file, err := c.FormFile("file")
if nil != err {
result.Msg = "get file failed"
c.JSON(http.StatusOK, result)
return
}
if 128 > file.Size {
result.Msg = "file is too small"
c.JSON(http.StatusOK, result)
return
}
f, err := file.Open()
if nil != err {
result.Msg = "read file failed"
c.JSON(http.StatusOK, result)
return
}
fileData, err := ioutil.ReadAll(f)
if nil != err {
result.Msg = "read file data failed"
c.JSON(http.StatusOK, result)
return
}
repoName := c.PostForm("repoName")
repoReadme := c.PostForm("repoReadme")
repoDesc := c.PostForm("repoDesc")
repoHomepage := c.PostForm("repoHomepage")
favicon := c.PostForm("favicon")
stat := c.PostForm("stat")
owner := user["login"].(string)
repoFullName := owner + "/" + repoName
//logger.Infof("pushing repo [%s]", repoFullName)
repo := createOrUpdateRepo(user, repoName, repoDesc, repoHomepage)
if nil == repo {
result.Msg = "create or update repo failed"
c.JSON(http.StatusOK, result)
return
}
repoReadme = strings.Replace(repoReadme, "${repoFullName}", repoFullName, -1)
ok := updateFile(user, repoName, "README.md", []byte(repoReadme))
if ok {
ok = updateFile(user, repoName, "backup.zip", fileData)
}
if ok {
result.Code = 0
}
c.JSON(http.StatusOK, result)
var articleCnt int
var recentArticleTime uint64
statMap := map[string]interface{}{}
if err := json.Unmarshal([]byte(stat), &statMap); nil == err {
articleCnt = int(statMap["articleCount"].(float64))
recentArticleTime = uint64(statMap["recentArticleTime"].(float64))
}
blogs.Store(repoFullName, &blog{repoDesc, repoHomepage, repoFullName, favicon, articleCnt, recentArticleTime})
}
func updateFile(user map[string]interface{}, repoName, filePath string, content []byte) (ok bool) {
ak := user["ak"].(string)
owner := user["login"].(string)
fullRepoName := owner + "/" + repoName
result := map[string]interface{}{}
response, bytes, errors := gorequest.New().Get("https://api.github.com/repos/"+fullRepoName+"/git/trees/master?access_token="+ak).
Set("User-Agent", UserAgent).Timeout(30 * time.Second).EndStruct(&result)
if nil != errors {
logger.Errorf("get git tree of file [%s] failed: %s", filePath, errors[0])
return
}
if http.StatusOK != response.StatusCode && http.StatusConflict != response.StatusCode {
logger.Errorf("get git tree of file [%s] status code [%d], body [%s]", filePath, response.StatusCode, string(bytes))
return
}
body := map[string]interface{}{
"message": ":memo: 更新博客",
"content": base64.StdEncoding.EncodeToString(content),
}
if http.StatusOK == response.StatusCode {
tree := result["tree"].([]interface{})
for _, f := range tree {
file := f.(map[string]interface{})
if filePath == file["path"].(string) {
body["sha"] = file["sha"]
break
}
}
}
response, bytes, errors = gorequest.New().Put("https://api.github.com/repos/"+fullRepoName+"/contents/"+filePath+"?access_token="+ak).
Set("User-Agent", UserAgent).Timeout(2 * time.Minute).
SendMap(body).EndStruct(&result)
if nil != errors {
logger.Errorf("update repo [%s] file [%s] failed: %s", fullRepoName, filePath, errors[0])
return
}
if http.StatusOK != response.StatusCode && http.StatusCreated != response.StatusCode {
logger.Errorf("update repo [%s] file [%s] status code: %d, body: %s", fullRepoName, filePath, response.StatusCode, string(bytes))
return
}
//logger.Infof("updated repo [%s] file [%s]", fullRepoName, filePath)
return true
}
func createOrUpdateRepo(user map[string]interface{}, repoName, repoDesc, repoHomepage string) (repo map[string]interface{}) {
body := map[string]interface{}{
"name": repoName,
"description": repoDesc,
"homepage": repoHomepage,
"has_wiki": false,
}
ak := user["ak"].(string)
response, bytes, errors := gorequest.New().Post("https://api.github.com/user/repos?access_token="+ak).
Set("User-Agent", UserAgent).Timeout(5 * time.Second).
SendMap(body).EndStruct(&repo)
if nil != errors {
logger.Errorf("create repo failed: %v", errors[0])
return nil
}
if http.StatusCreated != response.StatusCode && http.StatusUnprocessableEntity != response.StatusCode {
logger.Errorf("create repo [%s] status code [%d], body [%s]", repo["full_name"], response.StatusCode, string(bytes))
return nil
}
if http.StatusCreated == response.StatusCode {
logger.Infof("created repo [%s]", repo["full_name"])
return
}
owner := user["login"].(string)
response, bytes, errors = gorequest.New().Patch("https://api.github.com/repos/"+owner+"/"+repoName+"?access_token="+ak).
Set("User-Agent", UserAgent).Timeout(5 * time.Second).
SendMap(body).EndStruct(&repo)
if nil != errors {
logger.Errorf("update repo failed: %v", errors[0])
return nil
}
if http.StatusOK != response.StatusCode {
logger.Errorf("update repo [%s] status code [%d], body [%s]", repo["full_name"], response.StatusCode, string(bytes))
return nil
}
logger.Infof("updated repo [%s]", repo["full_name"])
return
}
func user(ak string) (ret map[string]interface{}) {
response, bytes, errors := gorequest.New().Get("https://api.github.com/user?access_token="+ak).
Set("User-Agent", UserAgent).Timeout(5 * time.Second).EndStruct(&ret)
if nil != errors {
logger.Errorf("get user failed: %v", errors[0])
return nil
}
if http.StatusOK != response.StatusCode {
logger.Errorf("get user status code [%d], body [%s]", response.StatusCode, string(bytes))
return nil
}
return
}