Half of features implemented #2

Merged
andrewlalis merged 19 commits from repo_creation into master 2018-08-28 18:26:28 +00:00
3 changed files with 119 additions and 27 deletions
Showing only changes of commit 7e9a027a43 - Show all commits

View File

@ -8,6 +8,7 @@ import nl.andrewlalis.util.TeamGenerator;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.andrewlalis.util.CommandLine;
@ -49,7 +50,7 @@ public class Main {
Initializer initializer = new Initializer(
userOptions.get("organization"),
userOptions.get("token"),
"assignments"
"assignments_2018"
);
initializer.initializeGithubRepos(studentTeams);

View File

@ -1,6 +1,9 @@
package nl.andrewlalis.git_api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import nl.andrewlalis.model.StudentTeam;
import nl.andrewlalis.model.TATeam;
import org.apache.http.HttpResponse;
@ -16,13 +19,13 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* This class is responsible for initializing the Github repositories and setting permissions, adding teams, etc.
@ -78,51 +81,95 @@ public class Initializer {
* @param studentTeams The list of student studentTeams.
*/
public void initializeGithubRepos(List<StudentTeam> studentTeams) {
listMemberTeams();
//this.createRepository(this.assignmentsRepo, )
List<TATeam> taTeams = listMemberTeams();
TATeam teamAll = this.getTeamAll(taTeams, "teaching-assistants");
this.createRepository(this.assignmentsRepo, teamAll);
}
private boolean createRepository(String repositoryName, TATeam taTeam) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("name", repositoryName));
nvps.add(new BasicNameValuePair("private", "True"));
nvps.add(new BasicNameValuePair("has_issues", "True"));
nvps.add(new BasicNameValuePair("has_wiki", "True"));
nvps.add(new BasicNameValuePair("team_id", taTeam.getId()));
nvps.add(new BasicNameValuePair("auto_init", "False"));
nvps.add(new BasicNameValuePair("gitignore_template", "Java"));
logger.info("Creating repository: " + repositoryName);
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = mapper.createObjectNode();
root.put("name", repositoryName);
root.put("private", false);
root.put("has_issues", true);
root.put("has_wiki", true);
root.put("team_id", taTeam.getId());
root.put("auto_init", false);
root.put("gitignore_template", "Java");
String json = null;
try {
json = mapper.writer().writeValueAsString(root);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(this.urlBuilder.buildRepoURL());
try {
post.setEntity(new StringEntity(nvps.toString()));
post.setEntity(new StringEntity(json));
post.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(post);
logger.info("Response from creating repository POST: " + response.getStatusLine().getStatusCode());
logger.info(getResponseBody(response));
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* Extracts the team containing all TA's from a list of all teams.
* @param taTeams The list of all teams in the organization.
* @param teamAllName The name of the team representing all TA's.
* @return The team which holds all TA's.
*/
private TATeam getTeamAll(List<TATeam> taTeams, String teamAllName) {
for (TATeam team : taTeams) {
if (team.getName().equals(teamAllName)) {
return team;
}
}
return null;
}
/**
* Obtains a list of all teams in the organization, meaning all teaching assistant teams.
* @return A list of all teams in the organization.
*/
private Map<String, String> listMemberTeams() {
private List<TATeam> listMemberTeams() {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(this.urlBuilder.buildTeamURL());
try (CloseableHttpResponse response = client.execute(get)) {
StatusLine line = response.getStatusLine();
logger.finest("Response code from listing member teams: " + line.getStatusCode());
logger.finest("Response entity: " + response.getEntity().toString());
HashMap<String,Object> result =
new ObjectMapper().readValue(response.getEntity().getContent(), HashMap.class);
for (Map.Entry<String, Object> e : result.entrySet()) {
logger.finest(e.getKey() + ": " + e.getValue());
logger.finer("Response code from listing member teams: " + line.getStatusCode());
String result = getResponseBody(response);
logger.finer("Response entity: " + result);
ObjectMapper mapper = new ObjectMapper();
List<TATeam> memberTeams = mapper.readValue(result, new TypeReference<List<TATeam>>(){});
for (TATeam r : memberTeams) {
logger.finest(r.toString());
}
return memberTeams;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Extracts a string which represents the response body of an HttpResponse.
* @param response The response to a previous request.
* @return A string containing the whole response body.
*/
private static String getResponseBody(HttpResponse response) {
try {
return new BufferedReader(new InputStreamReader(response.getEntity().getContent())).lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}

View File

@ -1,21 +1,65 @@
package nl.andrewlalis.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a teaching assistant team, which is itself a 'team' in the organization. This class is used for parsing
* json from requests to github to get a list of all teams in the organization.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class TATeam {
private List<TeachingAssistant> teachingAssistants;
public TATeam(List<TeachingAssistant> teachingAssistants) {
/**
* The team's display name.
*/
@JsonProperty("name")
private String name;
/**
* The team's unique identifier.
*/
@JsonProperty("id")
private int id;
/**
* Constructs a team without any teaching assistant members.
* @param name The name of the team.
* @param id The unique identifier for this team.
*/
@JsonCreator
public TATeam(@JsonProperty("name") String name, @JsonProperty("id") int id) {
this.name = name;
this.id = id;
this.teachingAssistants = new ArrayList<TeachingAssistant>();
}
/**
* Constructs a team with a list of teaching assistants that are part of it.
* @param teachingAssistants The list of teaching assistants that are part of the team.
*/
public TATeam(List<TeachingAssistant> teachingAssistants, String name, int id) {
this.teachingAssistants = teachingAssistants;
this.name = name;
this.id = id;
}
/**
* Gets the unique identification for this TA team.
* @return A string representing the id of this team.
* @return An integer representing the id of this team.
*/
public String getId() {
return null;
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
}