Skip to content

Commit e82fef5

Browse files
committed
adding team members to team
1 parent e290483 commit e82fef5

File tree

4 files changed

+220
-2
lines changed

4 files changed

+220
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
/.gradle/
25+
.project
26+
*.settings*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ public static void main(String[] args) throws Exception {
178178
continue;
179179
}
180180
System.out.println("Team Found: " + team.getName());
181+
for(GHUser existing: team.getMembers()) {
182+
team.remove(existing);
183+
}
181184
for (String member : members) {
182185
try {
183186
GHUser memberGH = github.getUser(member);
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
}

LabCodeRepoSetup/teamAssignments2002.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
"05":["mtcharter","conkrr"],
1414
"06":["jcybul","SabLiu","mariasharman137"],
1515
"07":["markhogan1001","mjlaks"],
16-
"08":["Htrom14"],
16+
"08":["Htrom14","seekerzero"],
1717
"09":["Zacoulou","amalik3099","pelund"],
1818
"10":["cbennet","manjushachava1","T-Reiser"],
1919
"11":["TheGreencoat","cmonyenokwe","pheobeyeung"],
2020
"12":["crayhj"],
2121
"13":["17craigiec","zseker","nastallings"],
22-
"14":["ishchan2","julia-dag"],
22+
"14":["ishchan2","julia-dag","mhgomes"],
2323
"15":["atkneedler","lcreid1","jwestlake1"],
2424
"19":["clbesch","emmettmccann","trschaeffer"],
2525
"20":["madhephaestus","cbp952","Max5254"]

0 commit comments

Comments
 (0)