Skip to content

Commit

Permalink
Merge pull request jenkinsci#49 from 2ndquadrant-it/avoid-failing-whe…
Browse files Browse the repository at this point in the history
…n-delete-a-branch

Avoid failing when delete or rename a branch
  • Loading branch information
guipal authored Nov 9, 2017
2 parents 4f76d48 + 16704b2 commit 16e5593
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.command.remove.RemoveScmResult;
import org.apache.maven.scm.command.status.StatusScmResult;
import org.apache.maven.scm.command.update.UpdateScmResult;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.manager.ScmManager;
Expand All @@ -18,6 +19,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -124,7 +126,17 @@ public List<File> deleteHierarchy(File hierarchyToDelete){

try {
ScmFileSet deleteFileSet = new ScmFileSet(enclosingDirectory, hierarchyToDelete);
RemoveScmResult removeResult = this.scmManager.remove(this.scmRepository, deleteFileSet, "");
StatusScmResult checkForChanges = this.scmManager.status(scmRepository, deleteFileSet);
LOGGER.fine("Checking for changes on SCM hierarchy ["+hierarchyToDelete.getAbsolutePath()+"] from SCM ...");
for (ScmFile changedFile : checkForChanges.getChangedFiles()) {
//check in this change as it affect our hierarchy
LOGGER.fine("[checkForChanges] Found changed file "+changedFile.toString()+", try to check-in...");
CheckInScmResult checkedInChangedFile = scmManager.checkIn(scmRepository, new ScmFileSet(enclosingDirectory.getParentFile(), new File(changedFile.getPath())), "Check-In changes for "+changedFile.getPath());
if(!checkedInChangedFile.isSuccess()){
LOGGER.severe("[checkForChanges] Failed to check-in changed file ["+changedFile.getPath()+"]: "+checkedInChangedFile.getProviderMessage());
}
}
RemoveScmResult removeResult = this.scmManager.remove(this.scmRepository, deleteFileSet, "Delete hierarchy "+hierarchyToDelete.getPath());
if(!removeResult.isSuccess()){
LOGGER.severe("[deleteHierarchy] Problem during remove : "+removeResult.getProviderMessage());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ScmSyncConfigurationBusiness {
private SCMManipulator scmManipulator;
private File checkoutScmDirectory = null;
private ScmSyncConfigurationStatusManager scmSyncConfigurationStatusManager = null;
private List<String> manualSynchronizationIncludes = new ArrayList<String>();

/**
* Use of a size 1 thread pool frees us from worrying about accidental thread death and
Expand Down Expand Up @@ -187,8 +188,17 @@ private void processCommitsQueue() {
String firstNonExistingParentScmPath = pathRelativeToJenkinsRoot.getFirstNonExistingParentScmPath();

try {
FileUtils.copyDirectory(JenkinsFilesHelper.buildFileFromPathRelativeToHudsonRoot(pathRelativeToJenkinsRoot.getPath()),
fileTranslatedInScm);
File buildFileFromPathRelativeToHudsonRoot = JenkinsFilesHelper.buildFileFromPathRelativeToHudsonRoot(pathRelativeToJenkinsRoot.getPath());
FileUtils.copyDirectory(buildFileFromPathRelativeToHudsonRoot, fileTranslatedInScm, new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.getPath().endsWith(".xml")
|| getManualSynchronizationIncludes().contains(pathname)){
return true;
}
return false;
}
});
} catch (IOException e) {
throw new LoggableException("Error while copying file hierarchy to SCM directory", FileUtils.class, "copyDirectory", e);
}
Expand All @@ -212,7 +222,8 @@ private void processCommitsQueue() {
}
for(Path path : commit.getChangeset().getPathsToDelete()){
List<File> deletedFiles = deleteHierarchy(commit.getScmContext(), path);
updatedFiles.addAll(deletedFiles);
if(deletedFiles != null)
updatedFiles.addAll(deletedFiles);
}

if(updatedFiles.isEmpty()){
Expand Down Expand Up @@ -245,6 +256,15 @@ private void processCommitsQueue() {
}
}

public List<String> getManualSynchronizationIncludes() {
return manualSynchronizationIncludes;
}

public void setManualSynchronizationIncludes(
List<String> manualSynchronizationIncludes) {
this.manualSynchronizationIncludes = manualSynchronizationIncludes;
}

private boolean writeScmContentOnlyIfItDiffers(Path pathRelativeToJenkinsRoot, byte[] content, File fileTranslatedInScm)
throws LoggableException {
boolean scmContentUpdated = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public void loadData(ScmSyncConfigurationPOJO pojo){
this.displayStatus = pojo.isDisplayStatus();
this.commitMessagePattern = pojo.getCommitMessagePattern();
this.manualSynchronizationIncludes = pojo.getManualSynchronizationIncludes();
this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes);
}

protected void initialInit() throws Exception {
Expand Down Expand Up @@ -228,6 +229,8 @@ public void configure(StaplerRequest req, JSONObject formData)
this.manualSynchronizationIncludes = new ArrayList<String>();
}

this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes);

// Repo initialization should be made _before_ plugin save, in order to let scm-sync-configuration.xml
// file synchronizable
if(repoInitializationRequired){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public void registerPath(String path) {
public void registerPathForDeletion(String path){
// We should determine if path is a directory by watching scm path (and not hudson path) because in most of time,
// when we are here, directory is already deleted in hudson hierarchy...
boolean isDirectory = new Path(path).getScmFile().isDirectory();
pathsToDelete.add(new Path(path, isDirectory));
if(new Path(path).getScmFile().exists()) {
boolean isDirectory = new Path(path).getScmFile().isDirectory();
pathsToDelete.add(new Path(path, isDirectory));
}
}

public boolean isEmpty(){
Expand All @@ -75,7 +77,7 @@ public Map<Path, byte[]> getPathContents(){
for(Path pathToAdd : filteredPathContents.keySet()){
for(Path pathToDelete : pathsToDelete){
// Removing paths being both in pathsToDelete and pathContents
if(pathToDelete.contains(pathToAdd)){
if(pathToDelete.equals(pathToAdd)){
filteredPaths.add(pathToAdd);
}
}
Expand Down

0 comments on commit 16e5593

Please sign in to comment.