Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable Lifecycle test #621

Merged
merged 3 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/org/kohsuke/github/GHRelease.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -240,11 +241,10 @@ public GHAsset uploadAsset(File file, String contentType) throws IOException {
*/
public GHAsset uploadAsset(String filename, InputStream stream, String contentType) throws IOException {
Requester builder = new Requester(owner.root);

String url = format("https://uploads.github.com%s/releases/%d/assets?name=%s",
owner.getApiTailUrl(""),
getId(),
filename);
String url = getUploadUrl();
// strip the helpful garbage from the url
url = url.substring(0, url.indexOf('{'));
url += "?name=" + URLEncoder.encode(filename, "UTF-8");
return builder.contentType(contentType).with(stream).to(url, GHAsset.class).wrap(this);
}

Expand Down
12 changes: 4 additions & 8 deletions src/test/java/org/kohsuke/github/AbstractGitHubWireMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,21 @@ protected GHRepository getTempRepository(String name) throws IOException {
.description("A test repository for testing the github-api project: " + name)
.homepage("http://github-api.kohsuke.org/")
.autoInit(true)
.wiki(true)
.downloads(true)
.issues(true)
.private_(false)
.create();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}

configureTempRepository(repository);
}

return gitHub.getRepository(fullName);
}

protected void configureTempRepository(GHRepository repository) throws IOException {
repository.enableIssueTracker(true);
repository.enableDownloads(true);
repository.enableWiki(true);
}

@Before
@After
public void cleanupTempRepositories() throws IOException {
Expand Down
94 changes: 25 additions & 69 deletions src/test/java/org/kohsuke/github/LifecycleTest.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,42 @@
package org.kohsuke.github;

import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Properties;

public class LifecycleTest extends AbstractGitHubApiTestBase {
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.core.Is.is;

public class LifecycleTest extends AbstractGitHubWireMockTest {
@Test
public void testCreateRepository() throws IOException, GitAPIException, InterruptedException {
public void testCreateRepository() throws IOException {
GHMyself myself = gitHub.getMyself();
GHOrganization org = gitHub.getOrganization("github-api-test-org");
GHRepository repository = org.getRepository("github-api-test");
if (repository != null) {
repository.delete();
Thread.sleep(1000);
}
repository = org.createRepository("github-api-test",
"a test repository used to test kohsuke's github-api",
"http://github-api.kohsuke.org/",
"Core Developers",
true);
Thread.sleep(1000); // wait for the repository to become ready
// GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);

GHRepository repository = getTempRepository();
assertTrue(repository.getReleases().isEmpty());
try {
GHMilestone milestone = repository.createMilestone("Initial Release", "first one");
GHIssue issue = repository.createIssue("Test Issue")
.body("issue body just for grins")
.milestone(milestone)
.assignee(myself)
.label("bug")
.create();
File repoDir = new File(System.getProperty("java.io.tmpdir"), "github-api-test");
delete(repoDir);
Git origin = Git.cloneRepository()
.setBare(false)
.setURI(repository.getSshUrl())
.setDirectory(repoDir)
.setCredentialsProvider(getCredentialsProvider(myself))
.call();

commitTestFile(myself, repoDir, origin);

GHRelease release = createRelease(repository);

GHAsset asset = uploadAsset(release);

updateAsset(release, asset);

deleteAsset(release, asset);
} finally {
repository.delete();
}

GHMilestone milestone = repository.createMilestone("Initial Release", "first one");
GHIssue issue = repository.createIssue("Test Issue")
.body("issue body just for grins")
.milestone(milestone)
.assignee(myself)
.label("bug")
.create();

assertThat(issue, is(notNullValue()));

GHRelease release = createRelease(repository);

GHAsset asset = uploadAsset(release);

updateAsset(release, asset);

deleteAsset(release, asset);
}

private void updateAsset(GHRelease release, GHAsset asset) throws IOException {
Expand Down Expand Up @@ -96,25 +71,6 @@ private GHRelease createRelease(GHRepository repository) throws IOException {
return release;
}

private void commitTestFile(GHMyself myself, File repoDir, Git origin) throws IOException, GitAPIException {
File dummyFile = createDummyFile(repoDir);
DirCache cache = origin.add().addFilepattern(dummyFile.getName()).call();
origin.commit().setMessage("test commit").call();
origin.push().setCredentialsProvider(getCredentialsProvider(myself)).call();
}

private UsernamePasswordCredentialsProvider getCredentialsProvider(GHMyself myself) throws IOException {
Properties props = new Properties();
File homeDir = new File(System.getProperty("user.home"));
FileInputStream in = new FileInputStream(new File(homeDir, ".github"));
try {
props.load(in);
} finally {
IOUtils.closeQuietly(in);
}
return new UsernamePasswordCredentialsProvider(props.getProperty("login"), props.getProperty("oauth"));
}

private void delete(File toDelete) {
if (toDelete.isDirectory()) {
for (File file : toDelete.listFiles()) {
Expand Down
85 changes: 70 additions & 15 deletions src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public WireMockServer rawServer() {
return servers.get("raw");
}

public WireMockServer uploadsServer() {
return servers.get("uploads");
}

public boolean isUseProxy() {
return GitHubWireMockRule.useProxy;
}
Expand All @@ -69,39 +73,69 @@ public boolean isTakeSnapshot() {
@Override
protected void initializeServers() {
super.initializeServers();
initializeServer("raw");
initializeServer("default", new GitHubApiResponseTransformer(this));

// only start non-api servers if we might need them
if (new File(apiServer().getOptions().filesRoot().getPath() + "_raw").exists() || isUseProxy()) {
initializeServer("raw");
}
if (new File(apiServer().getOptions().filesRoot().getPath() + "_uploads").exists() || isUseProxy()) {
initializeServer("uploads");
}
}

@Override
protected void before() {
super.before();
if (isUseProxy()) {
this.apiServer().stubFor(proxyAllTo("https://api.github.com").atPriority(100));
if (!isUseProxy()) {
return;
}

this.apiServer().stubFor(proxyAllTo("https://api.github.com").atPriority(100));

if (this.rawServer() != null) {
this.rawServer().stubFor(proxyAllTo("https://raw.githubusercontent.com").atPriority(100));
}

if (this.uploadsServer() != null) {
this.uploadsServer().stubFor(proxyAllTo("https://uploads.github.com").atPriority(100));
}
}

@Override
protected void after() {
super.after();
if (isTakeSnapshot()) {
this.apiServer()
.snapshotRecord(recordSpec().forTarget("https://api.github.com")
.captureHeader("If-None-Match")
.extractTextBodiesOver(255));
if (!isTakeSnapshot()) {
return;
}

this.apiServer()
.snapshotRecord(recordSpec().forTarget("https://api.github.com")
.captureHeader("If-None-Match")
.extractTextBodiesOver(255));

// After taking the snapshot, format the output
formatJsonFiles(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath());

if (this.rawServer() != null) {
this.rawServer()
.snapshotRecord(recordSpec().forTarget("https://raw.githubusercontent.com")
.captureHeader("If-None-Match")
.extractTextBodiesOver(255));

// After taking the snapshot, format the output
formatJsonFiles(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath());

// For raw server, only fix up mapping files
formatJsonFiles(new File(this.rawServer().getOptions().filesRoot().child("mappings").getPath()).toPath());
}

if (this.uploadsServer() != null) {
this.uploadsServer()
.snapshotRecord(recordSpec().forTarget("https://uploads.github.com")
.captureHeader("If-None-Match")
.extractTextBodiesOver(255));

formatJsonFiles(new File(this.uploadsServer().getOptions().filesRoot().getPath()).toPath());

}
}

public int getRequestCount() {
Expand Down Expand Up @@ -137,8 +171,17 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
String fileText = new String(Files.readAllBytes(filePath));
// while recording responses we replaced all github calls localhost
// now we reverse that for storage.
fileText = fileText.replace(this.apiServer().baseUrl(), "https://api.github.com")
.replace(this.rawServer().baseUrl(), "https://raw.githubusercontent.com");
fileText = fileText.replace(this.apiServer().baseUrl(), "https://api.github.com");

if (this.rawServer() != null) {
fileText = fileText.replace(this.rawServer().baseUrl(),
"https://raw.githubusercontent.com");
}

if (this.uploadsServer() != null) {
fileText = fileText.replace(this.uploadsServer().baseUrl(), "https://uploads.github.com");
}

// Can be Array or Map
Object parsedObject = g.fromJson(fileText, Object.class);
if (parsedObject instanceof Map && filePath.toString().contains("mappings")) {
Expand Down Expand Up @@ -196,9 +239,21 @@ public Response transform(Request request, Response response, FileSource files,

String body;
body = getBodyAsString(response, headers);
body = body.replace("https://api.github.com", rule.apiServer().baseUrl());

if (rule.rawServer() != null) {
body = body.replace("https://raw.githubusercontent.com", rule.rawServer().baseUrl());
} else {
body = body.replace("https://raw.githubusercontent.com", rule.apiServer().baseUrl() + "/raw");
}

if (rule.uploadsServer() != null) {
body = body.replace("https://uploads.github.com", rule.uploadsServer().baseUrl());
} else {
body = body.replace("https://uploads.github.com", rule.apiServer().baseUrl() + "/uploads");
}

builder.body(body.replace("https://api.github.com", rule.apiServer().baseUrl())
.replace("https://raw.githubusercontent.com", rule.rawServer().baseUrl()));
builder.body(body);

}
builder.headers(new HttpHeaders(headers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"request": {
"url": "/app",
"method": "GET",
"headers" : {
"Accept" : {
"equalTo" : "application/vnd.github.machine-man-preview+json"
"headers": {
"Accept": {
"equalTo": "application/vnd.github.machine-man-preview+json"
}
}
},
Expand All @@ -17,7 +17,10 @@
"Content-Type": "application/json; charset=utf-8",
"Status": "200 OK",
"Cache-Control": "public, max-age=60, s-maxage=60",
"Vary": ["Accept","Accept-Encoding"],
"Vary": [
"Accept",
"Accept-Encoding"
],
"ETag": "W/\"01163b1a237898d328ed56cd0e9aefca\"",
"X-GitHub-Media-Type": "github.machine-man-preview; format=json",
"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",
Expand Down
Loading