Skip to content

Commit

Permalink
Handle private repositories in GitHub APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Mar 12, 2024
1 parent d3d96be commit 8db65be
Showing 1 changed file with 109 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -871,12 +871,22 @@ private void populateWithGitHubAPI(String cloneURL, String currentCommitId, List
if (commitFile.getStatus().equals("modified")) {
Runnable r = () -> {
try {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
String currentRawFile = IOUtils.toString(currentRawFileInputStream);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
String parentRawFile = IOUtils.toString(parentRawFileInputStream);
String currentRawFile = null;
String parentRawFile = null;
if(repository.isPrivate()) {
InputStream currentRawFileInputStream = repository.getFileContent(fileName, currentCommitId).read();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
InputStream parentRawFileInputStream = repository.getFileContent(fileName, parentCommitId).read();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
else {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
filesBefore.put(fileName, parentRawFile);
filesCurrent.put(fileName, currentRawFile);
}
Expand All @@ -889,9 +899,16 @@ private void populateWithGitHubAPI(String cloneURL, String currentCommitId, List
else if (commitFile.getStatus().equals("added")) {
Runnable r = () -> {
try {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
String currentRawFile = IOUtils.toString(currentRawFileInputStream);
String currentRawFile = null;
if(repository.isPrivate()) {
InputStream currentRawFileInputStream = repository.getFileContent(fileName, currentCommitId).read();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
}
else {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
}
filesCurrent.put(fileName, currentRawFile);
}
catch(IOException e) {
Expand All @@ -903,10 +920,17 @@ else if (commitFile.getStatus().equals("added")) {
else if (commitFile.getStatus().equals("removed")) {
Runnable r = () -> {
try {
URL rawURL = commitFile.getRawUrl();
InputStream rawFileInputStream = rawURL.openStream();
String rawFile = IOUtils.toString(rawFileInputStream);
filesBefore.put(fileName, rawFile);
String parentRawFile = null;
if(repository.isPrivate()) {
InputStream parentRawFileInputStream = repository.getFileContent(fileName, parentCommitId).read();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
else {
URL rawURL = commitFile.getRawUrl();
InputStream rawFileInputStream = rawURL.openStream();
parentRawFile = IOUtils.toString(rawFileInputStream);
}
filesBefore.put(fileName, parentRawFile);
if(fileName.contains("/")) {
deletedAndRenamedFileParentDirectories.add(fileName.substring(0, fileName.lastIndexOf("/")));
}
Expand All @@ -922,14 +946,24 @@ else if (commitFile.getStatus().equals("renamed")) {
Runnable r = () -> {
try {
String previousFilename = commitFile.getPreviousFilename();
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
String currentRawFile = IOUtils.toString(currentRawFileInputStream);
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
String encodedPreviousFilename = URLEncoder.encode(previousFilename, StandardCharsets.UTF_8);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId).replace(encodedFileName, encodedPreviousFilename);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
String parentRawFile = IOUtils.toString(parentRawFileInputStream);
String currentRawFile = null;
String parentRawFile = null;
if(repository.isPrivate()) {
InputStream currentRawFileInputStream = repository.getFileContent(fileName, currentCommitId).read();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
InputStream parentRawFileInputStream = repository.getFileContent(previousFilename, parentCommitId).read();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
else {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
String encodedPreviousFilename = URLEncoder.encode(previousFilename, StandardCharsets.UTF_8);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId).replace(encodedFileName, encodedPreviousFilename);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
filesBefore.put(previousFilename, parentRawFile);
filesCurrent.put(fileName, currentRawFile);
renamedFilesHint.put(previousFilename, fileName);
Expand Down Expand Up @@ -1069,12 +1103,22 @@ public ChangedFileInfo populateWithGitHubAPIAndSaveFiles(String cloneURL, String
if (commitFile.getStatus().equals("modified")) {
Runnable r = () -> {
try {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
String currentRawFile = IOUtils.toString(currentRawFileInputStream);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
String parentRawFile = IOUtils.toString(parentRawFileInputStream);
String currentRawFile = null;
String parentRawFile = null;
if(repository.isPrivate()) {
InputStream currentRawFileInputStream = repository.getFileContent(fileName, currentCommitId).read();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
InputStream parentRawFileInputStream = repository.getFileContent(fileName, parentCommitId).read();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
else {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
filesBefore.put(fileName, parentRawFile);
filesCurrent.put(fileName, currentRawFile);
File parentFilePath = new File(rootFolder, repoName + "-" + parentCommitId + "/" + fileName);
Expand All @@ -1091,9 +1135,16 @@ public ChangedFileInfo populateWithGitHubAPIAndSaveFiles(String cloneURL, String
else if (commitFile.getStatus().equals("added")) {
Runnable r = () -> {
try {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
String currentRawFile = IOUtils.toString(currentRawFileInputStream);
String currentRawFile = null;
if(repository.isPrivate()) {
InputStream currentRawFileInputStream = repository.getFileContent(fileName, currentCommitId).read();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
}
else {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
}
filesCurrent.put(fileName, currentRawFile);
File currentFilePath = new File(rootFolder, repoName + "-" + currentCommitId + "/" + fileName);
FileUtils.writeStringToFile(currentFilePath, currentRawFile);
Expand All @@ -1107,9 +1158,16 @@ else if (commitFile.getStatus().equals("added")) {
else if (commitFile.getStatus().equals("removed")) {
Runnable r = () -> {
try {
URL rawURL = commitFile.getRawUrl();
InputStream rawFileInputStream = rawURL.openStream();
String parentRawFile = IOUtils.toString(rawFileInputStream);
String parentRawFile = null;
if(repository.isPrivate()) {
InputStream parentRawFileInputStream = repository.getFileContent(fileName, parentCommitId).read();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
else {
URL rawURL = commitFile.getRawUrl();
InputStream rawFileInputStream = rawURL.openStream();
parentRawFile = IOUtils.toString(rawFileInputStream);
}
filesBefore.put(fileName, parentRawFile);
if(fileName.contains("/")) {
deletedAndRenamedFileParentDirectories.add(fileName.substring(0, fileName.lastIndexOf("/")));
Expand All @@ -1128,14 +1186,24 @@ else if (commitFile.getStatus().equals("renamed")) {
Runnable r = () -> {
try {
String previousFilename = commitFile.getPreviousFilename();
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
String currentRawFile = IOUtils.toString(currentRawFileInputStream);
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
String encodedPreviousFilename = URLEncoder.encode(previousFilename, StandardCharsets.UTF_8);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId).replace(encodedFileName, encodedPreviousFilename);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
String parentRawFile = IOUtils.toString(parentRawFileInputStream);
String currentRawFile = null;
String parentRawFile = null;
if(repository.isPrivate()) {
InputStream currentRawFileInputStream = repository.getFileContent(fileName, currentCommitId).read();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
InputStream parentRawFileInputStream = repository.getFileContent(previousFilename, parentCommitId).read();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
else {
URL currentRawURL = commitFile.getRawUrl();
InputStream currentRawFileInputStream = currentRawURL.openStream();
currentRawFile = IOUtils.toString(currentRawFileInputStream);
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
String encodedPreviousFilename = URLEncoder.encode(previousFilename, StandardCharsets.UTF_8);
String rawURLInParentCommit = currentRawURL.toString().replace(commitId, parentCommitId).replace(encodedFileName, encodedPreviousFilename);
InputStream parentRawFileInputStream = new URL(rawURLInParentCommit).openStream();
parentRawFile = IOUtils.toString(parentRawFileInputStream);
}
filesBefore.put(previousFilename, parentRawFile);
filesCurrent.put(fileName, currentRawFile);
renamedFilesHint.put(previousFilename, fileName);
Expand Down

0 comments on commit 8db65be

Please sign in to comment.