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.lutecetools.service;
35
36 import fr.paris.lutece.plugins.lutecetools.business.Component;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38 import fr.paris.lutece.util.signrequest.BasicAuthorizationAuthenticator;
39 import fr.paris.lutece.util.signrequest.RequestAuthenticator;
40
41
42
43
44 public class JenkinsService implements ComponentInfoFiller
45 {
46 public static final String DEFAULT_BADGE_URL = "images/skin/plugins/lutecetools/job-not-found.svg";
47
48 private static final String JENKINS_JOB_BUILD_URL = "jenkinsJobBuildUrl";
49 private static final String JENKINS_JOB_BADGE_ICON_URL = "jenkinsJobBadgeIconUrl";
50 private static final String JENKINS_JOB_STATUS = "jenkinsStatus";
51
52 private static final String SERVICE_NAME = "Jenkins Info filler service registered";
53 private static final String PROPERTY_JENKINS_JOB_URL = "lutecetools.jenkins.job.url";
54 private static final String PROPERTY_JENKINS_BADGE_URL = "lutecetools.jenkins.badge.url";
55 private static final String PROPERTY_JENKINS_CREDENTIALS_USER = "lutecetools.jenkins.user";
56 private static final String PROPERTY_JENKINS_CREDENTIALS_PWD = "lutecetools.jenkins.pwd";
57 private static final String PREFIX_LUTECE_PLATFORM = "https://github.com/lutece-platform/lutece-";
58 private static final String PREFIX_LUTECE_SECTEUR_PUBLIC = "https://github.com/lutece-secteur-public/";
59 private static final String PREFIX_GITLAB = "https://dev.lutece.paris.fr/gitlab/";
60 private static final String SUFFIX_GIT = ".git";
61 private static final String SUFFIX_DEPLOY_JOB = "-deploy";
62 private static final String [ ] GITLAB_GROUPS = {
63 "MDP", "TMMA", "Projets"
64 };
65
66
67
68
69 @Override
70 public String getName( )
71 {
72 return SERVICE_NAME;
73 }
74
75
76
77
78 @Override
79 public void fill( Component component, StringBuilder sbLogs )
80 {
81 String strScmInfos = component.get( Component.SNAPSHOT_SCM_URL );
82 if ( strScmInfos != null )
83 {
84 String strJobName = getJobName( strScmInfos.trim( ) );
85 String strJenkinsJobUrl = AppPropertiesService.getProperty( PROPERTY_JENKINS_JOB_URL );
86 String strJenkinsBadgeUrl = AppPropertiesService.getProperty( PROPERTY_JENKINS_BADGE_URL );
87 if ( strJobName != null )
88 {
89 String strJobUrl = strJenkinsJobUrl + strJobName;
90 component.set( JENKINS_JOB_BUILD_URL, strJobUrl );
91 String strBadgeUrl = strJenkinsBadgeUrl + strJobName;
92 component.set( JENKINS_JOB_BADGE_ICON_URL, strBadgeUrl );
93 }
94 component.set( JENKINS_JOB_STATUS, String.valueOf( strJobName != null ) );
95 }
96 }
97
98
99
100
101
102
103 public RequestAuthenticator getJenkinsAuthenticator( )
104 {
105 String strUser = AppPropertiesService.getProperty( PROPERTY_JENKINS_CREDENTIALS_USER );
106 String strPassword = AppPropertiesService.getProperty( PROPERTY_JENKINS_CREDENTIALS_PWD );
107 return new BasicAuthorizationAuthenticator( strUser, strPassword );
108 }
109
110
111
112
113
114
115
116
117 private String getJobName( String strScmInfos )
118 {
119 if ( strScmInfos == null )
120 {
121 return null;
122 }
123 if ( strScmInfos.contains( "lutece-core" ) )
124 {
125 return "lutece-core-deploy";
126 }
127
128 String strJobName;
129 if ( strScmInfos.startsWith( PREFIX_LUTECE_PLATFORM ) )
130 {
131 strJobName = strScmInfos.substring( PREFIX_LUTECE_PLATFORM.length( ) );
132 }
133 else
134 if ( strScmInfos.startsWith( PREFIX_LUTECE_SECTEUR_PUBLIC ) )
135 {
136 strJobName = strScmInfos.substring( PREFIX_LUTECE_SECTEUR_PUBLIC.length( ) );
137 }
138 else
139 if ( strScmInfos.startsWith( PREFIX_GITLAB ) )
140 {
141 strJobName = getGitlabJobName( strScmInfos.substring( PREFIX_GITLAB.length( ) ) );
142 }
143 else
144 {
145 strJobName = getSvnJobName( strScmInfos );
146 }
147
148 if ( strJobName != null )
149 {
150 if ( strJobName.endsWith( SUFFIX_GIT ) )
151 {
152 strJobName = strJobName.substring( 0, strJobName.length( ) - SUFFIX_GIT.length( ) );
153 }
154 strJobName += SUFFIX_DEPLOY_JOB;
155 }
156 return strJobName;
157 }
158
159
160
161
162
163
164
165
166 private String getSvnJobName( String strScmInfos )
167 {
168 String strJobName = null;
169
170 if ( strScmInfos.endsWith( "/" ) )
171 {
172 strScmInfos = strScmInfos.substring( 0, strScmInfos.length( ) - 1 );
173 }
174 int nPos = strScmInfos.lastIndexOf( '/' );
175 if ( nPos > 0 )
176 {
177 strJobName = strScmInfos.substring( nPos + 1 );
178 String strPath = strScmInfos.substring( 0, nPos );
179 if ( nPos > 0 )
180 {
181 nPos = strPath.lastIndexOf( '/' );
182 String strCategory = strPath.substring( nPos + 1 );
183 if ( strCategory.equals( "trunk" ) )
184 {
185 strCategory = "application";
186 }
187 strJobName = strCategory + "-" + strJobName;
188 }
189 }
190 return strJobName;
191 }
192
193 private String getGitlabJobName( String strScmInfos )
194 {
195 String strJobName = strScmInfos;
196 for ( int i = 0; i < GITLAB_GROUPS.length; i++ )
197 {
198 strJobName = strJobName.replace( GITLAB_GROUPS [i].toLowerCase( ), GITLAB_GROUPS [i] );
199 }
200 if ( strJobName.endsWith( "/" ) )
201 {
202 strJobName = strJobName.substring( 0, strJobName.length( ) - 1 );
203 }
204 strJobName = strJobName.replace( '/', '-' );
205 return strJobName;
206 }
207 }