package org.jenkins.tools.test.hook;

import org.jenkins.tools.test.model.PluginCompatTesterConfig;
import org.jenkins.tools.test.model.PomData;
import org.jenkins.tools.test.model.hook.PluginCompatTesterHookBeforeCheckout;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * An example hook for dealing with a multiplugin repository. Since the plugin-compat-tester assumes 
 * each plugin is in it's own repository, these plugins automatically fail since they are "not found".
 *
 * This is an example of what needs to change to handle multimodule parents.
 * 
 */
public class ExampleMultiParent { //extends PluginCompatTesterHookBeforeCheckout {
    private String parentUrl = "scm:git:git@github.com:jenkinsci/parent_repo.git";
    private String parentName = "parent_repo";
    private List<String> allBundlePlugins = Arrays.asList("possible", "plugins");
    boolean firstRun = true;

    public ExampleMultiParent() {}

    /**
     * All the plugins that are part of this repository.
     */
    public List<String> transformedPlugins() {
        return allBundlePlugins;
    }

    /*
     * No check implementation is required because transformedPlugins
     * returns your specific list.
     */

    /**
     * Point to the shared location.  Check if this needs to be downloaded. 
     */
    public Map<String, Object> action(Map<String, Object> moreInfo) throws Exception {
        // Change the url to the location of the parent
        PomData pomData = (PomData)moreInfo.get("pomData");
        pomData.setConnectionUrl(parentUrl);

        // Change the download directory
        PluginCompatTesterConfig config = (PluginCompatTesterConfig)moreInfo.get("config");
        File parentPath = new File(config.workDirectory.getAbsolutePath()+"/"+parentName+"/");
        moreInfo.put("checkoutDir", parentPath);
        
        // Determine if you need to run the download
        // Optional; the PluginCompatTester automatically overwrites old files
        // and you shouldn't break that for new runs.  Implement with caution  
        if(!firstRun){
            moreInfo.put("runCheckout", false);
        }
        firstRun = false;

        return moreInfo;
    }
}
@@ -31,16 +31,65 @@
 .hook;%0A%0A
+import org.jenkins.tools.test.SCMManagerFactory;%0A
 import o
@@ -267,16 +267,298 @@
 ckout;%0A%0A
+import org.apache.maven.scm.ScmFileSet;%0Aimport org.apache.maven.scm.ScmTag;%0Aimport org.apache.maven.scm.command.checkout.CheckOutScmResult;%0Aimport org.apache.maven.scm.manager.ScmManager;%0Aimport org.apache.maven.scm.repository.ScmRepository;%0Aimport hudson.model.UpdateSite.Plugin;%0A%0A
 import j
@@ -640,17 +640,16 @@
 il.Map;%0A
-%0A
 /**%0A * A
@@ -1771,90 +1771,130 @@
     
-// Change the url to the location of the parent%0A        PomData pomData = (PomData
+PluginCompatTesterConfig config = (PluginCompatTesterConfig)moreInfo.get(%22config%22);%0A        Plugin currentPlugin = (Plugin
 )mor
@@ -1909,26 +1909,26 @@
 t(%22p
-omData
+lugin
 %22);%0A
+%0A
         
 pomD
@@ -1927,179 +1927,302 @@
     
-pomData.setConnectionUrl(parentUrl);%0A%0A        // Change the download directory%0A        PluginCompatTesterConfig config = (PluginCompatTesterConfig)moreInfo.get(%22config%22);%0A
+// Determine if we need to run the download; only run for first identified plugin in the series%0A        if(firstRun)%7B%0A            System.out.println(%22Preparing for Multimodule checkout.%22);%0A%0A            // Checkout to the parent directory. All other processes will be on the child directory%0A    
     
@@ -2305,20 +2305,16 @@
 rentName
-+%22/%22
 );%0A     
@@ -2320,46 +2320,141 @@
     
-moreInfo.put(%22c
+    %0A            System.out.println(%22C
 heck
-outDir%22, parentPath
+ing out from SCM connection URL : %22+parentUrl+%22 (%22+parentName+%22-%22+currentPlugin.version+%22)%22
 );%0A 
@@ -2464,218 +2464,328 @@
     
-%0A
     
-    // Determine if you need to run the download%0A        // Optional; the PluginCompatTester automatically overwrites old files%0A        // and you shouldn't break that for new runs.  Implement with caution
+ScmManager scmManager = SCMManagerFactory.getInstance().createScmManager();%0A            ScmRepository repository = scmManager.makeScmRepository(parentUrl);%0A            CheckOutScmResult result = scmManager.checkOut(repository, new ScmFileSet(parentPath), new ScmTag(parentName+%22-%22+currentPlugin.version));%0A          
   %0A 
@@ -2795,105 +2795,555 @@
     
+    
 if(!
-firstRun)%7B%0A            moreInfo.put(%22runCheckout%22, false);%0A        %7D%0A        firstRun = false
+result.isSuccess())%7B%0A                throw new RuntimeException(result.getProviderMessage() + %22%7C%7C%22 + result.getCommandOutput());%0A            %7D %0A        %7D%0A%0A        // Checkout already happened, don't run through again%0A        moreInfo.put(%22runCheckout%22, false);%0A        firstRun = false;%0A%0A        // Change the %22download%22%22 directory; after download, it's simply used for reference%0A        File childPath = new File(config.workDirectory.getAbsolutePath()+%22/%22+parentName+%22/%22+currentPlugin.name);%0A        moreInfo.put(%22checkoutDir%22, childPath)
 ;%0A%0A 

Update Multimodule parent example