View Javadoc

1   package org.apache.continuum.buildagent.action;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
23  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
24  import org.apache.continuum.scm.ContinuumScm;
25  import org.apache.continuum.scm.ContinuumScmConfiguration;
26  import org.apache.maven.continuum.model.project.Project;
27  import org.apache.maven.scm.ChangeSet;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
30  import org.apache.maven.scm.command.changelog.ChangeLogSet;
31  import org.codehaus.plexus.action.AbstractAction;
32  
33  import java.io.File;
34  import java.util.Date;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * @plexus.component role="org.codehaus.plexus.action.Action" role-hint="changelog-agent-project"
40   */
41  public class ChangeLogProjectAction
42      extends AbstractAction
43  {
44      /**
45       * @plexus.requirement
46       */
47      private BuildAgentConfigurationService buildAgentConfigurationService;
48  
49      /**
50       * @plexus.requirement
51       */
52      private ContinuumScm scm;
53  
54      public void execute( Map context )
55          throws Exception
56      {
57          Project project = ContinuumBuildAgentUtil.getProject( context );
58  
59          try
60          {
61              File workingDirectory = buildAgentConfigurationService.getWorkingDirectory( project.getId() );
62              ContinuumScmConfiguration config = createScmConfiguration( project, workingDirectory );
63              config.setLatestUpdateDate( ContinuumBuildAgentUtil.getLatestUpdateDate( context ) );
64              getLogger().info( "Getting changeLog of project: " + project.getName() );
65              ChangeLogScmResult changeLogResult = scm.changeLog( config );
66  
67              if ( !changeLogResult.isSuccess() )
68              {
69                  getLogger().warn( "Error getting change log of project " + project.getName() );
70  
71                  getLogger().warn( "Command Output: " + changeLogResult.getCommandOutput() );
72  
73                  getLogger().warn( "Provider Message: " + changeLogResult.getProviderMessage() );
74              }
75  
76              context.put( ContinuumBuildAgentUtil.KEY_LATEST_UPDATE_DATE, getLatestUpdateDate( changeLogResult ) );
77          }
78          catch ( ScmException e )
79          {
80              context.put( ContinuumBuildAgentUtil.KEY_LATEST_UPDATE_DATE, null );
81  
82              getLogger().error( e.getMessage(), e );
83          }
84      }
85  
86      private ContinuumScmConfiguration createScmConfiguration( Project project, File workingDirectory )
87      {
88          ContinuumScmConfiguration config = new ContinuumScmConfiguration();
89          config.setUrl( project.getScmUrl() );
90          config.setUsername( project.getScmUsername() );
91          config.setPassword( project.getScmPassword() );
92          config.setUseCredentialsCache( project.isScmUseCache() );
93          config.setWorkingDirectory( workingDirectory );
94          config.setTag( project.getScmTag() );
95  
96          return config;
97      }
98  
99      private Date getLatestUpdateDate( ChangeLogScmResult changeLogScmResult )
100     {
101         ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
102 
103         if ( changeLogSet != null )
104         {
105             List<ChangeSet> changes = changeLogSet.getChangeSets();
106 
107             if ( changes != null && !changes.isEmpty() )
108             {
109                 long date = 0;
110 
111                 for ( ChangeSet change : changes )
112                 {
113                     if ( date < change.getDate().getTime() )
114                     {
115                         date = change.getDate().getTime();
116                     }
117                 }
118 
119                 if ( date != 0 )
120                 {
121                     return new Date( date );
122                 }
123             }
124         }
125 
126         return null;
127     }
128 
129     public void setScm( ContinuumScm scm )
130     {
131         this.scm = scm;
132     }
133 
134     public void setBuildAgentConfigurationService( BuildAgentConfigurationService buildAgentConfigurationService )
135     {
136         this.buildAgentConfigurationService = buildAgentConfigurationService;
137     }
138 }