1#include "JobFetcher.h"
2
3#include <JobDetailsFetcher.h>
4
5#include <QNetworkAccessManager>
6#include <QJsonObject>
7#include <QJsonArray>
8#include <QJsonDocument>
9
10#include <QLogger.h>
11
12using namespace QLogger;
13
14namespace Jenkins
15{
16
17JobFetcher::JobFetcher(const Config &config, const QString &jobUrl, QObject *parent)
18 : IFetcher(config, parent)
19 , mJobUrl(jobUrl)
20{
21}
22
23void JobFetcher::triggerFetch()
24{
25 get(mJobUrl);
26}
27
28void JobFetcher::processData(const QJsonDocument &json)
29{
30 const auto jsonObject = json.object();
31 QMultiMap<QString, JenkinsJobInfo> jobsMap;
32
33 if (!jsonObject.contains(QStringLiteral("views")))
34 QLog_Debug("Jenkins", "Views not found.");
35 else if (!jsonObject.contains(QStringLiteral("jobs")))
36 QLog_Debug("Jenkins", "Jobs not found.");
37
38 const auto views = jsonObject[QStringLiteral("views")].toArray();
39
40 for (const auto &view : views)
41 {
42 const auto _class = view[QStringLiteral("_class")].toString();
43 const auto generalView = _class.contains("AllView");
44 const auto jsonObject = view.toObject();
45 const auto jobs = jsonObject[QStringLiteral("jobs")].toArray();
46
47 for (const auto &job : jobs)
48 {
49 QJsonObject jobObject = job.toObject();
50 QString url;
51
52 if (jobObject.contains(QStringLiteral("url")))
53 url = jobObject[QStringLiteral("url")].toString();
54
55 if (jobObject[QStringLiteral("_class")].toString().contains("WorkflowMultiBranchProject") && !generalView)
56 {
57 get(url);
58 }
59 else if (jobObject[QStringLiteral("_class")].toString().contains("WorkflowJob"))
60 {
61 JenkinsJobInfo jobInfo;
62 jobInfo.url = url;
63
64 if (jobObject.contains(QStringLiteral("displayName")))
65 jobInfo.name = jobObject[QStringLiteral("displayName")].toString();
66 else
67 jobInfo.name = jobObject[QStringLiteral("name")].toString();
68
69 if (jobObject.contains(QStringLiteral("color")))
70 jobInfo.color = jobObject[QStringLiteral("color")].toString();
71
72 jobsMap.insert(view[QStringLiteral("name")].toString(), jobInfo);
73 }
74 }
75 }
76
77 emit signalJobsReceived(jobsMap);
78}
79
80}
81