|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package edu.wpi.rbe; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.lang.reflect.Type; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.apache.commons.io.FileUtils; |
| 15 | +import org.kohsuke.github.GHOrganization; |
| 16 | +import org.kohsuke.github.GHRepository; |
| 17 | +import org.kohsuke.github.GHTeam; |
| 18 | +import org.kohsuke.github.GHUser; |
| 19 | +import org.kohsuke.github.GitHub; |
| 20 | +import org.kohsuke.github.PagedIterable; |
| 21 | +import org.kohsuke.github.GHTeam.Role; |
| 22 | + |
| 23 | +import com.google.gson.Gson; |
| 24 | +import com.google.gson.GsonBuilder; |
| 25 | +import com.google.gson.reflect.TypeToken; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author hephaestus |
| 29 | + * |
| 30 | + */ |
| 31 | +public class DownloadAllHomeworkRepos { |
| 32 | + |
| 33 | + /** |
| 34 | + * @param args |
| 35 | + */ |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + String teamAssignmentsFile = args[0]; |
| 38 | + int numberOfTeams = 0; |
| 39 | + |
| 40 | + Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); |
| 41 | + Type collectionType = new TypeToken<HashMap<String, ArrayList<String>>>() { |
| 42 | + }.getType(); |
| 43 | + String json = FileUtils.readFileToString(new File(teamAssignmentsFile)); |
| 44 | + HashMap<String, ArrayList<String>> teamAssignments = gson.fromJson(json, collectionType); |
| 45 | + String projectDestBaseName = teamAssignments.get("projectName").get(0); |
| 46 | + ArrayList<String> repoDestBaseNames = teamAssignments.get("repoDestBaseNames"); |
| 47 | + numberOfTeams = Integer.parseInt(teamAssignments.get("numberOfTeams").get(0)); |
| 48 | + |
| 49 | + GitHub github = GitHub.connect(); |
| 50 | + GHOrganization dest = github.getMyOrganizations().get(projectDestBaseName); |
| 51 | + |
| 52 | + if (dest == null) { |
| 53 | + System.out.println("FAIL, you do not have access to " + projectDestBaseName); |
| 54 | + return; |
| 55 | + } |
| 56 | + System.out.println("Found " + projectDestBaseName); |
| 57 | + |
| 58 | + Map<String, GHTeam> teams = dest.getTeams(); |
| 59 | + PagedIterable<GHUser> teachingStaff = teams.get("TeachingStaff").listMembers(); |
| 60 | + for (GHUser t : teachingStaff) { |
| 61 | + System.out.println("Teacher: " + t.getLogin()); |
| 62 | + } |
| 63 | + Map<String, GHRepository> allRepos = dest.getRepositories(); |
| 64 | + // String repoDestBaseName = repoDestBaseNames.get(x); |
| 65 | + for (String key : allRepos.keySet()) { |
| 66 | + if (key.startsWith("HomeworkCode")) { |
| 67 | + String repoFullName = key; |
| 68 | + File tmp = new File(System.getProperty("java.io.tmpdir") + "/gittmp/"); |
| 69 | + if (!tmp.exists()) { |
| 70 | + tmp.mkdirs(); |
| 71 | + } |
| 72 | + tmp.deleteOnExit(); |
| 73 | + String cloneDirString = tmp.getAbsolutePath() + "/"; |
| 74 | + File cloneDir = new File(cloneDirString); |
| 75 | + File myDir = new File(cloneDirString + repoFullName); |
| 76 | + if (!myDir.exists()) { |
| 77 | + System.out.println("Cloning " + repoFullName + " to " + cloneDirString); |
| 78 | + List<String> commands = new ArrayList<String>(); |
| 79 | + commands.add("git"); // command |
| 80 | + commands.add("clone"); // command |
| 81 | + commands.add("git@github.com:" + projectDestBaseName + "/" + repoFullName + ".git"); // command |
| 82 | + LabCodeRepoSetupMain.run(commands, cloneDir); |
| 83 | + myDir = new File(cloneDirString + repoFullName); |
| 84 | + } else { |
| 85 | + System.out.println(myDir.getName() + " exists"); |
| 86 | + List<String> commands = new ArrayList<String>(); |
| 87 | + commands = new ArrayList<String>(); |
| 88 | + commands.add("git"); // command |
| 89 | + commands.add("pull"); // command |
| 90 | + commands.add("origin"); // command |
| 91 | + commands.add("master"); // command |
| 92 | + LabCodeRepoSetupMain.run(commands, myDir); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments