Can create assignments repository now.
This commit is contained in:
		
							parent
							
								
									6394e9d1d4
								
							
						
					
					
						commit
						7e9a027a43
					
				| 
						 | 
					@ -8,6 +8,7 @@ import nl.andrewlalis.util.TeamGenerator;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					import java.util.logging.Level;
 | 
				
			||||||
import java.util.logging.Logger;
 | 
					import java.util.logging.Logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import nl.andrewlalis.util.CommandLine;
 | 
					import nl.andrewlalis.util.CommandLine;
 | 
				
			||||||
| 
						 | 
					@ -49,7 +50,7 @@ public class Main {
 | 
				
			||||||
        Initializer initializer = new Initializer(
 | 
					        Initializer initializer = new Initializer(
 | 
				
			||||||
                userOptions.get("organization"),
 | 
					                userOptions.get("organization"),
 | 
				
			||||||
                userOptions.get("token"),
 | 
					                userOptions.get("token"),
 | 
				
			||||||
                "assignments"
 | 
					                "assignments_2018"
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        initializer.initializeGithubRepos(studentTeams);
 | 
					        initializer.initializeGithubRepos(studentTeams);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,9 @@
 | 
				
			||||||
package nl.andrewlalis.git_api;
 | 
					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.ObjectMapper;
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
				
			||||||
import nl.andrewlalis.model.StudentTeam;
 | 
					import nl.andrewlalis.model.StudentTeam;
 | 
				
			||||||
import nl.andrewlalis.model.TATeam;
 | 
					import nl.andrewlalis.model.TATeam;
 | 
				
			||||||
import org.apache.http.HttpResponse;
 | 
					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.impl.client.HttpClients;
 | 
				
			||||||
import org.apache.http.message.BasicNameValuePair;
 | 
					import org.apache.http.message.BasicNameValuePair;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.BufferedReader;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.io.UnsupportedEncodingException;
 | 
					import java.io.InputStreamReader;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.HashMap;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					 | 
				
			||||||
import java.util.logging.Logger;
 | 
					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.
 | 
					 * 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.
 | 
					     * @param studentTeams The list of student studentTeams.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public void initializeGithubRepos(List<StudentTeam> studentTeams) {
 | 
					    public void initializeGithubRepos(List<StudentTeam> studentTeams) {
 | 
				
			||||||
        listMemberTeams();
 | 
					        List<TATeam> taTeams = listMemberTeams();
 | 
				
			||||||
        //this.createRepository(this.assignmentsRepo, )
 | 
					        TATeam teamAll = this.getTeamAll(taTeams, "teaching-assistants");
 | 
				
			||||||
 | 
					        this.createRepository(this.assignmentsRepo, teamAll);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private boolean createRepository(String repositoryName, TATeam taTeam) {
 | 
					    private boolean createRepository(String repositoryName, TATeam taTeam) {
 | 
				
			||||||
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
 | 
					        logger.info("Creating repository: " + repositoryName);
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("name", repositoryName));
 | 
					
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("private", "True"));
 | 
					        ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("has_issues", "True"));
 | 
					        ObjectNode root = mapper.createObjectNode();
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("has_wiki", "True"));
 | 
					        root.put("name", repositoryName);
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("team_id", taTeam.getId()));
 | 
					        root.put("private", false);
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("auto_init", "False"));
 | 
					        root.put("has_issues", true);
 | 
				
			||||||
        nvps.add(new BasicNameValuePair("gitignore_template", "Java"));
 | 
					        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();
 | 
					        HttpClient client = HttpClientBuilder.create().build();
 | 
				
			||||||
        HttpPost post = new HttpPost(this.urlBuilder.buildRepoURL());
 | 
					        HttpPost post = new HttpPost(this.urlBuilder.buildRepoURL());
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            post.setEntity(new StringEntity(nvps.toString()));
 | 
					            post.setEntity(new StringEntity(json));
 | 
				
			||||||
            post.setHeader("Content-type", "application/json");
 | 
					            post.setHeader("Content-type", "application/json");
 | 
				
			||||||
            HttpResponse response = client.execute(post);
 | 
					            HttpResponse response = client.execute(post);
 | 
				
			||||||
 | 
					            logger.info("Response from creating repository POST: " + response.getStatusLine().getStatusCode());
 | 
				
			||||||
 | 
					            logger.info(getResponseBody(response));
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            e.printStackTrace();
 | 
					            e.printStackTrace();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return false;
 | 
					        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.
 | 
					     * Obtains a list of all teams in the organization, meaning all teaching assistant teams.
 | 
				
			||||||
     * @return A list of all teams in the organization.
 | 
					     * @return A list of all teams in the organization.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private Map<String, String> listMemberTeams() {
 | 
					    private List<TATeam> listMemberTeams() {
 | 
				
			||||||
        CloseableHttpClient client = HttpClients.createDefault();
 | 
					        CloseableHttpClient client = HttpClients.createDefault();
 | 
				
			||||||
        HttpGet get = new HttpGet(this.urlBuilder.buildTeamURL());
 | 
					        HttpGet get = new HttpGet(this.urlBuilder.buildTeamURL());
 | 
				
			||||||
        try (CloseableHttpResponse response = client.execute(get)) {
 | 
					        try (CloseableHttpResponse response = client.execute(get)) {
 | 
				
			||||||
            StatusLine line = response.getStatusLine();
 | 
					            StatusLine line = response.getStatusLine();
 | 
				
			||||||
            logger.finest("Response code from listing member teams: " + line.getStatusCode());
 | 
					            logger.finer("Response code from listing member teams: " + line.getStatusCode());
 | 
				
			||||||
            logger.finest("Response entity: " + response.getEntity().toString());
 | 
					            String result = getResponseBody(response);
 | 
				
			||||||
            HashMap<String,Object> result =
 | 
					            logger.finer("Response entity: " + result);
 | 
				
			||||||
                    new ObjectMapper().readValue(response.getEntity().getContent(), HashMap.class);
 | 
					
 | 
				
			||||||
            for (Map.Entry<String, Object> e : result.entrySet()) {
 | 
					            ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
                logger.finest(e.getKey() + ": " + e.getValue());
 | 
					            List<TATeam> memberTeams = mapper.readValue(result, new TypeReference<List<TATeam>>(){});
 | 
				
			||||||
 | 
					            for (TATeam r : memberTeams) {
 | 
				
			||||||
 | 
					                logger.finest(r.toString());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            return memberTeams;
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            e.printStackTrace();
 | 
					            e.printStackTrace();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return null;
 | 
					        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 "";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,21 +1,65 @@
 | 
				
			||||||
package nl.andrewlalis.model;
 | 
					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;
 | 
					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 {
 | 
					public class TATeam {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private List<TeachingAssistant> teachingAssistants;
 | 
					    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.teachingAssistants = teachingAssistants;
 | 
				
			||||||
 | 
					        this.name = name;
 | 
				
			||||||
 | 
					        this.id = id;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Gets the unique identification for this TA team.
 | 
					     * 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() {
 | 
					    public int getId() {
 | 
				
			||||||
        return null;
 | 
					        return this.id;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String getName() {
 | 
				
			||||||
 | 
					        return this.name;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue