GitHubPRStatusBuilder extends Builder implements SimpleBuildStep { @Override public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { final GitHubPRTrigger trigger = ghPRTriggerFromRun(run); if (isNull(trigger)) { return; } GitHubPRCause cause = ghPRCauseFromRun(run); if (isNull(cause)) { return; } try { if (nonNull(statusMessage)) { String url = trigger.getDescriptor().getJenkinsURL() + run.getUrl(); trigger.getRemoteRepository().createCommitStatus( cause.getHeadSha(), GHCommitState.PENDING, url, statusMessage.expandAll(run, listener), run.getParent().getFullName() ); } } catch (Exception e) { listener.getLogger().println("Can't update build description"); LOGGER.error("Can't set commit status", e); } } @Restricted(NoExternalUse.class)  GitHubPRStatusBuilder(); @DataBoundConstructor  GitHubPRStatusBuilder(GitHubPRMessage statusMessage); GitHubPRMessage getStatusMessage(); @Override void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher,
                        @Nonnull TaskListener listener); static final GitHubPRMessage DEFAULT_MESSAGE; }
@Ignore("Can't mock workspace") @Test public void runBuilderWithNullTrigger() throws IOException, InterruptedException { GitHubPRStatusBuilder builder = new GitHubPRStatusBuilder(new GitHubPRMessage(CUSTOM_MESSAGE)); when(build.getWorkspace()).thenReturn(ws); triggerExpectations(null); Assert.assertTrue(builder.perform(build, launcher, listener)); }
@Ignore("Can't mock workspace") @Test public void runBuilderWithNullCause() throws IOException, InterruptedException { GitHubPRStatusBuilder builder = new GitHubPRStatusBuilder(new GitHubPRMessage(CUSTOM_MESSAGE)); triggerExpectations(trigger); when(build.getCause(GitHubPRCause.class)).thenReturn(null); when(build.getWorkspace()).thenReturn(ws); Assert.assertTrue(builder.perform(build, launcher, listener)); }
@Ignore("Can't mock workspace") @Test public void runBuilderWithIOExceptionOnGettingRemoteRepo() throws IOException, InterruptedException { GitHubPRStatusBuilder builder = new GitHubPRStatusBuilder(new GitHubPRMessage(CUSTOM_MESSAGE)); triggerExpectations(trigger); when(build.getCause(GitHubPRCause.class)).thenReturn(cause); doReturn(ws).when(build.getWorkspace()); urlExpectations(); when(trigger.getRemoteRepository()).thenThrow(new IllegalStateException("on getting remote repo")); when(listener.getLogger()).thenReturn(logger); Assert.assertTrue(builder.perform(build, launcher, listener)); }
@Ignore("Can't mock workspace") @Test public void runBuilderWithIOExceptionOnSettingDescription() throws IOException, InterruptedException { when(message.getContent()).thenReturn(CUSTOM_MESSAGE); GitHubPRStatusBuilder builder = new GitHubPRStatusBuilder(message); triggerExpectations(trigger); when(build.getCause(GitHubPRCause.class)).thenReturn(cause); when(build.getWorkspace()).thenReturn(ws); urlExpectations(); when(trigger.getRemoteRepository()).thenReturn(remoteRepository); when(message.expandAll(build, listener)).thenReturn("expanded"); when(build.getProject()).thenReturn(project); when(project.getParent()).thenReturn(itemGroup); when(itemGroup.getFullName()).thenReturn("project full name"); doThrow(new IOException("on setting description")).when(build).setDescription(any(String.class)); Assert.assertTrue(builder.perform(build, launcher, listener)); }