-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildhistoryfetcher.cpp
84 lines (71 loc) · 2.8 KB
/
buildhistoryfetcher.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
#include "buildhistoryfetcher.h"
#include "jenkinspluginconstants.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
using namespace JenkinsCI::Internal;
BuildHistoryFetcher::BuildHistoryFetcher(std::shared_ptr< RestRequestBuilder > restRequestBuilder,
QObject *parent)
: QObject(parent)
{
_manager = std::make_unique< QNetworkAccessManager >();
_restRequestBuilder = restRequestBuilder;
connect(_manager.get(), &QNetworkAccessManager::finished, this,
&BuildHistoryFetcher::readBuildInfoReply);
connect(this, &BuildHistoryFetcher::nextBuildInfoFetchRequested, this,
&BuildHistoryFetcher::processFirstPendingUrl);
}
void BuildHistoryFetcher::fetchBuildHistory(QStringList urls)
{
_urlsToFetch = urls;
processFirstPendingUrl();
}
void BuildHistoryFetcher::readBuildInfoReply(QNetworkReply *reply)
{
if (reply == nullptr)
return;
QByteArray data = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
if (jsonDoc.isNull())
{
qDebug() << Q_FUNC_INFO << "json is null";
return;
}
QJsonObject jsonObject = jsonDoc.object();
if (!jsonObject.contains(QStringLiteral("url")))
{
return;
}
QString buildUrl = jsonObject[QStringLiteral("url")].toString();
// fill build info
BuildInfo info;
info.setUrl(buildUrl);
if (jsonObject.contains(QStringLiteral("number")))
info.setNumber(jsonObject[QStringLiteral("number")].toInt());
if (jsonObject.contains(QStringLiteral("timestamp")))
info.setTimestamp(jsonObject[QStringLiteral("timestamp")].toVariant().toULongLong());
if (jsonObject.contains(QStringLiteral("result")))
info.setResult(jsonObject[QStringLiteral("result")].toString());
if (jsonObject.contains(QStringLiteral("displayName")))
info.setDisplayName(jsonObject[QStringLiteral("displayName")].toString());
if (jsonObject.contains(QStringLiteral("fullDisplayName")))
info.setFullDisplayName(jsonObject[QStringLiteral("fullDisplayName")].toString());
if (jsonObject.contains(QStringLiteral("description")))
info.setDescription(jsonObject[QStringLiteral("description")].toString());
if (jsonObject.contains(QStringLiteral("duration")))
info.setDuration(jsonObject[QStringLiteral("duration")].toInt());
emit buildInfoFetched(info);
emit nextBuildInfoFetchRequested();
}
void BuildHistoryFetcher::processFirstPendingUrl()
{
if (_urlsToFetch.isEmpty())
return;
QString url = _urlsToFetch.takeFirst();
QNetworkRequest request = _restRequestBuilder->buildBuildInfoRequest(url);
_manager->get(request);
}
std::shared_ptr<RestRequestBuilder> BuildHistoryFetcher::restRequestBuilder() const
{
return _restRequestBuilder;
}