Skip to content

Commit 166a81f

Browse files
author
gdy
committed
Merge branch 'master' into B1
# Conflicts: # build.gradle
2 parents e6a6d98 + f0e3096 commit 166a81f

File tree

4 files changed

+33
-85
lines changed

4 files changed

+33
-85
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Features:
5858
```
5959
- Use methods of `IVCS` interface. See [pk-vcs-api](https://github.com/ProjectKaiser/pk-vcs-api) for details
6060
- Use `vcs.setProxy()` and `vcs.setCredentials()` if necessary
61-
- Github has some latency for exposing results of previously executed operations. For example if create a new branch and immediately check branches list then Github could return old branches list. Use `GitVCS.setExpectedLatency()` to set delay which will be executed after each operation which may have server latency
6261

6362
# Implementation details
6463
- [JGit](https://eclipse.org/jgit/) is used as framework to work with Git repositories
@@ -75,7 +74,6 @@ Features:
7574
- `PK_VCS_TEST_GITHUB_PASS` environment var or JVM var is used as user password for access to Github
7675
- New Test Repository is created before each test and deletes automatically after each test
7776
- To execute tests just run GitVCSTest class as JUnit test. Tests from VCSAbstractTest class will be executed. See [pk-vcs-test](https://github.com/ProjectKaiser/pk-vcs-test) for details
78-
- NOTE: Github has some latency for exposing results of previously executed operations. For example if create a new branch and immediately check branches list then Github could return old branches list. If a test is failed then try to execute it again. Also use `GitVCS.setExpectedLatency()` to set delay for 'dangerous' operations.
7977

8078
# Limitations
8179
- Commit messages can not be attached to branch create and delete operations because Git does not exposes these operations as separate commits

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies {
1919
compile 'commons-logging:commons-logging:1.2'
2020
compile 'org.eclipse.jgit:org.eclipse.jgit:4.3.0.201604071810-r'
2121

22-
testCompile 'org.kohsuke:github-api:1.75'
2322
testCompile 'org.mockito:mockito-core:2.0.62-beta'
2423
testCompile 'junit:junit:4.12'
2524
testCompile 'com.github.ProjectKaiser:pk-vcs-test:1.1'

src/main/java/com/projectkaiser/scm/vcs/GitVCS.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
3131
import org.eclipse.jgit.diff.DiffEntry.Side;
3232
import org.eclipse.jgit.diff.DiffFormatter;
33+
import org.eclipse.jgit.lib.Constants;
3334
import org.eclipse.jgit.lib.ObjectReader;
3435
import org.eclipse.jgit.lib.Ref;
3536
import org.eclipse.jgit.lib.Repository;
@@ -55,20 +56,11 @@ public class GitVCS implements IVCS {
5556

5657
private static final String MASTER_BRANCH_NAME = "master";
5758
public static final String GIT_VCS_TYPE_STRING = "git";
58-
private static final String REFS_REMOTES_ORIGIN = "refs/remotes/origin/";
59-
private Integer expectedLatency = 0;
60-
59+
private static final String REFS_REMOTES_ORIGIN = Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/";
60+
6161
private CredentialsProvider credentials;
6262
private IVCSRepositoryWorkspace repo;
6363

64-
public Integer getExpectedLatency() {
65-
return expectedLatency;
66-
}
67-
68-
public void setExpectedLatency(Integer expectedLatency) {
69-
this.expectedLatency = expectedLatency;
70-
}
71-
7264
public CredentialsProvider getCredentials() {
7365
return credentials;
7466
}
@@ -110,8 +102,6 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
110102
.setRefSpecs(refSpec)
111103
.setCredentialsProvider(credentials)
112104
.call();
113-
Thread.sleep(expectedLatency); // github has some latency on branch operations
114-
// so next request branches operation will return old branches list
115105
} finally {
116106
git.getRepository().close();
117107
}
@@ -162,8 +152,6 @@ public void deleteBranch(String branchName, String commitMessage) {
162152
.setCredentialsProvider(credentials)
163153
.call();
164154
git.getRepository().close();
165-
Thread.sleep(expectedLatency); // github has some latency on branch operations
166-
// so next request branches operation will return old branches list
167155
}
168156
}
169157
} catch (GitAPIException e) {
@@ -174,7 +162,6 @@ public void deleteBranch(String branchName, String commitMessage) {
174162
}
175163

176164
public Git getLocalGit(IVCSLockedWorkingCopy wc) {
177-
178165
Repository gitRepo;
179166
try {
180167
gitRepo = new FileRepositoryBuilder()
@@ -186,6 +173,7 @@ public Git getLocalGit(IVCSLockedWorkingCopy wc) {
186173
Boolean repoInited = gitRepo
187174
.getObjectDatabase()
188175
.exists();
176+
Git git = new Git(gitRepo);
189177
if (!repoInited) {
190178
try {
191179
Git
@@ -194,15 +182,16 @@ public Git getLocalGit(IVCSLockedWorkingCopy wc) {
194182
.setURI(repo.getRepoUrl())
195183
.setCredentialsProvider(credentials)
196184
.setNoCheckout(true)
185+
.setBranch(Constants.R_HEADS + Constants.MASTER)
197186
.call()
198187
.close();
199-
} catch (GitAPIException e) {
188+
return git;
189+
} catch (Exception e) {
200190
throw new EVCSException(e);
191+
201192
}
202-
203193
}
204-
Git res = new Git(gitRepo);
205-
return res;
194+
return git;
206195
}
207196

208197
@Override
@@ -231,12 +220,12 @@ public VCSMergeResult merge(String srcBranchName, String dstBranchName, String c
231220

232221
VCSMergeResult res = new VCSMergeResult();
233222

234-
res.setSuccess(!mr.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING) &&
223+
res.setSuccess(
224+
!mr.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING) &&
235225
!mr.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) &&
236226
!mr.getMergeStatus().equals(MergeResult.MergeStatus.ABORTED) &&
237227
!mr.getMergeStatus().equals(MergeResult.MergeStatus.NOT_SUPPORTED));
238228

239-
240229
if (!res.getSuccess()) {
241230
res.getConflictingFiles().addAll(mr.getConflicts().keySet());
242231
try {
@@ -348,13 +337,15 @@ public void setFileContent(String branchName, String filePath, String content, S
348337
.pull()
349338
.setCredentialsProvider(credentials)
350339
.call();
351-
340+
352341
git
353342
.checkout()
354343
.setCreateBranch(git.getRepository().exactRef("refs/heads/" + bn) == null)
355344
.setName(bn)
356345
.call();
357346

347+
348+
358349
File file = new File(wc.getFolder(), filePath);
359350
if (!file.exists()) {
360351
FileUtils.forceMkdir(file.getParentFile());

src/test/java/com/projectkaiser/scm/vcs/GitVCSTest.java

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import static org.junit.Assert.assertTrue;
55

6+
import java.io.File;
67
import java.io.IOException;
78

9+
import org.apache.commons.io.FileUtils;
810
import org.eclipse.jgit.api.Git;
11+
import org.eclipse.jgit.lib.Repository;
912
import org.junit.After;
1013
import org.junit.BeforeClass;
11-
import org.kohsuke.github.GHRepository;
12-
import org.kohsuke.github.GitHub;
1314
import org.mockito.Mockito;
1415

1516
import com.projectkaiser.scm.vcs.api.IVCS;
@@ -22,17 +23,9 @@ public class GitVCSTest extends VCSAbstractTest {
2223
System.getenv("PK_VCS_TEST_GITHUB_USER") : System.getProperty("PK_VCS_TEST_GITHUB_USER");
2324
private static final String GITHUB_PASS = System.getProperty("PK_VCS_TEST_GITHUB_PASS") == null ?
2425
System.getenv("PK_VCS_TEST_GITHUB_PASS") : System.getProperty("PK_VCS_TEST_GITHUB_PASS");
25-
private static final String PROXY_HOST = getJvmProperty("https.proxyHost");
26-
private static final Integer PROXY_PORT = getJvmProperty("https.proxyPort") == null ? null :
27-
Integer.parseInt(getJvmProperty("https.proxyPort"));
28-
private static final String PROXY_USER = getJvmProperty("https.proxyUser");
29-
private static final String PROXY_PASS = getJvmProperty("https.proxyPassword");
30-
31-
private GitHub github;
32-
33-
private GHRepository gitHubRepo;
34-
private String gitUrl = "https://github.com/" + GITHUB_USER + "/";
26+
3527
private Git mockedGit;
28+
private Repository localGitRepo;
3629
private RuntimeException testGitResetException = new RuntimeException("test exeption on git.reset()");
3730

3831
@BeforeClass
@@ -43,68 +36,35 @@ public static void setUpClass() {
4336
GITHUB_PASS != null);
4437
}
4538

46-
private static String getJvmProperty(String name) {
47-
if (name == null) {
48-
return null;
49-
}
50-
51-
String res = System.getProperty(name);
52-
if (res != null) {
53-
return res;
54-
}
55-
56-
res = System.getenv("JAVA_OPTS");
57-
if (res == null) {
58-
return null;
59-
}
60-
61-
Integer st = res.indexOf(name);
62-
if (st < 0) {
63-
return null;
64-
}
65-
66-
res = res.substring(st + name.length() + 1, res.indexOf(" -D", st + name.length() + 1) < 0 ? name.length() :
67-
res.indexOf(" -D", st + name.length())).trim();
68-
return res;
69-
}
70-
71-
7239
@Override
7340
public void setUp() throws Exception {
7441
super.setUp();
75-
github = GitHub.connectUsingPassword(GITHUB_USER, GITHUB_PASS);
76-
gitHubRepo = github.createRepository(repoName)
77-
.issues(false)
78-
.wiki(false)
79-
.autoInit(true)
80-
.downloads(false)
81-
.create();
42+
Git git = Git
43+
.init()
44+
.setDirectory(new File(localVCSWorkspace.getHomeFolder(), repoName))
45+
.setBare(false)
46+
.call();
47+
localGitRepo = git.getRepository();
48+
git
49+
.commit()
50+
.setMessage("Initial commit")
51+
.call();
8252
}
8353

8454
@After
85-
public void tearDown() {
86-
if (gitHubRepo != null) {
87-
try {
88-
gitHubRepo.delete();
89-
} catch (IOException e) {
90-
// do not affect the test
91-
}
92-
}
55+
public void tearDown() throws IOException {
56+
localGitRepo.close();
57+
FileUtils.deleteDirectory(localGitRepo.getDirectory());
9358
}
9459

9560
@Override
9661
protected String getTestRepoUrl() {
97-
return gitUrl;
62+
return ("file:///" + localVCSWorkspace.getHomeFolder() + "/").replace("\\", "/");
9863
}
9964

10065
@Override
10166
protected IVCS getVCS(IVCSRepositoryWorkspace mockedVCSRepo) {
10267
IVCS vcs = Mockito.spy(new GitVCS(mockedVCSRepo));
103-
vcs.setCredentials(GITHUB_USER, GITHUB_PASS);
104-
if (PROXY_HOST != null) {
105-
vcs.setProxy(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS);
106-
}
107-
((GitVCS) vcs).setExpectedLatency(2000);
10868
return vcs;
10969
}
11070

0 commit comments

Comments
 (0)