1 package org.apache.continuum.buildagent.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
28 import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
29 import org.apache.continuum.scm.ContinuumScm;
30 import org.apache.continuum.scm.ContinuumScmConfiguration;
31 import org.apache.maven.continuum.model.project.Project;
32 import org.apache.maven.scm.ChangeSet;
33 import org.apache.maven.scm.ScmException;
34 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
35 import org.apache.maven.scm.command.changelog.ChangeLogSet;
36 import org.codehaus.plexus.action.AbstractAction;
37
38
39
40
41 public class ChangeLogProjectAction
42 extends AbstractAction
43 {
44
45
46
47 private BuildAgentConfigurationService buildAgentConfigurationService;
48
49
50
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 getLogger().info( "Getting changeLog of project: " + project.getName() );
64 ChangeLogScmResult changeLogResult = scm.changeLog( config );
65
66 if ( !changeLogResult.isSuccess() )
67 {
68 getLogger().warn( "Error getting change log of project " + project.getName() );
69
70 getLogger().warn( "Command Output: " + changeLogResult.getCommandOutput() );
71
72 getLogger().warn( "Provider Message: " + changeLogResult.getProviderMessage() );
73 }
74
75 context.put( ContinuumBuildAgentUtil.KEY_LATEST_UPDATE_DATE, getLatestUpdateDate( changeLogResult ) );
76 }
77 catch ( ScmException e )
78 {
79 context.put( ContinuumBuildAgentUtil.KEY_LATEST_UPDATE_DATE, null );
80
81 getLogger().error( e.getMessage(), e );
82 }
83 }
84
85 private ContinuumScmConfiguration createScmConfiguration( Project project, File workingDirectory )
86 {
87 ContinuumScmConfiguration config = new ContinuumScmConfiguration();
88 config.setUrl( project.getScmUrl() );
89 config.setUsername( project.getScmUsername() );
90 config.setPassword( project.getScmPassword() );
91 config.setUseCredentialsCache( project.isScmUseCache() );
92 config.setWorkingDirectory( workingDirectory );
93 config.setTag( project.getScmTag() );
94 return config;
95 }
96
97 private Date getLatestUpdateDate( ChangeLogScmResult changeLogScmResult )
98 {
99 ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
100
101 if ( changeLogSet != null )
102 {
103 List<ChangeSet> changes = changeLogSet.getChangeSets();
104
105 if ( changes != null && !changes.isEmpty() )
106 {
107 long date = 0;
108
109 for ( ChangeSet change : changes )
110 {
111 if ( date < change.getDate().getTime() )
112 {
113 date = change.getDate().getTime();
114 }
115 }
116
117 if ( date != 0 )
118 {
119 return new Date( date );
120 }
121 }
122 }
123
124 return null;
125 }
126 }