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

The following examples show how to use hudson.model.Job#getProperty() . 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: HubotSite.java    From hubot-steps-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the effective {@link HubotSite} associated with the given project.
 *
 * @return null if no such was found.
 */
public static HubotSite get(Job<?, ?> job, final TaskListener listener) {
  HubotJobProperty jpp = job.getProperty(HubotJobProperty.class);
  boolean enableNotifications = false;
  String siteName = null;

  if (jpp != null) {
    enableNotifications = jpp.isEnableNotifications();
    siteName = Util.fixEmpty(jpp.getSiteNames());
  }

  if (enableNotifications) {
    return get(job, listener, siteName);
  }

  return null;
}
 
Example 2
Source File: JobHelper.java    From github-integration-plugin with MIT License 6 votes vote down vote up
/**
 * @see jenkins.model.ParameterizedJobMixIn#getDefaultParametersValues()
 */
public static List<ParameterValue> getDefaultParametersValues(Job<?, ?> job) {
    ParametersDefinitionProperty paramDefProp = job.getProperty(ParametersDefinitionProperty.class);
    List<ParameterValue> defValues = new ArrayList<>();

    /*
     * This check is made ONLY if someone will call this method even if isParametrized() is false.
     */
    if (isNull(paramDefProp)) {
        return defValues;
    }

    /* Scan for all parameter with an associated default values */
    for (ParameterDefinition paramDefinition : paramDefProp.getParameterDefinitions()) {
        ParameterValue defaultValue = paramDefinition.getDefaultParameterValue();

        if (defaultValue != null) {
            defValues.add(defaultValue);
        }
    }

    return defValues;
}
 
Example 3
Source File: GitHubTrigger.java    From github-integration-plugin with MIT License 6 votes vote down vote up
public GitHubRepositoryName getRepoFullName(Job item) {
    Job<?, ?> job = (Job) item;
    if (isNull(repoName)) {
        checkNotNull(job, "job object is null, race condition?");
        GithubProjectProperty ghpp = job.getProperty(GithubProjectProperty.class);

        checkNotNull(ghpp, "GitHub project property is not defined. Can't setup GitHub trigger for job %s",
                job.getName());
        checkNotNull(ghpp.getProjectUrl(), "A GitHub project url is required");

        GitHubRepositoryName repo = GitHubRepositoryName.create(ghpp.getProjectUrl().baseUrl());

        checkNotNull(repo, "Invalid GitHub project url: %s", ghpp.getProjectUrl().baseUrl());

        setRepoName(repo);
    }

    return repoName;
}
 
Example 4
Source File: GithubWebhook.java    From DotCi with MIT License 6 votes vote down vote up
private List<ParameterValue> getParametersValues(final Job job, final String branch) {
    final ParametersDefinitionProperty paramDefProp = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);
    final ArrayList<ParameterValue> defValues = new ArrayList<>();

    for (final ParameterDefinition paramDefinition : paramDefProp.getParameterDefinitions()) {
        if ("BRANCH".equals(paramDefinition.getName())) {
            final StringParameterValue branchParam = new StringParameterValue("BRANCH", branch);
            defValues.add(branchParam);
        } else {
            final ParameterValue defaultValue = paramDefinition.getDefaultParameterValue();
            if (defaultValue != null)
                defValues.add(defaultValue);
        }
    }

    return defValues;
}
 
Example 5
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 6
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 7
Source File: GitLabSCMItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private boolean updateProperties(Job<?, ?> job, SCMSourceOwner sourceOwner, String sourceId) {
    SCMSource source = sourceOwner.getSCMSource(sourceId);
    if (source instanceof GitLabSCMSource) {
        String connectionName = ((GitLabSCMSource) source).getSourceSettings().getConnectionName();
        GitLabConnectionProperty property = job.getProperty(GitLabConnectionProperty.class);
        if (property == null || !connectionName.equals(property.getGitLabConnection())) {
            updateProperties(job, connectionName);
            return true;
        }
    }

    return false;
}
 
Example 8
Source File: AbstractPipelineImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
public static List<Object> getParameterDefinitions(Job job){
    ParametersDefinitionProperty pp = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);
    List<Object> pds = new ArrayList<>();
    if(pp != null){
        for(ParameterDefinition pd : pp.getParameterDefinitions()){
            pds.add(pd);
        }
    }
    return pds;
}
 
Example 9
Source File: Office365ConnectorWebhookNotifier.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
private static List<Webhook> extractWebhooks(Job job) {
    WebhookJobProperty property = (WebhookJobProperty) job.getProperty(WebhookJobProperty.class);
    if (property != null && property.getWebhooks() != null) {
        return property.getWebhooks();
    }
    return Collections.emptyList();
}
 
Example 10
Source File: GitLabConnectionProperty.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
public static GitLabClient getClient(@NotNull Run<?, ?> build) {
    Job<?, ?> job = build.getParent();
    if(job != null) {
        final GitLabConnectionProperty connectionProperty = job.getProperty(GitLabConnectionProperty.class);
        if (connectionProperty != null) {
            return connectionProperty.getClient();
        }
    }
    
    return null;
}
 
Example 11
Source File: OpenMergeRequestPushHookTriggerHandler.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
	try {
        if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
            ParameterizedJob project = (ParameterizedJobMixIn.ParameterizedJob) job;
            GitLabConnectionProperty property = job.getProperty(GitLabConnectionProperty.class);
            Collection<Trigger> triggerList = project.getTriggers().values();
            for (Trigger t : triggerList) {
            	if (t instanceof GitLabPushTrigger) {
            		final GitLabPushTrigger trigger = (GitLabPushTrigger) t;
                    Integer projectId = hook.getProjectId();
                    if (property != null && property.getClient() != null && projectId != null && trigger != null) {
                        GitLabClient client = property.getClient();
                        for (MergeRequest mergeRequest : getOpenMergeRequests(client, projectId.toString())) {
                            if (mergeRequestLabelFilter.isMergeRequestAllowed(mergeRequest.getLabels())) {
                            	handleMergeRequest(job, hook, ciSkip, branchFilter, client, mergeRequest);
                            }
                        }
                    }
            	}
            }

        } else {
        	LOGGER.log(Level.FINE, "Not a ParameterizedJob: {0}",LoggerUtil.toArray(job.getClass().getName()));
        }
    } catch (WebApplicationException | ProcessingException e) {
        LOGGER.log(Level.WARNING, "Failed to communicate with gitlab server to determine if this is an update for a merge request: " + e.getMessage(), e);
    }
}
 
Example 12
Source File: PipelineHookTriggerHandlerImpl.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(Job<?, ?> job, PipelineHook hook, boolean ciSkip, BranchFilter branchFilter, MergeRequestLabelFilter mergeRequestLabelFilter) {
    PipelineEventObjectAttributes objectAttributes = hook.getObjectAttributes();
    try {
        if (job instanceof AbstractProject<?, ?>) {
            GitLabConnectionProperty property = job.getProperty(GitLabConnectionProperty.class);

            if (property != null && property.getClient() != null) {
                GitLabClient client = property.getClient();
                com.dabsquared.gitlabjenkins.gitlab.api.model.Project projectForName = client.getProject(hook.getProject().getPathWithNamespace());
                hook.setProjectId(projectForName.getId());
            }
        }
    } catch (WebApplicationException e) {
        LOGGER.log(Level.WARNING, "Failed to communicate with gitlab server to determine project id: " + e.getMessage(), e);
    }
    if (allowedStates.contains(objectAttributes.getStatus()) && !isLastAlreadyBuild(job,hook)) {
        if (ciSkip && isCiSkip(hook)) {
            LOGGER.log(Level.INFO, "Skipping due to ci-skip.");
            return;
        }
        //we do not call super here, since we do not want the status to be changed
        //in case of pipeline events that could lead to a deadlock
        String sourceBranch = getSourceBranch(hook);
        String targetBranch = getTargetBranch(hook);
        if (branchFilter.isBranchAllowed(sourceBranch, targetBranch)) {
            LOGGER.log(Level.INFO, "{0} triggered for {1}.", LoggerUtil.toArray(job.getFullName(), getTriggerType()));

            super.scheduleBuild(job, createActions(job, hook));
        } else {
            LOGGER.log(Level.INFO, "branch {0} is not allowed", sourceBranch + " or " + targetBranch);
        }

    }
}
 
Example 13
Source File: ProjectLabelsProvider.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private List<String> getProjectLabels(Job<?, ?> project) {
    final URIish sourceRepository = getSourceRepoURLDefault(project);
    GitLabConnectionProperty connectionProperty = project.getProperty(GitLabConnectionProperty.class);
    if (connectionProperty != null && connectionProperty.getClient() != null) {
        return GitLabProjectLabelsService.instance().getLabels(connectionProperty.getClient(), sourceRepository.toString());
    } else {
        LOGGER.log(Level.WARNING, "getProjectLabels: gitlabHostUrl hasn't been configured globally. Job {0}.", project.getFullName());
        return Collections.emptyList();
    }
}
 
Example 14
Source File: ProjectBranchesProvider.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private List<String> getProjectBranches(Job<?, ?> project) {
    final URIish sourceRepository = getSourceRepoURLDefault(project);
    GitLabConnectionProperty connectionProperty = project.getProperty(GitLabConnectionProperty.class);
    if (connectionProperty != null && connectionProperty.getClient() != null) {
        return GitLabProjectBranchesService.instance().getBranches(connectionProperty.getClient(), sourceRepository.toString());
    } else {
        LOGGER.log(Level.WARNING, "getProjectBranches: gitlabHostUrl hasn't been configured globally. Job {0}.", project.getFullName());
        return Collections.emptyList();
    }
}
 
Example 15
Source File: GitHubPRRepositoryFactory.java    From github-integration-plugin with MIT License 4 votes vote down vote up
@Nonnull
private static GitHubPRRepository forProject(Job<?, ?> job) throws IOException {
    XmlFile configFile = new XmlFile(new File(job.getRootDir(), GitHubPRRepository.FILE));

    GitHubPRTrigger trigger = ghPRTriggerFromJob(job);
    requireNonNull(trigger, "Can't extract PR trigger from " + job.getFullName());

    final GitHubRepositoryName repoFullName = trigger.getRepoFullName(job); // ask with job because trigger may not yet be started
    GithubProjectProperty property = job.getProperty(GithubProjectProperty.class);
    String githubUrl = property.getProjectUrl().toString();

    boolean save = false;
    GitHubPRRepository localRepository;
    if (configFile.exists()) {
        try {
            localRepository = (GitHubPRRepository) configFile.read();
        } catch (IOException e) {
            LOGGER.info("Can't read saved repository, re-creating new one", e);
            localRepository = new GitHubPRRepository(repoFullName.toString(), new URL(githubUrl));
            save = true;
        }
    } else {
        localRepository = new GitHubPRRepository(repoFullName.toString(), new URL(githubUrl));
        save = true;
    }

    localRepository.setJob(job);
    localRepository.setConfigFile(configFile);

    GitHubPRTrigger.DescriptorImpl prTriggerDescriptor = GitHubPRTrigger.DescriptorImpl.get();
    if (prTriggerDescriptor.isActualiseOnFactory()) {
        try {
            localRepository.actualise(trigger.getRemoteRepository(), TaskListener.NULL);
            save = true;
        } catch (Throwable ignore) {
            //silently try actualise
        }
    }

    if (save) localRepository.save();

    return localRepository;
}
 
Example 16
Source File: GitHubBranchRepositoryFactory.java    From github-integration-plugin with MIT License 4 votes vote down vote up
@Nonnull
private static GitHubBranchRepository forProject(@Nonnull Job<?, ?> job) throws IOException {
    XmlFile configFile = new XmlFile(new File(job.getRootDir(), GitHubBranchRepository.FILE));

    GitHubBranchTrigger trigger = ghBranchTriggerFromJob(job);
    requireNonNull(trigger, "Can't extract Branch trigger from " + job.getFullName());

    final GitHubRepositoryName repoFullName = trigger.getRepoFullName(job); // ask with job because trigger may not yet be started
    GithubProjectProperty property = job.getProperty(GithubProjectProperty.class);
    String githubUrl = property.getProjectUrl().toString();

    GitHubBranchRepository localRepository;
    boolean created = false;
    if (configFile.exists()) {
        try {
            localRepository = (GitHubBranchRepository) configFile.read();
        } catch (IOException e) {
            LOGGER.info("Can't read saved repository, re-creating new one", e);
            localRepository = new GitHubBranchRepository(repoFullName.toString(), new URL(githubUrl));
            created = true;
        }
    } else {
        LOGGER.info("Creating new Branch Repository for '{}'", job.getFullName());
        localRepository = new GitHubBranchRepository(repoFullName.toString(), new URL(githubUrl));
        created = true;
    }

    // set transient cached fields
    localRepository.setJob(job);
    localRepository.setConfigFile(configFile);


    GitHubPRTrigger.DescriptorImpl prTriggerDescriptor = GitHubPRTrigger.DescriptorImpl.get();
    if (prTriggerDescriptor.isActualiseOnFactory()) {
        try {
            localRepository.actualise(trigger.getRemoteRepository(), TaskListener.NULL);
            created = true;
        } catch (Throwable ignore) {
            //silently try actualise
        }
    }

    if (created) localRepository.save();

    return localRepository;
}