Half of features implemented #2
			
				
			
		
		
		
	
							
								
								
									
										6
									
								
								pom.xml
								
								
								
								
							
							
						
						
									
										6
									
								
								pom.xml
								
								
								
								
							| 
						 | 
				
			
			@ -44,9 +44,9 @@
 | 
			
		|||
            <scope>compile</scope>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.google.http-client</groupId>
 | 
			
		||||
            <artifactId>google-http-client</artifactId>
 | 
			
		||||
            <version>1.23.0</version>
 | 
			
		||||
            <groupId>org.apache.httpcomponents</groupId>
 | 
			
		||||
            <artifactId>httpclient</artifactId>
 | 
			
		||||
            <version>RELEASE</version>
 | 
			
		||||
        </dependency>
 | 
			
		||||
    </dependencies>
 | 
			
		||||
    
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
package nl.andrewlalis;
 | 
			
		||||
 | 
			
		||||
import nl.andrewlalis.git_api.Initializer;
 | 
			
		||||
import nl.andrewlalis.model.Team;
 | 
			
		||||
import nl.andrewlalis.util.Logging;
 | 
			
		||||
import nl.andrewlalis.util.TeamGenerator;
 | 
			
		||||
| 
						 | 
				
			
			@ -33,7 +34,7 @@ public class Main {
 | 
			
		|||
        logger.info("Initializer for Github Repositories in Educational Organizations.");
 | 
			
		||||
 | 
			
		||||
        // Get teams from CSV file.
 | 
			
		||||
        List<Team> teams;
 | 
			
		||||
        List<Team> teams = null;
 | 
			
		||||
        try {
 | 
			
		||||
            teams = TeamGenerator.generateFromCSV(
 | 
			
		||||
                    userOptions.get("input"),
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +46,12 @@ public class Main {
 | 
			
		|||
            System.exit(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        Initializer initializer = new Initializer(
 | 
			
		||||
                userOptions.get("organization"),
 | 
			
		||||
                userOptions.get("token"),
 | 
			
		||||
                "assignments"
 | 
			
		||||
        );
 | 
			
		||||
        initializer.initializeGithubRepos(teams);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,59 @@
 | 
			
		|||
package nl.andrewlalis.git_api;
 | 
			
		||||
 | 
			
		||||
import nl.andrewlalis.model.Team;
 | 
			
		||||
import org.apache.http.client.methods.CloseableHttpResponse;
 | 
			
		||||
import org.apache.http.client.methods.HttpGet;
 | 
			
		||||
import org.apache.http.impl.client.CloseableHttpClient;
 | 
			
		||||
import org.apache.http.impl.client.HttpClients;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class is responsible for initializing the Github repositories and setting permissions, adding teams, etc.
 | 
			
		||||
 */
 | 
			
		||||
public class Initializer {
 | 
			
		||||
 | 
			
		||||
    private String organizationName;
 | 
			
		||||
 | 
			
		||||
    private String accessToken;
 | 
			
		||||
 | 
			
		||||
    private URLBuilder urlBuilder;
 | 
			
		||||
 | 
			
		||||
    private String assignmentsRepo;
 | 
			
		||||
 | 
			
		||||
    public Initializer(String organizationName, String accessToken, String assignmentsRepo) {
 | 
			
		||||
        this.organizationName = organizationName;
 | 
			
		||||
        this.accessToken = accessToken;
 | 
			
		||||
        this.assignmentsRepo = assignmentsRepo;
 | 
			
		||||
        this.urlBuilder = new URLBuilder(organizationName, accessToken);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Initializes the github repository for all teams given.
 | 
			
		||||
     *
 | 
			
		||||
     * Creates for each team:
 | 
			
		||||
     * - a repository
 | 
			
		||||
     * - protected master branch
 | 
			
		||||
     * - development branch
 | 
			
		||||
     * - adds students to repository
 | 
			
		||||
     * - creates assignments repository
 | 
			
		||||
     * - adds all students to assignments repository.
 | 
			
		||||
     * @param teams The list of student teams.
 | 
			
		||||
     */
 | 
			
		||||
    public void initializeGithubRepos(List<Team> teams) {
 | 
			
		||||
        listMemberTeams();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Map<String, String> listMemberTeams() {
 | 
			
		||||
        CloseableHttpClient client = HttpClients.createDefault();
 | 
			
		||||
        HttpGet get = new HttpGet(this.urlBuilder.buildTeamURL());
 | 
			
		||||
        try (CloseableHttpResponse response = client.execute(get)) {
 | 
			
		||||
            System.out.println(response.getStatusLine());
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,140 @@
 | 
			
		|||
package nl.andrewlalis.git_api;
 | 
			
		||||
 | 
			
		||||
public class URLBuilder {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The main URL from which all requests must be formed.
 | 
			
		||||
     */
 | 
			
		||||
    private static final String baseURL = "https://api.github.com";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The name of the github organization in which to create repositories.
 | 
			
		||||
     */
 | 
			
		||||
    private String organizationName;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The token needed to use the API.
 | 
			
		||||
     */
 | 
			
		||||
    private String accessToken;
 | 
			
		||||
 | 
			
		||||
    public URLBuilder(String organizationName, String accessToken) {
 | 
			
		||||
        this.organizationName = organizationName;
 | 
			
		||||
        this.accessToken = accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return The URL for adding a repository.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildRepoURL() {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/orgs/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + "/repos?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param repoName The name of the repository.
 | 
			
		||||
     * @param branch The name of the branch in the repository above.
 | 
			
		||||
     * @return The URL for setting branch protection.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildBranchProtectionURL(String repoName, String branch) {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/repos/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + '/'
 | 
			
		||||
                + repoName
 | 
			
		||||
                + "/branches/"
 | 
			
		||||
                + branch
 | 
			
		||||
                + "/protection?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param repoName The name of the repository the branch is in.
 | 
			
		||||
     * @return The URL for getting a branch reference.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildReferenceGetURL(String repoName) {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/repos/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + '/'
 | 
			
		||||
                + repoName
 | 
			
		||||
                + "/git/refs/heads/master?acces_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param repoName The repository name.
 | 
			
		||||
     * @return The URL for creating a new branch, once a reference has been obtained.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildReferencePostURL(String repoName) {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/repos/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + '/'
 | 
			
		||||
                + repoName
 | 
			
		||||
                + "/git/refs?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param repoName The name of the repository.
 | 
			
		||||
     * @param collaborator The collaborator's name.
 | 
			
		||||
     * @return The URL for adding a collaborator to a repository.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildCollaboratorURL(String repoName, String collaborator) {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/repos/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + '/'
 | 
			
		||||
                + repoName
 | 
			
		||||
                + "/collaborators/"
 | 
			
		||||
                + collaborator
 | 
			
		||||
                + "?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return The URL for obtaining the teams.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildTeamURL() {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/orgs/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + "/teams?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param repoName The name of the repository.
 | 
			
		||||
     * @param teamName The name of the team to set permissions for.
 | 
			
		||||
     * @return The URL for setting team permissions of a repository.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildTeamPermissionsURL(String repoName, String teamName) {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/teams/"
 | 
			
		||||
                + teamName
 | 
			
		||||
                + "/repos/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + '/'
 | 
			
		||||
                + repoName
 | 
			
		||||
                + "?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param repoName The name of the repository.
 | 
			
		||||
     * @return The URL for archiving a repository.
 | 
			
		||||
     */
 | 
			
		||||
    public String buildArchiveRepoURL(String repoName) {
 | 
			
		||||
        return baseURL
 | 
			
		||||
                + "/repos/"
 | 
			
		||||
                + this.organizationName
 | 
			
		||||
                + '/'
 | 
			
		||||
                + repoName
 | 
			
		||||
                + "?access_token="
 | 
			
		||||
                + this.accessToken;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -111,6 +111,11 @@ public class Team {
 | 
			
		|||
        return sb.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns a pretty formatting of this team so that it can be viewed in the command line. This is mainly for
 | 
			
		||||
     * debugging purposes.
 | 
			
		||||
     * @return A string representing the team.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        StringBuilder sb = new StringBuilder("Team: ");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue