Search in sources :

Example 26 with Item

use of hudson.model.Item in project blueocean-plugin by jenkinsci.

the class GithubPipelineCreateRequestTest method shouldFindUserStoreCredential.

@Test
public void shouldFindUserStoreCredential() throws IOException {
    // add username password credential to user's credential store in user domain and in USER scope
    User user = login();
    CredentialsStore store = null;
    for (CredentialsStore s : CredentialsProvider.lookupStores(user)) {
        if (s.hasPermission(CredentialsProvider.CREATE) && s.hasPermission(CredentialsProvider.UPDATE)) {
            store = s;
            break;
        }
    }
    assertNotNull(store);
    store.addDomain(new Domain("github-domain", "GitHub Domain to store personal access token", Collections.<DomainSpecification>singletonList(new BlueOceanDomainSpecification())));
    Domain domain = store.getDomainByName("github-domain");
    StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "GitHub Access Token", user.getId(), "12345");
    store.addCredentials(domain, credential);
    // create another credentials with same id in system store with different description
    for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.get())) {
        s.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "System GitHub Access Token", user.getId(), "12345"));
    }
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "demo");
    AbstractFolderProperty prop = new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credential.getId(), BlueOceanCredentialsProvider.createDomain("https://api.github.com"));
    mp.addProperty(prop);
    // lookup for created credential id in system store, it should resolve to previously created user store credential
    StandardCredentials c = Connector.lookupScanCredentials((Item) mp, "https://api.github.com", credential.getId());
    assertEquals("GitHub Access Token", c.getDescription());
    assertNotNull(c);
    assertTrue(c instanceof StandardUsernamePasswordCredentials);
    StandardUsernamePasswordCredentials usernamePasswordCredentials = (StandardUsernamePasswordCredentials) c;
    assertEquals(credential.getId(), usernamePasswordCredentials.getId());
    assertEquals(credential.getPassword().getPlainText(), usernamePasswordCredentials.getPassword().getPlainText());
    assertEquals(credential.getUsername(), usernamePasswordCredentials.getUsername());
    // check the domain
    Domain d = CredentialsUtils.findDomain(credential.getId(), user);
    assertNotNull(d);
    assertTrue(d.test(new BlueOceanDomainRequirement()));
    // now remove this property
    mp.getProperties().remove(prop);
    // it must resolve to system credential
    c = Connector.lookupScanCredentials((Item) mp, null, credential.getId());
    assertEquals("System GitHub Access Token", c.getDescription());
}
Also used : User(hudson.model.User) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) DomainSpecification(com.cloudbees.plugins.credentials.domains.DomainSpecification) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) Item(hudson.model.Item) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 27 with Item

use of hudson.model.Item in project blueocean-plugin by jenkinsci.

the class BlueRunChangesetPreloader method getPipeline.

private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
    Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null) {
        return null;
    }
    String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
    try {
        Item pipelineJob = jenkins.getItemByFullName(pipelineFullName);
        return (BluePipeline) BluePipelineFactory.resolve(pipelineJob);
    } catch (Exception e) {
        LOGGER.log(Level.FINE, String.format("Unable to find Job named '%s'.", pipelineFullName), e);
        return null;
    }
}
Also used : Jenkins(jenkins.model.Jenkins) Item(hudson.model.Item) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) IOException(java.io.IOException)

Example 28 with Item

use of hudson.model.Item in project blueocean-plugin by jenkinsci.

the class FavoriteListStatePreloader method getStateJson.

@CheckForNull
@Override
public String getStateJson() {
    User jenkinsUser = User.current();
    if (jenkinsUser == null) {
        return null;
    }
    FavoriteUserProperty fup = jenkinsUser.getProperty(FavoriteUserProperty.class);
    if (fup == null) {
        return null;
    }
    Set<String> favorites = fup.getAllFavorites();
    if (favorites == null) {
        return null;
    }
    final Jenkins jenkins = Jenkins.get();
    final List<FavoritPreload> favoritPreloads = favorites.stream().map(name -> {
        final Item item = jenkins.getItemByFullName(name);
        if (item instanceof Job) {
            final Job<?, ?> job = (Job<?, ?>) item;
            if (job.getAction(PrimaryInstanceMetadataAction.class) != null) {
                return new FavoritPreload(name, true);
            }
        }
        return new FavoritPreload(name, false);
    }).collect(Collectors.toList());
    return JSONArray.fromObject(favoritPreloads).toString();
}
Also used : FavoriteUserProperty(hudson.plugins.favorite.user.FavoriteUserProperty) Jenkins(jenkins.model.Jenkins) Jenkins(jenkins.model.Jenkins) FavoriteUserProperty(hudson.plugins.favorite.user.FavoriteUserProperty) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) PageStatePreloader(io.jenkins.blueocean.commons.PageStatePreloader) PrimaryInstanceMetadataAction(jenkins.scm.api.metadata.PrimaryInstanceMetadataAction) JSONArray(net.sf.json.JSONArray) Item(hudson.model.Item) Extension(hudson.Extension) Job(hudson.model.Job) User(hudson.model.User) CheckForNull(javax.annotation.CheckForNull) Nonnull(javax.annotation.Nonnull) Item(hudson.model.Item) User(hudson.model.User) Job(hudson.model.Job) CheckForNull(javax.annotation.CheckForNull)

Example 29 with Item

use of hudson.model.Item in project blueocean-plugin by jenkinsci.

the class BlueOceanUrlMapperImpl method getUrl.

@Override
public String getUrl(@Nonnull ModelObject modelObject) {
    BlueOrganization organization = getOrganization(modelObject);
    if (organization == null) {
        // no organization, best we can do is to land user on landing page
        return getLandingPagePath();
    }
    String organizationName = organization.getName();
    String baseUrl = getOrgPrefix(organizationName);
    if (modelObject instanceof ModifiableTopLevelItemGroup) {
        return baseUrl;
    } else if (modelObject instanceof Job) {
        BluePipeline blueResource = getJobResource(modelObject);
        if (blueResource != null) {
            return getPipelineUrl(baseUrl, blueResource);
        }
    } else if (modelObject instanceof Run) {
        Run run = (Run) modelObject;
        Job job = run.getParent();
        BluePipeline blueResource = getJobResource(job);
        if (blueResource != null) {
            // encoded and re-encode to do the full monty. Nasty :)
            return baseUrl + "/" + encodeURIComponent(blueResource.getFullName()) + "/detail/" + encodeURIComponent(decodeURIComponent(job.getName())) + "/" + encodeURIComponent(run.getId()) + "/";
        }
    } else if (modelObject instanceof Item) {
        Resource bluePipeline = BluePipelineFactory.resolve((Item) modelObject);
        if (bluePipeline instanceof BlueMultiBranchPipeline) {
            return getPipelineUrl(baseUrl, (BluePipeline) bluePipeline);
        }
    }
    return null;
}
Also used : Item(hudson.model.Item) ModifiableTopLevelItemGroup(jenkins.model.ModifiableTopLevelItemGroup) BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) Resource(io.jenkins.blueocean.rest.model.Resource) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) Run(hudson.model.Run) Job(hudson.model.Job) BlueMultiBranchPipeline(io.jenkins.blueocean.rest.model.BlueMultiBranchPipeline)

Example 30 with Item

use of hudson.model.Item in project blueocean-plugin by jenkinsci.

the class PipelineActivityStatePreloader method getPipeline.

private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
    if (addPipelineRuns(blueUrl)) {
        Jenkins jenkins = Jenkins.get();
        String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
        try {
            Item pipelineJob = jenkins.getItemByFullName(pipelineFullName);
            return (BluePipeline) BluePipelineFactory.resolve(pipelineJob);
        } catch (Exception e) {
            LOGGER.log(Level.FINE, String.format("Unable to find Job named '%s'.", pipelineFullName), e);
            return null;
        }
    }
    return null;
}
Also used : Jenkins(jenkins.model.Jenkins) Item(hudson.model.Item) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) IOException(java.io.IOException)

Aggregations

Item (hudson.model.Item)36 ItemGroup (hudson.model.ItemGroup)7 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)7 IOException (java.io.IOException)6 Run (hudson.model.Run)5 Job (hudson.model.Job)4 BlueOrganization (io.jenkins.blueocean.rest.model.BlueOrganization)4 ArrayList (java.util.ArrayList)4 Nonnull (javax.annotation.Nonnull)4 MultiBranchProject (jenkins.branch.MultiBranchProject)4 Jenkins (jenkins.model.Jenkins)4 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)4 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)4 StaplerRequest (org.kohsuke.stapler.StaplerRequest)4 TopLevelItem (hudson.model.TopLevelItem)3 User (hudson.model.User)3 Resource (io.jenkins.blueocean.rest.model.Resource)3 CheckForNull (javax.annotation.CheckForNull)3 AbstractFolder (com.cloudbees.hudson.plugins.folder.AbstractFolder)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)2