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 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
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 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 }