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

The following examples show how to use hudson.model.Job#getAction() . 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: BlueJiraIssue.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public Collection<BlueIssue> getIssues(Job job) {
    JiraSite jiraSite = JiraSite.get(job);
    if (jiraSite == null) {
        return null;
    }
    JiraJobAction action = job.getAction(JiraJobAction.class);
    if (action == null) {
        return null;
    }
    BlueIssue issue = BlueJiraIssue.create(jiraSite, action.getIssue());
    if (issue == null) {
        return null;
    }
    return ImmutableList.of(issue);
}
 
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: 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 4
Source File: DeclarativePipelineAnalyticsCheck.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Boolean apply(Item item) {
    if (!(item instanceof MultiBranchProject)) {
        return false;
    }
    MultiBranchProject project = (MultiBranchProject)item;
    Job resolve = PrimaryBranch.resolve(project);
    return resolve != null && resolve.getAction(DeclarativeJobAction.class) != null;
}
 
Example 5
Source File: ScriptedPipelineAnalyticsCheck.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Boolean apply(Item item) {
    if (!(item instanceof MultiBranchProject)) {
        return false;
    }
    MultiBranchProject project = (MultiBranchProject)item;
    Job resolve = PrimaryBranch.resolve(project);
    return resolve != null && resolve.getAction(DeclarativeJobAction.class) == null;
}
 
Example 6
Source File: ActionableBuilder.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
private void pullRequestActionable() {
    Job job = run.getParent();
    SCMHead head = SCMHead.HeadByItem.findHead(job);
    if (head instanceof ChangeRequestSCMHead) {
        String pronoun = StringUtils.defaultIfBlank(
                head.getPronoun(),
                Messages.Office365ConnectorWebhookNotifier_ChangeRequestPronoun()
        );
        String viewHeader = Messages.Office365ConnectorWebhookNotifier_ViewHeader(pronoun);
        String titleHeader = Messages.Office365ConnectorWebhookNotifier_TitleHeader(pronoun);
        String authorHeader = Messages.Office365ConnectorWebhookNotifier_AuthorHeader(pronoun);

        ObjectMetadataAction oma = job.getAction(ObjectMetadataAction.class);
        if (oma != null) {
            String urlString = oma.getObjectUrl();
            PotentialAction viewPRPotentialAction = new PotentialAction(viewHeader, urlString);
            potentialActions.add(viewPRPotentialAction);
            factsBuilder.addFact(titleHeader, oma.getObjectDisplayName());
        }
        ContributorMetadataAction cma = job.getAction(ContributorMetadataAction.class);
        if (cma != null) {
            String contributor = cma.getContributor();
            String contributorDisplayName = cma.getContributorDisplayName();

            if (StringUtils.isNotBlank(contributor) && StringUtils.isNotBlank(contributorDisplayName)) {
                factsBuilder.addFact(authorHeader, String.format("%s (%s)", contributor, contributorDisplayName));
            } else {
                factsBuilder.addFact(authorHeader, StringUtils.defaultIfBlank(contributor, contributorDisplayName));
            }
        }
    }
}
 
Example 7
Source File: ItemHelpers.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@CheckForNull
public static GitHubBranchRepository getBranchRepositoryFor(Item item) {
    if (item instanceof Job) {
        Job<?, ?> job = (Job) item;
        return job.getAction(GitHubBranchRepository.class);
    }

    return null;
}
 
Example 8
Source File: ItemHelpers.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@CheckForNull
public static GitHubPRRepository getPRRepositoryFor(Item item) {
    if (item instanceof Job) {
        Job<?, ?> job = (Job) item;
        return job.getAction(GitHubPRRepository.class);
    }

    return null;
}
 
Example 9
Source File: AbstractPRTest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
public void basicTest(Job job) throws Exception {
    // fails with workflow
    if (job instanceof FreeStyleProject || job instanceof MatrixProject) {
        jRule.configRoundtrip(job); // activate trigger
    }

    // update trigger (maybe useless)
    GitHubPRTrigger trigger = ghPRTriggerFromJob(job);
    if (job instanceof WorkflowJob) {
        trigger.start(job, true); // hack configRountrip that doesn't work with workflow
    }

    await().pollInterval(3, TimeUnit.SECONDS)
            .timeout(100, SECONDS)
            .until(ghTriggerRunAndEnd(trigger));

    jRule.waitUntilNoActivity();

    assertThat(job.getLastBuild(), is(nullValue()));

    GitHubPRRepository ghPRRepository = job.getAction(GitHubPRRepository.class);
    assertThat("Action storage should be available", ghPRRepository, notNullValue());

    Map<Integer, GitHubPRPullRequest> pulls = ghPRRepository.getPulls();
    assertThat("Action storage should be empty", pulls.entrySet(), Matchers.hasSize(0));

    final GHPullRequest pullRequest1 = ghRule.getGhRepo().createPullRequest("title with \"quote\" text",
            "branch-1", "master", "body");

    await().pollInterval(2, SECONDS)
            .timeout(100, SECONDS)
            .until(ghPRAppeared(pullRequest1));

    await().pollInterval(3, TimeUnit.SECONDS)
            .timeout(100, SECONDS)
            .until(ghTriggerRunAndEnd(trigger));

    jRule.waitUntilNoActivity();

    // refresh objects
    ghPRRepository = job.getAction(GitHubPRRepository.class);
    assertThat("Action storage should be available", ghPRRepository, notNullValue());

    pulls = ghPRRepository.getPulls();
    assertThat("Pull request 1 should appear in action storage", pulls.entrySet(), Matchers.hasSize(1));

    jRule.assertBuildStatusSuccess(job.getLastBuild());
    assertThat(job.getBuilds().size(), is(1));

    // now push changes that should trigger again
    ghRule.commitFileToBranch(BRANCH1, BRANCH1 + ".file2", "content", "commit 2 for " + BRANCH1);

    trigger.doRun(null);

    await().pollInterval(5, TimeUnit.SECONDS)
            .timeout(100, SECONDS)
            .until(ghTriggerRunAndEnd(trigger));

    jRule.waitUntilNoActivity();

    // refresh objects
    ghPRRepository = job.getAction(GitHubPRRepository.class);
    assertThat("Action storage should be available", ghPRRepository, notNullValue());

    pulls = ghPRRepository.getPulls();
    assertThat("Pull request 1 should appear in action storage", pulls.entrySet(), Matchers.hasSize(1));

    jRule.assertBuildStatusSuccess(job.getLastBuild());
    assertThat(job.getBuilds().size(), is(2));
}
 
Example 10
Source File: BranchITest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
public void smokeTest(Job<?, ?> job) throws Exception {
    GitHubBranchTrigger trigger;
    // fails with workflow
    if (job instanceof FreeStyleProject || job instanceof MatrixProject) {
        jRule.configRoundtrip(job); // activate trigger
        trigger = ghBranchTriggerFromJob(job);
    } else {
        trigger = ghBranchTriggerFromJob(job);
        trigger.start(job, true);
    }

    await().pollInterval(3, TimeUnit.SECONDS)
            .timeout(100, SECONDS)
            .until(ghTriggerRunAndEnd(trigger));

    jRule.waitUntilNoActivity();

    assertThat(job.getBuilds(), hasSize(3));

    GitHubBranchRepository ghRepository = job.getAction(GitHubBranchRepository.class);
    assertThat("Action storage should be available", ghRepository, notNullValue());

    Map<String, GitHubBranch> branches = ghRepository.getBranches();

    assertThat("Action storage should not to be empty", branches.entrySet(), Matchers.hasSize(3));


    ghRule.commitFileToBranch("branch-4", "someFile", "content", "With this magessage");
    await().pollInterval(3, TimeUnit.SECONDS)
            .timeout(100, SECONDS)
            .until(ghTriggerRunAndEnd(trigger));

    jRule.waitUntilNoActivity();

    // refresh objects
    ghRepository = job.getAction(GitHubBranchRepository.class);
    assertThat("Action storage should be available", ghRepository, notNullValue());

    branches = ghRepository.getBranches();
    assertThat(branches.entrySet(), Matchers.hasSize(4));

    assertThat(job.getBuilds().size(), is(4));

    jRule.assertBuildStatusSuccess(job.getLastBuild());
}