Skip to content

Commit

Permalink
Added TravisCI Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
David Witherspoon committed Jan 16, 2015
1 parent 902325c commit 5d26585
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 28 deletions.
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jgit.version>3.3.1.201403241930-r</jgit.version>
<jsch.agent.version>0.0.7</jsch.agent.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -49,6 +50,26 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch.agentproxy.jsch</artifactId>
<version>${jsch.agent.version}</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch.agentproxy.usocket-jna</artifactId>
<version>${jsch.agent.version}</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch.agentproxy.sshagent</artifactId>
<version>${jsch.agent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jsch</artifactId>
<version>0.1.49_1</version>
</dependency>
</dependencies>

<build>
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/io/dpwspoon/github/utils/DataReaper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.dpwspoon.github.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;

public class DataReaper {
Expand All @@ -26,4 +24,21 @@ public static List<GHRepository> getRepositoriesWithFile(String path, String nam
}, r -> repositories.add(r));
return repositories;
}

public static List<GHRepository> getRepositoriesWithOutFile(String path, String name)
throws IOException {
List<GHRepository> repositories = new ArrayList<GHRepository>();

RepoUtils.processRepositories("kaazing",
r -> {
try {
return !RepoUtils.hasFile(r, path, name);
} catch (IOException e) {
throw new RuntimeException(
"Could not determine if repo \"" + r.getName()
+ "\" has a pom.xml", e);
}
}, r -> repositories.add(r));
return repositories;
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/dpwspoon/github/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.dpwspoon.github.utils;

import java.io.File;

public final class FileUtils {
public static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/dpwspoon/github/utils/GithubUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.dpwspoon.github.utils;

import java.io.IOException;

import org.kohsuke.github.GitHubBuilder;

public final class GithubUtils {

public static final String MY_GITHUB_USER_NAME;

static {
try {
MY_GITHUB_USER_NAME = GitHubBuilder.fromCredentials().build().getMyself().getLogin();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
12 changes: 1 addition & 11 deletions src/main/java/io/dpwspoon/github/utils/RepoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,11 @@ public boolean test(GHRepository t) {
}
};

public static final String MY_GITHUB_USER_NAME;
static {
try {
MY_GITHUB_USER_NAME = GitHubBuilder.fromCredentials().build()
.getMyself().getLogin();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void processRepositories(String user,
Predicate<GHRepository> predicate, Consumer<GHRepository> consumer)
throws IOException {
PagedIterable<GHRepository> repositories;
if (MY_GITHUB_USER_NAME.equalsIgnoreCase(user)) {
if (GithubUtils.MY_GITHUB_USER_NAME.equalsIgnoreCase(user)) {
repositories = GitHubBuilder.fromCredentials().build().getMyself()
.listRepositories();
} else {
Expand Down
210 changes: 197 additions & 13 deletions src/main/java/io/dpwspoon/github/utils/TravisCIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,218 @@
import io.dpwspoon.github.utils.egit.PropertyFileCredentialsProvider;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.kohsuke.github.GHRepository;

public final class TravisCIUtils {

public static void addTravisCIBuildToAllJavaProjects(File workingDir)
throws IOException, InvalidRemoteException, TransportException,
GitAPIException {
List<GHRepository> repos = DataReaper.getRepositoriesWithFile("/",
"pom.xml");
public static void addTravisCIBuildToAllJavaProjects(File workingDir) throws IOException, InvalidRemoteException,
TransportException, GitAPIException {

List<String> alreadyCompletedButPullRequestNotMerged = new ArrayList<String>();
alreadyCompletedButPullRequestNotMerged.add("netx");
alreadyCompletedButPullRequestNotMerged.add("qpid.jms.itest");
alreadyCompletedButPullRequestNotMerged.add("code.quality");
alreadyCompletedButPullRequestNotMerged.add("nuklei.amqp_1_0.jms");
alreadyCompletedButPullRequestNotMerged.add("community");
alreadyCompletedButPullRequestNotMerged.add("common");
alreadyCompletedButPullRequestNotMerged.add("neoload.codec.jms");
alreadyCompletedButPullRequestNotMerged.add("community.license");
alreadyCompletedButPullRequestNotMerged.add("license-maven-plugin");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.amqp");
alreadyCompletedButPullRequestNotMerged.add("gateway.bridge");
alreadyCompletedButPullRequestNotMerged.add("gateway.client.java.bridge");
alreadyCompletedButPullRequestNotMerged.add("gateway.client.java.common");
alreadyCompletedButPullRequestNotMerged.add("gateway.client.java");
alreadyCompletedButPullRequestNotMerged.add("gateway.client.java.transport");
alreadyCompletedButPullRequestNotMerged.add("gateway.distribution");
alreadyCompletedButPullRequestNotMerged.add("gateway.management");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.http");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.httpx");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.httpxdraft");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.httpxe");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.pipe");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.rtmp");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.sse");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.ssl");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.tcp");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.udp");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.ws");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.wsdraft");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.wse");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.wsn");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.wsr");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.wsx");
alreadyCompletedButPullRequestNotMerged.add("gateway.resource.address.wsxdraft");
alreadyCompletedButPullRequestNotMerged.add("gateway.security");
alreadyCompletedButPullRequestNotMerged.add("gateway.server");
alreadyCompletedButPullRequestNotMerged.add("gateway.server.api");
alreadyCompletedButPullRequestNotMerged.add("gateway.server.demo");
alreadyCompletedButPullRequestNotMerged.add("gateway.server.spi");
alreadyCompletedButPullRequestNotMerged.add("gateway.service");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.broadcast");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.echo");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.http.balancer");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.http.directory");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.proxy");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.update.check");
alreadyCompletedButPullRequestNotMerged.add("gateway.service.update.check.management");
alreadyCompletedButPullRequestNotMerged.add("gateway.test.ca");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.bio");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.http");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.nio");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.pipe");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.sse");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.ssl");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.ws");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.wseb");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.wsn");
alreadyCompletedButPullRequestNotMerged.add("gateway.transport.wsr");
alreadyCompletedButPullRequestNotMerged.add("gateway.truststore");
alreadyCompletedButPullRequestNotMerged.add("gateway.util");
alreadyCompletedButPullRequestNotMerged.add("mina.core");
alreadyCompletedButPullRequestNotMerged.add("mina.netty");
alreadyCompletedButPullRequestNotMerged.add("test.util");
alreadyCompletedButPullRequestNotMerged.add("tools.wscat");
alreadyCompletedButPullRequestNotMerged.add("version-properties-maven-plugin");
alreadyCompletedButPullRequestNotMerged.add("gateway.client.java.demo");
alreadyCompletedButPullRequestNotMerged.add("amqp.client.java");
alreadyCompletedButPullRequestNotMerged.add("amqp.client.java.demo");
alreadyCompletedButPullRequestNotMerged.add("truststore-maven-plugin");
alreadyCompletedButPullRequestNotMerged.add("sigar.dist");
alreadyCompletedButPullRequestNotMerged.add("unpack-bower-dependency-maven-plugin");
alreadyCompletedButPullRequestNotMerged.add("snmp4j");
alreadyCompletedButPullRequestNotMerged.add("snmp4j.agent");



List<GHRepository> repos = DataReaper.getRepositoriesWithFile("/", "pom.xml");
for (GHRepository repo : repos) {
if (!RepoUtils.hasFile(repo, "/", ".travis.yml")) {
GHRepository forkedRepo = repo.fork();
String url = forkedRepo.getUrl();
File directory = new File("workingDir", forkedRepo.getName());
CredentialsProvider credentialsProvider = new PropertyFileCredentialsProvider();
Repository localRepo = Git.cloneRepository().setCredentialsProvider(credentialsProvider).setDirectory(directory)
.setURI(url).call().getRepository();
String mainRepoName = repo.getName();
if (!mainRepoName.contains("ios")) {
if (!alreadyCompletedButPullRequestNotMerged.contains(mainRepoName) && !RepoUtils.hasFile(repo, "/", ".travis.yml")) {
GHRepository forkedRepo = repo.fork();
String branchName = "feature-travisci";
String repoName = forkedRepo.getName();
addTravisFileToRepo(forkedRepo.getUrl(), branchName, repo.getOwner().getLogin(), repoName,
workingDir);
System.out.println("Added Repo: " + mainRepoName);
repo.createPullRequest("Added .travis.yml and badge to README.md", GithubUtils.MY_GITHUB_USER_NAME
+ ":" + branchName, repo.getMasterBranch(), "");
}
} else {
System.out.println("ignoring ios repo: " + mainRepoName);
}
}
}

private static final CredentialsProvider credentialsProvider = new PropertyFileCredentialsProvider();

public static void addTravisFileToRepo(final String uri, final String branchName, final String orgName,
final String repoName, File workingDir) throws IOException, InvalidRemoteException, TransportException,
GitAPIException {
File directory = new File(workingDir, repoName);
Git localRepo = null;
for (int trys = 0; true; trys++) {
try {
localRepo = Git.cloneRepository().setCredentialsProvider(credentialsProvider).setDirectory(directory)
.setURI(uri).call();
break;
} catch (TransportException e) {
// sporadic failure
FileUtils.deleteFolder(directory);
if (trys > 3) {
throw e;
}
}
}
localRepo.checkout().setCreateBranch(true).setName(branchName).call();
addTravisFileTo(localRepo, directory, orgName, repoName);
localRepo.commit().setAll(true).setMessage("Added .travis.yml and badge to README.md").call();
localRepo.push().setCredentialsProvider(credentialsProvider).call();
}

public static void addTravisFileTo(Git localRepo, File dir, String orgName, String repoName) throws IOException,
NoFilepatternException, GitAPIException {
String travisFileName = ".travis.yml";
try (FileWriter travisWriter = new FileWriter(new File(dir, travisFileName))) {
travisWriter.write("language: Java\n" + "jdk:\n" + " - oraclejdk7\n" + " - openjdk7\n"
+ "script: mvn verify\n");
}
localRepo.add().addFilepattern(travisFileName).call();

String readmeFileName = "README.md";
File readme = new File(dir, readmeFileName);
if (readme.exists()) {

String TEXT_TO_ADD = "[![Build Status][build-status-image]][build-status]\n\n"
+ String.format("[build-status-image]: https://travis-ci.org/%s/%s.svg?branch=develop\n", orgName,
repoName)
+ String.format("[build-status]: https://travis-ci.org/%s/%s\n", orgName, repoName);

List<String> fileLines = new ArrayList<String>();
Scanner scanner = null;
try {
scanner = new Scanner(readme);
boolean linesAdded = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
fileLines.add(line);
if (fileLines.size() == 2) {
linesAdded = true;
fileLines.add(TEXT_TO_ADD);
}
}
if (!linesAdded) {
fileLines.add(TEXT_TO_ADD);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}

PrintWriter pw = null;
try {
pw = new PrintWriter(readme);
for (String line : fileLines) {
pw.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}

} else {
try (FileWriter readmeWriter = new FileWriter(readme)) {
readmeWriter.write("#" + repoName + "\n" + "[![Build Status][build-status-image]][build-status]\n\n");
readmeWriter.write(String.format(
"[build-status-image]: https://travis-ci.org/%s/%s.svg?branch=develop\n", orgName, repoName));
readmeWriter.write(String.format("[build-status]: https://travis-ci.org/%s/%s", orgName, repoName));
readmeWriter.write("\n");
}
localRepo.add().addFilepattern(readmeFileName).call();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class PropertyFileCredentialsProvider extends
}

public PropertyFileCredentialsProvider() {

super(USERNAME, PASSWORD);
}

Expand Down
13 changes: 12 additions & 1 deletion src/test/java/io/dpwspoon/github/utils/DataReaperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
import java.io.IOException;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.kohsuke.github.GHRepository;

public class DataReaperTest {

@Test
public void printReposWithPomOnMainBranchViaGeneric() throws IOException{
@Ignore
public void printReposWithPomOnMainBranch() throws IOException{
List<GHRepository> repos = DataReaper.getRepositoriesWithFile("/", "pom.xml");
for(GHRepository repo: repos){
System.out.println(repo.getName());
}
}

@Test
public void printReposWithoutREADMEOnMainBranch() throws IOException{
List<GHRepository> repos = DataReaper.getRepositoriesWithOutFile("/", "README.md");
for(GHRepository repo: repos){
System.out.println(repo.getName());
}
}

}
Loading

0 comments on commit 5d26585

Please sign in to comment.