Skip to content

Commit 403b22a

Browse files
committed
Working CI Build
This change automatically turns off tests where we haven't had a chance to implement wiremocking. They can still be run locally by setting test.github.useProxy (even though most of them do actually use the proxy).
1 parent 909a274 commit 403b22a

9 files changed

Lines changed: 81 additions & 58 deletions

File tree

.github/workflows/maven-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
- name: Maven Download all dependencies
1818
run: mvn -B org.apache.maven.plugins:maven-dependency-plugin:3.1.1:go-offline
1919
- name: Maven Build
20-
run: mvn -B package --file pom.xml -Dtest=CommitTest,GistTest,PullRequestTest,UserTest,WireMockStatusReporterTest
20+
run: mvn -B package --file pom.xml

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public GitHubBuilder() {
4242
*
4343
* If there is still no user it means there are no credentials defined and throw an IOException.
4444
*
45-
* @return the configured Builder from credentials defined on the system or in the environment.
45+
* @return the configured Builder from credentials defined on the system or in the environment. Otherwise returns null.
4646
*
4747
* @throws IOException If there are no credentials defined in the ~/.github properties file or the process environment.
4848
*/
49-
public static GitHubBuilder fromCredentials() throws IOException {
49+
static GitHubBuilder fromCredentials() throws IOException {
5050
Exception cause = null;
51-
GitHubBuilder builder;
51+
GitHubBuilder builder = null;
5252

5353
try {
5454
builder = fromPropertyFile();

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,16 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313

14+
import static org.junit.Assume.assumeTrue;
15+
1416
/**
1517
* @author Kohsuke Kawaguchi
1618
*/
17-
public abstract class AbstractGitHubApiTestBase extends Assert {
18-
19-
protected GitHub gitHub;
20-
19+
public abstract class AbstractGitHubApiTestBase extends AbstractGitHubApiWireMockTest {
2120

2221
@Before
2322
public void setUp() throws Exception {
24-
File f = new File(System.getProperty("user.home"), ".github.kohsuke2");
25-
if (f.exists()) {
26-
Properties props = new Properties();
27-
FileInputStream in = null;
28-
try {
29-
in = new FileInputStream(f);
30-
props.load(in);
31-
} finally {
32-
IOUtils.closeQuietly(in);
33-
}
34-
// use the non-standard credential preferentially, so that developers of this library do not have
35-
// to clutter their event stream.
36-
gitHub = GitHubBuilder.fromProperties(props).withRateLimitHandler(RateLimitHandler.FAIL).build();
37-
} else {
38-
gitHub = GitHubBuilder.fromCredentials().withRateLimitHandler(RateLimitHandler.FAIL).build();
39-
}
23+
assumeTrue( "All tests inheriting from this class are not guaranteed to work without proxy", useProxy);
4024
}
4125

4226
protected GHUser getUser() {
@@ -49,7 +33,7 @@ protected GHUser getUser() {
4933

5034
protected void kohsuke() {
5135
String login = getUser().getLogin();
52-
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
36+
assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
5337
}
5438

5539
protected static final RandomNameGenerator rnd = new RandomNameGenerator();

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
77
import com.github.tomakehurst.wiremock.http.Request;
88
import com.github.tomakehurst.wiremock.http.Response;
9+
import org.apache.commons.io.IOUtils;
910
import org.junit.After;
1011
import org.junit.Assert;
1112
import org.junit.Before;
1213
import org.junit.Rule;
1314
import org.kohsuke.github.junit.WireMockRule;
1415

16+
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.io.IOException;
1519
import java.util.Properties;
1620

1721
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@@ -21,12 +25,13 @@
2125
*/
2226
public abstract class AbstractGitHubApiWireMockTest extends Assert {
2327

24-
// By default the wiremock tests will, run without proxy or taking a snapshot.
28+
// By default the wiremock tests will run without proxy or taking a snapshot.
2529
// The tests will use only the stubbed data and will fail if requests are made for missing data.
2630
// You can use the proxy without taking a snapshot while writing and debugging tests.
2731
// You cannot take a snapshot without proxying.
2832
protected final static boolean takeSnapshot = System.getProperty("test.github.takeSnapshot", "false") != "false";
2933
protected final static boolean useProxy = takeSnapshot || System.getProperty("test.github.useProxy", "false") != "false";
34+
private final GitHubBuilder githubBuilder = createGitHubBuilder();
3035

3136
public final static String STUBBED_USER_LOGIN = "placeholder-user";
3237
public final static String STUBBED_USER_PASSWORD = "placeholder-password";
@@ -67,33 +72,63 @@ public String getName() {
6772
})
6873
);
6974

70-
@Before
71-
public void wireMockSetup() throws Exception {
75+
private static GitHubBuilder createGitHubBuilder() {
7276

73-
GitHubBuilder builder = GitHubBuilder.fromEnvironment()
74-
.withEndpoint("http://localhost:" + githubApi.port())
75-
.withRateLimitHandler(RateLimitHandler.FAIL);
77+
GitHubBuilder builder = new GitHubBuilder();
78+
79+
try {
80+
File f = new File(System.getProperty("user.home"), ".github.kohsuke2");
81+
if (f.exists()) {
82+
Properties props = new Properties();
83+
FileInputStream in = null;
84+
try {
85+
in = new FileInputStream(f);
86+
props.load(in);
87+
} finally {
88+
IOUtils.closeQuietly(in);
89+
}
90+
// use the non-standard credential preferentially, so that developers of this library do not have
91+
// to clutter their event stream.
92+
builder = GitHubBuilder.fromProperties(props);
93+
} else {
94+
builder = GitHubBuilder.fromCredentials();
95+
}
96+
} catch (IOException e) {
97+
}
98+
99+
if (!useProxy) {
100+
// This sets the user and password to a placeholder for wiremock testing
101+
// This makes the tests believe they are running with permissions
102+
// The recorded stubs will behave like they running with permissions
103+
builder.withPassword(STUBBED_USER_LOGIN, STUBBED_USER_PASSWORD);
104+
}
76105

106+
return builder.withRateLimitHandler(RateLimitHandler.FAIL);
107+
}
108+
109+
protected GitHubBuilder getGitHubBuilder() {
110+
return githubBuilder;
111+
}
77112

113+
@Before
114+
public void wireMockSetup() throws Exception {
78115
if(useProxy) {
79116
githubApi.stubFor(
80117
proxyAllTo("https://api.github.com/")
81118
.atPriority(100)
82119
);
83120
} else {
84-
// This sets the user and password to a placeholder for wiremock testing
85-
// This makes the tests believe they are running with permissions
86-
// The recorded stubs will behave like they running with permissions
87-
builder.withPassword(STUBBED_USER_LOGIN, STUBBED_USER_PASSWORD);
88-
89121
// Just to be super clear
90122
githubApi.stubFor(
91123
any(urlPathMatching(".*"))
92124
.willReturn(status(500).withBody("Stubbed data not found. Set test.github.use-proxy to have WireMock proxy to github"))
93125
.atPriority(100));
94126
}
95127

96-
gitHub = builder.build();
128+
129+
gitHub = getGitHubBuilder()
130+
.withEndpoint("http://localhost:" + githubApi.port())
131+
.build();
97132
}
98133

99134
@After

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@
3535
/**
3636
* @author Duncan Dickinson
3737
*/
38-
public class GHLicenseTest extends Assert {
39-
private GitHub gitHub;
40-
41-
@Before
42-
public void setUp() throws Exception {
43-
gitHub = new GitHubBuilder()
44-
.fromCredentials()
45-
.build();
46-
}
38+
public class GHLicenseTest extends AbstractGitHubApiTestBase {
4739

4840
/**
4941
* Basic test to ensure that the list of licenses from {@link GitHub#listLicenses()} is returned

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public class GHOrganizationTest extends AbstractGitHubApiTestBase {
1414
@Override
1515
public void setUp() throws Exception {
1616
super.setUp();
17-
org = gitHub.getOrganization("github-api-test-org");
17+
if (useProxy) {
18+
org = gitHub.getOrganization("github-api-test-org");
19+
}
1820
}
1921

2022
@Test
@@ -37,7 +39,9 @@ public void testCreateRepositoryWithAutoInitialization() throws IOException {
3739

3840
@After
3941
public void cleanUp() throws Exception {
40-
GHRepository repository = org.getRepository(GITHUB_API_TEST);
41-
repository.delete();
42+
if (useProxy) {
43+
GHRepository repository = org.getRepository(GITHUB_API_TEST);
44+
repository.delete();
45+
}
4246
}
4347
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
/**
2525
* Unit test for {@link GitHub}.
2626
*/
27-
public class GitHubTest {
27+
public class GitHubTest extends AbstractGitHubApiTestBase {
28+
2829
@Test
2930
public void testOffline() throws Exception {
3031
GitHub hub = GitHub.offline();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,11 @@ public void getUser() throws IOException {
222222

223223
@After
224224
public void cleanUp() throws Exception {
225-
for (GHPullRequest pr : getRepository().getPullRequests(GHIssueState.OPEN)) {
226-
pr.close();
225+
// Cleanup is only needed when proxying
226+
if (useProxy) {
227+
for (GHPullRequest pr : getRepository().getPullRequests(GHIssueState.OPEN)) {
228+
pr.close();
229+
}
227230
}
228231
}
229232

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717
import java.util.List;
1818
import java.util.TimeZone;
1919

20-
public class RepositoryTrafficTest {
20+
public class RepositoryTrafficTest extends AbstractGitHubApiTestBase {
2121
final private String login = "kohsuke", repositoryName = "github-api";
2222

23+
@Override
24+
protected GitHubBuilder getGitHubBuilder() {
25+
return new GitHubBuilder()
26+
.withPassword(login, null);
27+
}
28+
2329
@SuppressWarnings("unchecked")
2430
private <T extends GHRepositoryTraffic> void checkResponse(T expected, T actual){
2531
Assert.assertEquals(expected.getCount(), actual.getCount());
@@ -49,8 +55,6 @@ private <T extends GHRepositoryTraffic> void testTraffic(T expectedResult) throw
4955
ObjectMapper mapper = new ObjectMapper().setDateFormat(dateFormat);
5056
String mockedResponse = mapper.writeValueAsString(expectedResult);
5157

52-
53-
GitHub gitHub = GitHub.connect(login, null);
5458
GitHub gitHubSpy = Mockito.spy(gitHub);
5559
GHRepository repo = gitHubSpy.getUser(login).getRepository(repositoryName);
5660

@@ -71,8 +75,8 @@ private <T extends GHRepositoryTraffic> void testTraffic(T expectedResult) throw
7175

7276

7377
// this covers calls on "uc" in Requester.setupConnection and Requester.buildRequest
74-
URL trafficURL = new URL(
75-
"https://api.github.com/repos/"+login+"/"+repositoryName+"/traffic/" +
78+
URL trafficURL = gitHub.getApiURL(
79+
"/repos/"+login+"/"+repositoryName+"/traffic/" +
7680
((expectedResult instanceof GHRepositoryViewTraffic) ? "views" : "clones")
7781
);
7882
Mockito.doReturn(mockHttpURLConnection).when(connectorSpy).connect(Mockito.eq(trafficURL));
@@ -149,7 +153,7 @@ public void testGetClones() throws IOException{
149153
@Test
150154
public void testGetTrafficStatsAccessFailureDueToInsufficientPermissions() throws IOException {
151155
String errorMsg = "Exception should be thrown, since we don't have permission to access repo traffic info.";
152-
GitHub gitHub = GitHub.connect(login, null);
156+
153157
GHRepository repo = gitHub.getUser(login).getRepository(repositoryName);
154158
try {
155159
repo.getViewTraffic();

0 commit comments

Comments
 (0)