buildframework/helium/sf/java/scm/src/com/nokia/helium/scm/ant/actions/ChangelogAction.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 package com.nokia.helium.scm.ant.actions;
    18 package com.nokia.helium.scm.ant.actions;
    19 
    19 
       
    20 import java.io.BufferedWriter;
       
    21 import java.io.File;
       
    22 import java.io.FileWriter;
       
    23 import java.io.IOException;
       
    24 import java.text.ParseException;
       
    25 import java.text.SimpleDateFormat;
    20 import java.util.Date;
    26 import java.util.Date;
    21 import java.text.SimpleDateFormat;
       
    22 import java.util.Iterator;
    27 import java.util.Iterator;
    23 import java.io.*;
    28 
    24 import java.text.ParseException;
    29 import org.apache.maven.scm.ChangeSet;
    25 
       
    26 import org.dom4j.Document;
       
    27 import org.dom4j.Element;
       
    28 import org.dom4j.io.SAXReader;
       
    29 import org.dom4j.DocumentException;
       
    30 
       
    31 import org.apache.maven.scm.ScmException;
    30 import org.apache.maven.scm.ScmException;
    32 import org.apache.maven.scm.ScmRevision;
    31 import org.apache.maven.scm.ScmRevision;
    33 import org.apache.maven.scm.ChangeSet;
       
    34 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
    32 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
    35 import org.apache.maven.scm.command.changelog.ChangeLogSet;
    33 import org.apache.maven.scm.command.changelog.ChangeLogSet;
    36 import org.apache.maven.scm.manager.ScmManager;
    34 import org.apache.maven.scm.manager.ScmManager;
    37 import org.apache.maven.scm.repository.ScmRepository;
    35 import org.apache.maven.scm.repository.ScmRepository;
    38 import org.apache.tools.ant.BuildException;
    36 import org.apache.tools.ant.BuildException;
    39 
    37 import org.dom4j.Document;
       
    38 import org.dom4j.DocumentException;
       
    39 import org.dom4j.Element;
       
    40 import org.dom4j.io.SAXReader;
    40 
    41 
    41 /**
    42 /**
    42  * Get change log within a date range i.e startDate and endDate; 
    43  * Get change log within a date range i.e startDate and endDate; OR get change log of a number of
    43  * OR get change log of a number of days i.e numDays
    44  * days i.e numDays OR get change log within a range of starVersion and endVersion Parameters either
    44  * OR get change log within a range of starVersion and endVersion
    45  * startDate="20090317 18:49:31" endDate="20090318 24:49:31" datePattern="yyyyMMdd HH:mm:ss" Or
    45  * Parameters either startDate="20090317 18:49:31" endDate="20090318 24:49:31" datePattern="yyyyMMdd HH:mm:ss"
    46  * numDays='1' Or startVersion="1" endVersion="2" Add logOutput="xml" to output log in xml format
    46  * Or numDays='1'
       
    47  * Or startVersion="1" endVersion="2"
       
    48  * Add logOutput="xml" to output log in xml format
       
    49  * 
    47  * 
    50  * <pre>
    48  * <pre>
    51  * &lt;hlm:scm verbose="false" scmUrl="scm:hg:${repo.dir}/changelog"&gt;
    49  * &lt;hlm:scm verbose="false" scmUrl="scm:hg:${repo.dir}/changelog"&gt;
    52  *     &lt;hlm:changelog basedir="${repo.dir}/changelog" startVersion="1" endVersion="2"/&gt;
    50  *     &lt;hlm:changelog basedir="${repo.dir}/changelog" startVersion="1" endVersion="2"/&gt;
    53  *     &lt;hlm:changelog baseDir="${repo.dir}/changelog" numDays='1' /&gt;
    51  *     &lt;hlm:changelog baseDir="${repo.dir}/changelog" numDays='1' /&gt;
    54  * &lt;/hlm:scm&gt;
    52  * &lt;/hlm:scm&gt;
    55  * </pre>
    53  * </pre>
    56  *
    54  * 
    57  * @ant.type name="changelog" category="SCM"
    55  * @ant.type name="changelog" category="SCM"
    58  */
    56  */
    59 public class ChangelogAction extends BaseDirectoryScmAction {
    57 public class ChangelogAction extends BaseDirectoryScmAction {
    60 
    58 
    61     private String startDate;
    59     private String startDate;
    63     private String endDate;
    61     private String endDate;
    64 
    62 
    65     private String datePattern;
    63     private String datePattern;
    66 
    64 
    67     private String logOutput;
    65     private String logOutput;
    68     
    66 
    69     private int numDays;
    67     private int numDays;
    70 
    68 
    71     private String startVersion;
    69     private String startVersion;
    72 
    70 
    73     private String endVersion;
    71     private String endVersion;
    74     private File xmlbom;
    72     private File xmlbom;
    75 
    73 
    76 
       
    77     /**
    74     /**
    78      * Start version
    75      * Start version
    79      * 
    76      * 
    80      * @ant.not-required
    77      * @ant.not-required
    81      */
    78      */
   130 
   127 
   131     /**
   128     /**
   132      * Output pattern default is log output, to get xml output set logoutput=xml
   129      * Output pattern default is log output, to get xml output set logoutput=xml
   133      * 
   130      * 
   134      * @ant.not-required
   131      * @ant.not-required
   135      */    
   132      */
   136     public void setLogOutput(String logOutput)
   133     public void setLogOutput(String logOutput) {
   137     {
       
   138         this.logOutput = logOutput;
   134         this.logOutput = logOutput;
   139     }
   135     }
   140     
   136 
   141     /**
   137     /**
   142      * File for xml output of changeset list
   138      * File for xml output of changeset list
   143      * 
   139      * 
   144      * @ant.not-required
   140      * @ant.not-required
   145      */ 
   141      */
   146     public void setXmlbom(File xmlbom)
   142     public void setXmlbom(File xmlbom) {
   147     {
       
   148         this.xmlbom = xmlbom;
   143         this.xmlbom = xmlbom;
   149     }
   144     }
   150 
   145 
   151     /**
   146     /**
   152      * {@inheritDoc}
   147      * {@inheritDoc}
   153      */
   148      */
   154     @SuppressWarnings("unchecked")
   149     @SuppressWarnings("unchecked")
   155     @Override
   150     @Override
   156     public void execute(ScmRepository repository) throws ScmException
   151     public void execute(ScmRepository repository) throws ScmException {
   157     {
       
   158         ScmManager scmManager = getTask().getScmManager();
   152         ScmManager scmManager = getTask().getScmManager();
   159         ScmRevision startRevision = new ScmRevision(startVersion);
   153         ScmRevision startRevision = new ScmRevision(startVersion);
   160         ScmRevision endRevision = new ScmRevision(endVersion);
   154         ScmRevision endRevision = new ScmRevision(endVersion);
   161 
   155 
   162         ChangeLogScmResult result;
   156         ChangeLogScmResult result;
   163         Date start = null;
   157         Date start = null;
   164         Date end = null;
   158         Date end = null;
   165 
   159 
   166         if (numDays == 0 && (startDate != null || endDate != null) )
   160         if (numDays == 0 && (startDate != null || endDate != null)) {
   167         {
       
   168             try {
   161             try {
   169                 SimpleDateFormat format = new SimpleDateFormat(datePattern);
   162                 SimpleDateFormat format = new SimpleDateFormat(datePattern);
   170                 start = format.parse(startDate);
   163                 start = format.parse(startDate);
   171                 end = format.parse(endDate);
   164                 end = format.parse(endDate);
   172             } catch (ParseException e) {
   165             }
       
   166             catch (ParseException e) {
   173                 throw new ScmException("Date Format not supported:" + e.getMessage());
   167                 throw new ScmException("Date Format not supported:" + e.getMessage());
   174             }
   168             }
   175         }
   169         }
   176         else 
   170         else {
   177         {
       
   178             start = null;
   171             start = null;
   179             end = null;
   172             end = null;
   180         }
   173         }
   181         try
   174         try {
   182         {
   175             if (startVersion == null) {
   183         if (startVersion == null)
   176                 result = scmManager.changeLog(repository, getScmFileSet(), start, end, numDays, null, datePattern);
   184             result = scmManager.changeLog(repository, getScmFileSet(), start, end, numDays, null, datePattern);
   177             } else {
   185         else
   178                 result = scmManager.changeLog(repository, getScmFileSet(), startRevision, endRevision, datePattern);
   186             result = scmManager.changeLog(repository, getScmFileSet(), startRevision, endRevision, datePattern);
   179             }
   187         } catch (ScmException e) {
   180         }
       
   181         catch (ScmException e) {
   188             throw new BuildException("Execution of SCM changelog action failed.");
   182             throw new BuildException("Execution of SCM changelog action failed.");
   189         }
   183         }
   190         if (!result.isSuccess()) {
   184         if (!result.isSuccess()) {
   191             throw new BuildException("SCM changelog command unsuccessful.");
   185             throw new BuildException("SCM changelog command unsuccessful.");
   192         }
   186         }
   193         // Output changelog information
   187         // Output changelog information
   194         ChangeLogSet changelogSet = result.getChangeLog();
   188         ChangeLogSet changelogSet = result.getChangeLog();
   195         if (logOutput != null && logOutput.equals("xml"))
   189         if (logOutput != null && logOutput.equals("xml")) {
   196         {
       
   197             getTask().log(changelogSet.toXML());
   190             getTask().log(changelogSet.toXML());
   198         }
   191         }
   199         else if (xmlbom != null)
   192         else if (xmlbom != null) {
   200         {
       
   201             String output = "";
   193             String output = "";
   202             for (Object object : changelogSet.getChangeSets())
   194             for (Object object : changelogSet.getChangeSets()) {
   203             {
       
   204                 String revision = "";
   195                 String revision = "";
   205                 ChangeSet changeSet = (ChangeSet)object;
   196                 ChangeSet changeSet = (ChangeSet) object;
   206                 for (String line : changeSet.toString().split("\n"))
   197                 for (String line : changeSet.toString().split("\n")) {
   207                 {
   198                     if (line.contains("revision:")) {
   208                     if (line.contains("revision:"))
       
   209                         revision = line.replace("revision:", "");
   199                         revision = line.replace("revision:", "");
       
   200                     }
   210                 }
   201                 }
   211                 
   202 
   212                 output = output + "<task><id>" + revision + "</id><synopsis>" + changeSet.getComment() + "</synopsis><completed>" + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(changeSet.getDate()) + "</completed></task>";
   203                 output = output + "<task><id>" + revision + "</id><synopsis>"
       
   204                     + changeSet.getComment() + "</synopsis><completed>"
       
   205                     + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(changeSet.getDate())
       
   206                     + "</completed></task>";
   213             }
   207             }
   214             String[] path = getTask().getScmUrl().split("/");
   208             String[] path = getTask().getScmUrl().split("/");
   215             String xml = "<bom><build>untitled</build><content>\n";
   209             String xml = "<bom><build>untitled</build><content>\n";
   216             String thisproject = "<project>" + "<name>" + path[path.length - 1] + "</name>" + "<baseline>" + getTask().getScmUrl() + "</baseline>" + "<database>mercurial</database>" + output + "</project>\n";
   210             String thisproject = "<project>" + "<name>" + path[path.length - 1] + "</name>"
       
   211                 + "<baseline>" + getTask().getScmUrl() + "</baseline>"
       
   212                 + "<database>mercurial</database>" + output + "</project>\n";
   217             xml = xml + thisproject;
   213             xml = xml + thisproject;
   218             try {
   214             try {
   219                 if (xmlbom.exists())
   215                 if (xmlbom.exists()) {
   220                 {
       
   221                     SAXReader xmlReader = new SAXReader();
   216                     SAXReader xmlReader = new SAXReader();
   222                     Document antDoc = xmlReader.read(xmlbom);
   217                     Document antDoc = xmlReader.read(xmlbom);
   223                     for (Iterator iterator = antDoc.selectNodes("//project").iterator(); iterator.hasNext();)
   218                     for (Iterator iterator = antDoc.selectNodes("//project").iterator(); iterator.hasNext();) {
   224                     {
       
   225                         boolean equal = false;
   219                         boolean equal = false;
   226                         Element element = (Element) iterator.next();
   220                         Element element = (Element) iterator.next();
   227                         for (Iterator iterator2 = antDoc.selectNodes("//baseline").iterator(); iterator2.hasNext();)
   221                         for (Iterator iterator2 = antDoc.selectNodes("//baseline").iterator(); iterator2.hasNext();) {
   228                         {
   222                             Element e2 = (Element) iterator2.next();
   229                               Element e2 = (Element) iterator2.next();
   223                             if (e2.getText().equals(getTask().getScmUrl())) {
   230                               if (e2.getText().equals(getTask().getScmUrl()))
   224                                 equal = true;
   231                                   equal = true;
   225                             }
   232                         }
   226                         }
   233                         if (!equal)
   227                         if (!equal) {
   234                             xml = xml + element.asXML() + "\n";
   228                             xml = xml + element.asXML() + "\n";
       
   229                         }
   235                     }
   230                     }
   236                 }
   231                 }
   237                 xml = xml + "</content></bom>";
   232                 xml = xml + "</content></bom>";
   238                 
   233 
   239                 FileWriter fstream = new FileWriter(xmlbom);
   234                 FileWriter fstream = new FileWriter(xmlbom);
   240                 BufferedWriter out = new BufferedWriter(fstream);
   235                 BufferedWriter out = new BufferedWriter(fstream);
   241                 out.write(xml);
   236                 out.write(xml);
   242                 out.close();
   237                 out.close();
   243             } catch (DocumentException e) { e.printStackTrace(); }
   238             } catch (DocumentException e) {
   244             catch (IOException e) { e.printStackTrace(); }
   239                 e.printStackTrace();
   245         }
   240             } catch (IOException e) {
   246         else
   241                 e.printStackTrace();
   247         {
   242             }
       
   243         }
       
   244         else {
   248             Iterator iterator = changelogSet.getChangeSets().iterator();
   245             Iterator iterator = changelogSet.getChangeSets().iterator();
   249             while (iterator.hasNext())
   246             while (iterator.hasNext()) {
   250             {
       
   251                 getTask().log(iterator.next().toString());
   247                 getTask().log(iterator.next().toString());
   252             }
   248             }
   253         }
   249         }
   254     }
   250     }
   255 }
   251 }