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

com.liferay.jenkins.results.parser.GitHubRemoteGitRepository 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.google.common.collect.Lists;

import com.liferay.jenkins.results.parser.JenkinsResultsParserUtil.HttpRequestMethod;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

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

/**
 * @author Peter Yoo
 */
public class GitHubRemoteGitRepository extends BaseRemoteGitRepository {

	public boolean addLabel(String color, String description, String name) {
		if (hasLabel(name)) {
			return true;
		}

		JSONObject jsonObject = new JSONObject();

		jsonObject.put(
			"color", color
		).put(
			"name", name
		);

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

		String labelRequestURL = getLabelRequestURL();

		try {
			JenkinsResultsParserUtil.toString(
				labelRequestURL, jsonObject.toString());

			_labelsLists.remove(labelRequestURL);
		}
		catch (IOException ioException) {
			System.out.println("Unable to add label " + name);

			ioException.printStackTrace();

			return false;
		}

		return true;
	}

	public void deleteLabel(Label oldLabel) {
		updateLabel(null, null, null, oldLabel);
	}

	public List getCollaboratorUsernames() {
		if (_collaboratorUsernames != null) {
			return _collaboratorUsernames;
		}

		String url = JenkinsResultsParserUtil.getGitHubApiUrl(
			getName(), getUsername(), "collaborators");

		try {
			JSONArray collaboratorsJSONArray =
				JenkinsResultsParserUtil.toJSONArray(url);

			_collaboratorUsernames = new ArrayList<>(
				collaboratorsJSONArray.length());

			for (int i = 0; i < collaboratorsJSONArray.length(); i++) {
				JSONObject collaboratorUserJSONObject =
					collaboratorsJSONArray.getJSONObject(i);

				_collaboratorUsernames.add(
					collaboratorUserJSONObject.getString("login"));
			}
		}
		catch (IOException ioException) {
			throw new RuntimeException(
				"Unable to get collaborators", ioException);
		}

		return _collaboratorUsernames;
	}

	public String getHtmlURL() {
		return JenkinsResultsParserUtil.combine(
			"https://github.com/", getUsername(), "/", getName());
	}

	public Label getLabel(String name) {
		for (Label label : getLabels()) {
			if (name.equals(label.getName())) {
				return label;
			}
		}

		return null;
	}

	public List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy