Skip to content

Instantly share code, notes, and snippets.

@whiteship
Created December 12, 2020 14:12
Show Gist options
  • Save whiteship/5f0d9f800c0cfb7867c59cfc4fd6d5a7 to your computer and use it in GitHub Desktop.
Save whiteship/5f0d9f800c0cfb7867c59cfc4fd6d5a7 to your computer and use it in GitHub Desktop.
package me.whiteship.livestudyupdator;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class App {
public static void main(String[] args) throws IOException {
App app = new App();
app.printBoard();
}
private void printBoard() throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
List<Participant> participants = new ArrayList<>();
for (int index = 1 ; index <= 18 ; index++) {
GHIssue issue = repository.getIssue(index);
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
Participant participant = findParticipant(participants, comment.getUserName());
participant.markHomeworkIsDone(index);
}
}
participants.sort(Comparator.comparing(p -> p.userName));
participants.forEach(p -> {
System.out.printf("| %s %s | %.2f%% |\n", p.userName, checkMark(p), p.getRate(18));
});
}
private Participant findParticipant(List<Participant> participants, String userName) {
if (isNewUser(participants, userName)) {
Participant participant = new Participant(userName);
participants.add(participant);
return participant;
} else {
Optional<Participant> first = participants.stream().filter(p -> p.userName.equals(userName)).findFirst();
return first.orElseThrow();
}
}
private String checkMark(Participant p) {
StringBuilder result = new StringBuilder();
for (int i = 1 ; i < 19 ; i++) {
if(p.homeworkResults.containsKey(i) && p.homeworkResults.get(i)) {
result.append("|:white_check_mark:");
} else {
result.append("|");
}
}
return result.toString();
}
private boolean isNewUser(List<Participant> participants, String userName) {
return participants.stream().noneMatch(p -> p.userName.equals(userName));
}
}
package me.whiteship.livestudyupdator;
import java.util.HashMap;
import java.util.Map;
public class Participant {
String userName;
Map<Integer, Boolean> homeworkResults;
public Participant(String userName) {
this.userName = userName;
homeworkResults = new HashMap<>();
}
public double getRate(double total) {
long count = homeworkResults.values().stream().filter(value -> value).count();
return count * 100 / total;
}
public void markHomeworkIsDone(int index) {
this.homeworkResults.put(index, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment