From 6a93a3798b9747987de7da34999a62900284cc73 Mon Sep 17 00:00:00 2001 From: Victor Guerra Veloso Date: Sat, 2 Nov 2024 01:50:49 -0400 Subject: [PATCH] Introduce OauthToken parameter to HTTP endpoint to allow the Chrome extension to inspect private repos --- .../RefactoringMinerHttpServer.java | 4 ++++ .../RefactoringMinerHttpsServer.java | 4 ++++ .../api/GitHistoryRefactoringMiner.java | 3 +++ .../rm1/GitHistoryRefactoringMinerImpl.java | 17 +++++++++++++++++ 4 files changed, 28 insertions(+) diff --git a/src/main/java/org/refactoringminer/RefactoringMinerHttpServer.java b/src/main/java/org/refactoringminer/RefactoringMinerHttpServer.java index 49b5fab6b7..af7f96ae4c 100644 --- a/src/main/java/org/refactoringminer/RefactoringMinerHttpServer.java +++ b/src/main/java/org/refactoringminer/RefactoringMinerHttpServer.java @@ -53,10 +53,14 @@ public void handle(HttpExchange exchange) throws IOException { String gitURL = queryToMap.get("gitURL"); String commitId = queryToMap.get("commitId"); + String oAuthToken = queryToMap.getOrDefault("token", ""); int timeout = Integer.parseInt(queryToMap.get("timeout")); List detectedRefactorings = new ArrayList(); GitHistoryRefactoringMiner miner = new GitHistoryRefactoringMinerImpl(); + if (!oAuthToken.isEmpty()) { + miner.connectToGitHub(oAuthToken); + } miner.detectAtCommit(gitURL, commitId, new RefactoringHandler() { @Override public void handle(String commitId, List refactorings) { diff --git a/src/main/java/org/refactoringminer/RefactoringMinerHttpsServer.java b/src/main/java/org/refactoringminer/RefactoringMinerHttpsServer.java index 83c2a595d2..5ee7885789 100644 --- a/src/main/java/org/refactoringminer/RefactoringMinerHttpsServer.java +++ b/src/main/java/org/refactoringminer/RefactoringMinerHttpsServer.java @@ -102,10 +102,14 @@ public void handle(HttpExchange exchange) throws IOException { String gitURL = queryToMap.get("gitURL"); String commitId = queryToMap.get("commitId"); + String oAuthToken = queryToMap.getOrDefault("token", ""); int timeout = Integer.parseInt(queryToMap.get("timeout")); List detectedRefactorings = new ArrayList(); GitHistoryRefactoringMiner miner = new GitHistoryRefactoringMinerImpl(); + if (!oAuthToken.isEmpty()) { + miner.connectToGitHub(oAuthToken); + } miner.detectAtCommit(gitURL, commitId, new RefactoringHandler() { @Override public void handle(String commitId, List refactorings) { diff --git a/src/main/java/org/refactoringminer/api/GitHistoryRefactoringMiner.java b/src/main/java/org/refactoringminer/api/GitHistoryRefactoringMiner.java index dee8b0e439..26c06bfeff 100644 --- a/src/main/java/org/refactoringminer/api/GitHistoryRefactoringMiner.java +++ b/src/main/java/org/refactoringminer/api/GitHistoryRefactoringMiner.java @@ -5,6 +5,7 @@ import java.util.Map; import org.eclipse.jgit.lib.Repository; +import org.kohsuke.github.GitHub; import org.refactoringminer.astDiff.models.ProjectASTDiff; /** @@ -13,6 +14,8 @@ */ public interface GitHistoryRefactoringMiner { + GitHub connectToGitHub(String oAuthToken); + /** * Iterate over each commit of a git repository and detect all refactorings performed in the * entire repository history. Merge commits are ignored to avoid detecting the same refactoring diff --git a/src/main/java/org/refactoringminer/rm1/GitHistoryRefactoringMinerImpl.java b/src/main/java/org/refactoringminer/rm1/GitHistoryRefactoringMinerImpl.java index e611e74470..175410fc5f 100644 --- a/src/main/java/org/refactoringminer/rm1/GitHistoryRefactoringMinerImpl.java +++ b/src/main/java/org/refactoringminer/rm1/GitHistoryRefactoringMinerImpl.java @@ -594,6 +594,23 @@ else if (commitFile.getStatus().equals("renamed")) { } } + @Override + public GitHub connectToGitHub(String oAuthToken) { + if(gitHub == null) { + try { + gitHub = GitHub.connectUsingOAuth(oAuthToken); + if(gitHub.isCredentialValid()) { + logger.info("Connected to GitHub with OAuth token"); + } else { + connectToGitHub(); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + } + return gitHub; + } + private GitHub connectToGitHub() { if(gitHub == null) { try {