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

com.liferay.jenkins.results.parser.GitHubRemoteGitCommit 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 com.atlassian.jira.rest.client.api.domain.Issue;

import java.io.IOException;

import java.text.DateFormat;
import java.text.ParseException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Michael Hashimoto
 */
public class GitHubRemoteGitCommit extends BaseGitCommit {

	public String getGitHubCommitURL() {
		return JenkinsResultsParserUtil.combine(
			"https://github.com/", _gitHubUsername, "/", getGitRepositoryName(),
			"/commit/", getSHA());
	}

	public Issue getJIRAIssue() {
		Matcher matcher = _messagePattern.matcher(getMessage());

		if (matcher.find()) {
			String issueKey = matcher.group("jiraIssueKey");

			return JIRAUtil.getIssue(issueKey);
		}

		return null;
	}

	public List getModifiedFilenames() {
		if (modifiedFilenames == null) {
			init();
		}

		return modifiedFilenames;
	}

	public List getStatusDescriptions() {
		try {
			JSONObject jsonObject = JenkinsResultsParserUtil.toJSONObject(
				getGitHubStatusURL());

			JSONArray statusesJSONArray = jsonObject.getJSONArray("statuses");

			List statusDescriptions = new ArrayList<>(
				statusesJSONArray.length());

			for (int i = 0; i < statusesJSONArray.length(); i++) {
				JSONObject statusJSONObject = statusesJSONArray.getJSONObject(
					i);

				statusDescriptions.add(
					statusJSONObject.optString("description"));
			}

			return statusDescriptions;
		}
		catch (IOException ioException) {
			throw new RuntimeException(
				"Unable to get status data", ioException);
		}
	}

	public void setStatus(
		Status status, String context, String description, String targetURL) {

		JSONObject jsonObject = new JSONObject();

		jsonObject.put("state", StringUtils.lowerCase(status.toString()));

		if (context != null) {
			jsonObject.put("context", context);
		}

		if (description != null) {
			jsonObject.put("description", description);
		}

		if ((targetURL != null) && targetURL.matches("https?\\:\\/\\/.*")) {
			jsonObject.put("target_url", targetURL);
		}

		try {
			JenkinsResultsParserUtil.toJSONObject(
				getGitHubStatusURL(), jsonObject.toString());
		}
		catch (IOException ioException) {
			throw new RuntimeException(ioException);
		}
	}

	public enum Status {

		ERROR, FAILURE, PENDING, SUCCESS

	}

	protected GitHubRemoteGitCommit(
		String emailAddress, String gitHubUsername, String gitRepositoryName,
		String message, List modifiedFilenames, String sha, Type type,
		long commitTime) {

		super(emailAddress, gitRepositoryName, message, sha, type, commitTime);

		_gitHubUsername = gitHubUsername;
		this.modifiedFilenames = modifiedFilenames;
	}

	protected GitHubRemoteGitCommit(
		String gitHubUsername, String gitRepositoryName, String sha,
		Type type) {

		super(gitRepositoryName, sha, type);

		_gitHubUsername = gitHubUsername;
	}

	protected String getGitHubStatusURL() {
		return JenkinsResultsParserUtil.getGitHubApiUrl(
			getGitRepositoryName(), _gitHubUsername, "statuses/" + getSHA());
	}

	protected void init() {
		try {
			JSONObject jsonObject = JenkinsResultsParserUtil.toJSONObject(
				getGitHubCommitURL(), false);

			JSONObject commitJSONObject = jsonObject.getJSONObject("commit");

			message = commitJSONObject.getString("message");

			JSONObject committerJSONObject = commitJSONObject.getJSONObject(
				"committer");

			modifiedFilenames = Collections.emptyList();

			JSONArray filesJSONArray = jsonObject.getJSONArray("files");

			if (filesJSONArray != null) {
				modifiedFilenames = new ArrayList<>(filesJSONArray.length());

				for (int i = 0; i < filesJSONArray.length(); i++) {
					JSONObject fileJSONObject = filesJSONArray.getJSONObject(i);

					modifiedFilenames.add(fileJSONObject.getString("filename"));
				}
			}

			try {
				DateFormat gitHubDateFormat =
					JenkinsResultsParserUtil.getGitHubDateFormat();

				Date commitDate = gitHubDateFormat.parse(
					committerJSONObject.getString("date"));

				commitTime = commitDate.getTime();
			}
			catch (ParseException parseException) {
				throw new RuntimeException(
					"Unable to parse committer date " +
						committerJSONObject.getString("date"),
					parseException);
			}
		}
		catch (IOException ioException) {
			throw new RuntimeException(
				"Unable to get GitHub API JSON for commit " + getSHA(),
				ioException);
		}
	}

	@Override
	protected void initCommitTime() {
		init();
	}

	@Override
	protected void initEmailAddress() {
		init();
	}

	@Override
	protected void initMessage() {
		init();
	}

	protected List modifiedFilenames;

	private static final Pattern _messagePattern = Pattern.compile(
		"^(?[A-Z]+[-][\\d]+)\\s*(?.*)");

	private final String _gitHubUsername;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy