Search in sources :

Example 11 with BlueOrganization

use of io.jenkins.blueocean.rest.model.BlueOrganization in project blueocean-plugin by jenkinsci.

the class FavoritesStatePreloader method getFetchData.

@Override
protected FetchData getFetchData(@Nonnull BlueUrlTokenizer blueUrl) {
    User jenkinsUser = User.current();
    if (jenkinsUser != null) {
        BlueOrganization organization = Iterables.getFirst(OrganizationFactory.getInstance().list(), null);
        if (organization != null) {
            String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
            // don't need this list when at pipeline pages
            if (pipelineFullName != null) {
                return null;
            }
            UserImpl blueUser = new UserImpl(organization, jenkinsUser, organization.getUsers());
            BlueFavoriteContainer favoritesContainer = blueUser.getFavorites();
            if (favoritesContainer != null) {
                JSONArray favorites = new JSONArray();
                // Limit the number of favorites to return to a sane amount
                Iterator<BlueFavorite> favoritesIterator = favoritesContainer.iterator(0, DEFAULT_LIMIT);
                while (favoritesIterator.hasNext()) {
                    Reachable favorite = favoritesIterator.next();
                    try {
                        favorites.add(JSONObject.fromObject(Export.toJson(favorite)));
                    } catch (IOException e) {
                        LOGGER.log(Level.FINE, String.format("Unable to preload favorites for User '%s'. Serialization error.", jenkinsUser.getFullName()), e);
                        return null;
                    }
                }
                return new FetchData(favoritesContainer.getLink().getHref() + "?start=0&limit=" + DEFAULT_LIMIT, favorites.toString());
            }
        }
    }
    // Don't preload any data on the page.
    return null;
}
Also used : BlueFavoriteContainer(io.jenkins.blueocean.rest.model.BlueFavoriteContainer) BlueFavorite(io.jenkins.blueocean.rest.model.BlueFavorite) User(hudson.model.User) BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) UserImpl(io.jenkins.blueocean.service.embedded.rest.UserImpl) JSONArray(net.sf.json.JSONArray) Reachable(io.jenkins.blueocean.rest.Reachable) IOException(java.io.IOException)

Example 12 with BlueOrganization

use of io.jenkins.blueocean.rest.model.BlueOrganization in project blueocean-plugin by jenkinsci.

the class BlueMessageEnricher method maybeEnrichMessage.

private void maybeEnrichMessage(@Nonnull Message message) {
    String channelName = message.getChannelName();
    if (channelName.equals(Events.JobChannel.NAME) && message instanceof JobChannelMessage) {
        JobChannelMessage jobChannelMessage = (JobChannelMessage) message;
        Item jobChannelItem = jobChannelMessage.getJobChannelItem();
        if (jobChannelItem == null) {
            return;
        }
        Link jobUrl = LinkResolver.resolveLink(jobChannelItem);
        if (jobUrl == null) {
            return;
        }
        BlueOrganization org = OrganizationFactory.getInstance().getContainingOrg(jobChannelItem);
        if (org != null) {
            message.set(EventProps.Jenkins.jenkins_org, org.getName());
        }
        jobChannelMessage.set(BlueEventProps.blueocean_job_rest_url, jobUrl.getHref());
        jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, AbstractPipelineImpl.getFullName(org, jobChannelItem));
        if (jobChannelItem instanceof WorkflowJob) {
            ItemGroup<? extends Item> parent = jobChannelItem.getParent();
            if (parent instanceof WorkflowMultiBranchProject) {
                String multiBranchProjectName = AbstractPipelineImpl.getFullName(org, (WorkflowMultiBranchProject) parent);
                jobChannelMessage.set(BlueEventProps.blueocean_job_pipeline_name, multiBranchProjectName);
                jobChannelMessage.set(BlueEventProps.blueocean_job_branch_name, jobChannelItem.getName());
            }
        }
        if (message.containsKey("job_run_queueId") && jobChannelItem instanceof hudson.model.Job) {
            String queueIdStr = message.get("job_run_queueId");
            if (queueIdStr == null) {
                return;
            }
            final long queueId = Long.parseLong(queueIdStr);
            Queue.Item queueItem = Jenkins.getInstance().getQueue().getItem(queueId);
            if (queueItem == null) {
                return;
            }
            hudson.model.Job job = (hudson.model.Job) jobChannelItem;
            BlueQueueItem blueQueueItem = QueueUtil.getQueuedItem(null, queueItem, job);
            if (blueQueueItem != null) {
                jobChannelMessage.set(BlueEventProps.blueocean_queue_item_expected_build_number, Integer.toString(blueQueueItem.getExpectedBuildNumber()));
            } else {
                // If build is already running, we simply take the run id and pass it on
                if (message.get("job_run_status") != null) {
                    String buildNumberStr = message.get("jenkins_object_id");
                    if (StringUtils.isNotBlank(buildNumberStr)) {
                        jobChannelMessage.set(BlueEventProps.blueocean_queue_item_expected_build_number, message.get("jenkins_object_id"));
                    }
                }
            }
        }
    }
}
Also used : JobChannelMessage(org.jenkinsci.plugins.pubsub.JobChannelMessage) BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) Item(hudson.model.Item) BlueQueueItem(io.jenkins.blueocean.rest.model.BlueQueueItem) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Queue(hudson.model.Queue) BlueQueueItem(io.jenkins.blueocean.rest.model.BlueQueueItem) Link(io.jenkins.blueocean.rest.hal.Link)

Example 13 with BlueOrganization

use of io.jenkins.blueocean.rest.model.BlueOrganization in project blueocean-plugin by jenkinsci.

the class CredentialSearch method search.

@Override
public Pageable<CredentialApi.Credential> search(Query q) {
    List<CredentialApi.Credential> credentials = new ArrayList<>();
    String domain = q.param("domain");
    String store = q.param("store");
    BlueOrganization organization = getOrganization(q);
    CredentialContainer credentialContainer = new CredentialContainer(organization.getLink());
    for (CredentialApi api : credentialContainer) {
        if (store != null && !store.equals(api.getStore())) {
            continue;
        }
        if (domain != null) {
            CredentialApi.CredentialDomain d = api.getDomains().get(domain);
            if (d == null) {
                throw new ServiceException.BadRequestException("Credential domain " + domain + " not found");
            }
            for (CredentialApi.Credential c : d.getCredentials()) {
                credentials.add(c);
            }
        } else {
            for (CredentialApi.CredentialDomain d : api.getDomains()) {
                for (CredentialApi.Credential c : d.getCredentials()) {
                    credentials.add(c);
                }
            }
        }
    }
    return Pageables.wrap(credentials);
}
Also used : BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) ArrayList(java.util.ArrayList)

Example 14 with BlueOrganization

use of io.jenkins.blueocean.rest.model.BlueOrganization in project blueocean-plugin by jenkinsci.

the class OrganizationFolderTest method mockOrganization.

static BlueOrganization mockOrganization() {
    BlueOrganization organization = mock(BlueOrganization.class);
    when(organization.getName()).thenReturn("jenkins");
    when(organization.getLink()).thenReturn(new Link("/blue/rest/organizations/jenkins/"));
    return organization;
}
Also used : BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) Link(io.jenkins.blueocean.rest.hal.Link)

Example 15 with BlueOrganization

use of io.jenkins.blueocean.rest.model.BlueOrganization in project blueocean-plugin by jenkinsci.

the class RunSearch method getLatestRun.

private BlueRun getLatestRun(Job job) {
    if (job == null) {
        return null;
    }
    BlueOrganization org = OrganizationFactory.getInstance().getContainingOrg(job);
    if (org == null) {
        return null;
    }
    Run r = job.getLastBuild();
    if (r == null) {
        return null;
    }
    Resource resource = BluePipelineFactory.resolve(job);
    if (resource == null) {
        return null;
    }
    return BlueRunFactory.getRun(r, resource);
}
Also used : BlueOrganization(io.jenkins.blueocean.rest.model.BlueOrganization) Resource(io.jenkins.blueocean.rest.model.Resource) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) Run(hudson.model.Run)

Aggregations

BlueOrganization (io.jenkins.blueocean.rest.model.BlueOrganization)19 Item (hudson.model.Item)4 Link (io.jenkins.blueocean.rest.hal.Link)4 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Job (hudson.model.Job)3 Resource (io.jenkins.blueocean.rest.model.Resource)3 OrganizationFolder (jenkins.branch.OrganizationFolder)3 Run (hudson.model.Run)2 User (hudson.model.User)2 ModifiableTopLevelItemGroup (jenkins.model.ModifiableTopLevelItemGroup)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)1 Plugin (hudson.Plugin)1 FreeStyleProject (hudson.model.FreeStyleProject)1 ItemGroup (hudson.model.ItemGroup)1 Project (hudson.model.Project)1 Queue (hudson.model.Queue)1 TopLevelItem (hudson.model.TopLevelItem)1