Java Code Examples for jenkins.model.Jenkins#getDescriptorByType()

The following examples show how to use jenkins.model.Jenkins#getDescriptorByType() . 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: GitTool.java    From git-client-plugin with MIT License 6 votes vote down vote up
/**
 * Returns the default installation.
 *
 * @return default installation
 */
public static GitTool getDefaultInstallation() {
    Jenkins jenkinsInstance = Jenkins.getInstance();
    DescriptorImpl gitTools = jenkinsInstance.getDescriptorByType(GitTool.DescriptorImpl.class);
    GitTool tool = gitTools.getInstallation(GitTool.DEFAULT);
    if (tool != null) {
        return tool;
    } else {
        GitTool[] installations = gitTools.getInstallations();
        if (installations.length > 0) {
            return installations[0];
        } else {
            onLoaded();
            return gitTools.getInstallations()[0];
        }
    }
}
 
Example 2
Source File: GitLabConfigurationTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@ConfiguredWithReadme("gitlab/README.md")
public void configure_gitlab_connection() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    final GitLabConnectionConfig gitLabConnections = jenkins.getDescriptorByType(GitLabConnectionConfig.class);
    assertEquals(1, gitLabConnections.getConnections().size());
    final GitLabConnection gitLabConnection = gitLabConnections.getConnections().get(0);
    assertEquals("gitlab_token", gitLabConnection.getApiTokenId());
    assertEquals("my_gitlab_server", gitLabConnection.getName());
    assertEquals("autodetect", gitLabConnection.getClientBuilderId());
    assertEquals("https://gitlab.com/", gitLabConnection.getUrl());
    assertEquals(20, gitLabConnection.getConnectionTimeout());
    assertEquals(10, gitLabConnection.getReadTimeout());
    assertTrue(gitLabConnection.isIgnoreCertificateErrors());
}
 
Example 3
Source File: AdminWhitelistRuleConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("Issue #172")
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Agent2MasterSecurityKillSwitch_enabled.yml")
public void checkA2MAccessControl_enabled() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    MasterKillSwitchConfiguration config = jenkins.getDescriptorByType(MasterKillSwitchConfiguration.class);
    Assert.assertTrue("Agent → Master Access Control should be enabled", config.getMasterToSlaveAccessControl());
    AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(AdminWhitelistRule.class);
    final CNode node = c.describe(rule, context);
    final Mapping agent = node.asMapping();
    assertEquals("true", agent.get("enabled").toString());
}
 
Example 4
Source File: AdminWhitelistRuleConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("Issue #172")
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Agent2MasterSecurityKillSwitch_disabled.yml")
public void checkA2MAccessControl_disable() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    MasterKillSwitchConfiguration config = jenkins.getDescriptorByType(MasterKillSwitchConfiguration.class);
    Assert.assertFalse("Agent → Master Access Control should be disabled", config.getMasterToSlaveAccessControl());
    AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(AdminWhitelistRule.class);
    final CNode node = c.describe(rule, context);
    final Mapping agent = node.asMapping();
    assertEquals("false", agent.get("enabled").toString());
}
 
Example 5
Source File: GitLabConnectionConfigAsCodeTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
@ConfiguredWithCode("global-config.yml")
public void configure_gitlab_connection() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    final GitLabConnectionConfig gitLabConnections = jenkins.getDescriptorByType(GitLabConnectionConfig.class);
    assertEquals(1, gitLabConnections.getConnections().size());
    final GitLabConnection gitLabConnection = gitLabConnections.getConnections().get(0);
    assertEquals("gitlab_token", gitLabConnection.getApiTokenId());
    assertEquals("my_gitlab_server", gitLabConnection.getName());
    assertEquals("autodetect", gitLabConnection.getClientBuilderId());
    assertEquals("https://gitlab.com/", gitLabConnection.getUrl());
    assertEquals(20, gitLabConnection.getConnectionTimeout());
    assertEquals(10, gitLabConnection.getReadTimeout());
    assertTrue(gitLabConnection.isIgnoreCertificateErrors());
}
 
Example 6
Source File: MattermostSendStep.java    From jenkins-mattermost-plugin with MIT License 4 votes vote down vote up
@Override
protected Void run() throws Exception
{

 // default to global config values if not set in step, but allow step to
 // override all global settings
 Jenkins jenkins;
 // Jenkins.getInstance() may return null, no message sent in that case
 try
 {
  jenkins = Jenkins.get();
 } catch (NullPointerException ne)
 {
  listener.error(String.format("Mattermost notification failed with exception: %s", ne), ne);
  return null;
 }//TODO REFACTOR jenkins.getdescriptor by class
 MattermostNotifier.DescriptorImpl mattermostDesc =
	  jenkins.getDescriptorByType(MattermostNotifier.DescriptorImpl.class);
 if (mattermostDesc == null)
  mattermostDesc = (MattermostNotifier.DescriptorImpl) jenkins.getDescriptor("mattermostNotifier");//junit test fallback
 String team =
       step.getEndpoint() != null
	  ? step.getEndpoint()
	  : mattermostDesc.getEndpoint().getPlainText();
 String channel = step.channel != null ? step.channel : mattermostDesc.getRoom();
 String icon = step.icon != null ? step.icon : (mattermostDesc.getIcon() != null ? mattermostDesc.getIcon() : "");
 String color = step.color != null ? step.color : "";
 String text = step.text != null ? step.text : "";

 // placing in console log to simplify testing of retrieving values from global
 // config or from step field; also used for tests
 listener
       .getLogger()
       .printf(
           "Mattermost Send Pipeline step configured values from global config - connector: %s, icon: %s, channel: %s, color: %s",
           step.endpoint == null, step.icon == null, step.channel == null, step.color == null);

 MattermostService mattermostService = getMattermostService(team, channel, icon);
 boolean publishSuccess = mattermostService.publish(step.message, text, color);
 if (!publishSuccess && step.failOnError)
 {
  throw new AbortException("Mattermost notification failed. See Jenkins logs for details.");
 } else if (!publishSuccess)
 {
  listener.error("Mattermost notification failed. See Jenkins logs for details.");
 }
 return null;
}