1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.deployment.util;
35
36 import fr.paris.lutece.plugins.deployment.business.Application;
37 import fr.paris.lutece.plugins.deployment.business.InvalidRepositoryUrlException;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43 import org.apache.commons.lang3.StringUtils;
44 import org.codehaus.jackson.annotate.JsonProperty;
45 import org.codehaus.jackson.map.ObjectMapper;
46
47 public class RepositoryUtils
48 {
49
50
51
52
53
54
55
56
57 public static void checkRepository( Application application ) throws InvalidRepositoryUrlException
58 {
59 String strUrlRepo = application.getUrlRepo( );
60 if ( !strUrlRepo.isEmpty( ) )
61 {
62 URLUtils.checkUrl( strUrlRepo );
63 String strPath = URLUtils.getPath( strUrlRepo );
64
65 String strSvnUrlSites = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SVN_SITES_URL );
66 String strGithubUrlSites = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITHUB_BASE_URL );
67 String strGitlabUrlSites = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITLAB_BASE_URL );
68
69 if ( strUrlRepo.startsWith( strSvnUrlSites ) )
70 {
71 application.setRepoType( ConstanteUtils.CONSTANTE_REPO_TYPE_SVN );
72 }
73 else
74 if ( strUrlRepo.startsWith( strGithubUrlSites ) )
75 {
76
77 List<String> listAuthorizedGithubRepoKeys = AppPropertiesService.getKeys( ConstanteUtils.PROPERTY_GITHUB_AUTHORIZED_REPO_NAME );
78 List<String> listAuthorizedGithubRepo = new ArrayList<String>( );
79 for ( String strKey : listAuthorizedGithubRepoKeys )
80 {
81 listAuthorizedGithubRepo.add( AppPropertiesService.getProperty( strKey ) );
82 }
83
84 String strRepoName = strPath.split( ConstanteUtils.CONSTANTE_SEPARATOR_SLASH ) [1];
85 if ( listAuthorizedGithubRepo.contains( strRepoName ) )
86 {
87 application.setRepoType( ConstanteUtils.CONSTANTE_REPO_TYPE_GITHUB );
88 }
89 else
90 {
91 throw new InvalidRepositoryUrlException( );
92 }
93 }
94 else
95 if ( strUrlRepo.startsWith( strGitlabUrlSites ) )
96 {
97 application.setRepoType( ConstanteUtils.CONSTANTE_REPO_TYPE_GITLAB );
98 }
99 else
100 {
101 throw new InvalidRepositoryUrlException( );
102 }
103 application.setArtifactId( DeploymentUtils.getVCSService( application.getRepoType( ) ).getArtifactId( strUrlRepo ) );
104 }
105 else
106 {
107 throw new InvalidRepositoryUrlException( );
108 }
109 }
110
111
112
113
114
115
116
117
118 public static String checkRepository( String strUrlRepo )
119 {
120 ObjectMapper mapper = new ObjectMapper( );
121 RepoValidation repoValidation = new RepoValidation( );
122
123 Application application = new Application( );
124 application.setUrlRepo( strUrlRepo );
125 try
126 {
127 checkRepository( application );
128 repoValidation.setIsUrlValid( true );
129 repoValidation.setRepoType( application.getRepoType( ) );
130 }
131 catch( InvalidRepositoryUrlException e )
132 {
133 repoValidation.setIsUrlValid( false );
134 }
135 try
136 {
137 return mapper.writeValueAsString( repoValidation );
138 }
139 catch( IOException e )
140 {
141 AppLogService.error( "Unable to serealize repoValidation as Json obj", e );
142 }
143 return StringUtils.EMPTY;
144
145 }
146
147
148
149
150 private static class RepoValidation
151 {
152 @JsonProperty( "valid_url" )
153 private boolean _isUrlValid;
154 @JsonProperty( "repo_type" )
155 private String _strRepoType;
156
157 @JsonProperty( "valid_url" )
158 public boolean isIsUrlValid( )
159 {
160 return _isUrlValid;
161 }
162
163 @JsonProperty( "valid_url" )
164 public void setIsUrlValid( boolean isUrlValid )
165 {
166 _isUrlValid = isUrlValid;
167 }
168
169 @JsonProperty( "repo_type" )
170 public String getRepoType( )
171 {
172 return _strRepoType;
173 }
174
175 @JsonProperty( "repo_type" )
176 public void setRepoType( String strRepoType )
177 {
178 _strRepoType = strRepoType;
179 }
180
181 }
182 }