org.marvelution.jira.plugins.jenkins.parsers.BasicBuildParser Maven / Gradle / Ivy
/*
* Copyright (c) 2012-present Marvelution B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.marvelution.jira.plugins.jenkins.parsers;
import javax.annotation.Nullable;
import org.marvelution.jira.plugins.jenkins.model.Build;
import org.marvelution.jira.plugins.jenkins.model.Job;
import org.marvelution.jira.plugins.jenkins.model.Result;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import static org.marvelution.jira.plugins.jenkins.parsers.ParserUtils.optLong;
import static org.marvelution.jira.plugins.jenkins.parsers.ParserUtils.optString;
import static java.util.Objects.requireNonNull;
/**
* {@link MergingParser} for parsing the basic {@link Build} details
*
* @author Mark Rekveld
* @since 2.3.0
*/
public class BasicBuildParser implements Parser, MergingParser {
private final Job job;
public BasicBuildParser(Job job) {
this.job = requireNonNull(job);
}
@Nullable
@Override
public Build parse(JsonElement json) {
if (json.isJsonObject()) {
JsonObject object = json.getAsJsonObject();
Build build = new Build(job.getId(), object.getAsJsonPrimitive("number").getAsInt());
parse(json, build);
return build;
}
return null;
}
@Override
public void parse(JsonElement json, Build build) {
if (json.isJsonObject()) {
JsonObject object = json.getAsJsonObject();
String displayName = optString(object, "fullDisplayName");
if (displayName != null && !(displayName.startsWith(job.getDisplayName()) && displayName.endsWith("#" + build.getNumber()))) {
// Only set the full display name if it doesn't start with the job display name and end with #[number]
// which is the default format
build.setDisplayName(displayName);
}
build.setDescription(optString(object, "description"));
build.setBuiltOn(optString(object, "builtOn"));
build.setResult(Result.fromString(optString(object, "result")));
build.setTimestamp(optLong(object, "timestamp"));
build.setDuration(optLong(object, "duration"));
}
}
}