Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf1b0ed

Browse files
committed
Merge pull request hub4j#26 from johnou/remove-webclient
Removed web client and proprietary api usage.
2 parents 7064865 + f0ab946 commit bf1b0ed

File tree

6 files changed

+25
-54
lines changed

6 files changed

+25
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target
2+
.idea/
23
*.iml
34
*.ipr
45
*.iws

pom.xml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,14 @@
4444

4545
<dependencies>
4646
<dependency>
47-
<groupId>org.jvnet.hudson</groupId>
48-
<artifactId>htmlunit</artifactId>
49-
<version>2.6-hudson-2</version>
50-
<exclusions>
51-
<exclusion>
52-
<!-- hides JDK DOM classes in Eclipse -->
53-
<groupId>xml-apis</groupId>
54-
<artifactId>xml-apis</artifactId>
55-
</exclusion>
56-
</exclusions>
47+
<groupId>commons-lang</groupId>
48+
<artifactId>commons-lang</artifactId>
49+
<version>2.6</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>commons-codec</groupId>
53+
<artifactId>commons-codec</artifactId>
54+
<version>1.7</version>
5755
</dependency>
5856
<dependency>
5957
<groupId>junit</groupId>

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package org.kohsuke.github;
22

3-
import com.gargoylesoftware.htmlunit.WebClient;
4-
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
5-
import com.gargoylesoftware.htmlunit.html.HtmlPage;
6-
73
import java.io.IOException;
84
import java.util.AbstractList;
95
import java.util.ArrayList;
@@ -112,13 +108,12 @@ public GHTeam createTeam(String name, Permission p, GHRepository... repositories
112108
* List up repositories that has some open pull requests.
113109
*/
114110
public List<GHRepository> getRepositoriesWithOpenPullRequests() throws IOException {
115-
WebClient wc = root.createWebClient();
116-
HtmlPage pg = (HtmlPage)wc.getPage("https://github.com/organizations/"+login+"/dashboard/pulls");
117111
List<GHRepository> r = new ArrayList<GHRepository>();
118-
for (HtmlAnchor e : pg.getElementById("js-issue-list").<HtmlAnchor>selectNodes(".//UL[@class='smallnav']/LI[not(@class='zeroed')]/A")) {
119-
String a = e.getHrefAttribute();
120-
String name = a.substring(a.lastIndexOf('/')+1);
121-
r.add(getRepository(name));
112+
for (GHRepository repository : root.retrieve().to("/orgs/" + login + "/repos", GHRepository[].class)) {
113+
List<GHPullRequest> pullRequests = repository.getPullRequests(GHIssueState.OPEN);
114+
if (pullRequests.size() > 0) {
115+
r.add(repository);
116+
}
122117
}
123118
return r;
124119
}

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
*/
2424
package org.kohsuke.github;
2525

26-
import com.gargoylesoftware.htmlunit.WebClient;
27-
import com.gargoylesoftware.htmlunit.html.HtmlButton;
28-
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
29-
import com.gargoylesoftware.htmlunit.html.HtmlForm;
30-
import com.gargoylesoftware.htmlunit.html.HtmlInput;
31-
import com.gargoylesoftware.htmlunit.html.HtmlPage;
3226
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
3327

3428
import java.io.IOException;
@@ -266,15 +260,10 @@ private void modifyCollaborators(Collection<GHUser> users, String method) throws
266260
}
267261

268262
public void setEmailServiceHook(String address) throws IOException {
269-
WebClient wc = root.createWebClient();
270-
HtmlPage pg = (HtmlPage)wc.getPage(getUrl()+"/admin");
271-
HtmlInput email = (HtmlInput)pg.getElementById("email_address");
272-
email.setValueAttribute(address);
273-
HtmlCheckBoxInput active = (HtmlCheckBoxInput)pg.getElementById("email[active]");
274-
active.setChecked(true);
275-
276-
final HtmlForm f = email.getEnclosingFormOrDie();
277-
f.submit((HtmlButton) f.getElementsByTagName("button").get(0));
263+
Map<String, String> config = new HashMap<String, String>();
264+
config.put("address", address);
265+
new Requester(root).method("POST").with("name", "email").with("config", config).with("active", "true")
266+
.to(String.format("/repos/%s/%s/hooks", owner.login, name));
278267
}
279268

280269
private void edit(String key, String value) throws IOException {

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
*/
2424
package org.kohsuke.github;
2525

26-
import com.gargoylesoftware.htmlunit.WebClient;
27-
import com.gargoylesoftware.htmlunit.html.HtmlForm;
28-
import com.gargoylesoftware.htmlunit.html.HtmlPage;
2926
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
27+
import org.apache.commons.codec.binary.Base64;
3028
import org.apache.commons.io.IOUtils;
3129
import org.codehaus.jackson.map.DeserializationConfig.Feature;
3230
import org.codehaus.jackson.map.ObjectMapper;
3331
import org.codehaus.jackson.map.introspect.VisibilityChecker.Std;
34-
import sun.misc.BASE64Encoder;
3532

3633
import java.io.File;
3734
import java.io.FileInputStream;
@@ -86,10 +83,9 @@ private GitHub(String apiUrl, String login, String apiToken, String password) {
8683
this.apiToken = apiToken;
8784
this.password = password;
8885

89-
BASE64Encoder enc = new sun.misc.BASE64Encoder();
9086
if (apiToken!=null || password!=null) {
9187
String userpassword = password==null ? (login + "/token" + ":" + apiToken) : (login + ':'+password);
92-
encodedAuthorization = enc.encode(userpassword.getBytes());
88+
encodedAuthorization = Base64.encodeBase64String(userpassword.getBytes());
9389
} else
9490
encodedAuthorization = null;
9591
}
@@ -273,7 +269,7 @@ public Map<String, GHOrganization> getMyOrganizations() throws IOException {
273269
* Public events visible to you. Equivalent of what's displayed on https://github.com/
274270
*/
275271
public List<GHEventInfo> getEvents() throws IOException {
276-
// TODO: pagenation
272+
// TODO: pagination
277273
GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class);
278274
for (GHEventInfo e : events)
279275
e.wrapUp(this);
@@ -318,18 +314,6 @@ public boolean isCredentialValid() throws IOException {
318314
}
319315
}
320316

321-
WebClient createWebClient() throws IOException {
322-
WebClient wc = new WebClient();
323-
wc.setJavaScriptEnabled(false);
324-
wc.setCssEnabled(false);
325-
HtmlPage pg = (HtmlPage)wc.getPage("https://github.com/login");
326-
HtmlForm f = pg.getForms().get(0);
327-
f.getInputByName("login").setValueAttribute(login);
328-
f.getInputByName("password").setValueAttribute(password);
329-
f.submit();
330-
return wc;
331-
}
332-
333317
/*package*/ static URL parseURL(String s) {
334318
try {
335319
return s==null ? null : new URL(s);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public Requester with(String key, Collection<String> value) {
107107
return _with(key, value);
108108
}
109109

110+
public Requester with(String key, Map<String, String> value) {
111+
return _with(key, value);
112+
}
113+
110114
public Requester _with(String key, Object value) {
111115
if (value!=null) {
112116
args.add(new Entry(key,value));

0 commit comments

Comments
 (0)