Skip to content

Commit

Permalink
logging for unparsable dates
Browse files Browse the repository at this point in the history
  • Loading branch information
cnenning committed Dec 14, 2016
1 parent fec602f commit 328f5a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
32 changes: 15 additions & 17 deletions src/main/java/com/github/cnenning/artiscm/ArtifactoryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -33,14 +32,17 @@
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import com.thoughtworks.go.plugin.api.logging.Logger;

public class ArtifactoryClient {

protected Logger logger = Logger.getLoggerFor(getClass());

public void downloadFiles(String url, HttpClient client, String targetDirPath, String patternStr) throws ClientProtocolException, IOException {
// create target dir
File targetDir = new File(targetDirPath);
if (!targetDir.exists()) {
System.out.print("creating target dir: ");
System.out.println(targetDirPath);
logger.info("creating target dir: " + targetDirPath);
targetDir.mkdirs();
}

Expand All @@ -67,9 +69,7 @@ public void downloadFiles(String url, HttpClient client, String targetDirPath, S
filename = escapeName(filename);
String completeUrl = url + filename;

// print to sys out to see it in go-console-view
System.out.print("downloading ");
System.out.println(completeUrl);
logger.info("downloading " + completeUrl);

HttpGet httpget = new HttpGet(completeUrl);
HttpResponse response = client.execute(httpget);
Expand Down Expand Up @@ -212,7 +212,7 @@ protected List<Revision> revisions(String url, HttpClient client, Document docum
for (Element link : links) {
String href = link.attr("href");
if (isDir(href)) {
Revision rev = elementToRev(link, since);
Revision rev = elementToRev(link, since, url);
if (rev != null) {
revisions.add(rev);
if (since != null) {
Expand Down Expand Up @@ -251,7 +251,7 @@ public List<Revision> callback(String url, HttpClient client, Document document)
for (Element link : links) {
String href = link.attr("href");
if (isFile(href)) {
Revision rev = elementToRev(link, null);
Revision rev = elementToRev(link, null, url);
if (rev != null) {
revisions.add(rev);
}
Expand Down Expand Up @@ -290,13 +290,13 @@ public Revision latestFileMatching(String url, String patternStr, HttpClient cli
return latest;
}

protected Revision elementToRev(Element link, Date since) {
protected Revision elementToRev(Element link, Date since, String url) {
String name = link.text();
Node nextSibling = link.nextSibling();
if (nextSibling instanceof TextNode) {
TextNode textNode = (TextNode) nextSibling;
String text = textNode.text();
Date date = findDateInText(text);
Date date = findDateInText(text, url);

if (since == null || date.getTime() > since.getTime()) {
// remove trailing slash
Expand All @@ -316,17 +316,15 @@ protected Revision elementToRev(Element link, Date since) {

public static final DateFormat HTML_DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy HH:mm");

protected Date findDateInText(String text) {
protected Date findDateInText(String text, String url) {
if (text != null) {
text = text.trim();
// note: text contains more than just date which is OK for this java api
try
{
try {
return HTML_DATE_FORMAT.parse(text);
}
catch (ParseException e)
{
// we could log this, but never mind
} catch (Exception e) {
logger.warn("could not parse date: '" + text + "', url: " + url);
logger.debug(e.getMessage(), e);
}
}
return new Date(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.cnenning.artiscm;

import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.message.BasicHttpResponse;
Expand Down Expand Up @@ -81,10 +83,22 @@ public void isFileSlash() {
Assert.assertFalse(File);
}


@Test
public void isFileHash() {
boolean File = new ArtifactoryClient().isFile("foo.md5");
Assert.assertFalse(File);
}

@Test
public void findDateInText() {
//Date date = new ArtifactoryClient().findDateInText("01-Jan-2016 09:30", "");
Date date = new ArtifactoryClient().findDateInText("16-Nov-2016 01:40 4.88 MB", "");
Assert.assertTrue(date.getTime() > 0);
}

@Test
public void findDateInTextInvalid() {
Date date = new ArtifactoryClient().findDateInText("invalid", "");
Assert.assertEquals(0, date.getTime());
}
}

0 comments on commit 328f5a6

Please sign in to comment.