diff --git a/.gitignore b/.gitignore index b3b07cb..4516d5e 100644 --- a/.gitignore +++ b/.gitignore @@ -13,10 +13,5 @@ target/ # Repository wide ignore mac DS_Store files .DS_Store -# Created by Zanata -/org.uberfire -/org.dashbuilder - # Live editing asciidoc leaves .html files behind in the source dir -uberfire-docs/src/main/asciidoc/*.html **/dependency-reduced-pom.xml diff --git a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/GitHook.java b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/GitHook.java index fdd7948..f3dabba 100644 --- a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/GitHook.java +++ b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/GitHook.java @@ -31,10 +31,30 @@ public class GitHook { + private enum SyncMode { + ALWAYS, + ON_SYNC + } + public static void main(String[] args) throws IOException, GitAPIException { + final String username = System.getProperty("gh.username"); + final String password = System.getProperty("gh.password"); + final String bcurl = System.getProperty("bc.url"); + + final SyncMode syncMode = getSyncMode(); + + if (bcurl == null) { + System.err.println("System property 'bc.url' not provided."); + System.exit(1); + } + System.out.println("'gh.username' " + username); + System.out.println("'gh.password' " + password); + System.out.println("'bc.url' " + bcurl); + final Path currentPath = new File("").toPath().toAbsolutePath(); final String parentFolderName = currentPath.getParent().getName(currentPath.getParent().getNameCount() - 1).toString(); - if (parentFolderName.equalsIgnoreCase("system")) { + if (parentFolderName.equalsIgnoreCase("system") || + parentFolderName.equalsIgnoreCase("config")) { return; } final Repository repo = new FileRepositoryBuilder() @@ -66,15 +86,14 @@ public static void main(String[] args) throws IOException, GitAPIException { }) .max(comparing((RevCommit commit) -> commit.getAuthorIdent().getWhen())) .ifPresent(newestCommit -> { - RevCommit commit = newestCommit; try { - boolean hasSyncOnCommitMessage = commit.getFullMessage().trim().startsWith("sync:"); + boolean executeRemoteSync = syncMode.equals(SyncMode.ALWAYS) || newestCommit.getFullMessage().trim().startsWith("sync:"); - if (hasSyncOnCommitMessage) { + if (executeRemoteSync) { final Map branchesAffected = git .nameRev() .addPrefix("refs/heads") - .add(commit) + .add(newestCommit) .call(); for (String remoteName : remotes) { @@ -96,11 +115,13 @@ public static void main(String[] args) throws IOException, GitAPIException { } } else { git.fetch().call(); - final TrackingStatusCommand trackingStatusCommand = new TrackingStatusCommand(git.getRepository()); - final TrackingStatus counts = trackingStatusCommand.getCounts(ref); - if (counts.getCommitsAhead() > 0) { - final RevCommit id = new GetPreviousCommitCommand(repo).execute(commit, counts.getCommitsAhead() - 1); - new SquashCommand(git, ref, id.name(), commit.getFullMessage()).execute(commit); + if (!syncMode.equals(SyncMode.ALWAYS)) { + final TrackingStatusCommand trackingStatusCommand = new TrackingStatusCommand(git.getRepository()); + final TrackingStatus counts = trackingStatusCommand.getCounts(ref); + if (counts.getCommitsAhead() > 0) { + final RevCommit id = new GetPreviousCommitCommand(repo).execute(newestCommit, counts.getCommitsAhead() - 1); + new SquashCommand(git, ref, id.name(), newestCommit.getFullMessage()).execute(newestCommit); + } } git.push() .setRefSpecs(new RefSpec(ref + ":" + ref)) @@ -116,4 +137,12 @@ public static void main(String[] args) throws IOException, GitAPIException { } }); } + + private static SyncMode getSyncMode() { + try { + return SyncMode.valueOf(System.getProperty("sync.mode", SyncMode.ALWAYS.name()).toUpperCase()); + } catch (Exception ex) { + return SyncMode.ALWAYS; + } + } } diff --git a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/GetSpaceName.java b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/GetSpaceName.java new file mode 100644 index 0000000..de5fb9c --- /dev/null +++ b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/GetSpaceName.java @@ -0,0 +1,10 @@ +package porcelli.me.git.integration.githook.command; + +import java.nio.file.Path; + +public class GetSpaceName implements Command { + + public String execute(final Path currentPath) { + return currentPath.getName(currentPath.getNameCount() - 2).toString(); + } +} diff --git a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/SetupRemote.java b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/SetupRemote.java index 488d68f..d873aaa 100644 --- a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/SetupRemote.java +++ b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/command/SetupRemote.java @@ -28,8 +28,9 @@ public String execute(final Git git, final Path currentPath) throws IOException, GitAPIException { final StoredConfig storedConfig = git.getRepository().getConfig(); + final String spaceName = new GetSpaceName().execute(currentPath); final String repoName = new GetRepoName().execute(currentPath); - final String remoteURL = integration.createRepository(repoName); + final String remoteURL = integration.createRepository(spaceName, repoName); storedConfig.setString("remote", "origin", "url", remoteURL); storedConfig.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*"); diff --git a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubCredentials.java b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubCredentials.java index 6980d68..61fafc3 100644 --- a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubCredentials.java +++ b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubCredentials.java @@ -13,18 +13,25 @@ public class GitHubCredentials { private String space = null; public GitHubCredentials() { - File homeDir = new File(System.getProperty("user.home")); - File propertyFile = new File(homeDir, ".github"); - - Properties props = new Properties(); - try (FileInputStream in = new FileInputStream(propertyFile)) { - props.load(in); - } catch (Exception ex) { - ex.printStackTrace(); - throw new RuntimeException(ex); + final String username = System.getProperty("gh.username"); + final String password = System.getProperty("gh.password"); + + if (username != null && password != null) { + credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); + } else { + File homeDir = new File(System.getProperty("user.home")); + File propertyFile = new File(homeDir, ".github"); + + Properties props = new Properties(); + try (FileInputStream in = new FileInputStream(propertyFile)) { + props.load(in); + } catch (Exception ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } + space = props.getProperty("login"); + this.credentialsProvider = new UsernamePasswordCredentialsProvider(props.getProperty("login"), props.getProperty("password")); } - space = props.getProperty("login"); - this.credentialsProvider = new UsernamePasswordCredentialsProvider(props.getProperty("login"), props.getProperty("password")); } public CredentialsProvider getCredentials() { diff --git a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubIntegration.java b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubIntegration.java index ad99435..7d63ed4 100644 --- a/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubIntegration.java +++ b/bc-github-githook/src/main/java/porcelli/me/git/integration/githook/github/GitHubIntegration.java @@ -14,31 +14,38 @@ public class GitHubIntegration { + private static final String BC_URL = System.getProperty("bc.url"); + private static final String WH_URL = System.getProperty("wh.url", null); + private static final String GH_USERNAME = System.getProperty("gh.username"); + private static final String GH_PASSWORD = System.getProperty("gh.password"); + private final GitHubCredentials credentials; public GitHubIntegration(final GitHubCredentials credentials) { this.credentials = credentials; } - public String createRepository(final String repoName) throws IOException { - final GitHub github = GitHub.connect(); + public String createRepository(final String spaceName, final String repoName) throws IOException { + final GitHub github = GitHub.connect(GH_USERNAME, GH_PASSWORD); final GHRepository repo = github.createRepository(repoName) - .description("ssh://localhost:8001/MySpace/" + repoName) + .description("ssh://" + BC_URL + "/" + spaceName + "/" + repoName) .autoInit(false) .create(); - final Map config = new HashMap() {{ - put("url", new URL("http://e5997029.ngrok.io/api/hook/").toExternalForm()); - put("content_type", "json"); - }}; - repo.createHook("web", config, asList(GHEvent.PULL_REQUEST, GHEvent.PUSH), true); + if (WH_URL != null) { + final Map config = new HashMap() {{ + put("url", new URL(WH_URL).toExternalForm()); + put("content_type", "json"); + }}; + repo.createHook("web", config, asList(GHEvent.PULL_REQUEST, GHEvent.PUSH), true); + } return repo.getHttpTransportUrl(); } public void createPR(final String repoName, final String sourceBranch) throws IOException { - final GitHub github = GitHub.connect(); + final GitHub github = GitHub.connect(GH_USERNAME, GH_PASSWORD); final GHPullRequest pullRequest = github.getRepository(credentials.getSpace() + "/" + repoName).createPullRequest("PR from RHPAM", sourceBranch, "master", sourceBranch); } } diff --git a/bc-github-webhook/Dockerfile b/bc-github-webhook/Dockerfile new file mode 100644 index 0000000..cc307da --- /dev/null +++ b/bc-github-webhook/Dockerfile @@ -0,0 +1,13 @@ +FROM fabric8/java-centos-openjdk8-jre +ENV PORT=9090 + +USER root +RUN yum install -y openssh-server +RUN mkdir -p /root/.ssh +RUN ssh-keygen -t rsa -N "" -f /root/.ssh/id_rsa +RUN echo -e "Host * \nStrictHostKeyChecking no" > /root/.ssh/config +RUN cat /root/.ssh/id_rsa.pub + +COPY target/bc-github-webhook-1.0-SNAPSHOT.jar . + +CMD java -jar -Dport=$PORT bc-github-webhook-1.0-SNAPSHOT.jar diff --git a/bc-github-webhook/pom.xml b/bc-github-webhook/pom.xml index ba9e2cf..13b0d61 100644 --- a/bc-github-webhook/pom.xml +++ b/bc-github-webhook/pom.xml @@ -95,6 +95,32 @@ + + io.fabric8 + docker-maven-plugin + 0.28.0 + + + + + + + + + + + + + + + + + + + + + + diff --git a/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/BCIntegration.java b/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/BCIntegration.java index a312d3a..0ccce60 100644 --- a/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/BCIntegration.java +++ b/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/BCIntegration.java @@ -18,14 +18,12 @@ import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.SshTransport; import org.eclipse.jgit.transport.URIish; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import porcelli.me.git.integration.webhook.model.PullRequestEvent; import porcelli.me.git.integration.webhook.model.PushEvent; public class BCIntegration { private final Map repositoryMap = new HashMap<>(); - private final UsernamePasswordCredentialsProvider bcCredentials = new UsernamePasswordCredentialsProvider("porcelli", "pw"); public void onPush(final PushEvent pushEvent) throws GitAPIException, URISyntaxException, IOException { if (!pushEvent.getRef().contains("master")) { @@ -34,16 +32,16 @@ public void onPush(final PushEvent pushEvent) throws GitAPIException, URISyntaxE final Git git = getGit(pushEvent.getRepository()); try { - git.pull().setRemote("origin").setCredentialsProvider(bcCredentials).setRebase(true).call(); + git.pull().setRemote("origin").setRebase(true).call(); git.pull().setRemote("github").setRebase(true).call(); - git.push().setRemote("origin").setForce(true).setCredentialsProvider(bcCredentials).call(); + git.push().setRemote("origin").setForce(true).call(); } catch (Exception ex) { ex.printStackTrace(); } } public void onPullRequest(final PullRequestEvent pullRequestEvent) throws GitAPIException, IOException, URISyntaxException { - if (!pullRequestEvent.getAction().equals(PullRequestEvent.Action.CLOSED)){ + if (!pullRequestEvent.getAction().equals(PullRequestEvent.Action.CLOSED)) { return; } final String branchName = pullRequestEvent.getPullRequest().getBody(); @@ -52,7 +50,7 @@ public void onPullRequest(final PullRequestEvent pullRequestEvent) throws GitAPI git.branchDelete().setBranchNames("refs/heads/" + branchName).call(); final RefSpec refSpec = new RefSpec().setSource(null).setDestination("refs/heads/" + branchName); - git.push().setRefSpecs(refSpec).setRemote("origin").setCredentialsProvider(bcCredentials).call(); + git.push().setRefSpecs(refSpec).setRemote("origin").call(); } private Git getGit(porcelli.me.git.integration.webhook.model.Repository repository) throws GitAPIException, URISyntaxException, IOException { @@ -60,20 +58,24 @@ private Git getGit(porcelli.me.git.integration.webhook.model.Repository reposito if (!repositoryMap.containsKey(repository.getDescription())) { final String bcRepo = repository.getDescription(); - SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { - @Override - protected void configure(OpenSshConfig.Host host, Session session) { - } - }; + try { + SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { + @Override + protected void configure(OpenSshConfig.Host host, Session session) { + } + }; - git = Git.cloneRepository() - .setTransportConfigCallback(transport -> { - SshTransport sshTransport = (SshTransport) transport; - sshTransport.setSshSessionFactory(sshSessionFactory); - }).setURI(bcRepo) - .setCredentialsProvider(bcCredentials) - .setDirectory(tempDir(repository.getFullName())) - .call(); + git = Git.cloneRepository() + .setTransportConfigCallback(transport -> { + SshTransport sshTransport = (SshTransport) transport; + sshTransport.setSshSessionFactory(sshSessionFactory); + }).setURI(bcRepo) + .setDirectory(tempDir(repository.getFullName())) + .call(); + } catch (Exception ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } final RemoteAddCommand remoteAddCommand = git.remoteAdd(); remoteAddCommand.setName("github"); diff --git a/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/WebHook.java b/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/WebHook.java index 0d024fd..52b0540 100644 --- a/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/WebHook.java +++ b/bc-github-webhook/src/main/java/porcelli/me/git/integration/webhook/WebHook.java @@ -1,5 +1,7 @@ package porcelli.me.git.integration.webhook; +import java.io.File; + import com.jcraft.jsch.JSch; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.DefaultServlet; @@ -16,14 +18,20 @@ public static void main(String[] args) { JSch.setConfig("StrictHostKeyChecking", "no"); try { new WebHook().run(); + System.out.println("WebHook Up and running!"); } catch (Throwable t) { t.printStackTrace(); } } public void run() throws Exception { - - final int port = 9090; + int _port; + try { + _port = Integer.valueOf(System.getProperty("port", "9090")); + } catch (final Exception ex) { + _port = 9090; + } + final int port = _port; final Server server = new Server(port); // Setup the basic Application "context" at "/".