All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.jenkins.results.parser.BaseBuildReport Maven / Gradle / Ivy

There is a newer version: 1.0.1492
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.jenkins.results.parser;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;

/**
 * @author Michael Hashimoto
 */
public abstract class BaseBuildReport implements BuildReport {

	@Override
	public int getBuildNumber() {
		Matcher matcher = _buildURLPattern.matcher(
			String.valueOf(getBuildURL()));

		if (!matcher.find()) {
			throw new RuntimeException("Invalid Build URL: " + getBuildURL());
		}

		return Integer.parseInt(matcher.group("buildNumber"));
	}

	@Override
	public JSONObject getBuildReportJSONObject() {
		if ((buildReportJSONObject != null) &&
			!JenkinsResultsParserUtil.isNullOrEmpty(
				buildReportJSONObject.optString("result"))) {

			return buildReportJSONObject;
		}

		JSONObject buildJSONObject = getBuildJSONObject();

		buildReportJSONObject = new JSONObject();

		buildReportJSONObject.put(
			"duration", buildJSONObject.get("duration")
		).put(
			"result", buildJSONObject.get("result")
		).put(
			"startTime", buildJSONObject.get("timestamp")
		);

		return buildReportJSONObject;
	}

	@Override
	public URL getBuildURL() {
		return _buildURL;
	}

	@Override
	public long getDuration() {
		JSONObject buildReportJSONObject = getBuildReportJSONObject();

		return buildReportJSONObject.getLong("duration");
	}

	@Override
	public String getJobName() {
		Matcher matcher = _buildURLPattern.matcher(
			String.valueOf(getBuildURL()));

		if (!matcher.find()) {
			throw new RuntimeException("Invalid Build URL: " + getBuildURL());
		}

		return matcher.group("jobName");
	}

	@Override
	public JobReport getJobReport() {
		if (_jobReport != null) {
			return _jobReport;
		}

		Matcher matcher = _buildURLPattern.matcher(
			String.valueOf(getBuildURL()));

		if (!matcher.find()) {
			throw new RuntimeException("Invalid Build URL: " + getBuildURL());
		}

		try {
			_jobReport = JobReport.getInstance(
				new URL(matcher.group("jobURL")));
		}
		catch (MalformedURLException malformedURLException) {
			throw new RuntimeException(malformedURLException);
		}

		return _jobReport;
	}

	@Override
	public String getResult() {
		JSONObject buildReportJSONObject = getBuildReportJSONObject();

		return buildReportJSONObject.getString("result");
	}

	@Override
	public Date getStartDate() {
		if (_startDate != null) {
			return _startDate;
		}

		JSONObject buildJSONObject = getBuildJSONObject();

		_startDate = new Date(buildJSONObject.getLong("timestamp"));

		return _startDate;
	}

	@Override
	public StopWatchRecordsGroup getStopWatchRecordsGroup() {
		return new StopWatchRecordsGroup(getBuildReportJSONObject());
	}

	protected BaseBuildReport(JSONObject buildReportJSONObject) {
		this.buildReportJSONObject = buildReportJSONObject;

		String buildURLString = buildReportJSONObject.getString("buildURL");

		Matcher matcher = _buildURLPattern.matcher(buildURLString);

		if (!matcher.find()) {
			throw new RuntimeException("Invalid Build URL: " + buildURLString);
		}

		try {
			_buildURL = new URL(
				JenkinsResultsParserUtil.combine(
					"https://", matcher.group("masterHostname"),
					".liferay.com/job/", matcher.group("jobName"), "/",
					matcher.group("buildNumber")));
		}
		catch (MalformedURLException malformedURLException) {
			throw new RuntimeException(malformedURLException);
		}
	}

	protected BaseBuildReport(JSONObject buildJSONObject, JobReport jobReport) {
		_buildJSONObject = buildJSONObject;
		_jobReport = jobReport;

		String buildURLString = buildJSONObject.getString("url");

		Matcher matcher = _buildURLPattern.matcher(buildURLString);

		if (!matcher.find()) {
			throw new RuntimeException("Invalid Build URL: " + buildURLString);
		}

		try {
			_buildURL = new URL(
				JenkinsResultsParserUtil.combine(
					"https://", matcher.group("masterHostname"),
					".liferay.com/job/", matcher.group("jobName"), "/",
					matcher.group("buildNumber")));
		}
		catch (MalformedURLException malformedURLException) {
			throw new RuntimeException(malformedURLException);
		}
	}

	protected BaseBuildReport(String buildURLString) {
		Matcher matcher = _buildURLPattern.matcher(buildURLString);

		if (!matcher.find()) {
			throw new RuntimeException("Invalid Build URL: " + buildURLString);
		}

		try {
			_buildURL = new URL(
				JenkinsResultsParserUtil.combine(
					"https://", matcher.group("masterHostname"),
					".liferay.com/job/", matcher.group("jobName"), "/",
					matcher.group("buildNumber")));
		}
		catch (MalformedURLException malformedURLException) {
			throw new RuntimeException(malformedURLException);
		}
	}

	protected BaseBuildReport(URL buildURL) {
		this(String.valueOf(buildURL));
	}

	protected JSONObject getBuildJSONObject() {
		if (_buildJSONObject != null) {
			return _buildJSONObject;
		}

		try {
			_buildJSONObject = JenkinsResultsParserUtil.toJSONObject(
				getBuildURL() + "/api/json");
		}
		catch (IOException ioException) {
			throw new RuntimeException(ioException);
		}

		return _buildJSONObject;
	}

	protected void setStartDate(Date startDate) {
		_startDate = startDate;
	}

	protected JSONObject buildReportJSONObject;

	private static final Pattern _buildURLPattern = Pattern.compile(
		"(?https?://(?test-\\d+-\\d+)" +
			"(\\.liferay\\.com)?/job/(?[^/]+))" +
				"(/AXIS_VARIABLE=(?\\d+))?/(?\\d+)");

	private JSONObject _buildJSONObject;
	private final URL _buildURL;
	private JobReport _jobReport;
	private Date _startDate;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy