Skip to content

Commit a2396a0

Browse files
committed
Using Jgit rather than GitHub API to access repository contents
1 parent 5da3ba8 commit a2396a0

File tree

5 files changed

+161
-9
lines changed

5 files changed

+161
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ nbactions*.xml
55
.project
66
.vscode/launch.json
77
/psps/
8+
/checkout/

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@
8585
<artifactId>json-simple</artifactId>
8686
<version>1.1.1</version>
8787
</dependency>
88-
<dependency>
88+
<!-- <dependency>
8989
<groupId>com.github.scribejava</groupId>
9090
<artifactId>scribejava-apis</artifactId>
9191
<version>4.1.1</version>
92-
</dependency>
92+
</dependency> -->
9393
<dependency>
9494
<groupId>org.apache.httpcomponents</groupId>
9595
<artifactId>httpclient</artifactId>
@@ -101,6 +101,11 @@
101101
<version>1.6.1</version>
102102
<scope>runtime</scope>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.eclipse.jgit</groupId>
106+
<artifactId>org.eclipse.jgit</artifactId>
107+
<version>5.4.2.201908231537-r</version>
108+
</dependency>
104109
</dependencies>
105110
<profiles>
106111
<profile>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2019 me.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.factsmission.psps;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
31+
import java.util.Comparator;
32+
import java.util.List;
33+
34+
import org.eclipse.jgit.api.Git;
35+
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
36+
import org.eclipse.jgit.api.errors.GitAPIException;
37+
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
38+
import org.eclipse.jgit.errors.RevisionSyntaxException;
39+
import org.eclipse.jgit.lib.Ref;
40+
41+
import org.eclipse.jgit.lib.Constants;
42+
import org.eclipse.jgit.lib.ObjectId;
43+
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
44+
45+
/**
46+
*
47+
* @author me
48+
*/
49+
public class JGitRepository implements Repository {
50+
51+
private final static Path baseCheckout = Paths.get(System.getProperty("user.dir"), "checkout");
52+
private final String repoUri;
53+
private final String userName;
54+
private Git git;
55+
private final Path workingDir;
56+
private final String repoName;
57+
58+
JGitRepository(String repository, String token) throws IOException {
59+
this.repoName = repository;
60+
this.repoUri = "https://github.com/" + repository + ".git";
61+
this.userName = token;
62+
workingDir = baseCheckout.resolve(repository);
63+
if (Files.exists(workingDir)) {
64+
Files.walk(workingDir)
65+
.sorted(Comparator.reverseOrder())
66+
.map(Path::toFile)
67+
.forEach(File::delete); //TODO recycle
68+
}
69+
try {
70+
git = Git.cloneRepository()
71+
.setURI(repoUri)
72+
.setDirectory(workingDir.toFile())
73+
.setCloneAllBranches(false)
74+
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(token, ""))
75+
.call();
76+
77+
} catch (GitAPIException e) {
78+
throw new RuntimeException(e);
79+
} finally {
80+
git.close();
81+
}
82+
}
83+
84+
@Override
85+
public String[] getBranches() throws IOException {
86+
try {
87+
final List<Ref> branchRefs = git.branchList().setListMode(ListMode.REMOTE).call();
88+
final String[] result = new String[branchRefs.size()];
89+
int i = 0;
90+
for (Ref branchRef : branchRefs) {
91+
String[] parts = branchRef.getName().split("/");
92+
result[i++] = parts[parts.length-1];
93+
}
94+
return result;
95+
} catch (GitAPIException ex) {
96+
throw new IOException(ex);
97+
}
98+
99+
}
100+
101+
@Override
102+
public String getCommitURL() throws IOException {
103+
//something like https://github.com/factsmission/staging-website/commit/4467dc315fd80d4b98ad669e601b614d96e23041
104+
try {
105+
ObjectId id = git.getRepository().resolve(Constants.HEAD);
106+
String hexString = ObjectId.toString(id);
107+
return "https://github.com/"+repoName+"/commit/"+hexString;
108+
} catch (IncorrectObjectTypeException | RevisionSyntaxException ex) {
109+
throw new IOException(ex);
110+
}
111+
}
112+
113+
@Override
114+
public byte[] getContent(String repoPath) throws IOException {
115+
Path path = workingDir.resolve(repoPath);
116+
if (Files.isDirectory(path)) {
117+
return null;
118+
}
119+
return Files.readAllBytes(path);
120+
}
121+
122+
@Override
123+
public Iterable<String> getPaths() throws IOException {
124+
return (Iterable<String>)Files.walk(workingDir)
125+
.sorted(Comparator.reverseOrder())
126+
.map(p -> {System.out.println(p); System.out.println(workingDir.relativize(p)); return workingDir.relativize(p);})
127+
.map(Path::toString)
128+
.filter(s -> !s.startsWith(".git"))::iterator;
129+
}
130+
131+
@Override
132+
public void useBranch(String branch) throws IOException {
133+
try {
134+
git.checkout().setName("remotes/origin/"+branch).call();
135+
} catch (GitAPIException ex) {
136+
throw new IOException(ex);
137+
}
138+
}
139+
140+
}

src/main/java/com/factsmission/psps/Repository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public interface Repository {
3333

3434
String[] getBranches() throws IOException;
3535

36-
String getCommitURL();
36+
String getCommitURL() throws IOException;
3737

3838
byte[] getContent(String path) throws IOException;
3939

40-
Iterable<String> getPaths();
40+
Iterable<String> getPaths() throws IOException;
4141

4242
void useBranch(String branch) throws IOException;
4343

src/main/java/com/factsmission/psps/RepositoryProcessor.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ private IRI getBaseIRI(byte[] contentBytes) throws IOException {
142142
try {
143143
JSONParser jsonParser = new JSONParser();
144144
JSONObject contentObject = (JSONObject) jsonParser.parse(content);
145-
return new IRI((String) contentObject.get(branch));
145+
String branchPath = (String) contentObject.get(branch);
146+
if (branchPath == null) {
147+
return getDefaultBaseIRI();
148+
}
149+
return new IRI(branchPath);
146150
} catch (ParseException ex) {
147151
Logger.getLogger(RepositoryProcessor.class.getName()).log(Level.SEVERE, "Couln'd parse BASEURI, usind default base IRI", ex);
148152
return getDefaultBaseIRI();
@@ -154,12 +158,16 @@ private IRI getBaseIRI(byte[] contentBytes) throws IOException {
154158
return getDefaultBaseIRI();
155159
}
156160
}
161+
162+
private IRI getDefaultBaseIRI() {
163+
return new IRI("https://raw.githubusercontent.com/" + repositoryName + "/"+branch+"/");
164+
}
157165

158166
}
159167

160168
RepositoryProcessor(String repository, String token, boolean supressFileExtension) throws IOException {
161169
this.repositoryName = repository;
162-
this.repository = new ApiAccessRepository(repository, token);
170+
this.repository = new JGitRepository(repository, token); //ApiAccessRepository(repository, token);
163171
this.supressFileExtension = supressFileExtension;
164172
processRepository();
165173
}
@@ -203,9 +211,7 @@ private void processRepository() throws IOException {
203211
}
204212

205213

206-
private IRI getDefaultBaseIRI() {
207-
return new IRI("https://raw.githubusercontent.com/" + repositoryName + "/master/");
208-
}
214+
209215

210216
private IRI getRepoIRI() {
211217
return new IRI("https://github.com/" + repositoryName);

0 commit comments

Comments
 (0)