|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package edu.wpi.rbe; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.lang.reflect.Type; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collection; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import org.apache.commons.io.FileUtils; |
| 20 | +import org.kohsuke.github.GHCreateRepositoryBuilder; |
| 21 | +import org.kohsuke.github.GHOrganization; |
| 22 | +import org.kohsuke.github.GHRepository; |
| 23 | +import org.kohsuke.github.GHTeam; |
| 24 | +import org.kohsuke.github.GHTeam.Role; |
| 25 | +import org.kohsuke.github.GHUser; |
| 26 | +import org.kohsuke.github.GitHub; |
| 27 | +import org.kohsuke.github.PagedIterable; |
| 28 | + |
| 29 | +import com.google.gson.Gson; |
| 30 | +import com.google.gson.GsonBuilder; |
| 31 | +import com.google.gson.reflect.TypeToken; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author hephaestus |
| 35 | + * |
| 36 | + */ |
| 37 | +public class TeamPermissionSets { |
| 38 | + |
| 39 | + /** |
| 40 | + * @param args |
| 41 | + * @throws IOException |
| 42 | + * @throws InterruptedException |
| 43 | + */ |
| 44 | + public static void main(String[] args) throws Exception { |
| 45 | + HashSet<GHUser> allStudents = new HashSet<>(); |
| 46 | + String teamAssignmentsFile = args[0]; |
| 47 | + int numberOfTeams = 0; |
| 48 | + |
| 49 | + Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); |
| 50 | + Type collectionType = new TypeToken<HashMap<String, ArrayList<String>>>() { |
| 51 | + }.getType(); |
| 52 | + String json = FileUtils.readFileToString(new File(teamAssignmentsFile)); |
| 53 | + HashMap<String, ArrayList<String>> teamAssignments = gson.fromJson(json, collectionType); |
| 54 | + String projectDestBaseName = teamAssignments.get("projectName").get(0); |
| 55 | + ArrayList<String> repoDestBaseNames = teamAssignments.get("repoDestBaseNames"); |
| 56 | + String teamDestBaseName = teamAssignments.get("teamDestBaseName").get(0); |
| 57 | + numberOfTeams = Integer.parseInt(teamAssignments.get("numberOfTeams").get(0)); |
| 58 | + boolean useHW = true; |
| 59 | + try { |
| 60 | + useHW = Boolean.parseBoolean(teamAssignments.get("homework").get(0)); |
| 61 | + } catch (Throwable t) { |
| 62 | + } |
| 63 | + if (args.length == 2) { |
| 64 | + String csvFileName = args[1]; |
| 65 | + if (csvFileName.toLowerCase().endsWith(".csv")) { |
| 66 | + File csv = new File(csvFileName); |
| 67 | + String csvData = FileUtils.readFileToString(csv); |
| 68 | + if (csv.exists()) { |
| 69 | + String lines[] = csvData.split("\\r?\\n"); |
| 70 | + int teamNum = 0; |
| 71 | + ArrayList<String> team = new ArrayList<>(); |
| 72 | + for (String line : lines) { |
| 73 | + List<String> fields = Arrays.asList(line.split(",")); |
| 74 | + int lastTeamNum = teamNum; |
| 75 | + try { |
| 76 | + |
| 77 | + teamNum = Integer.parseInt(fields.get(5)); |
| 78 | + |
| 79 | + } catch (Exception ex) { |
| 80 | + // System.out.println(fields); |
| 81 | + |
| 82 | + // ex.printStackTrace(); |
| 83 | + } |
| 84 | + |
| 85 | + if (teamNum > 0 && teamNum != lastTeamNum) { |
| 86 | + if (lastTeamNum == numberOfTeams) |
| 87 | + break; |
| 88 | + System.out.println("Team # " + teamNum); |
| 89 | + team = new ArrayList<>(); |
| 90 | + String teamString = teamNum > 9 ? "" + teamNum : "0" + teamNum; |
| 91 | + teamAssignments.put(teamString, team); |
| 92 | + } |
| 93 | + if (teamNum > 0) { |
| 94 | + try { |
| 95 | + String username = fields.get(3); |
| 96 | + System.out.println("\t" + username); |
| 97 | + team.add(username); |
| 98 | + } catch (Exception e) { |
| 99 | + break;// end of the list |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + GitHub github = GitHub.connect(); |
| 110 | + GHOrganization dest = github.getMyOrganizations().get(projectDestBaseName); |
| 111 | + |
| 112 | + if (dest == null) { |
| 113 | + System.out.println("FAIL, you do not have access to " + projectDestBaseName); |
| 114 | + return; |
| 115 | + } |
| 116 | + System.out.println("Found " + projectDestBaseName); |
| 117 | + |
| 118 | + Map<String, GHTeam> teams = dest.getTeams(); |
| 119 | + |
| 120 | + for (int x = 0; x < repoDestBaseNames.size(); x++) { |
| 121 | + String repoDestBaseName = repoDestBaseNames.get(x); |
| 122 | + System.out.println("Looking for source information for " + repoDestBaseName); |
| 123 | + |
| 124 | + for (int i = 1; i <= numberOfTeams; i++) { |
| 125 | + String teamString = i > 9 ? "" + i : "0" + i; |
| 126 | + GHTeam team = teams.get(teamDestBaseName + teamString); |
| 127 | + |
| 128 | + if (team == null) { |
| 129 | + System.out.println("ERROR: no such team " + teamDestBaseName + teamString); |
| 130 | + continue; |
| 131 | + } |
| 132 | + ArrayList<String> members = teamAssignments.get(teamString); |
| 133 | + if (members == null) { |
| 134 | + System.out.println("ERROR: Team has no members in JSON " + teamString); |
| 135 | + continue; |
| 136 | + } |
| 137 | + System.out.println("Team Found: " + team.getName()); |
| 138 | + for(GHUser existing: team.getMembers()) { |
| 139 | + team.remove(existing); |
| 140 | + } |
| 141 | + for (String member : members) { |
| 142 | + try { |
| 143 | + GHUser memberGH = github.getUser(member); |
| 144 | + if (memberGH == null) { |
| 145 | + System.out.println("ERROR GitHub user " + member + " does not exist"); |
| 146 | + continue; |
| 147 | + } |
| 148 | + if (!team.hasMember(memberGH)) { |
| 149 | + try { |
| 150 | + team.add(memberGH, Role.MAINTAINER); |
| 151 | + System.out.println("Adding " + member + " to " + team.getName()); |
| 152 | + } catch (Exception e) { |
| 153 | + System.out.println("Inviting " + member + " to " + team.getName()); |
| 154 | + |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + } catch (Exception ex) { |
| 159 | + System.err.println("\r\n\r\n ERROR " + member + " is not a valid GitHub username\r\n\r\n"); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + public static void run(List<String> commands, File dir) throws Exception { |
| 169 | + // creating the process |
| 170 | + ProcessBuilder pb = new ProcessBuilder(commands); |
| 171 | + // setting the directory |
| 172 | + pb.directory(dir); |
| 173 | + // startinf the process |
| 174 | + Process process = pb.start(); |
| 175 | + process.waitFor(); |
| 176 | + int ev = process.exitValue(); |
| 177 | + // System.out.println("Running "+commands); |
| 178 | + if (ev != 0) { |
| 179 | + System.out.println("ERROR PROCESS Process exited with " + ev); |
| 180 | + } |
| 181 | + // for reading the ouput from stream |
| 182 | + BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 183 | + BufferedReader errInput = new BufferedReader(new InputStreamReader(process.getErrorStream())); |
| 184 | + |
| 185 | + String s = null; |
| 186 | + String e = null; |
| 187 | + Thread.sleep(100); |
| 188 | + while ((s = stdInput.readLine()) != null || (e = errInput.readLine()) != null) { |
| 189 | + if (s != null) |
| 190 | + System.out.println(s); |
| 191 | + if (e != null) |
| 192 | + System.err.println(e); |
| 193 | + // |
| 194 | + } |
| 195 | + while (process.isAlive()) |
| 196 | + ; |
| 197 | + } |
| 198 | + |
| 199 | + public static GHRepository createRepository(GHOrganization dest, String repoName, String description) |
| 200 | + throws IOException { |
| 201 | + GHCreateRepositoryBuilder builder; |
| 202 | + |
| 203 | + builder = dest.createRepository(repoName); |
| 204 | + |
| 205 | + // TODO link to the space URL? |
| 206 | + builder.private_(true).homepage("").issues(true).downloads(true).wiki(true); |
| 207 | + builder.description(description); |
| 208 | + |
| 209 | + return builder.create(); |
| 210 | + } |
| 211 | + |
| 212 | +} |
0 commit comments