Skip to content

Commit e80a83b

Browse files
feat: use DataBoundSetter
1 parent 2975766 commit e80a83b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/main/java/hudson/plugins/git/GitPublisher.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import org.eclipse.jgit.transport.URIish;
2626
import org.jenkinsci.plugins.gitclient.GitClient;
2727
import org.jenkinsci.plugins.gitclient.PushCommand;
28-
import org.kohsuke.stapler.AncestorInPath;
29-
import org.kohsuke.stapler.DataBoundConstructor;
30-
import org.kohsuke.stapler.QueryParameter;
31-
import org.kohsuke.stapler.StaplerRequest;
28+
import org.kohsuke.stapler.*;
3229

3330
import javax.servlet.ServletException;
3431
import java.io.IOException;
@@ -303,7 +300,7 @@ else if (!tagExists) {
303300
remote = gitSCM.getParamExpandedRepo(environment, remote);
304301
remoteURI = remote.getURIs().get(0);
305302

306-
if (b.isRebaseBeforePush()) {
303+
if (b.getRebaseBeforePush()) {
307304
listener.getLogger().println("Fetch and rebase with " + branchName + " of " + targetRepo);
308305
git.fetch_().from(remoteURI, remote.getFetchRefSpecs()).execute();
309306
if (!git.revParse("HEAD").equals(git.revParse(targetRepo + "/" + branchName))) {
@@ -505,15 +502,20 @@ public static final class BranchToPush extends PushConfig {
505502
public String getBranchName() {
506503
return branchName;
507504
}
508-
public boolean isRebaseBeforePush() {
509-
return rebaseBeforePush;
510-
}
511505

512506
@DataBoundConstructor
513-
public BranchToPush(String targetRepoName, String branchName, boolean rebaseBeforePush) {
507+
public BranchToPush(String targetRepoName, String branchName) {
514508
super(targetRepoName);
515509
this.branchName = Util.fixEmptyAndTrim(branchName);
516-
this.rebaseBeforePush = rebaseBeforePush;
510+
}
511+
512+
@DataBoundSetter
513+
public void setRebaseBeforePush(boolean shouldRebase) {
514+
this.rebaseBeforePush = shouldRebase;
515+
}
516+
517+
public boolean getRebaseBeforePush() {
518+
return rebaseBeforePush;
517519
}
518520

519521
@Extension

src/test/java/hudson/plugins/git/GitPublisherTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void testMergeAndPush() throws Exception {
140140

141141
project.getPublishersList().add(new GitPublisher(
142142
Collections.<TagToPush>emptyList(),
143-
Collections.singletonList(new BranchToPush("origin", "integration", false)),
143+
Collections.singletonList(new BranchToPush("origin", "integration")),
144144
Collections.<NoteToPush>emptyList(),
145145
true, true, false));
146146

@@ -177,7 +177,7 @@ public void testMergeAndPushFF() throws Exception {
177177

178178
project.getPublishersList().add(new GitPublisher(
179179
Collections.<TagToPush>emptyList(),
180-
Collections.singletonList(new BranchToPush("origin", "integration", false)),
180+
Collections.singletonList(new BranchToPush("origin", "integration")),
181181
Collections.<NoteToPush>emptyList(),
182182
true, true, false));
183183

@@ -262,7 +262,7 @@ public void testMergeAndPushNoFF() throws Exception {
262262

263263
project.getPublishersList().add(new GitPublisher(
264264
Collections.<TagToPush>emptyList(),
265-
Collections.singletonList(new BranchToPush("origin", "integration", false)),
265+
Collections.singletonList(new BranchToPush("origin", "integration")),
266266
Collections.<NoteToPush>emptyList(),
267267
true, true, false));
268268

@@ -351,7 +351,7 @@ public void testMergeAndPushFFOnly() throws Exception {
351351

352352
project.getPublishersList().add(new GitPublisher(
353353
Collections.<TagToPush>emptyList(),
354-
Collections.singletonList(new BranchToPush("origin", "integration", false)),
354+
Collections.singletonList(new BranchToPush("origin", "integration")),
355355
Collections.<NoteToPush>emptyList(),
356356
true, true, false));
357357

@@ -454,7 +454,7 @@ public void testPushEnvVarsInRemoteConfig() throws Exception{
454454

455455
project.getPublishersList().add(new GitPublisher(
456456
Collections.singletonList(new TagToPush("$TARGET_NAME", tag_name, "", false, false)),
457-
Collections.singletonList(new BranchToPush("$TARGET_NAME", "$TARGET_BRANCH", false)),
457+
Collections.singletonList(new BranchToPush("$TARGET_NAME", "$TARGET_BRANCH")),
458458
Collections.singletonList(new NoteToPush("$TARGET_NAME", note_content, Constants.R_NOTES_COMMITS, false)),
459459
true, false, true));
460460

@@ -485,7 +485,7 @@ public void testForcePush() throws Exception {
485485

486486
GitPublisher forcedPublisher = new GitPublisher(
487487
Collections.<TagToPush>emptyList(),
488-
Collections.singletonList(new BranchToPush("origin", "otherbranch", false)),
488+
Collections.singletonList(new BranchToPush("origin", "otherbranch")),
489489
Collections.<NoteToPush>emptyList(),
490490
true, true, true);
491491
project.getPublishersList().add(forcedPublisher);
@@ -532,7 +532,7 @@ public void testForcePush() throws Exception {
532532
project.getPublishersList().remove(forcedPublisher);
533533
GitPublisher unforcedPublisher = new GitPublisher(
534534
Collections.<TagToPush>emptyList(),
535-
Collections.singletonList(new BranchToPush("origin", "otherbranch", false)),
535+
Collections.singletonList(new BranchToPush("origin", "otherbranch")),
536536
Collections.<NoteToPush>emptyList(),
537537
true, true, false);
538538
project.getPublishersList().add(unforcedPublisher);
@@ -582,7 +582,7 @@ public void testMergeAndPushWithSkipTagEnabled() throws Exception {
582582

583583
project.getPublishersList().add(new GitPublisher(
584584
Collections.<TagToPush>emptyList(),
585-
Collections.singletonList(new BranchToPush("origin", "integration", false)),
585+
Collections.singletonList(new BranchToPush("origin", "integration")),
586586
Collections.<NoteToPush>emptyList(),
587587
true, true, false));
588588

@@ -613,9 +613,12 @@ public void testRebaseBeforePush() throws Exception {
613613
Collections.<GitSCMExtension>emptyList());
614614
project.setScm(scm);
615615

616+
BranchToPush btp = new BranchToPush("origin", "master");
617+
btp.setRebaseBeforePush(true);
618+
616619
GitPublisher rebasedPublisher = new GitPublisher(
617620
Collections.<TagToPush>emptyList(),
618-
Collections.singletonList(new BranchToPush("origin", "master", true)),
621+
Collections.singletonList(btp),
619622
Collections.<NoteToPush>emptyList(),
620623
true, true, true);
621624
project.getPublishersList().add(rebasedPublisher);
@@ -698,7 +701,7 @@ private void checkEnvVar(FreeStyleProject project, String envName, String envVal
698701
String noteValue = "note for " + envValue;
699702
GitPublisher publisher = new GitPublisher(
700703
Collections.singletonList(new TagToPush("origin", tagNameReference, tagMessageReference, false, true)),
701-
Collections.singletonList(new BranchToPush("origin", envReference, false)),
704+
Collections.singletonList(new BranchToPush("origin", envReference)),
702705
Collections.singletonList(new NoteToPush("origin", noteReference, Constants.R_NOTES_COMMITS, false)),
703706
true, true, true);
704707
assertTrue(publisher.isForcePush());

0 commit comments

Comments
 (0)