View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.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   * JenkinsService
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       * {@inheritDoc }
68       */
69      @Override
70      public String getName( )
71      {
72          return SERVICE_NAME;
73      }
74  
75      /**
76       * {@inheritDoc }
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       * Gets a Jenkins Authenticator
100      * 
101      * @return An Authenticator
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      * Get JobName from SCM infos
112      * 
113      * @param strScmInfos
114      *            SCM infos
115      * @return the Job name
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      * Gets Job name from a SVN SCM URL
161      * 
162      * @param strScmInfos
163      *            The SCM infos
164      * @return The JobName
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 }