| 1 | #include "RepoFetcher.h" | 
|---|---|
| 2 | |
| 3 | #include <QNetworkAccessManager> | 
| 4 | #include <QNetworkReply> | 
| 5 | #include <QJsonDocument> | 
| 6 | #include <QJsonObject> | 
| 7 | #include <QJsonArray> | 
| 8 | |
| 9 | #include <QLogger.h> | 
| 10 | |
| 11 | using namespace QLogger; | 
| 12 | |
| 13 | namespace Jenkins | 
| 14 | { | 
| 15 | |
| 16 | RepoFetcher::RepoFetcher(const IFetcher::Config &config, const QString &url, QObject *parent) | 
| 17 | : IFetcher(config, parent) | 
| 18 | , mUrl(url) | 
| 19 | { | 
| 20 | } | 
| 21 | |
| 22 | RepoFetcher::~RepoFetcher() | 
| 23 | { | 
| 24 |    QLog_Debug("Jenkins", "Destroying repo fetcher object.");  | 
| 25 | } | 
| 26 | |
| 27 | void RepoFetcher::triggerFetch() | 
| 28 | { | 
| 29 | get(mUrl); | 
| 30 | } | 
| 31 | |
| 32 | void RepoFetcher::processData(const QJsonDocument &json) | 
| 33 | { | 
| 34 | const auto jsonObject = json.object(); | 
| 35 | |
| 36 |    if (!jsonObject.contains(QStringLiteral("views")))  | 
| 37 | { | 
| 38 |       QLog_Info("Jenkins", "Views are absent.");  | 
| 39 | return; | 
| 40 | } | 
| 41 | |
| 42 |    const auto views = jsonObject[QStringLiteral("views")].toArray();  | 
| 43 | QVector<JenkinsViewInfo> viewsInfo; | 
| 44 | viewsInfo.reserve(views.count()); | 
| 45 | |
| 46 | for (const auto &view : views) | 
| 47 | { | 
| 48 | auto appendView = false; | 
| 49 | const auto viewObject = view.toObject(); | 
| 50 |       const auto jobs = viewObject[QStringLiteral("jobs")].toArray();  | 
| 51 | |
| 52 | for (const auto &job : jobs) | 
| 53 | { | 
| 54 | QJsonObject jobObject = job.toObject(); | 
| 55 | QString url; | 
| 56 | |
| 57 |          if (jobObject.contains(QStringLiteral("url")))  | 
| 58 |             url = jobObject[QStringLiteral("url")].toString();  | 
| 59 | |
| 60 |          if (jobObject[QStringLiteral("_class")].toString().contains( "WorkflowMultiBranchProject"))  | 
| 61 | { | 
| 62 | JenkinsViewInfo info; | 
| 63 | info.url = url; | 
| 64 | |
| 65 |             if (jobObject.contains(QStringLiteral("name")))  | 
| 66 |                info.name = jobObject[QStringLiteral("name")].toString();  | 
| 67 | |
| 68 | viewsInfo.append(info); | 
| 69 | } | 
| 70 |          else if (jobObject[QStringLiteral("_class")].toString().contains( "WorkflowJob"))  | 
| 71 | appendView = true; | 
| 72 | } | 
| 73 | |
| 74 | if (appendView) | 
| 75 | { | 
| 76 | #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) | 
| 77 | const auto flag = Qt::SkipEmptyParts; | 
| 78 | #else | 
| 79 | const auto flag = QString::SkipEmptyParts; | 
| 80 | #endif | 
| 81 | |
| 82 | JenkinsViewInfo info; | 
| 83 |          info.url = viewObject[QStringLiteral("url")].toString();  | 
| 84 |          info.name = info.url.split("/", flag).constLast();  | 
| 85 | viewsInfo.prepend(info); | 
| 86 | } | 
| 87 | } | 
| 88 | |
| 89 | emit signalViewsReceived(viewsInfo); | 
| 90 | } | 
| 91 | |
| 92 | } | 
| 93 |