001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.plugin;
020
021import javax.inject.Inject;
022
023import java.io.IOException;
024import java.text.ParseException;
025import java.text.SimpleDateFormat;
026import java.util.Date;
027
028import org.apache.maven.plugin.MojoExecutionException;
029import org.apache.maven.plugins.annotations.Mojo;
030import org.apache.maven.plugins.annotations.Parameter;
031import org.apache.maven.scm.ChangeSet;
032import org.apache.maven.scm.ScmBranch;
033import org.apache.maven.scm.ScmException;
034import org.apache.maven.scm.ScmVersion;
035import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
036import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
037import org.apache.maven.scm.command.changelog.ChangeLogSet;
038import org.apache.maven.scm.manager.ScmManager;
039import org.apache.maven.scm.provider.ScmProvider;
040import org.apache.maven.scm.repository.ScmRepository;
041import org.apache.maven.settings.crypto.SettingsDecrypter;
042
043/**
044 * Dump changelog contents to console. It is mainly used to test maven-scm-api's changelog command.
045 *
046 * @author <a href="dantran@gmail.com">Dan Tran</a>
047 * @author Olivier Lamy
048 */
049@Mojo(name = "changelog", aggregator = true)
050public class ChangeLogMojo extends AbstractScmMojo {
051
052    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
053
054    /**
055     * Start Date.
056     */
057    @Parameter(property = "startDate")
058    private String startDate;
059
060    /**
061     * End Date.
062     */
063    @Parameter(property = "endDate")
064    private String endDate;
065
066    /**
067     * Start Scm Version.
068     */
069    @Parameter(property = "startScmVersion")
070    private String startScmVersion;
071
072    /**
073     * End Scm Version.
074     */
075    @Parameter(property = "endScmVersion")
076    private String endScmVersion;
077
078    /**
079     * Start Scm Version Type.
080     */
081    @Parameter(property = "startScmVersionType")
082    private String startScmVersionType;
083
084    /**
085     * End Scm Version Type.
086     */
087    @Parameter(property = "endScmVersionType")
088    private String endScmVersionType;
089
090    /**
091     * Date Format in changelog output of scm tool.
092     */
093    @Parameter(property = "dateFormat")
094    private String dateFormat;
095
096    /**
097     * Date format to use for the specified startDate and/or endDate.
098     */
099    @Parameter(property = "userDateFormat", defaultValue = "yyyy-MM-dd")
100    private String userDateFormat = DEFAULT_DATE_FORMAT;
101
102    /**
103     * The version type (branch/tag) of scmVersion.
104     */
105    @Parameter(property = "scmVersionType")
106    private String scmVersionType;
107
108    /**
109     * The version (revision number/branch name/tag name).
110     */
111    @Parameter(property = "scmVersion")
112    private String scmVersion;
113
114    /**
115     * The branch name (TODO find out what this is for).
116     */
117    @Parameter(property = "scmBranch")
118    private String scmBranch;
119
120    /**
121     * The number of change log items to return.
122     */
123    @Parameter(property = "limit")
124    private Integer limit;
125
126    /**
127     * The number of days to look back for change log items to return.
128     */
129    @Parameter(property = "numDays")
130    private Integer numDays;
131
132    @Inject
133    public ChangeLogMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
134        super(manager, settingsDecrypter);
135    }
136
137    /**
138     * {@inheritDoc}
139     */
140    public void execute() throws MojoExecutionException {
141        super.execute();
142
143        SimpleDateFormat localFormat = new SimpleDateFormat(userDateFormat);
144
145        try {
146            ScmRepository repository = getScmRepository();
147
148            ScmProvider provider = getScmManager().getProviderByRepository(repository);
149
150            ChangeLogScmRequest request = new ChangeLogScmRequest(repository, getFileSet());
151
152            request.setDatePattern(dateFormat);
153
154            if (startDate != null && !startDate.isEmpty()) {
155                request.setStartDate(parseDate(localFormat, startDate));
156            }
157
158            if (endDate != null && !endDate.isEmpty()) {
159                request.setEndDate(parseDate(localFormat, endDate));
160            }
161
162            if (startScmVersion != null && !startScmVersion.isEmpty()) {
163                ScmVersion startRev = getScmVersion(
164                        (startScmVersionType == null || startScmVersionType.isEmpty())
165                                ? VERSION_TYPE_REVISION
166                                : startScmVersionType,
167                        startScmVersion);
168                request.setStartRevision(startRev);
169            }
170
171            if (endScmVersion != null && !endScmVersion.isEmpty()) {
172                ScmVersion endRev = getScmVersion(
173                        (endScmVersionType == null || endScmVersionType.isEmpty())
174                                ? VERSION_TYPE_REVISION
175                                : endScmVersionType,
176                        endScmVersion);
177                request.setEndRevision(endRev);
178            }
179
180            request.setLimit(limit);
181
182            if (numDays != null) {
183                request.setNumDays(numDays);
184            }
185
186            if (scmVersion != null && !scmVersion.isEmpty()) {
187                ScmVersion rev = getScmVersion(
188                        (scmVersionType == null || scmVersionType.isEmpty()) ? VERSION_TYPE_REVISION : scmVersionType,
189                        scmVersion);
190                request.setRevision(rev);
191            }
192
193            if (scmBranch != null && !scmBranch.isEmpty()) {
194                request.setScmBranch(new ScmBranch(scmBranch));
195            }
196
197            ChangeLogScmResult result = provider.changeLog(request);
198
199            checkResult(result);
200
201            ChangeLogSet changeLogSet = result.getChangeLog();
202
203            for (ChangeSet changeSet : changeLogSet.getChangeSets()) {
204                getLog().info(changeSet.toString());
205            }
206
207        } catch (IOException | ScmException e) {
208            throw new MojoExecutionException("Cannot run changelog command : ", e);
209        }
210    }
211
212    /**
213     * Converts the localized date string pattern to date object.
214     *
215     * @return A date
216     */
217    private Date parseDate(SimpleDateFormat format, String date) throws MojoExecutionException {
218        if (date == null || date.trim().length() == 0) {
219            return null;
220        }
221
222        try {
223            return format.parse(date.toString());
224        } catch (ParseException e) {
225            throw new MojoExecutionException(
226                    "Please use this date pattern: "
227                            + format.toLocalizedPattern().toString(),
228                    e);
229        }
230    }
231}