From 975ef1a43ded992c1ad521c24cc43708701806a0 Mon Sep 17 00:00:00 2001 From: Johno Crawford Date: Sun, 6 Jan 2013 04:06:55 +0100 Subject: [PATCH 1/2] Removed last traces of web client. --- .gitignore | 1 + pom.xml | 13 +++---------- .../org/kohsuke/github/GHOrganization.java | 15 +++++---------- .../java/org/kohsuke/github/GHRepository.java | 19 ++++--------------- src/main/java/org/kohsuke/github/GitHub.java | 17 +---------------- .../java/org/kohsuke/github/Requester.java | 4 ++++ 6 files changed, 18 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index 097367121c..cb9b4d6a4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target +.idea/ *.iml *.ipr *.iws diff --git a/pom.xml b/pom.xml index 76204fa53c..331e0eb207 100644 --- a/pom.xml +++ b/pom.xml @@ -44,16 +44,9 @@ - org.jvnet.hudson - htmlunit - 2.6-hudson-2 - - - - xml-apis - xml-apis - - + commons-lang + commons-lang + 2.6 junit diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index 188f26d930..b91037b16f 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -1,9 +1,5 @@ package org.kohsuke.github; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.html.HtmlAnchor; -import com.gargoylesoftware.htmlunit.html.HtmlPage; - import java.io.IOException; import java.util.AbstractList; import java.util.ArrayList; @@ -112,13 +108,12 @@ public GHTeam createTeam(String name, Permission p, GHRepository... repositories * List up repositories that has some open pull requests. */ public List getRepositoriesWithOpenPullRequests() throws IOException { - WebClient wc = root.createWebClient(); - HtmlPage pg = (HtmlPage)wc.getPage("https://github.com/organizations/"+login+"/dashboard/pulls"); List r = new ArrayList(); - for (HtmlAnchor e : pg.getElementById("js-issue-list").selectNodes(".//UL[@class='smallnav']/LI[not(@class='zeroed')]/A")) { - String a = e.getHrefAttribute(); - String name = a.substring(a.lastIndexOf('/')+1); - r.add(getRepository(name)); + for (GHRepository repository : root.retrieve().to("/orgs/" + login + "/repos", GHRepository[].class)) { + List pullRequests = repository.getPullRequests(GHIssueState.OPEN); + if (pullRequests.size() > 0) { + r.add(repository); + } } return r; } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 0260bb888a..703ff2378f 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -23,12 +23,6 @@ */ package org.kohsuke.github; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.html.HtmlButton; -import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; -import com.gargoylesoftware.htmlunit.html.HtmlForm; -import com.gargoylesoftware.htmlunit.html.HtmlInput; -import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.infradna.tool.bridge_method_injector.WithBridgeMethods; import java.io.IOException; @@ -266,15 +260,10 @@ private void modifyCollaborators(Collection users, String method) throws } public void setEmailServiceHook(String address) throws IOException { - WebClient wc = root.createWebClient(); - HtmlPage pg = (HtmlPage)wc.getPage(getUrl()+"/admin"); - HtmlInput email = (HtmlInput)pg.getElementById("email_address"); - email.setValueAttribute(address); - HtmlCheckBoxInput active = (HtmlCheckBoxInput)pg.getElementById("email[active]"); - active.setChecked(true); - - final HtmlForm f = email.getEnclosingFormOrDie(); - f.submit((HtmlButton) f.getElementsByTagName("button").get(0)); + Map config = new HashMap(); + config.put("address", address); + new Requester(root).method("POST").with("name", "email").with("config", config).with("active", "true") + .to(String.format("/repos/%s/%s/hooks", owner.login, name)); } private void edit(String key, String value) throws IOException { diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 4253d17e86..ea4f1ba67d 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -23,9 +23,6 @@ */ package org.kohsuke.github; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.html.HtmlForm; -import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.infradna.tool.bridge_method_injector.WithBridgeMethods; import org.apache.commons.io.IOUtils; import org.codehaus.jackson.map.DeserializationConfig.Feature; @@ -273,7 +270,7 @@ public Map getMyOrganizations() throws IOException { * Public events visible to you. Equivalent of what's displayed on https://github.com/ */ public List getEvents() throws IOException { - // TODO: pagenation + // TODO: pagination GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class); for (GHEventInfo e : events) e.wrapUp(this); @@ -318,18 +315,6 @@ public boolean isCredentialValid() throws IOException { } } - WebClient createWebClient() throws IOException { - WebClient wc = new WebClient(); - wc.setJavaScriptEnabled(false); - wc.setCssEnabled(false); - HtmlPage pg = (HtmlPage)wc.getPage("https://github.com/login"); - HtmlForm f = pg.getForms().get(0); - f.getInputByName("login").setValueAttribute(login); - f.getInputByName("password").setValueAttribute(password); - f.submit(); - return wc; - } - /*package*/ static URL parseURL(String s) { try { return s==null ? null : new URL(s); diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index c6bf92639c..feaa25e23a 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -107,6 +107,10 @@ public Requester with(String key, Collection value) { return _with(key, value); } + public Requester with(String key, Map value) { + return _with(key, value); + } + public Requester _with(String key, Object value) { if (value!=null) { args.add(new Entry(key,value)); From f0ab946b885c951d03881e8be35cc0b9875b2ca7 Mon Sep 17 00:00:00 2001 From: johnou Date: Sun, 6 Jan 2013 04:27:18 +0100 Subject: [PATCH 2/2] Removed proprietary api usage. --- pom.xml | 5 +++++ src/main/java/org/kohsuke/github/GitHub.java | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 331e0eb207..b9125ef229 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,11 @@ commons-lang 2.6 + + commons-codec + commons-codec + 1.7 + junit junit diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index ea4f1ba67d..a7b2ea8172 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -24,11 +24,11 @@ package org.kohsuke.github; import com.infradna.tool.bridge_method_injector.WithBridgeMethods; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.introspect.VisibilityChecker.Std; -import sun.misc.BASE64Encoder; import java.io.File; import java.io.FileInputStream; @@ -83,10 +83,9 @@ private GitHub(String apiUrl, String login, String apiToken, String password) { this.apiToken = apiToken; this.password = password; - BASE64Encoder enc = new sun.misc.BASE64Encoder(); if (apiToken!=null || password!=null) { String userpassword = password==null ? (login + "/token" + ":" + apiToken) : (login + ':'+password); - encodedAuthorization = enc.encode(userpassword.getBytes()); + encodedAuthorization = Base64.encodeBase64String(userpassword.getBytes()); } else encodedAuthorization = null; }