hudson.plugins.git.GitTool Java Examples

The following examples show how to use hudson.plugins.git.GitTool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TopReadmeTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
@ConfiguredWithReadme("README.md#0")
public void configure_demo_first_code_block() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    assertEquals("Jenkins configured automatically by Jenkins Configuration as Code plugin\n\n", jenkins.getSystemMessage());
    final LDAPSecurityRealm securityRealm = (LDAPSecurityRealm) jenkins.getSecurityRealm();
    assertEquals(1, securityRealm.getConfigurations().size());
    assertEquals(50000, jenkins.getSlaveAgentPort());

    assertEquals(1, jenkins.getNodes().size());
    assertEquals("static-agent", jenkins.getNode("static-agent").getNodeName());

    final GitTool.DescriptorImpl gitTool = (GitTool.DescriptorImpl) jenkins.getDescriptor(GitTool.class);
    assertEquals(1, gitTool.getInstallations().length);

    List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(
        BasicSSHUserPrivateKey.class, jenkins, ACL.SYSTEM, Collections.emptyList()
    );
    assertThat(sshPrivateKeys, hasSize(1));

    final BasicSSHUserPrivateKey ssh_with_passphrase = sshPrivateKeys.get(0);
    assertThat(ssh_with_passphrase.getPassphrase().getPlainText(), equalTo("ABCD"));

    final DirectEntryPrivateKeySource source = (DirectEntryPrivateKeySource) ssh_with_passphrase.getPrivateKeySource();
    assertThat(source.getPrivateKey().getPlainText(), equalTo("s3cr3t"));
}
 
Example #2
Source File: GitToolConfiguratorJenkinsRuleTest.java    From git-client-plugin with MIT License 6 votes vote down vote up
@Test
public void testDescribeGitToolEmptyProperties() throws Exception {
    String gitName = "git-2.19.1-name";
    String gitHome = "/opt/git-2.19.1/bin/git";

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);

    // Intentionally empty properties passed to GitTool constructor
    List<ToolProperty<ToolInstallation>> gitToolProperties = new ArrayList<>();

    GitTool gitTool = new GitTool(gitName, gitHome, gitToolProperties);

    CNode cNode = gitToolConfigurator.describe(gitTool, context);

    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));
}
 
Example #3
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 6 votes vote down vote up
@CheckForNull
@Override
public CNode describe(GitTool instance, ConfigurationContext context) throws Exception {
    Mapping mapping = new Mapping();
    if (instance instanceof JGitTool) {
        mapping.put("name", JGitTool.MAGIC_EXENAME);
    } else if (instance instanceof JGitApacheTool) {
        mapping.put("name", JGitApacheTool.MAGIC_EXENAME);
    } else if (instance != null) {
        mapping.put("name", instance.getName());
        mapping.put("home", instance.getHome());
    }
    if (context != null && instance != null && instance.getProperties() != null && !instance.getProperties().isEmpty()) {
        final Configurator<ToolProperty> configurator = context.lookupOrFail(ToolProperty.class);
        Sequence s = new Sequence(instance.getProperties().size());
        for (ToolProperty<?> property : instance.getProperties()) {
            s.add(configurator.describe(property, context));
        }
        mapping.put("properties", s);
    }
    return mapping;
}
 
Example #4
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 6 votes vote down vote up
@Override
protected GitTool instance(Mapping mapping, @NonNull ConfigurationContext context) throws ConfiguratorException {
    if (mapping == null) {
        return new GitTool("Default", "", instantiateProperties(null, context));
    }
    final CNode mproperties = mapping.remove("properties");
    final String name = mapping.getScalarValue("name");
    if (JGitTool.MAGIC_EXENAME.equals(name)) {
        if (mapping.remove("home") != null) { //Ignored but could be added, so removing to not fail handleUnknown
            logger.warning("property `home` is ignored for `" + JGitTool.MAGIC_EXENAME + "`");
        }
        return new JGitTool(instantiateProperties(mproperties, context));
    } else if (JGitApacheTool.MAGIC_EXENAME.equals(name)) {
        if (mapping.remove("home") != null) { //Ignored but could be added, so removing to not fail handleUnknown
            logger.warning("property `home` is ignored for `" + JGitApacheTool.MAGIC_EXENAME + "`");
        }
        return new JGitApacheTool(instantiateProperties(mproperties, context));
    } else {
        if (mapping.get("home") == null) {
            throw new ConfiguratorException(this, "Home required for cli git configuration.");
        }
        String home = mapping.getScalarValue("home");
        return new GitTool(name, home, instantiateProperties(mproperties, context));
    }
}
 
Example #5
Source File: GitCloneReadSaveRequest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
public GitCloneReadSaveRequest(AbstractGitSCMSource gitSource, String branch, String commitMessage, String sourceBranch, String filePath, byte[] contents) {
    super(gitSource, branch, commitMessage, sourceBranch, filePath, contents);

    GitTool.DescriptorImpl toolDesc = Jenkins.getInstance().getDescriptorByType(GitTool.DescriptorImpl.class);
    @SuppressWarnings("deprecation")
    GitTool foundGitTool = null;
    for (SCMSourceTrait trait : gitSource.getTraits()) {
        if (trait instanceof GitToolSCMSourceTrait) {
            foundGitTool = toolDesc.getInstallation(((GitToolSCMSourceTrait) trait).getGitTool());
        }
    }
    if (foundGitTool == null) {
        foundGitTool = GitTool.getDefaultInstallation();
    }

    this.gitTool = foundGitTool;
    try {
        repositoryPath = Files.createTempDirectory("git").toFile();
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Unable to create working directory for repository clone");
    }
}
 
Example #6
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitApacheTool() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitApacheTool.MAGIC_EXENAME);
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitApacheTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitTool.class))));
}
 
Example #7
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeJGitApacheTool() throws Exception {
    GitTool gitTool = new JGitApacheTool();
    CNode cNode = gitToolConfigurator.describe(gitTool, NULL_CONFIGURATION_CONTEXT);
    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(JGitApacheTool.MAGIC_EXENAME));
}
 
Example #8
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeJGitTool() throws Exception {
    GitTool gitTool = new JGitTool();
    CNode cNode = gitToolConfigurator.describe(gitTool, NULL_CONFIGURATION_CONTEXT);
    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(JGitTool.MAGIC_EXENAME));
}
 
Example #9
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeGitToolWithoutProperties() throws Exception {
    String gitName = "git-name";
    String gitHome = "/opt/git-2.23.0/bin/git";
    GitTool gitTool = new GitTool(gitName, gitHome, null);
    CNode cNode = gitToolConfigurator.describe(gitTool, NULL_CONFIGURATION_CONTEXT);
    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));
}
 
Example #10
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstance() throws Exception {
    Mapping mapping = null;
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(GitTool.class)));
    assertThat(gitTool.getName(), is("Default"));
    assertThat(gitTool.getHome(), is(""));
}
 
Example #11
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitTool() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitTool.MAGIC_EXENAME);
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitApacheTool.class))));
}
 
Example #12
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitToolWithHome() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitTool.MAGIC_EXENAME);
    mapping.put("home", "unused-value-for-home"); // Will log a message
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitApacheTool.class))));
}
 
Example #13
Source File: GitToolConfiguratorJenkinsRuleTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeGitTool() throws Exception {
    String gitName = "git-2.19.1-name";
    String gitHome = "/opt/git-2.19.1/bin/git";

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);

    List<ToolProperty<ToolInstallation>> gitToolProperties = new ArrayList<>();
    List<ToolInstaller> toolInstallers = new ArrayList<>();
    ToolInstaller toolInstaller = new ZipExtractionInstaller("tool-label", "tool-url", "tool-subdir");
    toolInstallers.add(toolInstaller);
    InstallSourceProperty installSourceProperty = new InstallSourceProperty(toolInstallers);
    gitToolProperties.add(installSourceProperty);

    GitTool gitTool = new GitTool(gitName, gitHome, gitToolProperties);

    CNode cNode = gitToolConfigurator.describe(gitTool, context);

    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));

    //         properties:
    //          - installSource:
    //              installers:
    //                - zip:
    //                    label: "tool-label"
    //                    subdir: "tool-subdir"
    //                    url: "tool-url"
    Sequence propertiesSequence = cNodeMapping.get("properties").asSequence();                             // properties:
    Mapping installSourceMapping = propertiesSequence.get(0).asMapping().get("installSource").asMapping(); //  - installSource:
    Sequence installersSequence = installSourceMapping.get("installers").asSequence();                     //      installers:
    Mapping zipMapping = installersSequence.get(0).asMapping().get("zip").asMapping();                     //        - zip:
    assertThat(zipMapping.getScalarValue("label"), is("tool-label"));
    assertThat(zipMapping.getScalarValue("subdir"), is("tool-subdir"));
    assertThat(zipMapping.getScalarValue("url"), is("tool-url"));
}
 
Example #14
Source File: KubernetesTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Test
@LocalData()
public void upgradeFrom_0_10() throws Exception {
    List<PodTemplate> templates = cloud.getTemplates();
    PodTemplate template = templates.get(0);
    DescribableList<NodeProperty<?>,NodePropertyDescriptor> nodeProperties = template.getNodeProperties();
    assertEquals(1, nodeProperties.size());
    ToolLocationNodeProperty property = (ToolLocationNodeProperty) nodeProperties.get(0);
    assertEquals(1, property.getLocations().size());
    ToolLocation location = property.getLocations().get(0);
    assertEquals("Default", location.getName());
    assertEquals("/custom/path", location.getHome());
    assertEquals(GitTool.class, location.getType().clazz);
    assertEquals(cloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
}
 
Example #15
Source File: GitToolInstallationTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
public void configure_git_installations() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    final GitTool.DescriptorImpl descriptor = (GitTool.DescriptorImpl) jenkins.getDescriptor(GitTool.class);
    assertEquals(2, descriptor.getInstallations().length);
    assertEquals("/usr/local/bin/git", descriptor.getInstallation("another_git").getGitExe());
    assertEquals("/bin/git", descriptor.getInstallation("git").getGitExe());
}
 
Example #16
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 5 votes vote down vote up
@NonNull
@Override
public List<Attribute<GitTool, ?>> getAttributes() {
    Attribute<GitTool, String> name = new Attribute<>("name", String.class);
    Attribute<GitTool, String> home = new Attribute<>("home", String.class);
    Attribute<GitTool, ToolProperty> p = new Attribute<>("properties", ToolProperty.class);
    p.multiple(true);
    return Arrays.asList(name, home, p);
}
 
Example #17
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitApacheToolWithHome() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitApacheTool.MAGIC_EXENAME);
    mapping.put("home", "unused-value-for-home"); // Will log a message
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitApacheTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitTool.class))));
}
 
Example #18
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceGitTool() throws Exception {
    Mapping mapping = new Mapping();
    String gitHome = "testGitHome";
    String gitName = "testGitName";
    mapping.put("home", gitHome);
    mapping.put("name", gitName);
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(GitTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitTool.class))));
    assertThat(gitTool, is(not(instanceOf(JGitApacheTool.class))));
    assertThat(gitTool.getHome(), is(gitHome));
    assertThat(gitTool.getName(), is(gitName));
}
 
Example #19
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetAttributes() {
    List<Attribute<GitTool, ?>> gitToolAttributes = gitToolConfigurator.getAttributes();
    Attribute<GitTool, String> name = new Attribute<>("name", String.class);
    Attribute<GitTool, String> home = new Attribute<>("home", String.class);
    Attribute<GitTool, ToolProperty> p = new Attribute<>("properties", ToolProperty.class);
    assertThat(gitToolAttributes, containsInAnyOrder(name, home, p));
}
 
Example #20
Source File: JcascTest.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenkinsRule, String s) {
    final ToolDescriptor descriptor = (ToolDescriptor) restartableJenkinsRule.j.jenkins.getDescriptor(GitTool.class);
    final ToolInstallation[] installations = descriptor.getInstallations();
    assertThat(installations, arrayWithSize(4));
    assertThat(installations, arrayContainingInAnyOrder(
            allOf(
                    instanceOf(JGitTool.class),
                    hasProperty("name", equalTo(JGitTool.MAGIC_EXENAME))
            ),
            allOf(
                    instanceOf(JGitApacheTool.class),
                    hasProperty("name", equalTo(JGitApacheTool.MAGIC_EXENAME))
            ),
            allOf(
                    instanceOf(GitTool.class),
                    not(instanceOf(JGitTool.class)),
                    not(instanceOf(JGitApacheTool.class)),
                    hasProperty("name", equalTo("Default")),
                    hasProperty("home", equalTo("git"))
            ),
            allOf(
                    instanceOf(GitTool.class),
                    not(instanceOf(JGitTool.class)),
                    not(instanceOf(JGitApacheTool.class)),
                    hasProperty("name", equalTo("optional")),
                    hasProperty("home", equalTo("/opt/git/git"))
            ))
    );
    final DescribableList<ToolProperty<?>, ToolPropertyDescriptor> properties = Arrays.stream(installations).filter(t -> t.getName().equals("optional")).findFirst().get().getProperties();
    assertThat(properties, iterableWithSize(1));
    final ToolProperty<?> property = properties.get(0);
    assertThat(property, instanceOf(InstallSourceProperty.class));
    assertThat(((InstallSourceProperty)property).installers,
            containsInAnyOrder(
                    allOf(
                            instanceOf(BatchCommandInstaller.class),
                            hasProperty("command", equalTo("echo \"got git\""))
                    ),
                    allOf(
                            instanceOf(ZipExtractionInstaller.class),
                            hasProperty("url", equalTo("file://some/path.zip"))
                    )
            )
    );
}
 
Example #21
Source File: JenkinsDemoTest.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
@Test
@ConfiguredWithCode("jenkins/jenkins.yaml")
public void configure_demo_yaml() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    assertEquals("Jenkins configured automatically by Jenkins Configuration as Code plugin\n\n", jenkins.getSystemMessage());
    assertEquals(5, jenkins.getNumExecutors());
    assertEquals(2, jenkins.getScmCheckoutRetryCount());
    assertEquals(Mode.NORMAL, jenkins.getMode());
    assertEquals("https://ci.example.com/", jenkins.getRootUrl());

    final FullControlOnceLoggedInAuthorizationStrategy strategy = (FullControlOnceLoggedInAuthorizationStrategy) jenkins.getAuthorizationStrategy();
    assertFalse(strategy.isAllowAnonymousRead());

    final DockerCloud docker = DockerCloud.getCloudByName("docker");
    assertNotNull(docker);
    assertNotNull(docker.getDockerApi());
    assertNotNull(docker.getDockerApi().getDockerHost());
    assertEquals("unix:///var/run/docker.sock", docker.getDockerApi().getDockerHost().getUri());

    final GitTool.DescriptorImpl gitTool = (GitTool.DescriptorImpl) jenkins.getDescriptor(GitTool.class);
    assertEquals(1, gitTool.getInstallations().length);

    assertEquals(1, GlobalLibraries.get().getLibraries().size());
    final LibraryConfiguration library = GlobalLibraries.get().getLibraries().get(0);
    assertEquals("awesome-lib", library.getName());

    final Mailer.DescriptorImpl descriptor = (Mailer.DescriptorImpl) jenkins.getDescriptor(Mailer.class);
    assertEquals("4441", descriptor.getSmtpPort());
    assertEquals("[email protected]", descriptor.getReplyToAddress());
    assertEquals("smtp.acme.org", descriptor.getSmtpHost() );

    final ArtifactoryBuilder.DescriptorImpl artifactory = (ArtifactoryBuilder.DescriptorImpl) jenkins.getDescriptor(ArtifactoryBuilder.class);
    assertTrue(artifactory.getUseCredentialsPlugin());

    final List<ArtifactoryServer> actifactoryServers = artifactory.getArtifactoryServers();
    assertThat(actifactoryServers, hasSize(1));
    assertThat(actifactoryServers.get(0).getName(), is(equalTo("artifactory")));
    assertThat(actifactoryServers.get(0).getUrl(), is(equalTo("http://acme.com/artifactory")));
    assertThat(actifactoryServers.get(0).getResolverCredentialsConfig().getUsername(), is(equalTo("artifactory_user")));
    assertThat(actifactoryServers.get(0).getResolverCredentialsConfig().getPassword(), is(equalTo("password123")));
}
 
Example #22
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Test
public void testDescribe() throws Exception {
    GitTool nullGitTool = null;
    assertThat(gitToolConfigurator.describe(nullGitTool, NULL_CONFIGURATION_CONTEXT), is(new Mapping()));
}
 
Example #23
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Test
public void testGetImplementedAPI() {
    assertEquals("Wrong implemented API", gitToolConfigurator.getImplementedAPI(), GitTool.class);
}
 
Example #24
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Test
public void testCanConfigure() {
    assertTrue("Can't configure GitTool", gitToolConfigurator.canConfigure(GitTool.class));
    assertFalse("Can configure GitToolConfigurator", gitToolConfigurator.canConfigure(GitToolConfigurator.class));
}
 
Example #25
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Test
public void testGetTarget() {
    assertEquals("Wrong target class", gitToolConfigurator.getTarget(), GitTool.class);
}
 
Example #26
Source File: JGitTool.java    From git-client-plugin with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public GitTool.DescriptorImpl getDescriptor() {
    return super.getDescriptor();
}
 
Example #27
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 4 votes vote down vote up
@NonNull
@Override
public List<Configurator<GitTool>> getConfigurators(ConfigurationContext context) {
    return Collections.singletonList(this);
}
 
Example #28
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Override
public boolean canConfigure(Class clazz) {
    return clazz == GitTool.class;
}
 
Example #29
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Override
public Class getTarget() {
    return GitTool.class;
}
 
Example #30
Source File: JGitApacheTool.java    From git-client-plugin with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public GitTool.DescriptorImpl getDescriptor() {
    return super.getDescriptor();
}