Skip to content
Permalink
Browse files
Simple build log downloader implemented
  • Loading branch information
dspavlov committed Jul 21, 2017
1 parent 8a0700e commit a06a177fbd4a4d713510462e0ec6b9a2a3a4f927
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 50 deletions.
@@ -1,9 +1,14 @@
apply plugin: 'java'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

repositories {
mavenCentral()
}

dependencies {
compile group: 'com.google.guava', name: 'guava', version: '22.0'

testCompile group: 'junit', name: 'junit', version: '4.+'
}
@@ -1,32 +1,33 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.Properties;
import org.apache.ignite.ci.DownloadBuildLog;
import org.apache.ignite.ci.HelperConfig;
import org.apache.ignite.ci.HttpUtil;

import static org.apache.ignite.ci.HelperConfig.ensureDirExist;

/**
* Created by Дмитрий on 20.07.2017
*/
public class DownloadBuildLogTest {
public static void main(String[] args) throws Exception {
String s = ".ignite-teamcity-helper";
String property = System.getProperty("user.home");
File workDir = new File(property, s);
File file = new File(workDir, "auth.properties");
Properties properties = new Properties();
try (FileReader reader = new FileReader(file)){
properties.load(reader);
}
File workDir = HelperConfig.resolveWorkDir();
Properties properties = HelperConfig.loadAuthProperties(workDir);

String basicAuthToken = HelperConfig.prepareBasicHttpAuthToken(properties);

File logsDir = ensureDirExist(new File(workDir, "logs"));

int buildId = 736822;
DownloadBuildLog buildLog = new DownloadBuildLog(buildId, basicAuthToken, logsDir);
File run = buildLog.run();
System.out.println("Cached locally: [" + run.getCanonicalPath()
+ "], " + run.toURI());

sendGet(properties);
}

// HTTP GET request
private static void sendGet(Properties properties) throws Exception {
private static void sendGet(String basicAuthToken) throws Exception {
//&archived=true
//https://confluence.jetbrains.com/display/TCD10/REST+API
String url = "http://ci.ignite.apache.org/downloadBuildLog.html?buildId=735562";
@@ -39,42 +40,11 @@ private static void sendGet(Properties properties) throws Exception {
String particularInvocation = "http://ci.ignite.apache.org/app/rest/testOccurrences/id:108126,build:(id:735392)";
String searchTest = "http://ci.ignite.apache.org/app/rest/tests/id:586327933473387239";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// optional default is GET
con.setRequestMethod("GET");


String user = properties.getProperty("username");
String password = properties.getProperty("password");
String encoding =
new String(Base64.getEncoder().encode ((user +
":" +
password).getBytes()));

con.setRequestProperty("Authorization", "Basic " + encoding);

//add request header
// con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
response.append("\n");
}
in.close();
String response = HttpUtil.sendGetAsString(basicAuthToken, url);

//print result
System.out.println(response.toString());
System.out.println(response);

}

}
@@ -0,0 +1,33 @@
package org.apache.ignite.ci;

import java.io.File;
import java.io.IOException;

/**
* Created by Дмитрий on 21.07.2017
*/
public class DownloadBuildLog {
private int buildId;
private final String token;
private final File dir;

public DownloadBuildLog(int buildId, String basicAuthToken, File logsDir) {
this.buildId = buildId;
token = basicAuthToken;
dir = logsDir;
}

public File run() throws IOException {
String buildIdStr = Integer.toString(buildId);
String url = "http://ci.ignite.apache.org/downloadBuildLog.html?buildId=" + buildIdStr;
File file = new File(dir, "buildId" + buildIdStr + ".log");
if(file.exists() && file.canRead() && file.length()>0) {
System.err.println("Nothing to do, file is cached " + file);
return file;
}
HttpUtil.sendGetCopyToFile(token, url, file);
return file;


}
}
@@ -0,0 +1,65 @@
package org.apache.ignite.ci;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Base64;
import java.util.Properties;

import static com.google.common.base.Preconditions.checkState;

/**
* Created by Дмитрий on 21.07.2017
*/
public class HelperConfig {
private static final String USERNAME = "username";
private static final String PASSWORD = "password";

public static Properties loadAuthProperties(File workDir) throws IOException {
String configFileName = "auth.properties";
File file = new File(workDir, configFileName);
if (!(file.exists())) {

String endl = String.format("%n");
try (FileWriter writer = new FileWriter(file)) {
writer.write(USERNAME +
"=" );
writer.write(endl);
writer.write(PASSWORD +
"=");
writer.write(endl);
}
throw new IllegalStateException("Please setup username and password in config file [" +
file.getCanonicalPath() + "]");
}
Properties properties = new Properties();
try (FileReader reader = new FileReader(file)) {
properties.load(reader);
}
return properties;
}

public static File ensureDirExist(File workDir) {
if (!workDir.exists())
checkState(workDir.mkdirs(), "Unable to make directory [" + workDir + "]");

return workDir;
}

public static File resolveWorkDir() {
String conf = ".ignite-teamcity-helper";
String property = System.getProperty("user.home");
File workDir = new File(property, conf);

return ensureDirExist(workDir);
}

public static String prepareBasicHttpAuthToken(Properties properties) {
String user = properties.getProperty(USERNAME);
String password = properties.getProperty(PASSWORD);
return new String(Base64.getEncoder().encode((user +
":" +
password).getBytes()));
}
}
@@ -0,0 +1,54 @@
package org.apache.ignite.ci;

import com.google.common.base.Preconditions;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* Created by Дмитрий on 21.07.2017
*/
public class HttpUtil {

public static String sendGetAsString(String basicAuthToken, String url) throws IOException {
InputStream inputStream = sendGetWithBasicAuth(basicAuthToken, url);
BufferedReader in = new BufferedReader(
new InputStreamReader(inputStream));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
response.append("\n");
}
in.close();
return response.toString();
}

private static InputStream sendGetWithBasicAuth(String basicAuthToken, String url) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection)obj.openConnection();

// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Basic " + basicAuthToken);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
Preconditions.checkState(200==(responseCode),
"Invalid Response Code : " + responseCode);

return con.getInputStream();
}

public static void sendGetCopyToFile(String token, String url, File file) throws IOException {
try (InputStream inputStream = sendGetWithBasicAuth(token, url)){
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}

0 comments on commit a06a177

Please sign in to comment.