| 1 | #include "JobDetailsFetcher.h" |
|---|---|
| 2 | |
| 3 | #include <QJsonArray> |
| 4 | #include <QJsonDocument> |
| 5 | #include <QJsonObject> |
| 6 | |
| 7 | namespace Jenkins |
| 8 | { |
| 9 | |
| 10 | JobDetailsFetcher::JobDetailsFetcher(const IFetcher::Config &config, const JenkinsJobInfo &info, QObject *parent) |
| 11 | : IFetcher(config, parent) |
| 12 | , mInfo(info) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | void JobDetailsFetcher::triggerFetch() |
| 17 | { |
| 18 | get(mInfo.url + QString::fromUtf8("api/json"), true); |
| 19 | } |
| 20 | |
| 21 | void JobDetailsFetcher::processData(const QJsonDocument &json) |
| 22 | { |
| 23 | auto jsonObject = json.object(); |
| 24 | |
| 25 | readHealthReportsPartFor(jsonObject); |
| 26 | readBuildsListFor(jsonObject); |
| 27 | retrieveBuildConfig(jsonObject[QStringLiteral("property")].toArray()); |
| 28 | readBuildableFlagFor(jsonObject); |
| 29 | readIsQueuedFlagFor(jsonObject); |
| 30 | |
| 31 | emit signalJobDetailsRecieved(mInfo); |
| 32 | } |
| 33 | |
| 34 | void JobDetailsFetcher::readHealthReportsPartFor(QJsonObject &jsonObject) |
| 35 | { |
| 36 | if (jsonObject.contains(QStringLiteral("healthReport"))) |
| 37 | { |
| 38 | const auto healthArray = jsonObject[QStringLiteral("healthReport")].toArray(); |
| 39 | for (const auto &item : healthArray) |
| 40 | { |
| 41 | JenkinsJobInfo::HealthStatus status; |
| 42 | QJsonObject healthObject = item.toObject(); |
| 43 | if (healthObject.contains(QStringLiteral("score"))) |
| 44 | status.score = healthObject[QStringLiteral("score")].toInt(); |
| 45 | if (healthObject.contains(QStringLiteral("description"))) |
| 46 | status.description = healthObject[QStringLiteral("description")].toString(); |
| 47 | if (healthObject.contains(QStringLiteral("iconClassName"))) |
| 48 | status.iconClassName = healthObject[QStringLiteral("iconClassName")].toString(); |
| 49 | |
| 50 | mInfo.healthStatus = status; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void JobDetailsFetcher::readBuildsListFor(QJsonObject &jsonObject) |
| 56 | { |
| 57 | if (jsonObject.contains(QStringLiteral("builds"))) |
| 58 | { |
| 59 | const auto buildsArray = jsonObject[QStringLiteral("builds")].toArray(); |
| 60 | |
| 61 | for (const auto &build : buildsArray) |
| 62 | { |
| 63 | const auto buildObject = build.toObject(); |
| 64 | if (buildObject.contains(QStringLiteral("number")) && buildObject.contains(QStringLiteral( "url"))) |
| 65 | { |
| 66 | JenkinsJobBuildInfo build; |
| 67 | build.number = buildObject[QStringLiteral("number")].toInt(); |
| 68 | build.url = buildObject[QStringLiteral("url")].toString(); |
| 69 | |
| 70 | mInfo.builds.append(build); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void JobDetailsFetcher::retrieveBuildConfig(const QJsonArray &propertyArray) |
| 77 | { |
| 78 | QVector<JenkinsJobBuildConfig> configParams; |
| 79 | |
| 80 | for (const auto &property : propertyArray) |
| 81 | { |
| 82 | auto propertyObj = property.toObject(); |
| 83 | |
| 84 | if (propertyObj[QStringLiteral("_class")].toString().contains( "ParametersDefinitionProperty")) |
| 85 | { |
| 86 | const auto params = propertyObj[QStringLiteral("parameterDefinitions")].toArray(); |
| 87 | |
| 88 | for (const auto &config : params) |
| 89 | { |
| 90 | JenkinsJobBuildConfig jobConfig; |
| 91 | jobConfig.name = config[QStringLiteral("name")].toString(); |
| 92 | |
| 93 | if (config[QStringLiteral("type")].toString() == "BooleanParameterDefinition") |
| 94 | jobConfig.fieldType = JobConfigFieldType::Bool; |
| 95 | else if (config[QStringLiteral("type")].toString() == "StringParameterDefinition") |
| 96 | jobConfig.fieldType = JobConfigFieldType::String; |
| 97 | else if (config[QStringLiteral("type")].toString() == "ChoiceParameterDefinition") |
| 98 | { |
| 99 | jobConfig.fieldType = JobConfigFieldType::Choice; |
| 100 | |
| 101 | const auto choices = config[QStringLiteral("choices")].toArray(); |
| 102 | |
| 103 | for (const auto &choiceValue : choices) |
| 104 | jobConfig.choicesValues.append(choiceValue.toString()); |
| 105 | } |
| 106 | |
| 107 | jobConfig.defaultValue |
| 108 | = config[QStringLiteral("defaultParameterValue")].toObject()[QStringLiteral( "value")].toVariant(); |
| 109 | |
| 110 | configParams.append(jobConfig); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | mInfo.configFields = configParams; |
| 116 | } |
| 117 | |
| 118 | void JobDetailsFetcher::readBuildableFlagFor(QJsonObject &) |
| 119 | { |
| 120 | /* |
| 121 | if (jsonObject.contains(QStringLiteral("buildable"))) |
| 122 | job.setIsBuildable(jsonObject[QStringLiteral("buildable")].toBool()); |
| 123 | else |
| 124 | job.setIsBuildable(true); |
| 125 | */ |
| 126 | } |
| 127 | |
| 128 | void JobDetailsFetcher::readIsQueuedFlagFor(QJsonObject &) |
| 129 | { |
| 130 | /* |
| 131 | if (jsonObject.contains(QStringLiteral("inQueue"))) |
| 132 | job.setIsQueued(jsonObject[QStringLiteral("inQueue")].toBool()); |
| 133 | else |
| 134 | job.setIsQueued(false); |
| 135 | */ |
| 136 | } |
| 137 | |
| 138 | } |
| 139 |