Coverage Report - fr.paris.lutece.plugins.releaser.service.GitMavenPrepareUpdateRemoteRepository
 
Classes in this File Line Coverage Branch Coverage Complexity
GitMavenPrepareUpdateRemoteRepository
0 %
1/104
0 %
0/26
9,333
 
 1  
 package fr.paris.lutece.plugins.releaser.service;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.util.List;
 5  
 import java.util.Locale;
 6  
 
 7  
 import org.apache.commons.lang.StringUtils;
 8  
 import org.eclipse.jgit.api.Git;
 9  
 import org.eclipse.jgit.api.LogCommand;
 10  
 import org.eclipse.jgit.api.ResetCommand.ResetType;
 11  
 import org.eclipse.jgit.api.errors.GitAPIException;
 12  
 import org.eclipse.jgit.api.errors.InvalidRemoteException;
 13  
 import org.eclipse.jgit.api.errors.TransportException;
 14  
 import org.eclipse.jgit.internal.storage.file.FileRepository;
 15  
 import org.eclipse.jgit.lib.Ref;
 16  
 import org.eclipse.jgit.revwalk.RevCommit;
 17  
 import org.eclipse.jgit.transport.RefSpec;
 18  
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 19  
 
 20  
 import fr.paris.lutece.plugins.releaser.business.Component;
 21  
 import fr.paris.lutece.plugins.releaser.business.WorkflowReleaseContext;
 22  
 import fr.paris.lutece.plugins.releaser.util.CommandResult;
 23  
 import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
 24  
 import fr.paris.lutece.plugins.releaser.util.github.GitUtils;
 25  
 
 26  1
 public class GitMavenPrepareUpdateRemoteRepository implements IMavenPrepareUpdateRemoteRepository
 27  
 {
 28  
 
 29  
   
 30  
     @Override
 31  
     public void updateDevelopBranch(String strLocalBasePath,WorkflowReleaseContext context, Locale locale, String strMessage )
 32  
     {
 33  
         
 34  
         
 35  0
         FileRepository fLocalRepo = null;
 36  0
         Git git = null;
 37  0
         CommandResult commandResult = context.getCommandResult( );
 38  0
         Component component = context.getComponent( );
 39  0
         String strComponentName = ReleaserUtils.getGitComponentName( component.getScmDeveloperConnection( ) );
 40  0
         String strLocalComponentPath = ReleaserUtils.getLocalComponentPath( strComponentName );
 41  
      
 42  
        
 43  
         try
 44  
         {
 45  
      
 46  0
             fLocalRepo = new FileRepository( strLocalComponentPath + "/.git" );
 47  
     
 48  0
             git = new Git( fLocalRepo );
 49  0
             git.checkout( ).setName( GitUtils.DEVELOP_BRANCH ).call( );
 50  0
             git.add( ).addFilepattern( "." ).setUpdate( true ).call( );
 51  0
             git.commit( ).setMessage( strMessage).call( );
 52  0
             git.push( )
 53  0
                     .setCredentialsProvider( new UsernamePasswordCredentialsProvider( context.getReleaserUser( ).getGithubComponentAccountLogin( ), context.getReleaserUser( ).getGithubComponentAccountPassword( ) ) )
 54  0
                     .call( );
 55  
         }
 56  0
         catch( InvalidRemoteException e )
 57  
         {
 58  
 
 59  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 60  
 
 61  
         }
 62  0
         catch( TransportException e )
 63  
         {
 64  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 65  
 
 66  
         }
 67  0
         catch( IOException e )
 68  
         {
 69  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 70  
         }
 71  0
         catch( GitAPIException e )
 72  
         {
 73  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 74  
         }
 75  
 
 76  
         finally
 77  
         {
 78  
 
 79  0
             if ( fLocalRepo != null )
 80  
             {
 81  
 
 82  0
                 fLocalRepo.close( );
 83  
 
 84  
             }
 85  0
             if ( git != null )
 86  
             {
 87  
 
 88  0
                 git.close( );
 89  
 
 90  
             }
 91  
 
 92  
         }
 93  
         
 94  
 
 95  0
     }
 96  
 
 97  
     @Override
 98  
     public void updateReleaseBranch(String strLocalBasePath, WorkflowReleaseContext context, Locale locale)
 99  
     {
 100  0
         FileRepository fLocalRepo = null;
 101  0
         Git git = null;
 102  0
         CommandResult commandResult = context.getCommandResult( );
 103  0
         Component component = context.getComponent( );
 104  0
         String strComponentName = ReleaserUtils.getGitComponentName( component.getScmDeveloperConnection( ) );
 105  0
         String strLocalComponentPath = ReleaserUtils.getLocalComponentPath( strComponentName );
 106  
      
 107  
        
 108  
         try
 109  
         {
 110  
      
 111  0
             fLocalRepo = new FileRepository( strLocalComponentPath + "/.git" );
 112  
     
 113  0
             git = new Git( fLocalRepo );
 114  0
             git.checkout( ).setName( GitUtils.DEVELOP_BRANCH ).call( );
 115  0
             GitUtils.mergeBack( git, context.getReleaserUser( ).getGithubComponentAccountLogin( ), context.getReleaserUser( ).getGithubComponentAccountPassword( ), commandResult );
 116  
             
 117  
     
 118  
         
 119  
         }
 120  0
         catch( InvalidRemoteException e )
 121  
         {
 122  
 
 123  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 124  
 
 125  
         }
 126  0
         catch( TransportException e )
 127  
         {
 128  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 129  
 
 130  
         }
 131  0
         catch( IOException e )
 132  
         {
 133  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 134  
         }
 135  0
         catch( GitAPIException e )
 136  
         {
 137  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 138  
         }
 139  
 
 140  
         finally
 141  
         {
 142  
 
 143  0
             if ( fLocalRepo != null )
 144  
             {
 145  
 
 146  0
                 fLocalRepo.close( );
 147  
 
 148  
             }
 149  0
             if ( git != null )
 150  
             {
 151  
 
 152  0
                 git.close( );
 153  
 
 154  
             }
 155  
 
 156  
         }
 157  
         
 158  
 
 159  0
     }
 160  
     
 161  
     
 162  
     @Override
 163  
     public void rollbackRelease(String strLocalBasePath, WorkflowReleaseContext context, Locale locale)
 164  
     {
 165  
         
 166  0
         FileRepository fLocalRepo = null;
 167  0
         Git git = null;
 168  0
         CommandResult commandResult = context.getCommandResult( );
 169  0
         Component component = context.getComponent( );
 170  0
         String strComponentName = ReleaserUtils.getGitComponentName( component.getScmDeveloperConnection( ) );
 171  0
         String strLocalComponentPath = ReleaserUtils.getLocalComponentPath( strComponentName );
 172  
         
 173  
         try
 174  
         {
 175  
      
 176  0
             fLocalRepo = new FileRepository( strLocalComponentPath + "/.git" );
 177  
     
 178  0
             git = new Git( fLocalRepo );
 179  
             
 180  
             
 181  
             
 182  
             //RESET commit on develop
 183  0
             if(!StringUtils.isEmpty( context.getRefBranchDev( )))
 184  
             {
 185  0
                 git.checkout().setName( GitUtils.DEVELOP_BRANCH ).call();
 186  0
                 git.reset().setRef( context.getRefBranchDev( ) ).setMode( ResetType.HARD ).call( );
 187  0
                 git.push( ).setForce( true )
 188  0
                 .setCredentialsProvider( new UsernamePasswordCredentialsProvider( context.getReleaserUser( ).getGithubComponentAccountLogin( ), context.getReleaserUser( ).getGithubComponentAccountPassword( ) ) )
 189  0
                 .call( );
 190  
                 
 191  
            }
 192  
             //Reset Commit on Master
 193  0
             if(!StringUtils.isEmpty( context.getRefBranchRelease( )))
 194  
             {
 195  
 
 196  0
                 git.checkout().setName( GitUtils.MASTER_BRANCH).call();
 197  0
                 git.reset().setRef( context.getRefBranchRelease( ) ).setMode( ResetType.HARD ).call( );
 198  0
                 git.push( ).setForce( true )
 199  0
                 .setCredentialsProvider( new UsernamePasswordCredentialsProvider( context.getReleaserUser( ).getGithubComponentAccountLogin( ), context.getReleaserUser( ).getGithubComponentAccountPassword( ) ) )
 200  0
                 .call( );       
 201  
              }
 202  
             //Delete Tag if exist
 203  0
             List<Ref> call = git.tagList().call();
 204  0
             String strTagName=component.getArtifactId( )+"-"+component.getTargetVersion( ) ;
 205  0
             for (Ref refTag : call) {
 206  
                 
 207  0
                 if(refTag.getName( ).contains(strTagName))
 208  
                 {
 209  
                 
 210  0
                     LogCommand log = git.log().setMaxCount(1);
 211  
     
 212  0
                     Ref peeledRef = git.getRepository( ).peel(refTag);
 213  0
                     if(peeledRef.getPeeledObjectId() != null) {
 214  0
                         log.add(peeledRef.getPeeledObjectId());
 215  
                     } else {
 216  0
                         log.add(refTag.getObjectId());
 217  
                     }
 218  
         
 219  0
                     Iterable<RevCommit> logs = log.call();
 220  0
                     for (RevCommit rev : logs) {
 221  
                         //Test if the tag was created by the release
 222  0
                         if(!rev.getName( ).equals( context.getRefBranchRelease( ) ))
 223  
                         {
 224  
                             
 225  0
                             git.branchDelete().setBranchNames(refTag.getName( )).setForce(true).call();
 226  0
                             RefSpec refSpec = new RefSpec()
 227  0
                                     .setSource(null)
 228  0
                                     .setDestination(refTag.getName( ));
 229  0
                             git.push().setRefSpecs(refSpec).setCredentialsProvider( new UsernamePasswordCredentialsProvider( context.getReleaserUser( ).getGithubComponentAccountLogin( ), context.getReleaserUser( ).getGithubComponentAccountPassword( ) ) ).
 230  0
                             setRemote("origin").call();
 231  
                         }
 232  
                         
 233  
                         
 234  0
                     }
 235  
                 
 236  
             
 237  
             }
 238  
     
 239  0
             }
 240  
         }
 241  0
         catch( InvalidRemoteException e )
 242  
         {
 243  
 
 244  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 245  
 
 246  
         }
 247  0
         catch( TransportException e )
 248  
         {
 249  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 250  
 
 251  
         }
 252  0
         catch( IOException e )
 253  
         {
 254  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 255  
         }
 256  0
         catch( GitAPIException e )
 257  
         {
 258  0
             ReleaserUtils.addTechnicalError( commandResult, e.getMessage( ), e );
 259  
         }
 260  
 
 261  
         finally
 262  
         {
 263  
 
 264  0
             if ( fLocalRepo != null )
 265  
             {
 266  
 
 267  0
                 fLocalRepo.close( );
 268  
 
 269  
             }
 270  0
             if ( git != null )
 271  
             {
 272  
 
 273  0
                 git.close( );
 274  
 
 275  
             }
 276  
 
 277  
         }
 278  
         
 279  
         
 280  0
     }
 281  
   
 282  
 
 283  
 }