Java Code Examples for hudson.model.Item#getParent()

The following examples show how to use hudson.model.Item#getParent() . 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: FolderVaultConfiguration.java    From hashicorp-vault-plugin with MIT License 6 votes vote down vote up
@NonNull
@Override
public VaultConfiguration forJob(@NonNull Item job) {
    VaultConfiguration resultingConfig = null;
    for (ItemGroup g = job.getParent(); g instanceof AbstractFolder;
        g = ((AbstractFolder) g).getParent()) {
        FolderVaultConfiguration folderProperty = ((AbstractFolder<?>) g).getProperties()
            .get(FolderVaultConfiguration.class);
        if (folderProperty == null) {
            continue;
        }
        if (resultingConfig != null) {
            resultingConfig = resultingConfig
                .mergeWithParent(folderProperty.getConfiguration());
        } else {
            resultingConfig = folderProperty.getConfiguration();
        }
    }

    return resultingConfig;
}
 
Example 2
Source File: GitLabConnection.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Restricted(NoExternalUse.class)
private String getApiToken(String apiTokenId, Item item) {
    ItemGroup<?> context = null != item ? item.getParent() : Jenkins.get();
    StandardCredentials credentials = CredentialsMatchers.firstOrNull(
        lookupCredentials(
                StandardCredentials.class,
                context, 
                ACL.SYSTEM,
                URIRequirementBuilder.fromUri(url).build()),
        CredentialsMatchers.withId(apiTokenId));
    if (credentials != null) {
        if (credentials instanceof GitLabApiToken) {
            return ((GitLabApiToken) credentials).getApiToken().getPlainText();
        }
        if (credentials instanceof StringCredentials) {
            return ((StringCredentials) credentials).getSecret().getPlainText();
        }
    }
    throw new IllegalStateException("No credentials found for credentialsId: " + apiTokenId);
}
 
Example 3
Source File: BranchImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public BluePipeline getPipeline(Item item, Reachable parent, BlueOrganization organization) {
    if (item instanceof WorkflowJob && item.getParent() instanceof MultiBranchProject) {
        return new BranchImpl(organization, (Job) item, parent.getLink());
    }
    return null;
}
 
Example 4
Source File: BranchImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Resource resolve(Item context, Reachable parent, Item target, BlueOrganization organization) {
    if (context==target.getParent()) {
        return getPipeline(context, parent, organization);
    }
    return null;
}
 
Example 5
Source File: MultiBranchPipelineImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Resource resolve(Item context, Reachable parent, Item target, BlueOrganization organization) {
    if (context instanceof MultiBranchProject) {
        if (context == target)
            return getPipeline(context, parent, organization);
        if (context == target.getParent()) {
            // target is a branch
            return getPipeline(context, parent, organization).getBranches().get(target.getName());
        }
    }
    return null;
}
 
Example 6
Source File: LocalRepoPlunger.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public void onDeleted(Item item) {
    if (!(item instanceof Job)) {
        return;
    }

    ItemGroup<? extends Item> parent = item.getParent();
    if (!(parent instanceof MultiBranchProject)) {
        return;
    }

    Job j = (Job) item;
    MultiBranchProject mb = (MultiBranchProject) parent;
    Branch branch = mb.getProjectFactory().getBranch(j);
    SCMHead head = branch.getHead();

    Consumer<GitHubRepo> plunger = null;
    if (head instanceof GitHubBranchSCMHead) {
        plunger = r -> r.getBranchRepository().getBranches().remove(head.getName());
    } else if (head instanceof GitHubTagSCMHead) {
        plunger = r -> r.getTagRepository().getTags().remove(head.getName());
    } else if (head instanceof GitHubPRSCMHead) {
        GitHubPRSCMHead prHead = (GitHubPRSCMHead) head;
        plunger = r -> r.getPrRepository().getPulls().remove(prHead.getPrNumber());
    }

    if (plunger != null) {
        for (SCMSource src : (List<SCMSource>) mb.getSCMSources()) {
            if (src instanceof GitHubSCMSource) {
                GitHubSCMSource gsrc = (GitHubSCMSource) src;
                plunger.accept(gsrc.getLocalRepo());
                LOG.info("Plunging local data for {}", item.getFullName());
            }
        }
    }
}
 
Example 7
Source File: GitLabPushTrigger.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private StringBuilder retrieveParentUrl(Item item) {
    if (item.getParent() instanceof Item) {
        Item parent = (Item) item.getParent();
        return retrieveParentUrl(parent).append('/').append(Util.rawEncode(parent.getName()));
    } else {
        return new StringBuilder();
    }
}
 
Example 8
Source File: ScriptedPipelineAnalyticsCheck.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Override
public Boolean apply(Item item) {
    return item.getParent() instanceof MultiBranchProject;
}
 
Example 9
Source File: MatrixAnalyticsCheck.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Override
public Boolean apply(Item item) {
    return item.getClass().getName().equals("hudson.matrix.MatrixConfiguration") || item.getParent() instanceof MatrixProject;
}
 
Example 10
Source File: BlueMessageEnricher.java    From blueocean-plugin with MIT License 4 votes vote down vote up
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"));
                    }
                }
            }
        }

    }
}