Skip to content

Commit e5d1304

Browse files
committed
additional utilities
1 parent 724e09a commit e5d1304

3 files changed

Lines changed: 208 additions & 4 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 DownloadAllLabRepos {
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+
for (int x = 0; x < repoDestBaseNames.size(); x++) {
64+
String repoDestBaseName = repoDestBaseNames.get(x);
65+
for (int i = 1; i <= numberOfTeams; i++) {
66+
String teamString = i > 9 ? "" + i : "0" + i;
67+
68+
String repoFullName = repoDestBaseName + teamString;
69+
File tmp = new File(System.getProperty("java.io.tmpdir") + "/gittmp/");
70+
if (!tmp.exists()) {
71+
tmp.mkdirs();
72+
}
73+
tmp.deleteOnExit();
74+
String cloneDirString = tmp.getAbsolutePath() + "/" ;
75+
File cloneDir = new File(cloneDirString);
76+
File myDir = new File(cloneDirString+repoFullName);
77+
if(!myDir.exists()) {
78+
System.out.println("Cloning "+repoFullName+" to "+cloneDirString);
79+
List<String> commands = new ArrayList<String>();
80+
commands.add("git"); // command
81+
commands.add("clone"); // command
82+
commands.add("git@github.com:" + projectDestBaseName + "/" + repoFullName + ".git"); // command
83+
LabCodeRepoSetupMain.run(commands, cloneDir);
84+
myDir = new File(cloneDirString+repoFullName);
85+
}else {
86+
System.out.println(myDir.getName()+" exists");
87+
List<String> commands = new ArrayList<String>();
88+
commands = new ArrayList<String>();
89+
commands.add("git"); // command
90+
commands.add("pull"); // command
91+
commands.add("origin"); // command
92+
commands.add("master"); // command
93+
LabCodeRepoSetupMain.run(commands, myDir);
94+
}
95+
}
96+
}
97+
98+
}
99+
100+
}

LabCodeRepoSetup/src/main/java/edu/wpi/rbe/LabCodeRepoSetupMain.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,16 @@ public static void run(List<String> commands, File dir) throws Exception {
353353
BufferedReader errInput = new BufferedReader(new InputStreamReader(process.getErrorStream()));
354354

355355
String s = null;
356-
while (process.isAlive()) {
357-
if ((s = stdInput.readLine()) != null)
358-
System.out.println(s);
359-
if ((s = errInput.readLine()) != null)
356+
String e = null;
357+
while ((s = stdInput.readLine()) != null||
358+
(e = errInput.readLine()) != null) {
359+
if (s != null)
360360
System.out.println(s);
361+
if (e != null)
362+
System.out.println(e);
361363
Thread.sleep(100);
362364
}
365+
while (process.isAlive()) ;
363366
}
364367

365368
public static GHRepository createRepository(GHOrganization dest, String repoName, String description)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 UpdateAllLabRepos {
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+
for (int x = 0; x < repoDestBaseNames.size(); x++) {
64+
String repoDestBaseName = repoDestBaseNames.get(x);
65+
for (int i = 1; i <= numberOfTeams; i++) {
66+
String teamString = i > 9 ? "" + i : "0" + i;
67+
68+
String repoFullName = repoDestBaseName + teamString;
69+
File tmp = new File(System.getProperty("java.io.tmpdir") + "/gittmp/");
70+
if (!tmp.exists()) {
71+
tmp.mkdirs();
72+
}
73+
tmp.deleteOnExit();
74+
String cloneDirString = tmp.getAbsolutePath() + "/" ;
75+
File cloneDir = new File(cloneDirString);
76+
File myDir = new File(cloneDirString+repoFullName);
77+
if(!myDir.exists()) {
78+
System.out.println("Cloning "+repoFullName+" to "+cloneDirString);
79+
List<String> commands = new ArrayList<String>();
80+
commands.add("git"); // command
81+
commands.add("clone"); // command
82+
commands.add("git@github.com:" + projectDestBaseName + "/" + repoFullName + ".git"); // command
83+
LabCodeRepoSetupMain.run(commands, cloneDir);
84+
myDir = new File(cloneDirString+repoFullName);
85+
}else {
86+
System.out.println(myDir.getName()+" exists");
87+
List<String> commands = new ArrayList<String>();
88+
commands = new ArrayList<String>();
89+
commands.add("git"); // command
90+
commands.add("pull"); // command
91+
commands.add("origin"); // command
92+
commands.add("master"); // command
93+
LabCodeRepoSetupMain.run(commands, myDir);
94+
}
95+
96+
}
97+
}
98+
99+
}
100+
101+
}

0 commit comments

Comments
 (0)