-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprojectforgejo.cpp
298 lines (239 loc) · 9.86 KB
/
projectforgejo.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "projectforgejo.h"
#include "chumpackage.h"
#include "main.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QLocale>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSharedPointer>
#include <QUrl>
#include <QVariantList>
#include <QVariantMap>
QMap<QString, QString> ProjectForgejo::s_sites;
//////////////////////////////////////////////////////
/// helper functions
static QString getName(const QVariant &v) {
QVariantMap m = v.toMap();
QString login = m[QLatin1String("username")].toString();
QString name = m[QLatin1String("full_name")].toString();
if (login.isEmpty() || name==login) return name;
if (name.isEmpty()) return login;
return QStringLiteral("%1 (%2)").arg(name, login);
}
static void parseUrl(const QString &u, QString &h, QString &path) {
QUrl url(u);
h = url.host();
QString p = url.path();
if (p.startsWith('/')) p = p.mid(1);
if (p.endsWith('/')) p.chop(1);
path = p;
}
//////////////////////////////////////////////////////
/// ProjectForgejo
ProjectForgejo::ProjectForgejo(const QString &url, ChumPackage *package) : ProjectAbstract(package) {
initSites();
parseUrl(url, m_host, m_path);
if (!s_sites.contains(m_host)) {
qWarning() << "Shouldn't happen: ProjectForgejo initialized with incorrect service" << url;
return;
}
m_token = s_sites.value(m_host, QString{});
// url is not set as it can be different homepage that is retrieved from query
m_package->setUrlIssues(QStringLiteral("https://%1/%2/issues").arg(m_host, m_path));
// fetch information from Forgejo
fetchRepoInfo();
}
// static
void ProjectForgejo::initSites() {
if (!s_sites.isEmpty()) return;
for (const QString &sitetoken: QStringLiteral(FORGEJO_TOKEN).split(QChar('|'))) {
QStringList st = sitetoken.split(QChar(':'));
if (st.size() != 2) {
qWarning() << "Error parsing provided Forgejo site-token pairs";
return;
}
s_sites[st[0]] = st[1];
qDebug() << "Forgejo support added for" << st[0];
}
}
// static
bool ProjectForgejo::isProject(const QString &url) {
initSites();
QString h, p;
parseUrl(url, h, p);
return s_sites.contains(h);
}
QNetworkReply* ProjectForgejo::sendQuery(const QString &query) {
QString reqAuth = QStringLiteral("token %1").arg(m_token);
QString reqUrl = QStringLiteral("https://%1/api/v1%2").arg(m_host).arg(query);
QNetworkRequest request;
request.setUrl(reqUrl);
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Authorization", reqAuth.toLocal8Bit());
return nMng->get(request);
}
void ProjectForgejo::fetchRepoInfo() {
QNetworkReply *reply = sendQuery(QStringLiteral("/repos/%1").arg(m_path));
connect(reply, &QNetworkReply::finished, this, [this, reply](){
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Forgejo: Failed to fetch repository data for Forgejo" << this->m_path;
qWarning() << "Forgejo: Error: " << reply->errorString();
}
QByteArray data = reply->readAll();
QJsonObject r = QJsonDocument::fromJson(data).object();
QString v;
int vi;
v = r.value("owner").toObject().value("login").toString();
if (!v.isEmpty()) m_package->setDeveloperLogin(v);
v = r.value("owner").toObject().value("full_name").toString();
if (!v.isEmpty()) m_package->setDeveloperName(v);
v = r.value("website").toString();
if (!v.isEmpty()) m_package->setUrl(v);
vi = r.value("stars_count").toInt(-1);
if (vi>=0) m_package->setStarsCount(vi);
vi = r.value("forks_count").toInt(-1);
if (vi>=0) m_package->setForksCount(vi);
vi = r.value("open_issues_count").toInt(-1);
if (vi>=0) m_package->setIssuesCount(vi);
vi = r.value("release_counter").toInt(-1);
if (vi>=0) m_package->setReleasesCount(vi);
reply->deleteLater();
});
}
void ProjectForgejo::comments(const QString &issue_id, const QVariantMap &comment, LoadableObject *value) {
QNetworkReply *reply = sendQuery( QStringLiteral("/repos/%1/issues/%2/comments").arg(m_path).arg(issue_id));
connect(reply, &QNetworkReply::finished, this, [this, issue_id, comment, reply, value](){
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Forgejo: Failed to fetch issue for" << this->m_path;
qWarning() << "Forgejo: Error: " << reply->errorString();
}
QByteArray data = reply->readAll();
QVariantList r = QJsonDocument::fromJson(data).array().toVariantList();
QVariantList rlist;
rlist.append(comment); // add issue comment as first comment
for (const auto &e: r) {
QVariantMap element = e.toMap();
QVariantMap m;
m["author"] = getName(element.value("user"));
m["created"] = parseDate(element.value("created_at").toString(), true);
m["updated"] = parseDate(element.value("updated_at").toString(), true);
m["body"] = element.value("body").toString();
rlist.append(m);
}
QVariantMap result = value->value();
result["discussion"] = rlist;
value->setValue(issue_id, result);
reply->deleteLater();
});
}
void ProjectForgejo::issue(const QString &issue_id, LoadableObject *value) {
if (value->ready() && value->valueId()==issue_id)
return; // value already corresponds to that issue
value->reset(issue_id);
QNetworkReply *reply = sendQuery( QStringLiteral("/repos/%1/issues/%2").arg(m_path).arg(issue_id));
connect(reply, &QNetworkReply::finished, this, [this, issue_id, reply, value](){
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Forgejo: Failed to fetch issue for" << this->m_path;
qWarning() << "Forgejo: Error: " << reply->errorString();
}
QByteArray data = reply->readAll();
QVariantMap r = QJsonDocument::fromJson(data).object().toVariantMap();
QVariantMap result;
result["id"] = r.value("number").toInt();
result["number"] = r.value("number");
result["title"] = r.value("title");
result["commentsCount"] = r.value("comments").toInt();
value->setValue(issue_id, result);
// load comments separately: save "first comment"
QVariantMap commentZero;
commentZero["author"] = getName(r.value("user"));
commentZero["created"] = parseDate(r.value("created_at").toString(), true);
commentZero["updated"] = parseDate(r.value("updated_at").toString(), true);
// FIXME: this is stored as markdown, not HTML
// maybe use /miscellaneous/renderMarkdown to get HTML
commentZero["body"] = r.value("body").toString();
// load comments
comments(result["id"].toString(), commentZero, value);
reply->deleteLater();
});
}
void ProjectForgejo::issues(LoadableObject *value) {
const QString issues_id{QStringLiteral("issues")};
value->reset(issues_id);
QNetworkReply *reply = sendQuery( QStringLiteral("/repos/%1/%2?state=open&type=issue").arg(m_path).arg(issues_id));
connect(reply, &QNetworkReply::finished, this, [this, issues_id, reply, value](){
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Forgejo: Failed to fetch issues for" << this->m_path;
qWarning() << "Forgejo: Error: " << reply->errorString();
}
QByteArray data = reply->readAll();
QVariantList r = QJsonDocument::fromJson(data).array().toVariantList();
QVariantList rlist;
for (const auto &e: r) {
QVariantMap element = e.toMap();
QVariantMap m;
m["id"] = element.value("number");
m["author"] = getName(element.value("user"));
m["commentsCount"] = element.value("comments");
m["number"] = element.value("number");
m["title"] = element.value("title");
m["created"] = parseDate(element.value("created_at").toString(), true);
m["updated"] = parseDate(element.value("updated_at").toString(), true);
rlist.append(m);
}
QVariantMap result;
result["issues"] = rlist;
value->setValue(issues_id, result);
reply->deleteLater();
});
}
void ProjectForgejo::release(const QString &release_id, LoadableObject *value) {
if (value->ready() && value->valueId()==release_id)
return; // value already corresponds to that release
value->reset(release_id);
QNetworkReply *reply = sendQuery( QStringLiteral("/repos/%1/releases/%2").arg(m_path).arg(release_id));
connect(reply, &QNetworkReply::finished, this, [this, release_id, reply, value](){
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Forgejo: Failed to fetch release for" << this->m_path;
qWarning() << "Forgejo: Error: " << reply->errorString();
}
QByteArray data = reply->readAll();
QVariantMap r = QJsonDocument::fromJson(data).object().toVariantMap();
QVariantMap result;
result["name"] = r.value("name");
result["description"] = r.value("body");
result["datetime"] = parseDate(r.value("created_at").toString());
value->setValue(release_id, result);
reply->deleteLater();
});
}
void ProjectForgejo::releases(LoadableObject *value) {
const QString releases_id{QStringLiteral("releases")};
value->reset(releases_id);
QNetworkReply *reply = sendQuery( QStringLiteral("/repos/%1/%2?pre-release=true").arg(m_path).arg(releases_id));
connect(reply, &QNetworkReply::finished, this, [this, releases_id, reply, value](){
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Forgejo: Failed to fetch releases for" << this->m_path;
qWarning() << "Forgejo: Error: " << reply->errorString();
}
QByteArray data = reply->readAll();
QVariantList r = QJsonDocument::fromJson(data).array().toVariantList();
QVariantList rlist;
for (const auto &e: r) {
QVariantMap element = e.toMap();
QVariantMap m;
m["id"] = element.value("id");
m["name"] = element.value("name");
m["datetime"] = parseDate(element.value("created_at").toString());
rlist.append(m);
}
QVariantMap result;
result["releases"] = rlist;
value->setValue(releases_id, result);
reply->deleteLater();
});
}