Skip to content

Commit 7d4f194

Browse files
authored
Merge pull request #621 from bitwiseman/lifecycle
Re-enable Lifecycle test
2 parents 0a40dc5 + eedefcd commit 7d4f194

File tree

45 files changed

+1396
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1396
-194
lines changed

src/main/java/org/kohsuke/github/GHRelease.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.net.URL;
8+
import java.net.URLEncoder;
89
import java.util.Arrays;
910
import java.util.Date;
1011
import java.util.List;
@@ -240,11 +241,10 @@ public GHAsset uploadAsset(File file, String contentType) throws IOException {
240241
*/
241242
public GHAsset uploadAsset(String filename, InputStream stream, String contentType) throws IOException {
242243
Requester builder = new Requester(owner.root);
243-
244-
String url = format("https://uploads.github.com%s/releases/%d/assets?name=%s",
245-
owner.getApiTailUrl(""),
246-
getId(),
247-
filename);
244+
String url = getUploadUrl();
245+
// strip the helpful garbage from the url
246+
url = url.substring(0, url.indexOf('{'));
247+
url += "?name=" + URLEncoder.encode(filename, "UTF-8");
248248
return builder.contentType(contentType).with(stream).to(url, GHAsset.class).wrap(this);
249249
}
250250

src/test/java/org/kohsuke/github/AbstractGitHubWireMockTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,21 @@ protected GHRepository getTempRepository(String name) throws IOException {
170170
.description("A test repository for testing the github-api project: " + name)
171171
.homepage("http://github-api.kohsuke.org/")
172172
.autoInit(true)
173+
.wiki(true)
174+
.downloads(true)
175+
.issues(true)
176+
.private_(false)
173177
.create();
174178
try {
175179
Thread.sleep(3000);
176180
} catch (InterruptedException e) {
177181
throw new RuntimeException(e.getMessage(), e);
178182
}
179-
180-
configureTempRepository(repository);
181183
}
182184

183185
return gitHub.getRepository(fullName);
184186
}
185187

186-
protected void configureTempRepository(GHRepository repository) throws IOException {
187-
repository.enableIssueTracker(true);
188-
repository.enableDownloads(true);
189-
repository.enableWiki(true);
190-
}
191-
192188
@Before
193189
@After
194190
public void cleanupTempRepositories() throws IOException {

src/test/java/org/kohsuke/github/LifecycleTest.java

Lines changed: 25 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,42 @@
11
package org.kohsuke.github;
22

3-
import org.apache.commons.io.IOUtils;
4-
import org.eclipse.jgit.api.Git;
5-
import org.eclipse.jgit.api.errors.GitAPIException;
6-
import org.eclipse.jgit.dircache.DirCache;
7-
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
83
import org.junit.Test;
94

105
import java.io.File;
11-
import java.io.FileInputStream;
126
import java.io.FileWriter;
137
import java.io.IOException;
148
import java.io.PrintWriter;
159
import java.util.List;
16-
import java.util.Properties;
1710

18-
public class LifecycleTest extends AbstractGitHubApiTestBase {
11+
import static org.hamcrest.CoreMatchers.notNullValue;
12+
import static org.hamcrest.core.Is.is;
13+
14+
public class LifecycleTest extends AbstractGitHubWireMockTest {
1915
@Test
20-
public void testCreateRepository() throws IOException, GitAPIException, InterruptedException {
16+
public void testCreateRepository() throws IOException {
2117
GHMyself myself = gitHub.getMyself();
22-
GHOrganization org = gitHub.getOrganization("github-api-test-org");
23-
GHRepository repository = org.getRepository("github-api-test");
24-
if (repository != null) {
25-
repository.delete();
26-
Thread.sleep(1000);
27-
}
28-
repository = org.createRepository("github-api-test",
29-
"a test repository used to test kohsuke's github-api",
30-
"http://github-api.kohsuke.org/",
31-
"Core Developers",
32-
true);
33-
Thread.sleep(1000); // wait for the repository to become ready
18+
// GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
3419

20+
GHRepository repository = getTempRepository();
3521
assertTrue(repository.getReleases().isEmpty());
36-
try {
37-
GHMilestone milestone = repository.createMilestone("Initial Release", "first one");
38-
GHIssue issue = repository.createIssue("Test Issue")
39-
.body("issue body just for grins")
40-
.milestone(milestone)
41-
.assignee(myself)
42-
.label("bug")
43-
.create();
44-
File repoDir = new File(System.getProperty("java.io.tmpdir"), "github-api-test");
45-
delete(repoDir);
46-
Git origin = Git.cloneRepository()
47-
.setBare(false)
48-
.setURI(repository.getSshUrl())
49-
.setDirectory(repoDir)
50-
.setCredentialsProvider(getCredentialsProvider(myself))
51-
.call();
52-
53-
commitTestFile(myself, repoDir, origin);
54-
55-
GHRelease release = createRelease(repository);
56-
57-
GHAsset asset = uploadAsset(release);
58-
59-
updateAsset(release, asset);
60-
61-
deleteAsset(release, asset);
62-
} finally {
63-
repository.delete();
64-
}
22+
23+
GHMilestone milestone = repository.createMilestone("Initial Release", "first one");
24+
GHIssue issue = repository.createIssue("Test Issue")
25+
.body("issue body just for grins")
26+
.milestone(milestone)
27+
.assignee(myself)
28+
.label("bug")
29+
.create();
30+
31+
assertThat(issue, is(notNullValue()));
32+
33+
GHRelease release = createRelease(repository);
34+
35+
GHAsset asset = uploadAsset(release);
36+
37+
updateAsset(release, asset);
38+
39+
deleteAsset(release, asset);
6540
}
6641

6742
private void updateAsset(GHRelease release, GHAsset asset) throws IOException {
@@ -96,25 +71,6 @@ private GHRelease createRelease(GHRepository repository) throws IOException {
9671
return release;
9772
}
9873

99-
private void commitTestFile(GHMyself myself, File repoDir, Git origin) throws IOException, GitAPIException {
100-
File dummyFile = createDummyFile(repoDir);
101-
DirCache cache = origin.add().addFilepattern(dummyFile.getName()).call();
102-
origin.commit().setMessage("test commit").call();
103-
origin.push().setCredentialsProvider(getCredentialsProvider(myself)).call();
104-
}
105-
106-
private UsernamePasswordCredentialsProvider getCredentialsProvider(GHMyself myself) throws IOException {
107-
Properties props = new Properties();
108-
File homeDir = new File(System.getProperty("user.home"));
109-
FileInputStream in = new FileInputStream(new File(homeDir, ".github"));
110-
try {
111-
props.load(in);
112-
} finally {
113-
IOUtils.closeQuietly(in);
114-
}
115-
return new UsernamePasswordCredentialsProvider(props.getProperty("login"), props.getProperty("oauth"));
116-
}
117-
11874
private void delete(File toDelete) {
11975
if (toDelete.isDirectory()) {
12076
for (File file : toDelete.listFiles()) {

src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public WireMockServer rawServer() {
5858
return servers.get("raw");
5959
}
6060

61+
public WireMockServer uploadsServer() {
62+
return servers.get("uploads");
63+
}
64+
6165
public boolean isUseProxy() {
6266
return GitHubWireMockRule.useProxy;
6367
}
@@ -69,39 +73,69 @@ public boolean isTakeSnapshot() {
6973
@Override
7074
protected void initializeServers() {
7175
super.initializeServers();
72-
initializeServer("raw");
7376
initializeServer("default", new GitHubApiResponseTransformer(this));
77+
78+
// only start non-api servers if we might need them
79+
if (new File(apiServer().getOptions().filesRoot().getPath() + "_raw").exists() || isUseProxy()) {
80+
initializeServer("raw");
81+
}
82+
if (new File(apiServer().getOptions().filesRoot().getPath() + "_uploads").exists() || isUseProxy()) {
83+
initializeServer("uploads");
84+
}
7485
}
7586

7687
@Override
7788
protected void before() {
7889
super.before();
79-
if (isUseProxy()) {
80-
this.apiServer().stubFor(proxyAllTo("https://api.github.com").atPriority(100));
90+
if (!isUseProxy()) {
91+
return;
92+
}
93+
94+
this.apiServer().stubFor(proxyAllTo("https://api.github.com").atPriority(100));
95+
96+
if (this.rawServer() != null) {
8197
this.rawServer().stubFor(proxyAllTo("https://raw.githubusercontent.com").atPriority(100));
8298
}
99+
100+
if (this.uploadsServer() != null) {
101+
this.uploadsServer().stubFor(proxyAllTo("https://uploads.github.com").atPriority(100));
102+
}
83103
}
84104

85105
@Override
86106
protected void after() {
87107
super.after();
88-
if (isTakeSnapshot()) {
89-
this.apiServer()
90-
.snapshotRecord(recordSpec().forTarget("https://api.github.com")
91-
.captureHeader("If-None-Match")
92-
.extractTextBodiesOver(255));
108+
if (!isTakeSnapshot()) {
109+
return;
110+
}
111+
112+
this.apiServer()
113+
.snapshotRecord(recordSpec().forTarget("https://api.github.com")
114+
.captureHeader("If-None-Match")
115+
.extractTextBodiesOver(255));
93116

117+
// After taking the snapshot, format the output
118+
formatJsonFiles(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath());
119+
120+
if (this.rawServer() != null) {
94121
this.rawServer()
95122
.snapshotRecord(recordSpec().forTarget("https://raw.githubusercontent.com")
96123
.captureHeader("If-None-Match")
97124
.extractTextBodiesOver(255));
98125

99-
// After taking the snapshot, format the output
100-
formatJsonFiles(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath());
101-
102126
// For raw server, only fix up mapping files
103127
formatJsonFiles(new File(this.rawServer().getOptions().filesRoot().child("mappings").getPath()).toPath());
104128
}
129+
130+
if (this.uploadsServer() != null) {
131+
this.uploadsServer()
132+
.snapshotRecord(recordSpec().forTarget("https://uploads.github.com")
133+
.captureHeader("If-None-Match")
134+
.extractTextBodiesOver(255));
135+
136+
formatJsonFiles(new File(this.uploadsServer().getOptions().filesRoot().getPath()).toPath());
137+
138+
}
105139
}
106140

107141
public int getRequestCount() {
@@ -137,8 +171,17 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
137171
String fileText = new String(Files.readAllBytes(filePath));
138172
// while recording responses we replaced all github calls localhost
139173
// now we reverse that for storage.
140-
fileText = fileText.replace(this.apiServer().baseUrl(), "https://api.github.com")
141-
.replace(this.rawServer().baseUrl(), "https://raw.githubusercontent.com");
174+
fileText = fileText.replace(this.apiServer().baseUrl(), "https://api.github.com");
175+
176+
if (this.rawServer() != null) {
177+
fileText = fileText.replace(this.rawServer().baseUrl(),
178+
"https://raw.githubusercontent.com");
179+
}
180+
181+
if (this.uploadsServer() != null) {
182+
fileText = fileText.replace(this.uploadsServer().baseUrl(), "https://uploads.github.com");
183+
}
184+
142185
// Can be Array or Map
143186
Object parsedObject = g.fromJson(fileText, Object.class);
144187
if (parsedObject instanceof Map && filePath.toString().contains("mappings")) {
@@ -196,9 +239,21 @@ public Response transform(Request request, Response response, FileSource files,
196239

197240
String body;
198241
body = getBodyAsString(response, headers);
242+
body = body.replace("https://api.github.com", rule.apiServer().baseUrl());
243+
244+
if (rule.rawServer() != null) {
245+
body = body.replace("https://raw.githubusercontent.com", rule.rawServer().baseUrl());
246+
} else {
247+
body = body.replace("https://raw.githubusercontent.com", rule.apiServer().baseUrl() + "/raw");
248+
}
249+
250+
if (rule.uploadsServer() != null) {
251+
body = body.replace("https://uploads.github.com", rule.uploadsServer().baseUrl());
252+
} else {
253+
body = body.replace("https://uploads.github.com", rule.apiServer().baseUrl() + "/uploads");
254+
}
199255

200-
builder.body(body.replace("https://api.github.com", rule.apiServer().baseUrl())
201-
.replace("https://raw.githubusercontent.com", rule.rawServer().baseUrl()));
256+
builder.body(body);
202257

203258
}
204259
builder.headers(new HttpHeaders(headers));

src/test/resources/org/kohsuke/github/GHAppTest/wiremock/createToken/mappings/mapping-githubapp-app.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"request": {
33
"url": "/app",
44
"method": "GET",
5-
"headers" : {
6-
"Accept" : {
7-
"equalTo" : "application/vnd.github.machine-man-preview+json"
5+
"headers": {
6+
"Accept": {
7+
"equalTo": "application/vnd.github.machine-man-preview+json"
88
}
99
}
1010
},
@@ -17,7 +17,10 @@
1717
"Content-Type": "application/json; charset=utf-8",
1818
"Status": "200 OK",
1919
"Cache-Control": "public, max-age=60, s-maxage=60",
20-
"Vary": ["Accept","Accept-Encoding"],
20+
"Vary": [
21+
"Accept",
22+
"Accept-Encoding"
23+
],
2124
"ETag": "W/\"01163b1a237898d328ed56cd0e9aefca\"",
2225
"X-GitHub-Media-Type": "github.machine-man-preview; format=json",
2326
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",

0 commit comments

Comments
 (0)