1#include "BuildGeneralInfoFetcher.h"
2
3#include <StageFetcher.h>
4
5#include <QJsonDocument>
6#include <QJsonObject>
7#include <QJsonArray>
8
9namespace Jenkins
10{
11
12BuildGeneralInfoFetcher::BuildGeneralInfoFetcher(const Config &config, const JenkinsJobBuildInfo &build,
13 QObject *parent)
14 : IFetcher(config, parent)
15 , mBuild(build)
16{
17}
18
19void BuildGeneralInfoFetcher::triggerFetch()
20{
21 get(mBuild.url + QString::fromUtf8("api/json"), true);
22}
23
24void BuildGeneralInfoFetcher::processData(const QJsonDocument &json)
25{
26 auto jsonObject = json.object();
27
28 if (jsonObject.contains(QStringLiteral("duration")))
29 mBuild.duration = jsonObject[QStringLiteral("duration")].toInt();
30 if (jsonObject.contains(QStringLiteral("result")))
31 mBuild.result = jsonObject[QStringLiteral("result")].toString();
32 if (jsonObject.contains(QStringLiteral("timestamp")))
33 mBuild.date = QDateTime::fromMSecsSinceEpoch(jsonObject[QStringLiteral("timestamp")].toInt());
34
35 if (jsonObject.contains(QStringLiteral("culprits")))
36 {
37 const auto culprits = jsonObject[QStringLiteral("culprits")].toArray();
38
39 for (const auto &item : culprits)
40 {
41 const auto obj = item.toObject();
42
43 if (obj.contains(QStringLiteral("fullName")))
44 {
45 mBuild.user = obj[QStringLiteral("fullName")].toString();
46 break;
47 }
48 }
49 }
50
51 if (jsonObject.contains(QStringLiteral("artifacts")))
52 {
53 const auto artifacts = jsonObject[QStringLiteral("artifacts")].toArray();
54
55 for (const auto &artifact : artifacts)
56 {
57 JenkinsJobBuildInfo::Artifact sArtifact;
58 sArtifact.fileName = artifact[QStringLiteral("fileName")].toString();
59 sArtifact.url = QString("%1artifact/%2").arg(jsonObject[QStringLiteral("url")].toString(), sArtifact.fileName);
60
61 mBuild.artifacts.append(sArtifact);
62 }
63 }
64
65 const auto stagesFetcher = new StageFetcher(mConfig, mBuild, this);
66 connect(stagesFetcher, &StageFetcher::signalStagesReceived, this, &BuildGeneralInfoFetcher::appendStages);
67
68 stagesFetcher->triggerFetch();
69}
70
71void BuildGeneralInfoFetcher::appendStages(const QVector<JenkinsStageInfo> &stages)
72{
73 mBuild.stages = stages;
74
75 emit signalBuildInfoReceived(mBuild);
76}
77
78}
79