<commit_before>package org.springframework.cloud.release.internal.sagan;

import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.release.internal.pom.ProjectVersion;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * @author <NAME>
 */
public class SaganUpdater {

	private static final Logger log = LoggerFactory.getLogger(SaganUpdater.class);

	private final SaganClient saganClient;

	public SaganUpdater(SaganClient saganClient) {
		this.saganClient = saganClient;
	}

	public void updateSagan(String branch, ProjectVersion originalVersion, ProjectVersion version) {
		ReleaseUpdate update = releaseUpdate(branch, originalVersion, version);
		updateSaganForGa(branch, originalVersion, version);
		log.info("Updating Sagan with \n\n{}", update);
		this.saganClient.updateRelease(version.projectName, Collections.singletonList(update));
	}

	private void updateSaganForGa(String branch, ProjectVersion originalVersion,
			ProjectVersion version) {
		if (version.isRelease() || version.isServiceRelease()) {
			log.info("Version is GA [{}]. Will remove all older versions and mark this as current", version);
			Project project = this.saganClient.getProject(version.projectName);
			if (project != null) {
				removeAllSameMinorVersions(version, project);
			}
			String snapshot = toSnapshot(version.version);
			removeVersionFromSagan(version, snapshot);
			String bumpedSnapshot = toSnapshot(version.bumpedVersion());
			ReleaseUpdate snapshotUpdate =
					releaseUpdate(branch, originalVersion, new ProjectVersion(version.projectName, bumpedSnapshot));
			log.info("Updating Sagan with \n\n[{}]", snapshotUpdate);
			this.saganClient.updateRelease(version.projectName, Collections.singletonList(snapshotUpdate));
		}
	}

	private void removeAllSameMinorVersions(ProjectVersion version, Project project) {
		project.projectReleases.stream()
				.filter(release -> version.isSameMinor(release.version))
				.collect(Collectors.toList())
				.forEach(release -> removeVersionFromSagan(version, release.version));
	}

	private void removeVersionFromSagan(ProjectVersion version, String snapshot) {
		log.info("Removing [{}/{}] from Sagan", version.projectName, snapshot);
		this.saganClient.deleteRelease(version.projectName, snapshot);
	}

	private ReleaseUpdate releaseUpdate(String branch, ProjectVersion originalVersion,<commit_msg>Won't fail on not being able to remove a sagan version<commit_after>package org.springframework.cloud.release.internal.sagan;

import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.release.internal.pom.ProjectVersion;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * @author <NAME>
 */
public class SaganUpdater {

	private static final Logger log = LoggerFactory.getLogger(SaganUpdater.class);

	private final SaganClient saganClient;

	public SaganUpdater(SaganClient saganClient) {
		this.saganClient = saganClient;
	}

	public void updateSagan(String branch, ProjectVersion originalVersion, ProjectVersion version) {
		ReleaseUpdate update = releaseUpdate(branch, originalVersion, version);
		updateSaganForGa(branch, originalVersion, version);
		log.info("Updating Sagan with \n\n{}", update);
		this.saganClient.updateRelease(version.projectName, Collections.singletonList(update));
	}

	private void updateSaganForGa(String branch, ProjectVersion originalVersion,
			ProjectVersion version) {
		if (version.isRelease() || version.isServiceRelease()) {
			log.info("Version is GA [{}]. Will remove all older versions and mark this as current", version);
			Project project = this.saganClient.getProject(version.projectName);
			if (project != null) {
				removeAllSameMinorVersions(version, project);
			}
			String snapshot = toSnapshot(version.version);
			removeVersionFromSagan(version, snapshot);
			String bumpedSnapshot = toSnapshot(version.bumpedVersion());
			ReleaseUpdate snapshotUpdate =
					releaseUpdate(branch, originalVersion, new ProjectVersion(version.projectName, bumpedSnapshot));
			log.info("Updating Sagan with \n\n[{}]", snapshotUpdate);
			this.saganClient.updateRelease(version.projectName, Collections.singletonList(snapshotUpdate));
		}
	}

	private void removeAllSameMinorVersions(ProjectVersion version, Project project) {
		project.projectReleases.stream()
				.filter(release -> version.isSameMinor(release.version))
				.collect(Collectors.toList())
				.forEach(release -> removeVersionFromSagan(version, release.version));
	}

	private void removeVersionFromSagan(ProjectVersion version, String snapshot) {
		log.info("Removing [{}/{}] from Sagan", version.projectName, snapshot);
		try {
			this.saganClient.deleteRelease(version.projectName, snapshot);
		} catch (Exception e) {
			log.error("Failed to remove [" + version.projectName + "/" + snapshot + "] from Sagan", e);
		}
	}

	private ReleaseUpdate releaseUpdate(String branch, ProjectVersion originalVersion,
{'fraction_non_alphanumeric': 0.10736135780540754, 'fraction_numerical': 0.0007894217485691731, 'mean_word_length': 5.839406207827261, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2684848', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1424, 'n_words': 309}