001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.scm; 017 018import java.io.File; 019import java.util.ArrayList; 020import java.util.List; 021 022import org.kuali.common.util.Assert; 023import org.kuali.common.util.CollectionUtils; 024import org.kuali.common.util.SyncResult; 025import org.kuali.common.util.execute.Executable; 026import org.kuali.common.util.file.DirRequest; 027import org.kuali.common.util.sync.DefaultSyncService; 028import org.kuali.common.util.sync.SyncService; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032public class UpdateScmExecutable implements Executable { 033 034 private static final Logger logger = LoggerFactory.getLogger(UpdateScmExecutable.class); 035 036 public static final boolean DEFAULT_SKIP_VALUE = false; 037 public static final boolean DEFAULT_SKIP_COMMIT_VALUE = true; 038 039 boolean skip = DEFAULT_SKIP_VALUE; 040 // Always skip the commit step, unless they specifically set this to false 041 boolean skipCommit = DEFAULT_SKIP_COMMIT_VALUE; 042 ScmService scmService; 043 SyncService syncService = new DefaultSyncService(); 044 List<DirRequest> requests; 045 String message = "Automated update"; 046 List<File> commitPaths; 047 048 @Override 049 public void execute() { 050 051 if (skip) { 052 return; 053 } 054 055 Assert.notNull(scmService); 056 Assert.notNull(syncService); 057 058 List<SyncResult> results = syncService.sync(requests); 059 060 ScmRequest request = getScmRequest(requests, results, commitPaths); 061 062 logger.info("---------- Sync results ----------"); 063 logger.info("Files added - {}", request.getAdds().size()); 064 logger.info("Files updated - {}", request.getUpdates().size()); 065 logger.info("Files deleted - {}", request.getDeletes().size()); 066 logger.info("---------- Sync results ----------"); 067 068 if (skipCommit) { 069 logger.info("Skipping SCM commit"); 070 } else { 071 scmService.add(request.getAdds()); 072 scmService.delete(request.getDeletes()); 073 scmService.commit(request.getCommits(), message); 074 } 075 } 076 077 protected ScmRequest getScmRequest(List<DirRequest> requests, List<SyncResult> results, List<File> commitPaths) { 078 List<File> adds = new ArrayList<File>(); 079 List<File> deletes = new ArrayList<File>(); 080 List<File> updates = new ArrayList<File>(); 081 082 for (SyncResult result : results) { 083 adds.addAll(result.getAdds()); 084 deletes.addAll(result.getDeletes()); 085 updates.addAll(result.getUpdates()); 086 } 087 088 // Add any commit paths they explicitly provided 089 List<File> commits = new ArrayList<File>(CollectionUtils.toEmptyList(commitPaths)); 090 091 // Add each target directory as a path to recursively commit 092 for (DirRequest request : CollectionUtils.toEmptyList(requests)) { 093 commits.add(request.getTargetDir()); 094 } 095 096 ScmRequest scmRequest = new ScmRequest(); 097 scmRequest.setAdds(adds); 098 scmRequest.setUpdates(updates); 099 scmRequest.setDeletes(deletes); 100 scmRequest.setCommits(commits); 101 return scmRequest; 102 } 103 104 public boolean isSkip() { 105 return skip; 106 } 107 108 public void setSkip(boolean skip) { 109 this.skip = skip; 110 } 111 112 public boolean isSkipCommit() { 113 return skipCommit; 114 } 115 116 public void setSkipCommit(boolean commitChanges) { 117 this.skipCommit = commitChanges; 118 } 119 120 public ScmService getScmService() { 121 return scmService; 122 } 123 124 public void setScmService(ScmService scmService) { 125 this.scmService = scmService; 126 } 127 128 public SyncService getSyncService() { 129 return syncService; 130 } 131 132 public void setSyncService(SyncService syncService) { 133 this.syncService = syncService; 134 } 135 136 public List<DirRequest> getRequests() { 137 return requests; 138 } 139 140 public void setRequests(List<DirRequest> requests) { 141 this.requests = requests; 142 } 143 144 public String getMessage() { 145 return message; 146 } 147 148 public void setMessage(String message) { 149 this.message = message; 150 } 151 152 public List<File> getCommitPaths() { 153 return commitPaths; 154 } 155 156 public void setCommitPaths(List<File> commitPaths) { 157 this.commitPaths = commitPaths; 158 } 159 160}