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

The following examples show how to use jenkins.model.Jenkins#getItemByFullName() . 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: PipelineActivityStatePreloader.java    From blueocean-plugin with MIT License 6 votes vote down vote up
private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
    if (addPipelineRuns(blueUrl)) {
        Jenkins jenkins = Jenkins.getInstance();
        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;
}
 
Example 2
Source File: Caches.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public Optional<PullRequest> load(String key) throws Exception {
    Jenkins jenkins = Objects.firstNonNull(this.jenkins, Jenkins.getInstance());
    Job job = jenkins.getItemByFullName(key, Job.class);
    if (job == null) {
        return Optional.absent();
    }
    // TODO probably want to be using SCMHeadCategory instances to categorize them instead of hard-coding for PRs
    SCMHead head = SCMHead.HeadByItem.findHead(job);
    if (head instanceof ChangeRequestSCMHead) {
        ChangeRequestSCMHead cr = (ChangeRequestSCMHead) head;
        ObjectMetadataAction om = job.getAction(ObjectMetadataAction.class);
        ContributorMetadataAction cm = job.getAction(ContributorMetadataAction.class);
        return Optional.of(new PullRequest(
                cr.getId(),
                om != null ? om.getObjectUrl() : null,
                om != null ? om.getObjectDisplayName() : null,
                cm != null ? cm.getContributor() : null
            )
        );
    }
    return Optional.absent();
}
 
Example 3
Source File: GogsUtils.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
/**
 * Search in Jenkins for a item with type T based on the job name
 * @param jobName job to find, for jobs inside a folder use : {@literal <folder>/<folder>/<jobName>}
 * @return the Job matching the given name, or {@code null} when not found
 */
static <T extends Item> T find(String jobName, Class<T> type) {
	Jenkins jenkins = Jenkins.getActiveInstance();
	// direct search, can be used to find folder based items <folder>/<folder>/<jobName>
	T item = jenkins.getItemByFullName(jobName, type);
	if (item == null) {
		// not found in a direct search, search in all items since the item might be in a folder but given without folder structure
		// (to keep it backwards compatible)
		for (T allItem : jenkins.getAllItems(type)) {
			 if (allItem.getName().equals(jobName)) {
			 	item = allItem;
			 	break;
			 }
		}
	}
	return item;
}
 
Example 4
Source File: BlueRunChangesetPreloader.java    From blueocean-plugin with MIT License 5 votes vote down vote up
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;
    }
}
 
Example 5
Source File: Caches.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Optional<Branch> load(String key) throws Exception {
    Jenkins jenkins = Objects.firstNonNull(this.jenkins, Jenkins.getInstance());
    Job job = jenkins.getItemByFullName(key, Job.class);
    if (job == null) {
        return Optional.absent();
    }
    ObjectMetadataAction om = job.getAction(ObjectMetadataAction.class);
    PrimaryInstanceMetadataAction pima = job.getAction(PrimaryInstanceMetadataAction.class);
    String url = om != null && om.getObjectUrl() != null ? om.getObjectUrl() : null;
    if (StringUtils.isEmpty(url)) {
        /*
         * Borrowed from https://github.com/jenkinsci/branch-api-plugin/blob/c4d394415cf25b6890855a08360119313f1330d2/src/main/java/jenkins/branch/BranchNameContributor.java#L63
         * for those that don't implement object metadata action
         */
        ItemGroup parent = job.getParent();
        if (parent instanceof MultiBranchProject) {
            BranchProjectFactory projectFactory = ((MultiBranchProject) parent).getProjectFactory();
            if (projectFactory.isProject(job)) {
                SCMHead head = projectFactory.getBranch(job).getHead();
                url = head.getName();
            }
        }
    }
    if (StringUtils.isEmpty(url) && pima == null) {
        return Optional.absent();
    }
    return Optional.of(new Branch(url, pima != null, BlueIssueFactory.resolve(job)));
}
 
Example 6
Source File: WorkspaceCopyFileBuilder.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    listener.getLogger().println("Copying a " + fileName + " from " + jobName + "#" + buildNumber);
    
    Jenkins inst = Jenkins.getInstance();
    AbstractProject<?,?> item = inst.getItemByFullName(jobName, AbstractProject.class);
    if (item == null) {
        throw new AbortException("Cannot find a source job: " + jobName);
    }
    
    AbstractBuild<?,?> sourceBuild = item.getBuildByNumber(buildNumber);
    if (sourceBuild == null) {
        throw new AbortException("Cannot find a source build: " + jobName + "#" + buildNumber);
    }
    
    FilePath sourceWorkspace = sourceBuild.getWorkspace();
    if (sourceWorkspace == null) {
        throw new AbortException("Cannot get the source workspace from " + sourceBuild.getDisplayName());
    }
    
    FilePath workspace = build.getWorkspace();
    if (workspace == null) {
        throw new IOException("Cannot get the workspace of the build");
    }
    workspace.child(fileName).copyFrom(sourceWorkspace.child(fileName));
    
    return true;
}
 
Example 7
Source File: AWSClientFactory.java    From aws-codebuild-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
public AWSClientFactory(String credentialsType, String credentialsId, String proxyHost, String proxyPort, String awsAccessKey, Secret awsSecretKey, String awsSessionToken,
                   String region, Run<?, ?> build, StepContext stepContext) {

    this.awsAccessKey = sanitize(awsAccessKey);
    this.awsSecretKey = awsSecretKey;
    this.awsSessionToken = sanitize(awsSessionToken);
    this.region = sanitize(region);
    this.properties = new Properties();

    CodeBuilderValidation.checkAWSClientFactoryRegionConfig(this.region);
    this.credentialsDescriptor = "";

    if(credentialsType.equals(CredentialsType.Jenkins.toString())) {
        credentialsId = sanitize(credentialsId);
        CodeBuilderValidation.checkAWSClientFactoryJenkinsCredentialsConfig(credentialsId);
        com.amazonaws.codebuild.jenkinsplugin.CodeBuildBaseCredentials codeBuildCredentials;

        codeBuildCredentials = (CodeBuildBaseCredentials) CredentialsMatchers.firstOrNull(SystemCredentialsProvider.getInstance().getCredentials(),
                CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId)));

        if(codeBuildCredentials == null) {
            Item folder;
            Jenkins instance = Jenkins.getInstance();
            if(instance != null) {
                folder = instance.getItemByFullName(build.getParent().getParent().getFullName());
                codeBuildCredentials = (CodeBuildBaseCredentials) CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(Credentials.class, folder),
                        CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId)));
            }
        }

        if(codeBuildCredentials != null) {
            this.awsCredentialsProvider = codeBuildCredentials;
            this.proxyHost = codeBuildCredentials.getProxyHost();
            this.proxyPort = parseInt(codeBuildCredentials.getProxyPort());
            this.credentialsDescriptor = codeBuildCredentials.getCredentialsDescriptor() + " (provided from Jenkins credentials " + credentialsId + ")";
        } else {
            throw new InvalidInputException(CodeBuilderValidation.invalidCredentialsIdError);
        }
    } else if(credentialsType.equals(CredentialsType.Keys.toString())) {
        if(this.awsSecretKey == null) {
            throw new InvalidInputException(invalidSecretKeyError);
        }

        if(stepContext != null && awsAccessKey.isEmpty() && awsSecretKey.getPlainText().isEmpty()) {
            try {
                EnvVars stepEnvVars = stepContext.get(EnvVars.class);
                awsCredentialsProvider = getStepCreds(stepEnvVars);
            } catch (IOException|InterruptedException e) {}
        }

        if(awsCredentialsProvider == null) {
            awsCredentialsProvider = getBasicCredentialsOrDefaultChain(sanitize(awsAccessKey), awsSecretKey.getPlainText(), sanitize(awsSessionToken));
        }
        this.proxyHost = sanitize(proxyHost);
        this.proxyPort = parseInt(proxyPort);
    } else {
        throw new InvalidInputException(invalidCredTypeError);
    }
}