org.jenkinsci.plugins.displayurlapi.DisplayURLProvider Java Examples

The following examples show how to use org.jenkinsci.plugins.displayurlapi.DisplayURLProvider. 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: GitHubNotificationContext.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * Retrieves default URL
 * @param listener Listener for the build, if any
 * @return Default notification URL backref
 * @since 2.3.2
 */
public String getDefaultUrl(TaskListener listener) {
    String url = null;
    try {
        if (null != build) {
            url = DisplayURLProvider.get().getRunURL(build);
        }
        else if (null != job) {
            url = DisplayURLProvider.get().getJobURL(job);
        }
    } catch (IllegalStateException e) {
        listener.getLogger().println(
                "Can not determine Jenkins root URL. Commit status notifications are sent without URL "
                        + "until a root URL is"
                        + " configured in Jenkins global configuration.");
    }
    return url;
}
 
Example #2
Source File: AbstractWebHookTriggerHandler.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
private void setCommitStatusPendingIfNecessary(Job<?, ?> job, H hook) {
    String buildName = PendingBuildsHandler.resolvePendingBuildName(job);
    if (StringUtils.isNotBlank(buildName)) {
        GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
        BuildStatusUpdate buildStatusUpdate = retrieveBuildStatusUpdate(hook);
        try {
            if (client == null) {
                LOGGER.log(Level.SEVERE, "No GitLab connection configured");
            } else {
                String ref = StringUtils.removeStart(buildStatusUpdate.getRef(), "refs/tags/");
                String targetUrl = DisplayURLProvider.get().getJobURL(job);
                client.changeBuildStatus(buildStatusUpdate.getProjectId(), buildStatusUpdate.getSha(),
                    BuildState.pending, ref, buildName, targetUrl, BuildState.pending.name());
            }
        } catch (WebApplicationException | ProcessingException e) {
            LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
        }
    }
}
 
Example #3
Source File: CommitStatusUpdaterTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testTagEvent() {
    causeData = causeData()
        .withActionType(CauseData.ActionType.TAG_PUSH)
        .withSourceProjectId(PROJECT_ID)
        .withTargetProjectId(PROJECT_ID)
        .withBranch("refs/tags/3.0.0")
        .withSourceBranch("refs/tags/3.0.0")
        .withUserName("")
        .withSourceRepoHomepage("https://gitlab.org/test")
        .withSourceRepoName("test")
        .withSourceNamespace("test-namespace")
        .withSourceRepoUrl("[email protected]:test.git")
        .withSourceRepoSshUrl("[email protected]:test.git")
        .withSourceRepoHttpUrl("https://gitlab.org/test.git")
        .withMergeRequestTitle("Test")
        .withMergeRequestId(1)
        .withMergeRequestIid(1)
        .withTargetBranch("master")
        .withTargetRepoName("test")
        .withTargetNamespace("test-namespace")
        .withTargetRepoSshUrl("[email protected]:test.git")
        .withTargetRepoHttpUrl("https://gitlab.org/test.git")
        .withTriggeredByUser("test")
        .withLastCommit(REVISION)
        .withTargetProjectUrl("https://gitlab.org/test")
        .build();

    when(build.getCause(GitLabWebHookCause.class)).thenReturn(gitlabCause);
    when(gitlabCause.getData()).thenReturn(causeData);

    CommitStatusUpdater.updateCommitStatus(build, taskListener, BuildState.success, STAGE);

    verify(client).changeBuildStatus(Integer.toString(PROJECT_ID), REVISION, BuildState.success, "3.0.0", STAGE, DisplayURLProvider.get().getRunURL(build), BuildState.success.name());
}
 
Example #4
Source File: ActionableBuilder.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
private void buildViewBuild() {
    String urlToJob = DisplayURLProvider.get().getRunURL(run);
    String build = Messages.Office365ConnectorWebhookNotifier_BuildPronoun();
    String viewHeader = Messages.Office365ConnectorWebhookNotifier_ViewHeader(build);

    potentialActions.add(new PotentialAction(viewHeader, urlToJob));
}
 
Example #5
Source File: ActionableBuilderTest.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    run = mock(Run.class);
    taskListener = mock(TaskListener.class);

    factsBuilder = new FactsBuilder(run, taskListener);
    actionableBuilder = new ActionableBuilder(run, factsBuilder);

    DisplayURLProvider displayURLProvider = mock(DisplayURLProvider.class);
    when(displayURLProvider.getRunURL(run)).thenReturn(JOB_URL);

    mockStatic(DisplayURLProvider.class);
    when(DisplayURLProvider.get()).thenReturn(displayURLProvider);
}
 
Example #6
Source File: CommitStatusUpdaterTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void buildStateUpdateTestSpecificConnectionSpecificBuild() {
    ArrayList builds = new ArrayList();
    builds.add(new GitLabBranchBuild(Integer.toString(PROJECT_ID), REVISION));
    CommitStatusUpdater.updateCommitStatus(build, taskListener, BuildState.success, STAGE, builds, connection);

    verify(client).changeBuildStatus(Integer.toString(PROJECT_ID), REVISION, BuildState.success, null, STAGE, DisplayURLProvider.get().getRunURL(build), BuildState.success.name());
}
 
Example #7
Source File: CommitStatusUpdaterTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void buildStateUpdateTestSpecificBuild() {
    ArrayList builds = new ArrayList();
    builds.add(new GitLabBranchBuild(Integer.toString(PROJECT_ID), REVISION));
    CommitStatusUpdater.updateCommitStatus(build, taskListener, BuildState.success, STAGE, builds, null);

    verify(client).changeBuildStatus(Integer.toString(PROJECT_ID), REVISION, BuildState.success, null, STAGE, DisplayURLProvider.get().getRunURL(build), BuildState.success.name());
}
 
Example #8
Source File: GitLabPipelineStatusNotifier.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
private static String getRootUrl(Run<?, ?> build) {
    try {
        return DisplayURLProvider.get().getRunURL(build);
    } catch (IllegalStateException e) {
        return "";
    }
}
 
Example #9
Source File: OpenMergeRequestPushHookTriggerHandler.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void setCommitStatusPendingIfNecessary(Job<?, ?> job, Integer projectId, String commit, String ref) {
    String buildName = PendingBuildsHandler.resolvePendingBuildName(job);
    if (StringUtils.isNotBlank(buildName)) {
        GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
        try {
            String fixedTagRef = StringUtils.removeStart(ref, "refs/tags/");
            String targetUrl = DisplayURLProvider.get().getJobURL(job);
            client.changeBuildStatus(projectId, commit, BuildState.pending, fixedTagRef, buildName, targetUrl, BuildState.pending.name());
        } catch (WebApplicationException | ProcessingException e) {
            LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
        }
    }
}
 
Example #10
Source File: PendingBuildsHandler.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void setCommitStatusCancelledIfNecessary(CauseData causeData, Job<?, ?> job) {
    String buildName = resolvePendingBuildName(job);
    if (StringUtils.isBlank(buildName)) {
        return;
    }
    String targetUrl = DisplayURLProvider.get().getJobURL(job);
    GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
    String ref = StringUtils.removeStart(causeData.getSourceBranch(), "refs/tags/");
    try {
        client.changeBuildStatus(causeData.getSourceProjectId(), causeData.getLastCommit(), BuildState.canceled,
            ref, buildName, targetUrl, BuildState.canceled.name());
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);
    }
}
 
Example #11
Source File: GitLabCommitStatusPublisherTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private HttpRequest prepareUpdateCommitStatus(final String apiLevel, String projectName, Run<?, ?> build, BuildState state) throws UnsupportedEncodingException {
    return request()
            .withPath("/gitlab/api/" + apiLevel + "/projects/" + URLEncoder.encode(projectName, "UTF-8") + "/statuses/" + SHA1)
            .withMethod("POST")
            .withHeader("PRIVATE-TOKEN", "secret")
            .withBody("state=" + URLEncoder.encode(state.name(), "UTF-8") + "&context=jenkins&" + "target_url=" + URLEncoder.encode(DisplayURLProvider.get().getRunURL(build), "UTF-8") + "&description=" + URLEncoder.encode(state.name(), "UTF-8"));
}
 
Example #12
Source File: CommitStatusUpdater.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
private static String getBuildUrl(Run<?, ?> build) {
    return DisplayURLProvider.get().getRunURL(build);
}
 
Example #13
Source File: CommitStatusUpdaterTest.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void buildStateUpdateTestSpecificConnection() {
    CommitStatusUpdater.updateCommitStatus(build, taskListener, BuildState.success, STAGE,null, connection);

    verify(client).changeBuildStatus(Integer.toString(PROJECT_ID), REVISION, BuildState.success, null, STAGE, DisplayURLProvider.get().getRunURL(build), BuildState.success.name());
}
 
Example #14
Source File: CommitStatusUpdaterTest.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void buildStateUpdateTest() {
	CommitStatusUpdater.updateCommitStatus(build, taskListener, BuildState.success, STAGE);

	verify(client).changeBuildStatus(Integer.toString(PROJECT_ID), REVISION, BuildState.success, null, STAGE, DisplayURLProvider.get().getRunURL(build), BuildState.success.name());
}
 
Example #15
Source File: BitbucketBuildStatusHelper.java    From bitbucket-build-status-notifier-plugin with MIT License 4 votes vote down vote up
public static String buildUrlFromBuild(Run<?, ?> build) {
    return DisplayURLProvider.get().getRunURL(build);
}
 
Example #16
Source File: GitHubStatusNotificationStep.java    From pipeline-githubnotify-step-plugin with MIT License 4 votes vote down vote up
private String getTargetUrl() {
    return (step.getTargetUrl() == null || step.getTargetUrl().isEmpty()) ?  DisplayURLProvider.get().getRunURL(run) : step.getTargetUrl();
}
 
Example #17
Source File: AbstractTest.java    From office-365-connector-plugin with Apache License 2.0 4 votes vote down vote up
protected void mockDisplayURLProvider(String jobName, int jobNumber) {
    mockStatic(DisplayURLProvider.class);
    when(DisplayURLProvider.get()).thenReturn(new ClassicDisplayURLProviderBuilder(jobName, jobNumber));
}
 
Example #18
Source File: PullRequestIT.java    From office-365-connector-plugin with Apache License 2.0 4 votes vote down vote up
private void mockDisplayURLProvider() {
    mockStatic(DisplayURLProvider.class);
    when(DisplayURLProvider.get()).thenReturn(
            new ClassicDisplayURLProviderBuilder(JOB_NAME, BUILD_NUMBER, URL_TEMPLATE));
}
 
Example #19
Source File: GithubBuildStatusGraphListener.java    From github-autostatus-plugin with MIT License 4 votes vote down vote up
/**
 * Checks whether the current build meets our requirements for providing
 * status, and adds a BuildStatusAction to the build if so.
 *
 * @param flowNode node of a workflow
 */
private static void checkEnableBuildStatus(FlowNode flowNode) {
    FlowExecution exec = flowNode.getExecution();
    try {
        BuildStatusAction buildStatusAction = buildStatusActionFor(exec);

        Run<?, ?> run = runFor(exec);
        if (null == run) {
            log(Level.WARNING, "Could not find Run - status will not be provided build");
            return;
        }

        // Declarative pipeline jobs come with a nice execution model, which allows you
        // to get all of the stages at once at the beginning of the job.
        // Older scripted pipeline jobs do not, so we have to add them one at a
        // time as we discover them.
        List<BuildStage> stageNames = getDeclarativeStages(run);
        boolean isDeclarativePipeline = stageNames != null;

        String targetUrl;
        try {
            targetUrl = DisplayURLProvider.get().getRunURL(run);
        } catch (Exception e) {
            targetUrl = "";
        }

        if (isDeclarativePipeline && buildStatusAction != null) {
            buildStatusAction.connectNotifiers(run, targetUrl);
            return;
        }
        if (stageNames == null) {
            ArrayList<BuildStage> stageNameList = new ArrayList<>();
            stageNameList.add(new BuildStage(flowNode.getDisplayName()));
            stageNames = stageNameList;
        }

        if (buildStatusAction == null) {
            buildStatusAction = BuildStatusAction.newAction(run, targetUrl, stageNames);
            buildStatusAction.setIsDeclarativePipeline(isDeclarativePipeline);

            run.addAction(buildStatusAction);
        } else {
            buildStatusAction.addBuildStatus(flowNode.getDisplayName());
        }
    } catch (Exception ex) {
        try {
            exec.getOwner().getListener().getLogger().println(ex.toString());
        } catch (IOException ex1) {
            Logger.getLogger(GithubBuildStatusGraphListener.class.getName()).log(Level.SEVERE, null, ex1);
        }
        Logger.getLogger(GithubBuildStatusGraphListener.class.getName()).log(Level.SEVERE, null, ex);
    }
}