View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.vcs;
35  
36  import java.util.Collection;
37  import java.util.List;
38  import java.util.regex.Matcher;
39  import java.util.regex.Pattern;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.eclipse.jgit.api.Git;
43  
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.vcs.GitUser;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.FileUtil;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.vcs.GitUtils;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.exception.VCSUserNotSignedException;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  import fr.paris.lutece.util.ReferenceItem;
53  import fr.paris.lutece.util.ReferenceList;
54  import java.io.File;
55  import java.io.FileNotFoundException;
56  import java.util.Comparator;
57  import org.eclipse.jgit.api.PullResult;
58  
59  public abstract class AbstractGitService implements IVCSService<GitUser>
60  {
61  
62      @Override
63      public void init( )
64      {
65          throw new UnsupportedOperationException( "Not supported yet." ); // To change body of generated methods, choose Tools | Templates.
66      }
67  
68      @Override
69      public ReferenceList getTagsSite( String strUrlSite, GitUser user, String strSiteName )
70      {
71          ReferenceList refList = new ReferenceList( );
72          CommandResultcenter/modules/fastdeployapplication/business/CommandResult.html#CommandResult">CommandResult commandResult = new CommandResult( );
73  
74          DeploymentUtils.startCommandResult( commandResult );
75  
76          Collection<String> listTagName = GitUtils.getTagNameList( strUrlSite, commandResult, user );
77  
78          for ( String strTagName : listTagName )
79          {
80              ReferenceItem item = new ReferenceItem( );
81              item.setName( strTagName );
82              item.setCode( strTagName );
83              refList.add( item );
84          }
85  
86          refList.sort( Comparator.comparing( ReferenceItem::getName ) );
87  
88          DeploymentUtils.stopCommandResult( commandResult );
89  
90          return refList;
91      }
92  
93      @Override
94      public String doCheckoutSite( String strSiteName, String strUrl, GitUser user, CommandResult commandResult, String strBranch, String strTagName )
95      {
96          String strClonePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
97          GitUtils.pullGit( strClonePath, strUrl, commandResult, user, strBranch, strTagName );
98  
99          return StringUtils.EMPTY;
100     }
101 
102     @Override
103     public ReferenceList getUpgradesFiles( String strSiteName, String strUrlSite, GitUser user )
104     {
105         CommandResultcenter/modules/fastdeployapplication/business/CommandResult.html#CommandResult">CommandResult commandResult = new CommandResult( );
106         DeploymentUtils.startCommandResult( commandResult );
107         ReferenceList upgradeResults = new ReferenceList( );
108 
109         doCheckoutSite( strSiteName, strUrlSite, user, commandResult, ConstanteUtils.CONSTANTE_BRANCH_DEVELOP, null );
110 
111         List<String> listUpgradeFiles = FileUtil.list( DeploymentUtils.getPathUpgradeFiles( DeploymentUtils.getPathCheckoutSite( strSiteName ) ), "sql" );
112 
113         for ( String upgradeFile : listUpgradeFiles )
114         {
115             upgradeResults.addItem( upgradeFile, upgradeFile );
116         }
117 
118         DeploymentUtils.stopCommandResult( commandResult );
119 
120         return upgradeResults;
121 
122     }
123 
124     @Override
125     public File getUpgradeFile( String strSiteName, String strUrlSite, GitUser user, String strFileName ) throws FileNotFoundException
126     {
127         CommandResultcenter/modules/fastdeployapplication/business/CommandResult.html#CommandResult">CommandResult commandResult = new CommandResult( );
128         DeploymentUtils.startCommandResult( commandResult );
129 
130         doCheckoutSite( strSiteName, strUrlSite, user, commandResult, ConstanteUtils.CONSTANTE_BRANCH_DEVELOP, null );
131 
132         File upgradeFile = new File( DeploymentUtils.getPathUpgradeFile( DeploymentUtils.getPathCheckoutSite( strSiteName ), strFileName ) );
133 
134         DeploymentUtils.stopCommandResult( commandResult );
135 
136         return upgradeFile;
137     }
138 
139     @Override
140     public String getArtifactId( String strPathRepo )
141     {
142         String strRegexExtractArtifact = AppPropertiesService.getProperty( ConstanteUtils.REGEX_GIT_EXTRACT_ARTIFACT_FROM_URL );
143         Pattern r = Pattern.compile( strRegexExtractArtifact );
144         String strArtifactId = null;
145 
146         Matcher m = r.matcher( strPathRepo );
147         if ( m.find( ) )
148         {
149             strArtifactId = m.group( 2 );
150         }
151         return strArtifactId;
152     }
153 
154     @Override
155     public boolean isPrivate( )
156     {
157         return true;
158     }
159 
160     @Override
161     public void checkAuthentication( String strRepoUrl, GitUser user ) throws VCSUserNotSignedException
162     {
163     }
164 }