1 | |
package fr.paris.lutece.plugins.releaser.util; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.util.Date; |
5 | |
import java.util.Locale; |
6 | |
|
7 | |
import javax.servlet.http.HttpServletRequest; |
8 | |
import javax.servlet.http.HttpSession; |
9 | |
|
10 | |
import org.apache.commons.lang.StringUtils; |
11 | |
|
12 | |
import fr.paris.lutece.plugins.releaser.business.Component; |
13 | |
import fr.paris.lutece.plugins.releaser.business.ReleaserUser; |
14 | |
import fr.paris.lutece.plugins.releaser.business.Site; |
15 | |
import fr.paris.lutece.plugins.releaser.business.WorkflowReleaseContext; |
16 | |
import fr.paris.lutece.portal.service.security.LuteceUser; |
17 | |
import fr.paris.lutece.portal.service.util.AppException; |
18 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
19 | |
import fr.paris.lutece.portal.service.util.AppPropertiesService; |
20 | |
|
21 | 0 | public class ReleaserUtils |
22 | |
{ |
23 | |
|
24 | |
public static final String REGEX_ID = "^[\\d]+$"; |
25 | |
|
26 | |
public static String getWorklowContextDataKey( String strArtifactId, int nContextId ) |
27 | |
{ |
28 | 0 | return ConstanteUtils.CONSTANTE_RELEASE_CONTEXT_PREFIX + strArtifactId + "_" + nContextId; |
29 | |
} |
30 | |
|
31 | |
public static String getLastReleaseVersionDataKey( String strArtifactId ) |
32 | |
{ |
33 | 0 | return ConstanteUtils.CONSTANTE_LAST_RELEASE_VERSION_PREFIX + strArtifactId ; |
34 | |
} |
35 | |
|
36 | |
public static String getLastReleaseNextSnapshotVersionDataKey( String strArtifactId ) |
37 | |
{ |
38 | 0 | return ConstanteUtils.CONSTANTE_LAST_RELEASE_NEXT_SNPASHOT_VERSION_PREFIX + strArtifactId ; |
39 | |
} |
40 | |
|
41 | |
|
42 | |
public static String getLocalSitePath( String strSiteName ) |
43 | |
{ |
44 | 0 | String strCheckoutBasePath = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_LOCAL_SITE_BASE_PAH ); |
45 | |
|
46 | 0 | return strCheckoutBasePath + File.separator + strSiteName; |
47 | |
} |
48 | |
|
49 | |
public static String getLocalSitePomPath( String strSiteName ) |
50 | |
{ |
51 | 0 | return getLocalSitePath( strSiteName ) + File.separator + ConstanteUtils.CONSTANTE_POM_XML; |
52 | |
} |
53 | |
|
54 | |
public static String getLocalComponentPath( String strComponentName ) |
55 | |
{ |
56 | 3 | String strLocaleComponentBasePath = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_LOCAL_COMPONENT_BASE_PAH ); |
57 | |
|
58 | 3 | return strLocaleComponentBasePath + File.separator + strComponentName; |
59 | |
} |
60 | |
|
61 | |
public static String getLocalComponentPomPath( String strComponentName ) |
62 | |
{ |
63 | 2 | return getLocalComponentPath( strComponentName ) + File.separator + ConstanteUtils.CONSTANTE_POM_XML; |
64 | |
} |
65 | |
|
66 | |
public static String getGitComponentName( String strScmDeveloperConnection ) |
67 | |
{ |
68 | |
|
69 | 2 | if ( strScmDeveloperConnection.contains( "/" ) && strScmDeveloperConnection.contains( ".git" ) ) |
70 | |
{ |
71 | 2 | String [ ] tabDevConnection = strScmDeveloperConnection.split( "/" ); |
72 | 2 | return tabDevConnection [tabDevConnection.length - 1].replace( ".git", "" ); |
73 | |
} |
74 | 0 | return null; |
75 | |
} |
76 | |
|
77 | |
public static String getSiteTagName(Site site) |
78 | |
{ |
79 | |
|
80 | 0 | String strTagName=""; |
81 | |
|
82 | 0 | if(site!=null) |
83 | |
{ |
84 | 0 | strTagName= site.getArtifactId( )+"-"+site.getNextReleaseVersion( ); |
85 | |
} |
86 | 0 | return strTagName; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
public static void addTechnicalError( CommandResult commandResult, String strError, Exception e ) throws AppException |
91 | |
{ |
92 | |
|
93 | 0 | if ( e != null ) |
94 | |
{ |
95 | 0 | AppLogService.error( strError, e ); |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | AppLogService.error( strError ); |
100 | |
} |
101 | |
|
102 | 0 | if ( commandResult != null ) |
103 | |
{ |
104 | 0 | commandResult.setError( strError ); |
105 | 0 | commandResult.setStatus( CommandResult.STATUS_ERROR ); |
106 | 0 | commandResult.setRunning( false ); |
107 | 0 | commandResult.setErrorType( CommandResult.ERROR_TYPE_STOP ); |
108 | 0 | commandResult.setDateEnd( new Date( ) ); |
109 | |
} |
110 | 0 | if ( e != null ) |
111 | |
{ |
112 | 0 | throw new AppException( strError, e ); |
113 | |
} |
114 | |
else |
115 | |
{ |
116 | 0 | throw new AppException( strError ); |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
public static void addTechnicalError( CommandResult commandResult, String strError ) throws AppException |
121 | |
{ |
122 | 0 | addTechnicalError( commandResult, strError, null ); |
123 | 0 | } |
124 | |
|
125 | |
public static void startCommandResult( WorkflowReleaseContext context ) |
126 | |
{ |
127 | 1 | CommandResult commandResult = new CommandResult( ); |
128 | 1 | commandResult.setDateBegin( new Date( ) ); |
129 | 1 | commandResult.setLog( new StringBuffer( ) ); |
130 | 1 | commandResult.setRunning( true ); |
131 | 1 | commandResult.setStatus( CommandResult.STATUS_OK ); |
132 | 1 | commandResult.setProgressValue( 0 ); |
133 | 1 | context.setCommandResult( commandResult ); |
134 | |
|
135 | 1 | } |
136 | |
|
137 | |
public static void logStartAction( WorkflowReleaseContext context, String strActionName ) |
138 | |
{ |
139 | |
|
140 | 1 | context.getCommandResult( ).getLog( ).append( "******************Start Action: \"" + strActionName + "\" *******************\n\r" ); |
141 | |
|
142 | 1 | } |
143 | |
|
144 | |
public static void logEndAction( WorkflowReleaseContext context, String strActionName ) |
145 | |
{ |
146 | 0 | context.getCommandResult( ).getLog( ).append( "******************End Action:\"" + strActionName + "\" *******************\n\r" ); |
147 | |
|
148 | 0 | } |
149 | |
|
150 | |
public static void stopCommandResult( WorkflowReleaseContext context ) |
151 | |
{ |
152 | 0 | context.getCommandResult( ).setRunning( false ); |
153 | 0 | context.getCommandResult( ).setDateEnd( new Date( ) ); |
154 | 0 | context.getCommandResult( ).setProgressValue( 100 ); |
155 | 0 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public static int convertStringToInt( String strParameter ) |
165 | |
{ |
166 | 0 | int nIdParameter = ConstanteUtils.CONSTANTE_ID_NULL; |
167 | |
|
168 | |
try |
169 | |
{ |
170 | 0 | if ( ( strParameter != null ) && strParameter.matches( REGEX_ID ) ) |
171 | |
{ |
172 | 0 | nIdParameter = Integer.parseInt( strParameter ); |
173 | |
} |
174 | |
} |
175 | 0 | catch( NumberFormatException ne ) |
176 | |
{ |
177 | 0 | AppLogService.error( ne ); |
178 | 0 | } |
179 | |
|
180 | 0 | return nIdParameter; |
181 | |
} |
182 | |
|
183 | |
public static ReleaserUser getReleaserUser( HttpServletRequest request, Locale locale ) |
184 | |
{ |
185 | |
|
186 | 1 | ReleaserUser releaserUser = null; |
187 | |
|
188 | 1 | if ( isApplicationAccountEnable( ) ) |
189 | |
{ |
190 | |
|
191 | 1 | releaserUser = new ReleaserUser( ); |
192 | 1 | releaserUser.setGithubComponentAccountLogin( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITHUB_RELEASE_COMPONET_ACCOUNT_LOGIN ) ); |
193 | 1 | releaserUser |
194 | 1 | .setGithubComponentAccountPassword( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITHUB_RELEASE_COMPONET_ACCOUNT_PASSWORD ) ); |
195 | 1 | releaserUser.setSvnComponentAccountLogin( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SVN_RELEASE_COMPONET_ACCOUNT_LOGIN ) ); |
196 | 1 | releaserUser.setSvnComponentAccountPassword( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SVN_RELEASE_COMPONET_ACCOUNT_PASSWORD ) ); |
197 | 1 | releaserUser.setSvnSiteAccountLogin( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SITE_REPOSITORY_LOGIN ) ); |
198 | 1 | releaserUser.setSvnSiteAccountPassword( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SITE_REPOSITORY_PASSWORD ) ); |
199 | |
} |
200 | |
else |
201 | |
{ |
202 | |
|
203 | 0 | HttpSession session = ( request != null ) ? request.getSession( true ) : null; |
204 | |
|
205 | 0 | if ( session != null ) |
206 | |
{ |
207 | 0 | return (ReleaserUser) session.getAttribute( ConstanteUtils.ATTRIBUTE_RELEASER_USER ); |
208 | |
} |
209 | |
|
210 | |
} |
211 | |
|
212 | 1 | return releaserUser; |
213 | |
} |
214 | |
|
215 | |
public static void setReleaserUser( HttpServletRequest request, ReleaserUser releaserUser ) |
216 | |
{ |
217 | |
|
218 | 0 | HttpSession session = ( request != null ) ? request.getSession( true ) : null; |
219 | |
|
220 | 0 | if ( session != null ) |
221 | |
{ |
222 | 0 | session.setAttribute( ConstanteUtils.ATTRIBUTE_RELEASER_USER, releaserUser ); |
223 | |
} |
224 | |
|
225 | 0 | } |
226 | |
|
227 | |
public static boolean isApplicationAccountEnable( ) |
228 | |
{ |
229 | |
|
230 | 1 | return AppPropertiesService.getPropertyBoolean( ConstanteUtils.PROPERTY_APPLICATION_ACCOUNT_ENABLE, false ); |
231 | |
|
232 | |
} |
233 | |
|
234 | |
|
235 | |
} |