Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Repository branch when using git repo. #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ Jenkins SCM Sync Configuration Plugin
=====================

Read more: [https://wiki.jenkins-ci.org/display/JENKINS/SCM+Sync+configuration+plugin](https://wiki.jenkins-ci.org/display/JENKINS/SCM+Sync+configuration+plugin)

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import hudson.plugins.scm_sync_configuration.model.ScmContext;
import hudson.plugins.scm_sync_configuration.scms.SCM;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.*;
import org.apache.maven.scm.command.add.AddScmResult;
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
Expand Down Expand Up @@ -36,6 +34,7 @@ public class SCMManipulator {
private final ScmManager scmManager;
private ScmRepository scmRepository = null;
private String scmSpecificFilename = null;
private String gitRepositoryBranch = null;

public SCMManipulator(ScmManager _scmManager) {
this.scmManager = _scmManager;
Expand All @@ -50,6 +49,7 @@ public SCMManipulator(ScmManager _scmManager) {
public boolean scmConfigurationSettledUp(ScmContext scmContext, boolean resetScmRepository){
String scmRepositoryUrl = scmContext.getScmRepositoryUrl();
SCM scm = scmContext.getScm();
gitRepositoryBranch = scmContext.getGitRepositoryBranch();
if(scmRepositoryUrl == null || scm == null){
return false;
}
Expand Down Expand Up @@ -91,7 +91,8 @@ public boolean checkout(File checkoutDirectory){
// Checkouting sources
LOGGER.fine("Checking out SCM files into ["+checkoutDirectory.getAbsolutePath()+"] ...");
try {
CheckOutScmResult result = scmManager.checkOut(this.scmRepository, new ScmFileSet(checkoutDirectory));
ScmVersion version = new NewScmVersion(this.gitRepositoryBranch);
CheckOutScmResult result = scmManager.checkOut(this.scmRepository, new ScmFileSet(checkoutDirectory), version);
if(!result.isSuccess()){
LOGGER.severe("[checkout] Error during checkout : "+result.getProviderMessage()+" || "+result.getCommandOutput());
return checkoutOk;
Expand Down Expand Up @@ -250,7 +251,8 @@ public boolean checkinFiles(File scmRoot, String commitMessage){

// Let's commit everything !
try {
CheckInScmResult result = this.scmManager.checkIn(this.scmRepository, fileSet, commitMessage);
ScmVersion version = new NewScmVersion(this.gitRepositoryBranch);
CheckInScmResult result = this.scmManager.checkIn(this.scmRepository, fileSet, version, commitMessage);
if(!result.isSuccess()){
LOGGER.severe("[checkinFiles] Problem during SCM commit : "+result.getCommandOutput());
return checkinOk;
Expand All @@ -273,5 +275,26 @@ public boolean checkinFiles(File scmRoot, String commitMessage){
public String getScmSpecificFilename() {
return scmSpecificFilename;
}
public class NewScmVersion implements ScmVersion {
String name = null;

public NewScmVersion(String name) {
this.name = name;
}

@Override
public String getType() {
return name;
}

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public static interface AtomicTransactionFactory {
// The [message] is a magic string that will be replaced with commit message
// when commit occurs
private String commitMessagePattern = "[message]";
private String gitRepositoryBranch = "master";
private List<File> filesModifiedByLastReload;
private List<String> manualSynchronizationIncludes;

Expand Down Expand Up @@ -201,7 +202,8 @@ public void configure(StaplerRequest req, JSONObject formData)
this.noUserCommitMessage = formData.getBoolean("noUserCommitMessage");
this.displayStatus = formData.getBoolean("displayStatus");
this.commitMessagePattern = req.getParameter("commitMessagePattern");

LOGGER.info("Here: "+ req.getParameter("gitRepositoryBranch"));
this.gitRepositoryBranch = req.getParameter("gitRepositoryBranch");
String oldScmRepositoryUrl = this.scmRepositoryUrl;
String scmType = req.getParameter("scm");
if(scmType != null){
Expand Down Expand Up @@ -362,7 +364,7 @@ public ScmSyncStrategy getStrategyForDeletedSaveable(Saveable s, String pathRela
}

public ScmContext createScmContext(){
return new ScmContext(this.scm, this.scmRepositoryUrl, this.commitMessagePattern);
return new ScmContext(this.scm, this.scmRepositoryUrl, this.commitMessagePattern, this.gitRepositoryBranch);
}

public boolean shouldDecorationOccursOnURL(String url){
Expand Down Expand Up @@ -416,6 +418,10 @@ public SCM getSCM(){
return this.scm;
}

public String getGitRepositoryBranch() {
return this.gitRepositoryBranch;
}

public String getScmUrl(){
if(this.scm != null){
return this.scm.extractScmUrlFrom(this.scmRepositoryUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
public class ScmContext {

private String scmRepositoryUrl;
private String gitRepositoryBranch;
private SCM scm;
private String commitMessagePattern;

public ScmContext(SCM _scm, String _scmRepositoryUrl){
this(_scm, _scmRepositoryUrl, "[message]");
this(_scm, _scmRepositoryUrl, "[message]", "master");
}

public ScmContext(SCM _scm, String _scmRepositoryUrl, String _commitMessagePattern){
public ScmContext(SCM _scm, String _scmRepositoryUrl, String _commitMessagePattern, String gitRepositoryBranch){
this.scm = _scm;
this.scmRepositoryUrl = _scmRepositoryUrl;
this.commitMessagePattern = _commitMessagePattern;
this.gitRepositoryBranch = gitRepositoryBranch;
}

public String getGitRepositoryBranch() {
return gitRepositoryBranch;
}

public String getScmRepositoryUrl() {
Expand All @@ -35,6 +41,7 @@ public String getCommitMessagePattern(){
@Override
public String toString() {
return new ToStringBuilder(this).append("scm", scm).append("scmRepositoryUrl", scmRepositoryUrl)
.append("commitMessagePattern", commitMessagePattern).toString();
.append("commitMessagePattern", commitMessagePattern)
.append("gitRepositoryBranch", gitRepositoryBranch).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ public void marshal(Object source, HierarchicalStreamWriter writer,
writer.addAttribute(AbstractMigrator.SCM_CLASS_ATTRIBUTE, plugin.getSCM().getId());
writer.endNode();
}

if(plugin.getGitRepositoryBranch() != null) {
writer.startNode(AbstractMigrator.SCM_REPOSITORY_BRANCH);
writer.setValue(plugin.getGitRepositoryBranch());
writer.endNode();
}

if(plugin.getScmRepositoryUrl() != null){
writer.startNode(AbstractMigrator.SCM_REPOSITORY_URL_TAG);
writer.setValue(plugin.getScmRepositoryUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class AbstractMigrator<TFROM extends ScmSyncConfigurationPOJO, T
public static final String SCM_DISPLAY_STATUS = "displayStatus";
public static final String SCM_COMMIT_MESSAGE_PATTERN = "commitMessagePattern";
public static final String SCM_MANUAL_INCLUDES = "manualSynchronizationIncludes";
public static final String SCM_REPOSITORY_BRANCH = "gitRepositoryBranch";

private static final Logger LOGGER = Logger.getLogger(AbstractMigrator.class.getName());

Expand All @@ -36,17 +37,20 @@ public TTO readScmSyncConfigurationPOJO(
TTO pojo = createMigratedPojo();

String scmRepositoryUrl = null;
String gitRepositoryBranch = null;
String scmClassAttribute = null;
String scmContent = null;
boolean noUserCommitMessage = false;
boolean displayStatus = true;
String commitMessagePattern = "[message]";
List<String> manualIncludes = null;
while(reader.hasMoreChildren()){

while(reader.hasMoreChildren()) {
reader.moveDown();
if(SCM_REPOSITORY_URL_TAG.equals(reader.getNodeName())){
if (SCM_REPOSITORY_URL_TAG.equals(reader.getNodeName())) {
scmRepositoryUrl = reader.getValue();
} else if (SCM_REPOSITORY_BRANCH.equals(reader.getNodeName())) {
gitRepositoryBranch = reader.getValue();
} else if(SCM_NO_USER_COMMIT_MESSAGE.equals(reader.getNodeName())){
noUserCommitMessage = Boolean.parseBoolean(reader.getValue());
} else if(SCM_DISPLAY_STATUS.equals(reader.getNodeName())){
Expand Down Expand Up @@ -74,6 +78,7 @@ public TTO readScmSyncConfigurationPOJO(

pojo.setScm(createSCMFrom(scmClassAttribute, scmContent));
pojo.setScmRepositoryUrl(scmRepositoryUrl);
pojo.setGitRepositoryBranch(gitRepositoryBranch);
pojo.setNoUserCommitMessage(noUserCommitMessage);
pojo.setDisplayStatus(displayStatus);
pojo.setCommitMessagePattern(commitMessagePattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class DefaultSSCPOJO implements ScmSyncConfigurationPOJO {

private String scmRepositoryUrl;
private String gitRepositoryBranch;
private SCM scm;
private boolean noUserCommitMessage;
private boolean displayStatus;
Expand All @@ -19,6 +20,17 @@ public String getScmRepositoryUrl() {
public void setScmRepositoryUrl(String scmRepositoryUrl) {
this.scmRepositoryUrl = scmRepositoryUrl;
}

@Override
public String getGitRepositoryBranch() {
return gitRepositoryBranch;
}

@Override
public void setGitRepositoryBranch(String gitRepositoryBranch) {
this.gitRepositoryBranch = gitRepositoryBranch;
}

public SCM getScm() {
return scm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public interface ScmSyncConfigurationPOJO {
public String getScmRepositoryUrl();
public void setScmRepositoryUrl(String scmRepositoryUrl);
public String getGitRepositoryBranch();
public void setGitRepositoryBranch(String gitRepositoryBranch);
public SCM getScm();
public void setScm(SCM scm);
public boolean isNoUserCommitMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
checkUrl="'${rootURL}/plugin/scm-sync-configuration/checkGitUrl?value=' + toValue(this)"
/>
</f:entry>
<f:entry title="${%Repository Branch}">
<f:textbox id="gitRepositoryBranch" name="gitRepositoryBranch" value="${it.isScmSelected(scm) ? it.getGitRepositoryBranch() : null}"/>
</f:entry>

</j:jelly>