Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/main/java/me/ahmadhajjar/GithubNotificationsApp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.ahmadhajjar.GithubNotificationsApp.utils.OSType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;

public class Main {
Expand All @@ -14,24 +15,25 @@ public class Main {
public static void main(String[] args) {
System.setProperty("apple.eawt.quitStrategy", "CLOSE_ALL_WINDOWS");
logger.debug("Starting application ...");
GitHubAPIService gitHubAPIService = new GitHubAPIService(GITHUB_TOKEN);

setUILookAndFeel();

GitHubAPIService gitHubAPIService = new GitHubAPIService(GITHUB_TOKEN);
RepoChecker checker = new RepoChecker(gitHubAPIService);
Thread uiThread = new Thread(GithubNotifierApp::new);
Thread uiThread = new Thread(() -> new GithubNotifierApp(checker));
logger.debug("Starting threads ...");
uiThread.start();
checker.start();
}

private static void setUILookAndFeel() {
try {
logger.debug("Trying to change look and feel ...");
switch (OSType.DETECTED) {
case MacOS -> UIManager.setLookAndFeel("com.formdev.flatlaf.themes.FlatMacDarkLaf");
case Windows -> UIManager.setLookAndFeel("com.formdev.flatlaf.FlatDarculaLaf");
default -> UIManager.setLookAndFeel("com.formdev.flatlaf.FlatDarkLaf");
}
logger.debug("Successfully changed look and feel!");
} catch (Exception ex) {
logger.error("Changing look and feel failed.");
logger.error(ex);
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/me/ahmadhajjar/GithubNotificationsApp/PR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.ahmadhajjar.GithubNotificationsApp;


import java.util.Objects;

public class PR {
private String username;
private Integer prId;

public PR() {
}

public PR(String username, Integer prId) {
this.username = username;
this.prId = prId;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Integer getPrId() {
return prId;
}

public void setPrId(Integer prId) {
this.prId = prId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

PR pr = (PR) o;
return Objects.equals(username, pr.username) && Objects.equals(prId, pr.prId);
}

@Override
public int hashCode() {
int result = Objects.hashCode(username);
result = 31 * result + Objects.hashCode(prId);
return result;
}

@Override
public String toString() {
return this.prId + " - " + this.username;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import me.ahmadhajjar.GithubNotificationsApp.service.GitHubAPIService;
import me.ahmadhajjar.GithubNotificationsApp.service.StorageService;
import me.ahmadhajjar.GithubNotificationsApp.ui.NotificationCenter;
import me.ahmadhajjar.GithubNotificationsApp.ui.TrayAdapter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
Expand All @@ -20,7 +19,7 @@ public class RepoChecker extends Thread {
private static final long INITIAL_WAIT_TIME = 5000;
private final GitHubAPIService gitHubAPIService;
private final StorageService storageService;
Map<String, List<Integer>> reposPRMap;
Map<String, List<PR>> reposPRMap;

public RepoChecker(GitHubAPIService gitHubAPIService) {
this.gitHubAPIService = gitHubAPIService;
Expand All @@ -29,16 +28,22 @@ public RepoChecker(GitHubAPIService gitHubAPIService) {

@Override
public void run() {
// todo Put the try-catch inside the loop. And implement incremental waiting strategy.
try {
Thread.sleep(INITIAL_WAIT_TIME);
//noinspection InfiniteLoopStatement
while (true) {
} catch (InterruptedException e) {
logger.error(e);
}
//noinspection InfiniteLoopStatement
while (true) {
try {
checkReposForOpenPullRequests();
//noinspection BusyWait
Thread.sleep(ONE_MINUTE);

} catch (Throwable e) {
logger.error(e);
}
} catch (Throwable e) {
logger.error(e);
}
}

Expand All @@ -55,8 +60,8 @@ public void checkReposForOpenPullRequests() throws Exception {

boolean newRepo = !reposPRMap.containsKey(repoName);

List<Integer> lastPRNumbers = reposPRMap.get(repoName);
List<Integer> newPRNumbers = createPullRequestsNumbersList(pullRequests);
List<PR> lastPRNumbers = reposPRMap.get(repoName);
List<PR> newPRNumbers = createPullRequestsNumbersList(pullRequests);

reposPRMap.put(repoName, newPRNumbers);

Expand All @@ -68,23 +73,24 @@ public void checkReposForOpenPullRequests() throws Exception {
storageService.saveReposPRList(reposPRMap);
}

private static List<Integer> createPullRequestsNumbersList(JSONArray pullRequests) {
List<Integer> prNumbers = new ArrayList<>();
private static List<PR> createPullRequestsNumbersList(JSONArray pullRequests) {
List<PR> prNumbers = new ArrayList<>();
for (int j = 0; j < pullRequests.length(); j++) {
JSONObject pr = pullRequests.getJSONObject(j);
int prNumber = pr.getInt("number");
prNumbers.add(prNumber);
String username = pr.getJSONObject("user").getString("login");
prNumbers.add(new PR(username, prNumber));
}
return prNumbers;
}

private static class RepoNotificationThread extends Thread {

private final String repoName;
private final List<Integer> lastPRNumbers;
private final List<Integer> newPRNumbers;
private final List<PR> lastPRNumbers;
private final List<PR> newPRNumbers;

public RepoNotificationThread(String repoName, List<Integer> lastPRNumbers, List<Integer> newPRNumbers) {
public RepoNotificationThread(String repoName, List<PR> lastPRNumbers, List<PR> newPRNumbers) {
this.repoName = repoName;
this.lastPRNumbers = lastPRNumbers;
this.newPRNumbers = newPRNumbers;
Expand All @@ -96,9 +102,9 @@ public void run() {
}

private void notifyAboutNewPullRequests() {
for (Integer newPRNumber : newPRNumbers) {
for (PR newPRNumber : newPRNumbers) {
if (!lastPRNumbers.contains(newPRNumber)) {
NotificationCenter.getInstance().showNotification(repoName, newPRNumber);
NotificationCenter.getInstance().showNotification(repoName, newPRNumber.getPrId());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.ahmadhajjar.GithubNotificationsApp.service;

import me.ahmadhajjar.GithubNotificationsApp.PR;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
Expand Down Expand Up @@ -61,9 +62,9 @@ public void saveReposList(List<String> repos) {
}

@Override
public Map<String, List<Integer>> loadReposPRList() {
public Map<String, List<PR>> loadReposPRList() {
logger.debug("Loading Pull Requests from disk ...");
Map<String, List<Integer>> result = new HashMap<>();
Map<String, List<PR>> result = new HashMap<>();
File file = new File(WATCHED_REPOS_KNOWN_PRS_FILE);
if (file.exists()) {
try {
Expand All @@ -74,10 +75,11 @@ public Map<String, List<Integer>> loadReposPRList() {

for (String key : map.keySet()) {
List<?> intObjects = (List<?>) map.get(key);
List<Integer> ints = new ArrayList<>();
List<PR> ints = new ArrayList<>();

for (Object object : intObjects) {
ints.add(Integer.parseInt(object.toString()));
Map<String, Object> prMap = (Map<String, Object>) object;
ints.add(new PR(prMap.get("username").toString(), (Integer) prMap.get("prId")));
}
result.put(key, ints);
}
Expand All @@ -90,7 +92,7 @@ public Map<String, List<Integer>> loadReposPRList() {
}

@Override
public void saveReposPRList(Map<String, List<Integer>> reposPrs) {
public void saveReposPRList(Map<String, List<PR>> reposPrs) {
logger.debug("Saving the pull requests to disk ...");
try (FileWriter writer = new FileWriter(WATCHED_REPOS_KNOWN_PRS_FILE)) {
writer.write(JSONWriter.valueToString(reposPrs));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.ahmadhajjar.GithubNotificationsApp.service;

import me.ahmadhajjar.GithubNotificationsApp.PR;

import java.util.List;
import java.util.Map;

Expand All @@ -8,7 +10,7 @@ public interface StorageService {

void saveReposList(List<String> repos);

Map<String, List<Integer>> loadReposPRList();
Map<String, List<PR>> loadReposPRList();

void saveReposPRList(Map<String, List<Integer>> repos);
void saveReposPRList(Map<String, List<PR>> repos);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.ahmadhajjar.GithubNotificationsApp.ui;

import me.ahmadhajjar.GithubNotificationsApp.RepoChecker;
import me.ahmadhajjar.GithubNotificationsApp.service.DiskStorageService;
import me.ahmadhajjar.GithubNotificationsApp.ui.actionlistner.*;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -35,7 +36,10 @@ public class GithubNotifierApp extends JFrame {
private JButton helpButton;
private JButton missedNotificationsButton;

public GithubNotifierApp() {
private RepoChecker checker;

public GithubNotifierApp(RepoChecker checker) {
this.checker = checker;
initialiseWindow();

addRepo.addActionListener(new AddRepoButtonListener(newRepoName, watchedReposList));
Expand All @@ -57,7 +61,13 @@ public void windowClosing(WindowEvent e) {
}

private void initialiseTrayPopupMenu() {
TrayAdapter.getInstance().addPopupMenuItem("Reload", e -> logger.warn("Tray reload is not implemented yet"));
TrayAdapter.getInstance().addPopupMenuItem("Reload", e -> {
try {
this.checker.checkReposForOpenPullRequests();
} catch (Exception ex) {
logger.error("Failed to reload!");
}
});
TrayAdapter.getInstance().addPopupMenuItem("Exit", e -> exitApplication(this));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.ahmadhajjar.GithubNotificationsApp.ui;

import me.ahmadhajjar.GithubNotificationsApp.PR;
import me.ahmadhajjar.GithubNotificationsApp.service.DiskStorageService;
import me.ahmadhajjar.GithubNotificationsApp.utils.PROpener;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -96,8 +97,8 @@ public void populateTree() {
for (String repoName : reposPRs.keySet()) {
var repoTreeNode = new DefaultMutableTreeNode(repoName);

for (Integer prNumber : reposPRs.get(repoName)) {
var child = new DefaultMutableTreeNode(prNumber);
for (PR pr : reposPRs.get(repoName)) {
var child = new DefaultMutableTreeNode(pr);
repoTreeNode.add(child);
}
if (repoTreeNode.getChildCount() == 0) {
Expand All @@ -124,6 +125,11 @@ private void handleSelectionInteraction(JTree jTree) {
if (Objects.equals(prNumber, NO_PR_LABEL)) {
return;
}

DefaultMutableTreeNode lastPathComponent = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
PR pr = (PR) lastPathComponent.getUserObject();
prNumber = pr.getPrId().toString();

var repoName = selectionPath.getPathComponent(1);

final URI prURI = URI.create("https://github.com/" + repoName + "/pull/" + prNumber);
Expand Down