package org.jfrog.hudson.util;

import hudson.model.AbstractBuild;
import hudson.tasks.LogRotator;
import org.jfrog.build.api.BuildRetention;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Tomer Cohen
 */
public class BuildRetentionFactory {

    /**
     * Create a Build retention object out of the build
     *
     * @param build The build to create the build retention out of
     * @return a new Build retention
     */
    public static BuildRetention createBuildRetention(AbstractBuild build) {
        BuildRetention buildRetention = new BuildRetention(true);
        LogRotator rotator = build.getProject().getLogRotator();
        if (rotator == null) {
            return buildRetention;
        }
        if (rotator.getNumToKeep() > -1) {
            buildRetention.setCount(rotator.getNumToKeep());
        }
        if (rotator.getDaysToKeep() > -1) {
            Calendar calendar = Calendar.getInstance();
            calendar.roll(Calendar.DAY_OF_MONTH, -rotator.getDaysToKeep());
            buildRetention.setMinimumBuildDate(new Date(calendar.getTimeInMillis()));
        }
        List<String> notToBeDeleted = ExtractorUtils.getBuildNumbersNotToBeDeleted(build);
        for (String notToDelete : notToBeDeleted) {
            buildRetention.addBuildNotToBeDiscarded(notToDelete);
        }
        return buildRetention;
    }
}

@@ -1227,64 +1227,8 @@
 d);%0A
-        for (String notToDelete : notToBeDeleted) %7B%0A    
     
@@ -1250,17 +1250,24 @@
 ion.
-add
+set
 BuildN
+umbersN
 otTo
@@ -1287,27 +1287,20 @@
 otTo
+Be
 Delete
+d
 );%0A
-        %7D%0A
     

Support saving pinned builds