Search in sources :

Example 11 with TopLevelItem

use of hudson.model.TopLevelItem in project configuration-as-code-plugin by jenkinsci.

the class GithubOrganisationFolderTest method configure_github_organisation_folder_seed_job.

// @Test
// Fails as Items do override submit() with manual data-binding implementation
@ConfiguredWithCode("GithubOrganisationFolderTest.yml")
public void configure_github_organisation_folder_seed_job() throws Exception {
    final TopLevelItem job = Jenkins.getInstance().getItem("ndeloof");
    assertNotNull(job);
    assertTrue(job instanceof OrganizationFolder);
    OrganizationFolder folder = (OrganizationFolder) job;
    assertEquals(1, folder.getNavigators().size());
    final GitHubSCMNavigator github = folder.getNavigators().get(GitHubSCMNavigator.class);
    assertNotNull(github);
    assertEquals("ndeloof", github.getRepoOwner());
}
Also used : OrganizationFolder(jenkins.branch.OrganizationFolder) TopLevelItem(hudson.model.TopLevelItem) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) ConfiguredWithCode(org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)

Example 12 with TopLevelItem

use of hudson.model.TopLevelItem in project promoted-builds-plugin by jenkinsci.

the class PromotionsDslContextExtensionTest method testShouldGenerateTheCopyArtifactsJob.

@Test
public void testShouldGenerateTheCopyArtifactsJob() throws Exception {
    // Given
    String dsl = FileUtils.readFileToString(new File("src/test/resources/copyartifacts-example-dsl.groovy"));
    FreeStyleProject seedJob = j.createFreeStyleProject();
    seedJob.getBuildersList().add(createScript(dsl));
    // When
    QueueTaskFuture<FreeStyleBuild> scheduleBuild2 = seedJob.scheduleBuild2(0);
    // Then (unstable b/c we aren't including the CopyArtifacts dependency)
    j.assertBuildStatus(Result.UNSTABLE, scheduleBuild2.get());
    TopLevelItem item = j.jenkins.getItem("copy-artifacts-test");
    File config = new File(item.getRootDir(), "promotions/Development/config.xml");
    String content = Files.toString(config, Charset.forName("UTF-8"));
    assert content.contains("<selector class=\"hudson.plugins.copyartifact.SpecificBuildSelector\">");
}
Also used : TopLevelItem(hudson.model.TopLevelItem) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) File(java.io.File) Test(org.junit.Test)

Example 13 with TopLevelItem

use of hudson.model.TopLevelItem in project promoted-builds-plugin by jenkinsci.

the class ItemPathResolver method getByPath.

/**
 * Gets an {@link Item} of the specified type by absolute or relative path.
 * <p>
 * The implementation retains the original behavior in {@link PromotedBuildParameterDefinition},
 * but this method also provides a support of multi-level addressing including special markups
 * for the relative addressing.
 * </p>
 * Effectively, the resolution order is following:
 * <ul>
 *   <li><b>Optional</b> Legacy behavior, which can be enabled by {@link #ENABLE_LEGACY_RESOLUTION_AGAINST_ROOT}.
 *       If an item for the name exists on the top Jenkins level, it will be returned</li>
 *   <li>If the path starts with &quot;/&quot;, a global addressing will be used</li>
 *   <li>If the path starts with &quot;./&quot; or &quot;../&quot;, a relative addressing will be used</li>
 *   <li>If there is no prefix, a relative addressing will be tried. If it
 *       fails, the method falls back to a global one</li>
 * </ul>
 * For the relative and absolute addressing the engine supports &quot;.&quot; and
 * &quot;..&quot; markers within the path.
 * The first one points to the current element, the second one - to the upper element.
 * If the search cannot get a new top element (e.g. reached the root), the method returns {@code null}.
 *
 * @param <T> Type of the {@link Item} to be retrieved
 * @param path Path string to the item.
 * @param baseItem Base {@link Item} for the relative addressing. If null,
 *      this addressing approach will be skipped
 * @param type Type of the {@link Item} to be retrieved
 * @return Found {@link Item}. Null if it has not been found by all addressing modes
 *  or the type differs.
 */
@CheckForNull
@SuppressWarnings("unchecked")
@Restricted(NoExternalUse.class)
public static <T extends Item> T getByPath(@Nonnull String path, @CheckForNull Item baseItem, @Nonnull Class<T> type) {
    final Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null) {
        return null;
    }
    // Legacy behavior
    if (isEnableLegacyResolutionAgainstRoot()) {
        TopLevelItem topLevelItem = jenkins.getItem(path);
        if (topLevelItem != null && type.isAssignableFrom(topLevelItem.getClass())) {
            return (T) topLevelItem;
        }
    }
    // Explicit global addressing
    if (path.startsWith("/")) {
        return findPath(jenkins, path.substring(1), type);
    }
    // Try the relative addressing if possible
    if (baseItem != null) {
        final ItemGroup<?> relativeRoot = baseItem instanceof ItemGroup<?> ? (ItemGroup<?>) baseItem : baseItem.getParent();
        final T item = findPath(relativeRoot, path, type);
        if (item != null) {
            return item;
        }
    }
    // Fallback to the default behavior (addressing from the Jenkins root)
    return findPath(jenkins, path, type);
}
Also used : Jenkins(jenkins.model.Jenkins) TopLevelItem(hudson.model.TopLevelItem) Restricted(org.kohsuke.accmod.Restricted) CheckForNull(javax.annotation.CheckForNull)

Example 14 with TopLevelItem

use of hudson.model.TopLevelItem in project hudson-2.x by hudson.

the class UpdateJobCommand method run.

protected int run() throws Exception {
    Hudson h = Hudson.getInstance();
    TopLevelItem item = h.getItem(name);
    if (item == null && !create) {
        stderr.println("Job '" + name + "' does not exist and create is set to false");
        return -1;
    }
    if (item == null) {
        h.checkPermission(Item.CREATE);
        h.createProjectFromXML(name, stdin);
    } else {
        try {
            h.checkPermission(Job.CONFIGURE);
            File rootDirOfJob = new File(new File(h.getRootDir(), "jobs"), name);
            // place it as config.xml
            File configXml = Items.getConfigFile(rootDirOfJob).getFile();
            IOUtils.copy(stdin, configXml);
            item = h.reloadProjectFromDisk(configXml.getParentFile());
        } catch (IOException e) {
            throw e;
        }
    }
    return 0;
}
Also used : Hudson(hudson.model.Hudson) TopLevelItem(hudson.model.TopLevelItem) IOException(java.io.IOException) File(java.io.File)

Example 15 with TopLevelItem

use of hudson.model.TopLevelItem in project hudson-2.x by hudson.

the class TopLevelItemOptionHandler method parseArguments.

@Override
public int parseArguments(Parameters params) throws CmdLineException {
    Hudson h = Hudson.getInstance();
    String src = params.getParameter(0);
    TopLevelItem s = h.getItem(src);
    if (s == null)
        throw new CmdLineException(owner, "No such job '" + src + "' perhaps you meant " + AbstractProject.findNearest(src) + "?");
    setter.addValue(s);
    return 1;
}
Also used : Hudson(hudson.model.Hudson) TopLevelItem(hudson.model.TopLevelItem) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

TopLevelItem (hudson.model.TopLevelItem)16 Test (org.junit.Test)5 File (java.io.File)3 IOException (java.io.IOException)3 Map (java.util.Map)3 OrganizationFolder (jenkins.branch.OrganizationFolder)3 Jenkins (jenkins.model.Jenkins)3 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)3 Domain (com.cloudbees.plugins.credentials.domains.Domain)2 Cause (hudson.model.Cause)2 FreeStyleBuild (hudson.model.FreeStyleBuild)2 FreeStyleProject (hudson.model.FreeStyleProject)2 Hudson (hudson.model.Hudson)2 Item (hudson.model.Item)2 User (hudson.model.User)2 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)2 ServiceException (io.jenkins.blueocean.commons.ServiceException)2 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)2 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)2 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)2