Skip to content

Avoid failing when delete or rename a branch #49

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

Merged
merged 4 commits into from
Nov 9, 2017
Merged
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
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