Skip to content

Commit

Permalink
When taking the survey, read the student's GitHub username from .git/…
Browse files Browse the repository at this point in the history
…config.
  • Loading branch information
DavidWhitlock committed May 31, 2024
1 parent 5b1878b commit f53a898
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
34 changes: 32 additions & 2 deletions grader/src/main/java/edu/pdx/cs/joy/grader/Survey.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public class Survey extends EmailSender {

private boolean sendEmail = true;
private final File xmlFileDir;
private File gitCheckoutDir;

public Survey(PrintStream out, PrintStream err, InputStream in, File xmlFileDir) {
public Survey(PrintStream out, PrintStream err, InputStream in, File xmlFileDir, File gitCheckoutDir) {
this.out = new PrintWriter(out, true);
this.err = new PrintWriter(err, true);
this.in = new BufferedReader(new InputStreamReader(in));
this.xmlFileDir = xmlFileDir;
this.gitCheckoutDir = gitCheckoutDir;
}

/**
Expand Down Expand Up @@ -87,7 +89,8 @@ private void usage() {
}

public static void main(String[] args) {
Survey survey = new Survey(System.out, System.err, System.in, new File(System.getProperty("user.dir")));
File currentDirectory = new File(System.getProperty("user.dir"));
Survey survey = new Survey(System.out, System.err, System.in, currentDirectory, currentDirectory);
survey.takeSurvey(args);
}

Expand Down Expand Up @@ -146,10 +149,37 @@ private Student gatherStudentInformation() {
askQuestionAndSetValue("What is your major?", student::setMajor);

askEnrolledSectionQuestion(student);
askGitHubUserNameQuestion(student);

return student;
}

private void askGitHubUserNameQuestion(Student student) {
String gitHubUserName = findGitHubUserName();
String okay = ask("Is it okay to record your GitHub username of \"" + gitHubUserName
+ "\" so you can be added to the shared GitHub repo for Pair/Mob Programming?");
if (okay.equalsIgnoreCase("y")) {
student.setGitHubUserName(gitHubUserName);
}
}

private String findGitHubUserName() {
File dotGit = new File(this.gitCheckoutDir, ".git");
File config = new File(dotGit, "config");
if (config.isFile()) {
try {
GitConfigParser parser = new GitConfigParser(new FileReader(config));
GitHubUserNameFinder finder = new GitHubUserNameFinder();
parser.parse(finder);
return finder.getGitHubUserName();

} catch (IOException e) {
return null;
}
}
return null;
}

@VisibleForTesting
static boolean isEmailAddress(String id) {
try {
Expand Down
33 changes: 31 additions & 2 deletions grader/src/test/java/edu/pdx/cs/joy/grader/SurveyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.io.TempDir;

import java.io.*;
import java.nio.file.Files;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -88,13 +89,17 @@ void successfulSurveyWritesStudentXmlFile(@TempDir File tempDir) throws IOExcept
String email = "email@email.com";
String major = "Major";
String section = "u";
String recordGitHubUserName = "y";
String learn = "A lot";
String anythingElse = "Nope";
String verify = "y";

InputStream in = getInputStreamWithLinesOfText(firstName, lastName, nickName, loginId, email, major, section, learn, anythingElse, verify);
String gitHubUserName = "GitHubUserName";

Survey survey = new Survey(new TextCapturingOutputStream().getPrintStream(), new TextCapturingOutputStream().getPrintStream(), in, tempDir);
InputStream in = getInputStreamWithLinesOfText(firstName, lastName, nickName, loginId, email, major, section, recordGitHubUserName, learn, anythingElse, verify);
createGitConfigWithUserName(tempDir, gitHubUserName);

Survey survey = new Survey(new TextCapturingOutputStream().getPrintStream(), new TextCapturingOutputStream().getPrintStream(), in, tempDir, tempDir);
survey.takeSurvey("-noEmail");

File xmlFile = new File(tempDir, Survey.STUDENT_XML_FILE_NAME);
Expand All @@ -108,6 +113,30 @@ void successfulSurveyWritesStudentXmlFile(@TempDir File tempDir) throws IOExcept
assertThat(student.getEmail(), equalTo(email));
assertThat(student.getMajor(), equalTo(major));
assertThat(student.getEnrolledSection(), equalTo(Student.Section.UNDERGRADUATE));
assertThat(student.getGitHubUserName(), equalTo(gitHubUserName));
}

private void createGitConfigWithUserName(File tempDir, String gitHubUserName) throws IOException {
File dotGit = new File(tempDir, ".git");
assertThat(dotGit.mkdir(), is(true));
File config = new File(dotGit, "config");

try (PrintWriter pw = new PrintWriter(new FileWriter(config))) {
pw.println("[core]\n" +
" repositoryformatversion = 0\n" +
" filemode = true\n" +
" bare = false\n" +
" logallrefupdates = true\n" +
" ignorecase = true\n" +
" precomposeunicode = true\n" +
"[remote \"origin\"]\n" +
" url = git@github.com:" + gitHubUserName + "/JoyOfCoding.git\n" +
" fetch = +refs/heads/*:refs/remotes/origin/*\n" +
"[branch \"main\"]\n" +
" remote = origin\n" +
" merge = refs/heads/main\n");
pw.flush();
}
}

private InputStream getInputStreamWithLinesOfText(String... lines) {
Expand Down

0 comments on commit f53a898

Please sign in to comment.