Skip to content

Commit

Permalink
Merge StashPullRequestsBuilder into StashRepository
Browse files Browse the repository at this point in the history
StashPullRequestsBuilder was a thin layer between StashBuildTrigger and
StashRepository without a well defined responsibility. Make
StashBuildTrigger create StashRepository object directly.

Move run() to StashRepository as pollRepository().
  • Loading branch information
proski authored and jakub-bochenski committed Aug 5, 2019
1 parent fb217ed commit b390814
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onCompleted(Run<?, ?> run, @Nonnull TaskListener listener) {
return;
}

StashRepository repository = trigger.getBuilder().getRepository();
StashRepository repository = trigger.getRepository();
Result result = run.getResult();
// Note: current code should no longer use "new JenkinsLocationConfiguration()"
// as only one instance per runtime is really supported by the current core.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class StashBuildTrigger extends Trigger<Job<?, ?>> {
private boolean onlyBuildOnComment;
private String ciBuildPhrases = DescriptorImpl.DEFAULT_CI_BUILD_PHRASES;

private transient StashPullRequestsBuilder stashPullRequestsBuilder;
private transient StashRepository stashRepository;

@Extension public static final DescriptorImpl descriptor = new DescriptorImpl();

Expand Down Expand Up @@ -230,15 +230,15 @@ public void start(Job<?, ?> job, boolean newInstance) {
super.start(job, newInstance);
try {
Objects.requireNonNull(job, "job is null");
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(job, this);
this.stashRepository = new StashRepository(job, this);
} catch (NullPointerException e) {
logger.log(Level.SEVERE, "Can't start trigger", e);
return;
}
}

public StashPullRequestsBuilder getBuilder() {
return this.stashPullRequestsBuilder;
public StashRepository getRepository() {
return this.stashRepository;
}

@Override
Expand All @@ -253,13 +253,13 @@ public void run() {
return;
}

stashPullRequestsBuilder.run();
stashRepository.pollRepository();
getDescriptor().save();
}

@Override
public void stop() {
stashPullRequestsBuilder = null;
stashRepository = null;
super.stop();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,10 @@ private boolean isSkipBuild(String pullRequestContentString) {
private boolean isPhrasesContain(String text, String phrase) {
return text != null && text.toLowerCase().contains(phrase.trim().toLowerCase());
}

public void pollRepository() {
logger.info(format("Build Start (%s).", job.getName()));
Collection<StashPullRequestResponseValue> targetPullRequests = getTargetPullRequests();
addFutureBuildTasks(targetPullRequests);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public void before() throws Exception {

private StashRepository setup_onCompleted(boolean mergeOnSuccess) throws Exception {
StashBuildTrigger trigger = mock(StashBuildTrigger.class);
StashPullRequestsBuilder builder = mock(StashPullRequestsBuilder.class);
StashRepository repository = mock(StashRepository.class);
FreeStyleProject project = spy(jenkinsRule.createFreeStyleProject("TestProject"));

Expand All @@ -93,8 +92,7 @@ private StashRepository setup_onCompleted(boolean mergeOnSuccess) throws Excepti
when(build.getCause(eq(StashCause.class))).thenReturn(stashCause);
when(build.getParent()).thenReturn(project);
when(project.getTriggers()).thenReturn(triggerMap);
when(trigger.getBuilder()).thenReturn(builder);
when(builder.getRepository()).thenReturn(repository);
when(trigger.getRepository()).thenReturn(repository);
when(build.getResult()).thenReturn(Result.SUCCESS);
when(trigger.getMergeOnSuccess()).thenReturn(mergeOnSuccess);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ public void addFutureBuildTasks_doesnt_schedule_build_if_postPullRequestComment_
assertThat(Jenkins.getInstance().getQueue().getItems(), is(emptyArray()));
}

@Test
public void pollRepository_schedules_build_for_open_pull_request() throws Exception {
StashPullRequestComment response = new StashPullRequestComment();
response.setCommentId(1);

when(trigger.getStashHost()).thenReturn("StashHost");
when(stashApiClient.getPullRequests()).thenReturn(pullRequestList);
when(stashApiClient.getPullRequestComments(any(), any(), any()))
.thenReturn(Collections.emptyList());
when(stashApiClient.postPullRequestComment(any(), any())).thenReturn(response);

stashRepository.pollRepository();

assertThat(Jenkins.getInstance().getQueue().getItems(), is(arrayWithSize(1)));
}

@Test
public void startJob_passes_parameter_with_default_value() {
cause = makeCause(null);
Expand Down

0 comments on commit b390814

Please sign in to comment.