| 1 | |
package fr.paris.lutece.plugins.releaser.util.github; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.text.MessageFormat; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.Iterator; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Map; |
| 10 | |
|
| 11 | |
import org.apache.commons.lang.StringUtils; |
| 12 | |
import org.eclipse.jgit.api.CloneCommand; |
| 13 | |
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode; |
| 14 | |
import org.eclipse.jgit.api.Git; |
| 15 | |
import org.eclipse.jgit.api.MergeResult; |
| 16 | |
import org.eclipse.jgit.api.PullResult; |
| 17 | |
import org.eclipse.jgit.api.errors.CanceledException; |
| 18 | |
import org.eclipse.jgit.api.errors.CheckoutConflictException; |
| 19 | |
import org.eclipse.jgit.api.errors.DetachedHeadException; |
| 20 | |
import org.eclipse.jgit.api.errors.GitAPIException; |
| 21 | |
import org.eclipse.jgit.api.errors.InvalidConfigurationException; |
| 22 | |
import org.eclipse.jgit.api.errors.InvalidRefNameException; |
| 23 | |
import org.eclipse.jgit.api.errors.InvalidRemoteException; |
| 24 | |
import org.eclipse.jgit.api.errors.NoHeadException; |
| 25 | |
import org.eclipse.jgit.api.errors.RefAlreadyExistsException; |
| 26 | |
import org.eclipse.jgit.api.errors.RefNotFoundException; |
| 27 | |
import org.eclipse.jgit.api.errors.TransportException; |
| 28 | |
import org.eclipse.jgit.api.errors.WrongRepositoryStateException; |
| 29 | |
import org.eclipse.jgit.lib.Ref; |
| 30 | |
import org.eclipse.jgit.lib.Repository; |
| 31 | |
import org.eclipse.jgit.revwalk.RevCommit; |
| 32 | |
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; |
| 33 | |
import org.eclipse.jgit.transport.RefSpec; |
| 34 | |
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
| 35 | |
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager; |
| 36 | |
|
| 37 | |
import fr.paris.lutece.plugins.releaser.util.CommandResult; |
| 38 | |
import fr.paris.lutece.plugins.releaser.util.ConstanteUtils; |
| 39 | |
import fr.paris.lutece.plugins.releaser.util.MapperJsonUtil; |
| 40 | |
import fr.paris.lutece.plugins.releaser.util.ReleaserUtils; |
| 41 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
| 42 | |
import fr.paris.lutece.portal.service.util.AppPropertiesService; |
| 43 | |
import fr.paris.lutece.util.httpaccess.HttpAccess; |
| 44 | |
import fr.paris.lutece.util.httpaccess.HttpAccessException; |
| 45 | |
import fr.paris.lutece.util.signrequest.BasicAuthorizationAuthenticator; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | public class GitUtils { |
| 52 | |
|
| 53 | |
public static final String MASTER_BRANCH = "master"; |
| 54 | |
public static final String DEVELOP_BRANCH = "develop"; |
| 55 | |
|
| 56 | |
|
| 57 | |
public static Git cloneRepo(String sClonePath, String sRepoURL, CommandResult commandResult,String strGitHubUserLogin) |
| 58 | |
{ |
| 59 | 1 | Git git=null; |
| 60 | 1 | Repository repository=null; |
| 61 | |
try |
| 62 | |
{ |
| 63 | 1 | FileRepositoryBuilder builder = new FileRepositoryBuilder(); |
| 64 | 1 | File fGitDir = new File(sClonePath); |
| 65 | |
|
| 66 | 1 | CloneCommand clone = Git.cloneRepository().setBare(false).setCloneAllBranches(true).setDirectory(fGitDir).setURI(getRepoUrl( sRepoURL )); |
| 67 | |
|
| 68 | 1 | git=clone.call( ); |
| 69 | |
|
| 70 | 1 | repository = builder.setGitDir(fGitDir).readEnvironment().findGitDir().build(); |
| 71 | 1 | repository.getConfig( ).setString( "user", null, "name", strGitHubUserLogin ); |
| 72 | 1 | repository.getConfig( ).setString( "user", null, "email", strGitHubUserLogin + "@users.noreply.github.com" ); |
| 73 | 1 | repository.getConfig( ).save( ); |
| 74 | |
|
| 75 | |
|
| 76 | |
} |
| 77 | 0 | catch( InvalidRemoteException e ) |
| 78 | |
{ |
| 79 | |
|
| 80 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 81 | |
|
| 82 | |
} |
| 83 | 0 | catch( TransportException e ) |
| 84 | |
{ |
| 85 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 86 | |
|
| 87 | |
} |
| 88 | 0 | catch( IOException e ) |
| 89 | |
{ |
| 90 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 91 | |
} |
| 92 | 0 | catch( GitAPIException e ) |
| 93 | |
{ |
| 94 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 95 | |
} |
| 96 | |
finally |
| 97 | |
{ |
| 98 | |
|
| 99 | 1 | repository.close(); |
| 100 | |
|
| 101 | 1 | } |
| 102 | 1 | return git; |
| 103 | |
|
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
public static void checkoutRepoBranch(Git git, String sBranchName,CommandResult commandResult) |
| 108 | |
{ |
| 109 | |
try |
| 110 | |
{ |
| 111 | 1 | git.checkout().setName(sBranchName).call(); |
| 112 | |
|
| 113 | |
} |
| 114 | 0 | catch( InvalidRemoteException e ) |
| 115 | |
{ |
| 116 | |
|
| 117 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 118 | |
|
| 119 | |
} |
| 120 | 0 | catch( TransportException e ) |
| 121 | |
{ |
| 122 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 123 | |
|
| 124 | |
} |
| 125 | |
|
| 126 | 0 | catch( GitAPIException e ) |
| 127 | |
{ |
| 128 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 129 | 1 | } |
| 130 | |
|
| 131 | 1 | } |
| 132 | |
|
| 133 | |
public static void createLocalBranch(Git git, String sBranchName,CommandResult commandResult) |
| 134 | |
{ |
| 135 | |
try |
| 136 | |
{ |
| 137 | 2 | git.branchCreate() |
| 138 | 2 | .setName(sBranchName) |
| 139 | 2 | .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM) |
| 140 | 2 | .setStartPoint("origin/" + sBranchName) |
| 141 | 2 | .setForce(true) |
| 142 | 2 | .call(); |
| 143 | |
} |
| 144 | 0 | catch( InvalidRemoteException e ) |
| 145 | |
{ |
| 146 | |
|
| 147 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 148 | |
|
| 149 | |
} |
| 150 | 0 | catch( TransportException e ) |
| 151 | |
{ |
| 152 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 153 | |
|
| 154 | |
} |
| 155 | |
|
| 156 | 0 | catch( GitAPIException e ) |
| 157 | |
{ |
| 158 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 159 | 2 | } |
| 160 | |
|
| 161 | 2 | } |
| 162 | |
|
| 163 | |
public static String getRefBranch(Git git, String sBranchName,CommandResult commandResult) |
| 164 | |
{ |
| 165 | |
|
| 166 | |
|
| 167 | 2 | String refLastCommit=null; |
| 168 | |
try |
| 169 | |
{ |
| 170 | 2 | git.checkout().setName(sBranchName).call(); |
| 171 | 2 | refLastCommit= getLastCommitId( git ); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | 0 | catch( RefAlreadyExistsException e ) |
| 176 | |
{ |
| 177 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 178 | |
} |
| 179 | 0 | catch( RefNotFoundException e ) |
| 180 | |
{ |
| 181 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 182 | |
} |
| 183 | 0 | catch( InvalidRefNameException e ) |
| 184 | |
{ |
| 185 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 186 | |
} |
| 187 | 0 | catch( CheckoutConflictException e ) |
| 188 | |
{ |
| 189 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 190 | |
} |
| 191 | 0 | catch( GitAPIException e ) |
| 192 | |
{ |
| 193 | 0 | ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e ); |
| 194 | 2 | } |
| 195 | 2 | return refLastCommit; |
| 196 | |
} |
| 197 | |
|
| 198 | |
public static void pushForce(Git git, String strRefSpec, String strUserName, String strPassword) throws InvalidRemoteException, TransportException, GitAPIException |
| 199 | |
{ |
| 200 | |
|
| 201 | 0 | git.push( ).setRemote( "origin" ).setRefSpecs( new RefSpec( strRefSpec ) ).setForce( true ).setCredentialsProvider(new UsernamePasswordCredentialsProvider(strUserName, strPassword)).call(); |
| 202 | |
|
| 203 | 0 | } |
| 204 | |
|
| 205 | |
public static PullResult pullRepoBranch(Git git, String sRepoURL, String sBranchName) throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, TransportException, GitAPIException |
| 206 | |
{ |
| 207 | 0 | PullResult pPullResult = git.pull().call(); |
| 208 | 0 | return pPullResult; |
| 209 | |
} |
| 210 | |
|
| 211 | |
public static MergeResult mergeRepoBranch(Git git, String strBranchToMerge) throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, TransportException, GitAPIException |
| 212 | |
{ |
| 213 | 0 | List<Ref> call = git.branchList().call(); |
| 214 | 0 | Ref mergedBranchRef = null; |
| 215 | 0 | for (Ref ref : call) |
| 216 | |
{ |
| 217 | 0 | if (ref.getName().equals("refs/heads/" + strBranchToMerge)) { |
| 218 | 0 | mergedBranchRef = ref; |
| 219 | 0 | break; |
| 220 | |
} |
| 221 | 0 | } |
| 222 | 0 | MergeResult mergeResult = git.merge().include(mergedBranchRef).call(); |
| 223 | 0 | return mergeResult; |
| 224 | |
} |
| 225 | |
|
| 226 | |
public static String getLastLog(Git git, int nMaxCommit) throws NoHeadException, GitAPIException |
| 227 | |
{ |
| 228 | 0 | Iterable<RevCommit> logList = git.log().setMaxCount(1).call(); |
| 229 | 0 | Iterator i = logList.iterator(); |
| 230 | 0 | String sCommitMessages = ""; |
| 231 | 0 | while (i.hasNext()) |
| 232 | |
{ |
| 233 | 0 | RevCommit revCommit = (RevCommit) i.next(); |
| 234 | 0 | sCommitMessages += revCommit.getFullMessage(); |
| 235 | 0 | sCommitMessages += "\n"; |
| 236 | 0 | sCommitMessages += revCommit.getCommitterIdent(); |
| 237 | 0 | } |
| 238 | 0 | return sCommitMessages; |
| 239 | |
} |
| 240 | |
|
| 241 | |
|
| 242 | |
public static String getLastCommitId(Git git) throws NoHeadException, GitAPIException |
| 243 | |
{ |
| 244 | 2 | Iterable<RevCommit> logList = git.log().setMaxCount(1).call(); |
| 245 | 2 | Iterator i = logList.iterator(); |
| 246 | 2 | String strCommitId = null; |
| 247 | 4 | while (i.hasNext()) |
| 248 | |
{ |
| 249 | 2 | RevCommit revCommit = (RevCommit) i.next(); |
| 250 | 2 | strCommitId = revCommit.getName( ); |
| 251 | |
|
| 252 | 2 | } |
| 253 | 2 | return strCommitId; |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public static MergeResult mergeBack(Git git, String strUserName, String strPassword, CommandResult commandResult) throws IOException, GitAPIException |
| 259 | |
{ |
| 260 | |
|
| 261 | |
|
| 262 | 0 | Ref tag = getTagLinkedToLastRelease(git); |
| 263 | |
|
| 264 | 0 | git.checkout().setName(MASTER_BRANCH).call(); |
| 265 | 0 | List<Ref> call = git.branchList().call(); |
| 266 | |
|
| 267 | 0 | Ref mergedBranchRef = null; |
| 268 | 0 | for (Ref ref : call) |
| 269 | |
{ |
| 270 | 0 | if (ref.getName().equals("refs/heads/"+DEVELOP_BRANCH)) { |
| 271 | 0 | mergedBranchRef = ref; |
| 272 | 0 | break; |
| 273 | |
} |
| 274 | 0 | } |
| 275 | |
|
| 276 | 0 | if (tag != null) { |
| 277 | 0 | mergedBranchRef = tag; |
| 278 | |
} |
| 279 | 0 | MergeResult mergeResult = git.merge().include(mergedBranchRef).call(); |
| 280 | 0 | if (mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.CHECKOUT_CONFLICT) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) || mergeResult.getMergeStatus().equals(MergeResult.MergeStatus.NOT_SUPPORTED)) |
| 281 | |
{ |
| 282 | |
|
| 283 | 0 | ReleaserUtils.addTechnicalError( commandResult, mergeResult.getMergeStatus().toString() |
| 284 | |
+ "\nPlease merge manually master into"+ DEVELOP_BRANCH +"branch." ); |
| 285 | |
} |
| 286 | |
else |
| 287 | |
{ |
| 288 | 0 | git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(strUserName, strPassword)).call(); |
| 289 | 0 | commandResult.getLog( ).append(mergeResult.getMergeStatus()); |
| 290 | |
} |
| 291 | 0 | return mergeResult; |
| 292 | |
|
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
public static GithubSearchResult searchRepo(String strSearch,String strOrganization,String strUserName, String strPassword) |
| 297 | |
{ |
| 298 | 0 | HttpAccess httpAccess = new HttpAccess( ); |
| 299 | |
|
| 300 | 0 | GithubSearchResult searchResult=null; |
| 301 | |
|
| 302 | 0 | String strUrl = MessageFormat.format( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITHUB_SEARCH_REPO_API ), strSearch,strOrganization ); |
| 303 | |
|
| 304 | |
|
| 305 | 0 | String strResponse = ""; |
| 306 | |
|
| 307 | |
try |
| 308 | |
{ |
| 309 | |
|
| 310 | 0 | strResponse = httpAccess.doGet(strUrl, new BasicAuthorizationAuthenticator( strUserName, strPassword ),null); |
| 311 | |
|
| 312 | 0 | if(!StringUtils.isEmpty( strResponse )) |
| 313 | |
{ |
| 314 | 0 | searchResult=MapperJsonUtil.parse( strResponse, GithubSearchResult.class ); |
| 315 | |
|
| 316 | |
} |
| 317 | |
|
| 318 | |
} |
| 319 | 0 | catch ( HttpAccessException ex ) |
| 320 | |
{ |
| 321 | 0 | AppLogService.error( ex ); |
| 322 | |
} |
| 323 | 0 | catch( IOException e ) |
| 324 | |
{ |
| 325 | 0 | AppLogService.error( e ); |
| 326 | 0 | } |
| 327 | |
|
| 328 | 0 | return searchResult; |
| 329 | |
} |
| 330 | |
|
| 331 | |
public static String getFileContent(String strFullName,String strPathFile,String strBranch,String strUserName, String strPassword) |
| 332 | |
{ |
| 333 | 0 | HttpAccess httpAccess = new HttpAccess( ); |
| 334 | 0 | String strUrl = "https://raw.githubusercontent.com/"+strFullName+"/"+strBranch+"/"+ strPathFile; |
| 335 | |
|
| 336 | |
|
| 337 | 0 | String strResponse = ""; |
| 338 | |
|
| 339 | |
try |
| 340 | |
{ |
| 341 | |
|
| 342 | |
|
| 343 | 0 | strResponse = httpAccess.doGet( strUrl, new BasicAuthorizationAuthenticator( strUserName, strPassword ),null ); |
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
} |
| 349 | 0 | catch ( HttpAccessException ex ) |
| 350 | |
{ |
| 351 | 0 | AppLogService.error( ex ); |
| 352 | 0 | } |
| 353 | |
|
| 354 | |
|
| 355 | 0 | return strResponse; |
| 356 | |
} |
| 357 | |
|
| 358 | |
|
| 359 | |
private static Ref getTagLinkedToLastRelease(Git git) throws GitAPIException { |
| 360 | 0 | final String TOKEN = "[maven-release-plugin] prepare release "; |
| 361 | 0 | Ref res = null; |
| 362 | 0 | String sTagName = null; |
| 363 | |
|
| 364 | 0 | Iterable<RevCommit> logList = git.log().setMaxCount(10).call(); |
| 365 | 0 | Iterator i = logList.iterator(); |
| 366 | 0 | String sCommitMessages = ""; |
| 367 | 0 | while (i.hasNext()) |
| 368 | |
{ |
| 369 | 0 | RevCommit revCommit = (RevCommit) i.next(); |
| 370 | 0 | sCommitMessages = revCommit.getFullMessage(); |
| 371 | 0 | int index = sCommitMessages.indexOf(TOKEN); |
| 372 | 0 | if (index >= 0) { |
| 373 | 0 | sTagName = sCommitMessages.replace(TOKEN, ""); |
| 374 | 0 | break; |
| 375 | |
} |
| 376 | 0 | } |
| 377 | |
|
| 378 | 0 | if ( (sTagName != null) && (!(sTagName.trim().equals(""))) ) { |
| 379 | 0 | List<Ref> tags = git.tagList().call(); |
| 380 | 0 | for (int j=0; j<tags.size(); j++) { |
| 381 | 0 | Ref tag = tags.get(tags.size() - 1 - j); |
| 382 | 0 | String tagName = tag.getName(); |
| 383 | 0 | if (tagName.equals("refs/tags/" + sTagName)) { |
| 384 | 0 | res = tag; |
| 385 | 0 | break; |
| 386 | |
} |
| 387 | |
} |
| 388 | |
} |
| 389 | |
|
| 390 | 0 | return res; |
| 391 | |
} |
| 392 | |
|
| 393 | |
|
| 394 | |
private static String getRepoUrl(String strRepoUrl) |
| 395 | |
{ |
| 396 | |
|
| 397 | 1 | if(strRepoUrl!=null && strRepoUrl.startsWith( "scm:git:" )) |
| 398 | |
{ |
| 399 | 0 | strRepoUrl=strRepoUrl.substring( 8 ); |
| 400 | |
|
| 401 | |
|
| 402 | |
} |
| 403 | |
|
| 404 | 1 | return strRepoUrl; |
| 405 | |
|
| 406 | |
|
| 407 | |
} |
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
} |