Java tutorial
import com.jcraft.jsch.Session; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.OpenSshConfig; import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.SshTransport; import org.eclipse.jgit.transport.Transport; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; /** Copyright 2016 Alianza Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ public class GitHelper { static JSONObject config; public static File cloneRepository(String cloneUrl, String repoPw) throws GitAPIException, JSONException, IOException { config = ConfigParser.getConfig(); File tmpDir = new File("temp_repo"); String key = null; String keyPassPhrase = null; if (config.has("privateKey")) { key = config.getString("privateKey"); keyPassPhrase = config.getString("privateKeyPassPhrase"); } // git clone will fail if the directory already exists, even if empty if (tmpDir.exists()) { FileUtils.deleteDirectory(tmpDir); } String pw = null; if (repoPw != null) { pw = repoPw; } else if (config.has("gitClonePassword")) { pw = config.getString("gitClonePassword"); } final String finalKeyPassPhrase = keyPassPhrase; final String finalKey = key; SshSessionFactory sessionFactory = new CustomJschConfigSessionFactory(); // use a private key if provided if (finalKey != null) { SshSessionFactory.setInstance(sessionFactory); } // use a password if provided if (pw != null) { final String finalPw = pw; SshSessionFactory.setInstance(new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session session) { session.setPassword(finalPw); } }); } SshSessionFactory.setInstance(sessionFactory); Git.cloneRepository().setURI(cloneUrl).setDirectory(tmpDir) .setTransportConfigCallback(new TransportConfigCallback() { @Override public void configure(Transport transport) { SshTransport sshTransport = (SshTransport) transport; sshTransport.setSshSessionFactory(sessionFactory); } }).call(); return tmpDir; } public static ArrayList<String> getRepositoryTests(File repositoryPath, JSONObject repository) throws IOException, JSONException { String testPath = repository.getString("path"); File fullPath = new File(repositoryPath.getAbsolutePath() + "/" + testPath); JSONArray arr = repository.getJSONArray("fileTypes"); String[] fileTypes = new String[arr.length()]; for (int i = 0; i < arr.length(); i++) { fileTypes[i] = (arr.getString(i)); } Iterator iter = FileUtils.iterateFiles(fullPath, fileTypes, true); ArrayList<String> tests = new ArrayList<>(); while (iter.hasNext()) { File file = (File) iter.next(); if (fileHasBadLines(file, repository)) { continue; } String test = file.getName(); if (test != null) { tests.add(test); } } return tests; } private static boolean fileHasBadLines(File file, JSONObject repository) { if (repository.has("excludeFilesIfNotContain") || repository.has("excludeFilesIfContain") || repository.has("excludeFilesIfNotStartsWith")) { try (BufferedReader br = new BufferedReader(new FileReader(file))) { JSONArray skipIfNotContained = (repository.has("excludeFilesIfNotContain") ? repository.getJSONArray("excludeFilesIfNotContain") : new JSONArray()); JSONArray skipIfContained = (repository.has("excludeFilesIfContain") ? repository.getJSONArray("excludeFilesIfContain") : new JSONArray()); JSONArray skipIfNotStartsWith = (repository.has("excludeFilesIfNotStartsWith") ? repository.getJSONArray("excludeFilesIfNotStartsWith") : new JSONArray()); String line; boolean neverFoundContains = true; boolean neverFoundStartsWith = true; while ((line = br.readLine()) != null) { if (line.trim().equals("")) continue; for (int i = 0; i < skipIfContained.length(); i++) { if (line.contains(skipIfContained.getString(i))) { return true; } } for (int i = 0; i < skipIfNotContained.length(); i++) { if (line.contains(skipIfNotContained.getString(i))) { neverFoundContains = false; } } for (int i = 0; i < skipIfNotStartsWith.length(); i++) { if (line.trim().startsWith(skipIfNotStartsWith.getString(i))) { neverFoundStartsWith = false; } } } if ((skipIfNotContained.length() > 0 && neverFoundContains) || (skipIfNotStartsWith.length() > 0 && neverFoundStartsWith)) { return true; } } catch (IOException | JSONException e) { e.printStackTrace(); } } return false; } }