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.deployment.service.vcs;
35  
36  import fr.paris.lutece.plugins.deployment.business.CommandResult;
37  import fr.paris.lutece.plugins.deployment.business.vcs.GitUser;
38  import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
39  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
40  import fr.paris.lutece.plugins.deployment.util.FileUtil;
41  import fr.paris.lutece.plugins.deployment.util.vcs.GitUtils;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.util.ReferenceItem;
44  import fr.paris.lutece.util.ReferenceList;
45  import java.util.Collection;
46  import java.util.List;
47  import java.util.regex.Matcher;
48  import java.util.regex.Pattern;
49  import org.apache.commons.lang3.StringUtils;
50  import org.eclipse.jgit.api.Git;
51  
52  public class AbstractGitService implements IVCSService<GitUser>
53  {
54  
55      @Override
56      public void init( )
57      {
58          throw new UnsupportedOperationException( "Not supported yet." ); // To change body of generated methods, choose Tools | Templates.
59      }
60  
61      @Override
62      public ReferenceList getTagsSite( String strUrlSite, GitUser user, String strSiteName )
63      {
64          String strClonePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
65          CommandResult commandResult = new CommandResult( );
66          DeploymentUtils.startCommandResult( commandResult );
67          Git git = GitUtils.cloneOrReturnGit( strClonePath, strUrlSite, commandResult, user, null, null );
68          Collection<String> listTagName = GitUtils.getTagNameList( git );
69  
70          ReferenceList refList = new ReferenceList( );
71          for ( String strTagName : listTagName )
72          {
73              ReferenceItem item = new ReferenceItem( );
74              item.setName( strTagName );
75              item.setCode( strTagName );
76              refList.add( item );
77          }
78          DeploymentUtils.stopCommandResult( commandResult );
79          return refList;
80      }
81  
82      @Override
83      public String doCheckoutSite( String strSiteName, String strUrl, GitUser user, CommandResult commandResult, String strBranch, String strTagName )
84      {
85          String strClonePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
86          GitUtils.cloneOrReturnGit( strClonePath, strUrl, commandResult, user, strBranch, strTagName );
87  
88          return StringUtils.EMPTY;
89      }
90  
91      @Override
92      public ReferenceList getUpgradesFiles( String strSiteName, String strUrlSite, GitUser user )
93      {
94          CommandResult commandResult = new CommandResult( );
95          DeploymentUtils.startCommandResult( commandResult );
96          ReferenceList upgradeResults = new ReferenceList( );
97  
98          doCheckoutSite( strSiteName, strUrlSite, user, commandResult, ConstanteUtils.CONSTANTE_BRANCH_DEVELOP, null );
99  
100         List<String> listUpgradeFiles = FileUtil.list( DeploymentUtils.getPathUpgradeFiles( DeploymentUtils.getPathCheckoutSite( strSiteName ) ), "sql" );
101 
102         for ( String upgradeFile : listUpgradeFiles )
103         {
104             upgradeResults.addItem( upgradeFile, upgradeFile );
105         }
106 
107         DeploymentUtils.stopCommandResult( commandResult );
108 
109         return upgradeResults;
110 
111     }
112 
113     @Override
114     public String getArtifactId( String strPathRepo )
115     {
116         String strRegexExtractArtifact = AppPropertiesService.getProperty( ConstanteUtils.REGEX_GIT_EXTRACT_ARTIFACT_FROM_URL );
117         Pattern r = Pattern.compile( strRegexExtractArtifact );
118         String strArtifactId = null;
119 
120         Matcher m = r.matcher( strPathRepo );
121         if ( m.find( ) )
122         {
123             strArtifactId = m.group( 2 );
124         }
125         return strArtifactId;
126     }
127 
128     @Override
129     public boolean isPrivate( )
130     {
131         return true;
132     }
133 
134     @Override
135     public void checkAuthentication( String strRepoUrl, GitUser user )
136     {
137     }
138 }