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

The following examples show how to use hudson.model.Job#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: GithubIssue.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public Collection<BlueIssue> getIssues(ChangeLogSet.Entry changeSetEntry) {
    Job job = changeSetEntry.getParent().getRun().getParent();
    if (!(job.getParent() instanceof MultiBranchProject)) {
        return null;
    }
    MultiBranchProject mbp = (MultiBranchProject)job.getParent();
    List<SCMSource> scmSources = (List<SCMSource>) mbp.getSCMSources();
    SCMSource source = scmSources.isEmpty() ? null : scmSources.get(0);
    if (!(source instanceof GitHubSCMSource)) {
        return null;
    }
    GitHubSCMSource gitHubSource = (GitHubSCMSource)source;
    String apiUri =  gitHubSource.getApiUri();
    final String repositoryUri = new HttpsRepositoryUriResolver().getRepositoryUri(apiUri, gitHubSource.getRepoOwner(), gitHubSource.getRepository());
    Collection<BlueIssue> results = new ArrayList<>();
    for (String input : findIssueKeys(changeSetEntry.getMsg())) {
        String uri = repositoryUri.substring(0, repositoryUri.length() - 4);
        results.add(new GithubIssue("#" + input, String.format("%s/issues/%s", uri, input)));
    }
    return results;
}
 
Example 2
Source File: WithMavenStepExecution2.java    From pipeline-maven-plugin with MIT License 6 votes vote down vote up
@CheckForNull
private MavenConfigFolderOverrideProperty getMavenConfigOverrideProperty() {
    Job<?, ?> job = build.getParent(); // Get the job

    // Iterate until we find an override or until we reach the top. We need it to be an item to be able to do
    // getParent, AbstractFolder which has the properties is also an Item
    for (ItemGroup<?> group = job.getParent(); group != null && group instanceof Item && !(group instanceof Jenkins); group = ((Item) group).getParent()) {
        if (group instanceof AbstractFolder) {
            MavenConfigFolderOverrideProperty mavenConfigProperty = ((AbstractFolder<?>) group).getProperties().get(MavenConfigFolderOverrideProperty.class);
            if (mavenConfigProperty != null && mavenConfigProperty.isOverride()) {
                return mavenConfigProperty;
            }
        }
    }
    return null;
}
 
Example 3
Source File: Utils.java    From lockable-resources-plugin with MIT License 6 votes vote down vote up
public static LockableResourcesStruct requiredResources(
		Job<?, ?> project) {
	RequiredResourcesProperty property = null;
	EnvVars env = new EnvVars();

	if (project instanceof MatrixConfiguration) {
		env.putAll(((MatrixConfiguration) project).getCombination());
		project = (Job<?, ?>) project.getParent();
	}

	property = project.getProperty(RequiredResourcesProperty.class);
	if (property != null)
		return new LockableResourcesStruct(property, env);

	return null;
}
 
Example 4
Source File: IssueCommentGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isApplicable(Item item) {
    if (item != null && item instanceof Job<?, ?>) {
        Job<?, ?> project = (Job<?, ?>) item;
        if (project.getParent() instanceof SCMSourceOwner) {
            SCMSourceOwner owner = (SCMSourceOwner) project.getParent();
            for (SCMSource source : owner.getSCMSources()) {
                if (source instanceof GitHubSCMSource) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 5
Source File: PRUpdateGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isApplicable(Item item) {
    if (item != null && item instanceof Job<?, ?>) {
        Job<?, ?> project = (Job<?, ?>) item;
        if (project.getParent() instanceof SCMSourceOwner) {
            SCMSourceOwner owner = (SCMSourceOwner) project.getParent();
            for (SCMSource source : owner.getSCMSources()) {
                if (source instanceof GitHubSCMSource) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 6
Source File: PRReviewGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isApplicable(Item item) {
    if (item != null && item instanceof Job<?, ?>) {
        Job<?, ?> project = (Job<?, ?>) item;
        if (project.getParent() instanceof SCMSourceOwner) {
            SCMSourceOwner owner = (SCMSourceOwner) project.getParent();
            for (SCMSource source : owner.getSCMSources()) {
                if (source instanceof GitHubSCMSource) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 7
Source File: GitLabSCMItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void onUpdated(Job<?, ?> job) {
    BranchJobProperty property = job.getProperty(BranchJobProperty.class);
    if (property != null && job.getParent() instanceof SCMSourceOwner) {
        // TODO: HACK ALERT! There must/should be a nicer way to do this!
        updateProperties(job, (SCMSourceOwner) job.getParent(), property.getBranch().getSourceId());
    }
}
 
Example 8
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 9
Source File: MultiBranchPipelineQueueContainer.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public BlueQueueItem get(String name) {
    try {
        Queue.Item item = Jenkins.getInstance().getQueue().getItem(Long.parseLong(name));
        if(item != null && item.task instanceof Job){
            Job job = ((Job) item.task);
            if(job.getParent() != null && job.getParent().getFullName().equals(multiBranchPipeline.mbp.getFullName())) {
                return QueueUtil.getQueuedItem(multiBranchPipeline.getOrganization(), item, job);
            }
        }
    }catch (NumberFormatException e){
        throw new ServiceException.BadRequestException("Invalid queue id: "+name+". Must be a number.",e);
    }
    return null;
}
 
Example 10
Source File: FolderConfig.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Override
public String getLabel(@Nullable Run run) {
    if (run != null) {
        Job job = run.getParent();
        ItemGroup parent = job.getParent();
        while (parent != null) {

            if (parent instanceof AbstractFolder) {
                AbstractFolder folder = (AbstractFolder) parent;
                FolderConfig config = (FolderConfig) folder.getProperties().get(FolderConfig.class);
                if (config != null) {
                    String label = config.getDockerLabel();
                    if (!StringUtils.isBlank(label)) {
                        return label;
                    }
                }
            }

            if (parent instanceof Item) {
                parent = ((Item) parent).getParent();
            } else {
                parent = null;
            }
        }
    }
    return null;
}
 
Example 11
Source File: FolderConfig.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Override
public String getRegistryUrl(@Nullable Run run) {
    if (run != null) {
        Job job = run.getParent();
        ItemGroup parent = job.getParent();
        while (parent != null) {

            if (parent instanceof AbstractFolder) {
                AbstractFolder folder = (AbstractFolder) parent;
                FolderConfig config = (FolderConfig) folder.getProperties().get(FolderConfig.class);
                if (config != null) {
                    DockerRegistryEndpoint registry = config.getRegistry();
                    if (registry != null && !StringUtils.isBlank(registry.getUrl())) {
                        return registry.getUrl();
                    }
                }
            }

            if (parent instanceof Item) {
                parent = ((Item) parent).getParent();
            } else {
                parent = null;
            }
        }
    }
    return null;
}
 
Example 12
Source File: FolderConfig.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Override
public String getRegistryCredentialsId(@Nullable Run run) {
    if (run != null) {
        Job job = run.getParent();
        ItemGroup parent = job.getParent();
        while (parent != null) {

            if (parent instanceof AbstractFolder) {
                AbstractFolder folder = (AbstractFolder) parent;
                FolderConfig config = (FolderConfig) folder.getProperties().get(FolderConfig.class);
                if (config != null) {
                    DockerRegistryEndpoint registry = config.getRegistry();
                    if (registry != null && !StringUtils.isBlank(registry.getCredentialsId())) {
                        return registry.getCredentialsId();
                    }
                }
            }

            if (parent instanceof Item) {
                parent = ((Item) parent).getParent();
            } else {
                parent = null;
            }
        }
    }
    return null;
}
 
Example 13
Source File: PodTemplateStepExecution.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the current Job is permitted to use the cloud.
 *
 * @param run
 * @param kubernetesCloud
 * @throws AbortException
 *             in case the Job has not been authorized to use the
 *             kubernetesCloud
 */
private void checkAccess(Run<?, ?> run, KubernetesCloud kubernetesCloud) throws AbortException {
    Job<?, ?> job = run.getParent(); // Return the associated Job for this Build
    ItemGroup<?> parent = job.getParent(); // Get the Parent of the Job (which might be a Folder)

    Set<String> allowedClouds = new HashSet<>();
    KubernetesFolderProperty.collectAllowedClouds(allowedClouds, parent);
    if (!allowedClouds.contains(kubernetesCloud.name)) {
        throw new AbortException(String.format("Not authorized to use Kubernetes cloud: %s", step.getCloud()));
    }
}
 
Example 14
Source File: GerritEnvironmentContributor.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void buildEnvironmentFor(
    @Nonnull Job j, @Nonnull EnvVars envs, @Nonnull TaskListener listener)
    throws IOException, InterruptedException {
  ItemGroup jobParent = j.getParent();
  if (!(jobParent instanceof WorkflowMultiBranchProject)) {
    return;
  }

  WorkflowMultiBranchProject multiBranchProject = (WorkflowMultiBranchProject) jobParent;
  List<BranchSource> sources = multiBranchProject.getSources();
  if (sources.isEmpty() || !(sources.get(0).getSource() instanceof GerritSCMSource)) {
    return;
  }

  WorkflowJob workflowJob = (WorkflowJob) j;

  GerritSCMSource gerritSCMSource =
      (GerritSCMSource) multiBranchProject.getSources().get(0).getSource();
  GerritURI gerritURI = gerritSCMSource.getGerritURI();

  envs.put("GERRIT_CREDENTIALS_ID", gerritSCMSource.getCredentialsId());
  envs.put("GERRIT_PROJECT", gerritURI.getProject());
  try {
    envs.put("GERRIT_API_URL", gerritURI.getApiURI().toString());
  } catch (URISyntaxException e) {
    throw new IOException("Unable to get Gerrit API URL from " + gerritURI, e);
  }
  if (Boolean.TRUE.equals(gerritSCMSource.getInsecureHttps())) {
    envs.put("GERRIT_API_INSECURE_HTTPS", "true");
  }

  String displayName = workflowJob.getDisplayName();
  Matcher matcher = REF_PATTERN.matcher(displayName);
  if (matcher.find()) {
    int patchSetNum = Integer.parseInt(matcher.group("patchSet"));

    Optional<ChangeInfo> changeInfo =
        gerritSCMSource.getChangeInfo(Integer.parseInt(matcher.group("changeNum")));
    changeInfo.ifPresent(
        (change) -> {
          publishChangeDetails(
              envs,
              matcher.group("changeNum"),
              matcher.group("patchSet"),
              patchSetNum,
              change,
              gerritURI);
        });
  }
}