Skip to content
Permalink
Browse files

[JENKINS-41528] Merge pull request #2 from jenkinsci/git_ref

[JENKINS-41528] Git Ref cloning might help to speed up your cloning time
  • Loading branch information...
v1v committed Feb 8, 2017
2 parents 2f95712 + 3df1462 commit 5eaffc09d9b048d3f03fa997811eaa4425913bfe
@@ -19,7 +19,7 @@ We are always happy for folk to help us out on this project. Here are some ways
## Git Protocol
If you want to make a change to the code on `jenkinsci/jenkinslint-plugin`, here's the protocol we follow (you need a Github account in order to do this):

1. Fork the `v1v/jenkinslint-plugin` repository to your account.
1. Fork the `jenkinsci/jenkinslint-plugin` repository to your account.
2. On your local machine, clone your copy of the `jenkinslint-plugin` repo.
3. Again on your local machine, create a branch, ideally named after a JIRA issue you've created for the work.
4. Switch to the local branch and make your changes. Commit them as you go, and when you're happy, push them to your repo branch.
@@ -35,13 +35,15 @@ TODO
- Jobs action to show those Jenkins lints.
- System.exit in system admin groovy scripts (prebuilder, builders, publishers and parameters)
- Support pipeline
- Exposed API

List available Checks
=====================

* Artifact Publisher check
* CleanUp Workspace check
* Git Shallow clone check
* Git Ref repo check
* Javadoc Publisher check
* Job Assigned Label check
* Master Assigned Label check
@@ -7,6 +7,7 @@
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.check.ArtifactChecker;
import org.jenkins.ci.plugins.jenkinslint.check.CleanupWorkspaceChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GitRefChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GitShallowChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GradleWrapperChecker;
import org.jenkins.ci.plugins.jenkinslint.check.GroovySystemExitChecker;
@@ -71,6 +72,7 @@ public void getData() throws IOException {
checkList.add(new GradleWrapperChecker());
checkList.add(new TimeoutChecker());
checkList.add(new GroovySystemExitChecker());
checkList.add(new GitRefChecker());

slaveCheckList.add(new SlaveDescriptionChecker());
slaveCheckList.add(new SlaveVersionChecker());
@@ -0,0 +1,61 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.Item;
import hudson.model.Project;
import jenkins.model.Jenkins;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.logging.Level;
import hudson.PluginWrapper;

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

public GitRefChecker() {
super();
this.setDescription("When setting Jenkins Jobs with Git SCM you might speed up the cloning time if " +
"you use git ref repos when cloning.");
this.setSeverity("Low");
}

public boolean executeCheck(Item item) {
if (item instanceof Project) {
PluginWrapper plugin = Jenkins.getInstance().pluginManager.getPlugin("git");
if (plugin!=null && !plugin.getVersionNumber().isOlderThan(new hudson.util.VersionNumber("2.0"))) {
if (((Project) item).getScm().getClass().getName().endsWith("GitSCM")) {
boolean status = true;
try {
Method method = ((Project) item).getScm().getClass().getMethod("getExtensions", null);
Object extensionsList = method.invoke( ((Project) item).getScm());
if (extensionsList instanceof AbstractList) {
for (Object extension : ((AbstractList) extensionsList) ) {
if (extension.getClass().getName().endsWith("CloneOption")) {
Object reference = extension.getClass().getMethod("getReference", null).invoke(extension);
if (reference instanceof String) {
status = ((String) reference).isEmpty();
}
}
}
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
status = false;
} finally {
return status;
}
} else {
LOG.log(Level.FINE, "Plugin GIT hasn't been configured in this project");
return false;
}
} else {
LOG.log(Level.FINE, "Plugin GIT doesn't exist");
return false;
}
}
return false;
}
}
@@ -0,0 +1,76 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.FreeStyleProject;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.CleanCheckout;
import hudson.plugins.git.extensions.impl.CloneOption;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.ArrayList;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* GitRefChecker Test Case.
*
* @author Victor Martinez
*/
public class GitRefCheckerTestCase {
private GitRefChecker checker = new GitRefChecker();

@Rule public JenkinsRule j = new JenkinsRule();

@Test public void testEmptyJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testGitSCMJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.setScm(new hudson.plugins.git.GitSCM(""));
assertTrue(checker.executeCheck(project));
}
@Test public void testGitSCMWithFurtherValuesJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", null));
assertTrue(checker.executeCheck(project));
}
@Test public void testGitSCMWithEmptyExtensionJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
ArrayList<GitSCMExtension> extensions = new ArrayList<GitSCMExtension>();
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertTrue(checker.executeCheck(project));
}
@Test public void testGitSCMWithSomeExtensionJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
ArrayList<GitSCMExtension> extensions = new ArrayList<GitSCMExtension>();
extensions.add(new CleanCheckout());
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertTrue(checker.executeCheck(project));
extensions.add(new CloneOption(false, "", 0));
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertTrue(checker.executeCheck(project));
}
@Test public void testGitSCMWithCloneOptionExtensionNoRefJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
ArrayList<GitSCMExtension> extensions = new ArrayList<GitSCMExtension>();
extensions.add(new CloneOption(false, "", 0));
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertTrue(checker.executeCheck(project));
}
@Test public void testGitSCMWithCloneOptionExtensionRefJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
ArrayList<GitSCMExtension> extensions = new ArrayList<GitSCMExtension>();
extensions.add(new CloneOption(false, "${WORKSPACE}/.git-cache", 0));
project.setScm(new hudson.plugins.git.GitSCM(null, null, false, null, null, "", extensions));
assertFalse(checker.executeCheck(project));
}
@Test public void testControlComment() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.isIgnored(project.getDescription()));
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
}

0 comments on commit 5eaffc0

Please sign in to comment.
You can’t perform that action at this time.