package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.matrix.MatrixProject;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.Builder;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.util.List;
import java.util.logging.Level;

/**
 * @author Victor Martinez
 */
public class GradleWrapperChecker extends AbstractCheck {

    public GradleWrapperChecker() {
        super();
        this.setDescription("By distributing the wrapper with your project, anyone can work with it without needing to " +
                            "install Gradle beforehand.<br/> Even better, users of the build are guaranteed to use the " +
                            "version of Gradle that the build was designed to work with.<br/>" +
                            "<a href=https://docs.gradle.org/current/userguide/gradle_wrapper.html>The Gradle Wrapper</a>");
        this.setSeverity("Medium");
    }

    public boolean executeCheck(Item item) {
        boolean found = false;
        //if (Jenkins.getInstance().pluginManager.getPlugin("gradle") != null) {
        if (item.getClass().getName().endsWith("hudson.maven.MavenModuleSet")) {
            found = false;
        } else {
            if (item instanceof Project) {
                found = isGradlew(((Project) item).getBuilders());
            }
            if (item instanceof MatrixProject) {
                found = isGradlew(((MatrixProject) item).getBuilders());
            }
        }
        //}
        return found;
    }

    private boolean isGradlew (List<Builder> builders) {
        boolean status = false;
        if (builders != null && builders.size() > 0 ) {
            for (Builder builder : builders) {
                if (builder.getClass().getName().endsWith("Gradle")) {
                    try {
                        Object isUseWrapper = builder.getClass().getMethod("isUseWrapper", null).invoke(builder);
                        if (isUseWrapper instanceof Boolean) {
                            status = ! ((Boolean) isUseWrapper).booleanValue();
                            LOG.log(Level.FINE, "isGradlew " + !status);
                        }
                    } catch (Exception e) {
                        LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
                        status = false;
                    }
                }
            }
        }
        return status;
    }
}

@@ -164,16 +164,46 @@
 uilder;%0A
+import jenkins.model.Jenkins;%0A
 import o
@@ -639,21 +639,16 @@
 orehand.
-%3Cbr/%3E
  Even be
@@ -671,16 +671,22 @@
 he build
+ %3Cbr/%3E
  are gua
@@ -796,21 +796,34 @@
 rk with.
-%3Cbr/%3E
+ Further details: 
 %22 +%0A    
@@ -921,11 +921,8 @@
 tml%3E
-The
  Gra
@@ -936,12 +936,18 @@
 pper
+ docs
 %3C/a%3E
+.
 %22);%0A
@@ -1073,18 +1073,16 @@
         
-//
 if (Jenk
@@ -1144,32 +1144,36 @@
 null) %7B%0A        
+    
 if (item.getClas
@@ -1233,32 +1233,36 @@
 ) %7B%0A            
+    
 found = false;%0A 
@@ -1268,16 +1268,20 @@
         
+   
+ 
 %7D else %7B
@@ -1281,16 +1281,20 @@
  else %7B%0A
+    
         
@@ -1320,32 +1320,36 @@
 ceof Project) %7B%0A
+    
                 
@@ -1403,34 +1403,42 @@
 ));%0A            
+ 
-%7D%0A
+   %7D%0A    
             if (
@@ -1462,32 +1462,36 @@
 atrixProject) %7B%0A
+    
                 
@@ -1551,34 +1551,42 @@
 ));%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -1587,18 +1587,16 @@
         
-//
 %7D%0A      

Change description and add plugin validation [JENKINS-29486]