Compare commits

...

9 Commits

Author SHA1 Message Date
Kohsuke Kawaguchi
c0dff74536 switched the Maven repository 2011-02-23 09:31:02 +09:00
Kohsuke Kawaguchi
7f82306908 [maven-release-plugin] prepare release github-api-1.5 2011-02-23 09:26:18 +09:00
Kohsuke Kawaguchi
7c599393bf added methods to parse timestamp 2011-02-23 06:58:14 +09:00
Kohsuke Kawaguchi
d1cd06aec4 incorporated the pagenation 2011-02-23 06:47:22 +09:00
Kohsuke Kawaguchi
13c9da9e91 missing the return statement 2010-12-16 15:31:29 -08:00
Kohsuke Kawaguchi
a827e51fa7 added the rename support. 2010-12-16 15:23:17 -08:00
Kohsuke Kawaguchi
637950c8be added a method to fork a repository into an organization. 2010-12-16 13:36:05 -08:00
Kohsuke Kawaguchi
fa40b625cc explicitly declare Wagon 2010-12-14 10:05:28 -08:00
Kohsuke Kawaguchi
15612e6bfd [maven-release-plugin] prepare for next development iteration 2010-12-14 09:54:23 -08:00
4 changed files with 105 additions and 11 deletions

17
pom.xml
View File

@@ -3,16 +3,16 @@
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<packaging>jar</packaging>
<version>1.4</version>
<version>1.5</version>
<name>GitHub API for Java</name>
<url>http://kohsuke.org/github-api/</url>
<description>GitHub API for Java</description>
<distributionManagement>
<repository>
<id>java.net-m2-repository</id>
<url>java-net:/maven2-repository/trunk/repository/</url>
</repository>
<distributionManagement>
<repository>
<id>maven.jenkins-ci.org</id>
<url>http://maven.jenkins-ci.org:8081/content/repositories/releases/</url>
</repository>
<site>
<id>kohsuke.org</id>
<url>scp://kohsuke.org/home/kohsuke/kohsuke.org/github-api/</url>
@@ -52,6 +52,11 @@
<artifactId>wagon-svn</artifactId>
<version>1.9</version>
</extension>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>1.0-beta-7</version>
</extension>
</extensions>
</build>

View File

@@ -1,13 +1,10 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import static org.kohsuke.github.GitHub.*;
/**
* Common part of {@link GHUser} and {@link GHOrganization}.
*
@@ -31,7 +28,11 @@ public abstract class GHPerson {
public synchronized Map<String,GHRepository> getRepositories() throws IOException {
if (repositories==null) {
repositories = Collections.synchronizedMap(new TreeMap<String, GHRepository>());
repositories.putAll(root.retrieve("/repos/show/" + login, JsonRepositories.class).wrap(root));
for (int i=1; ; i++) {
Map<String, GHRepository> map = root.retrieve("/repos/show/" + login + "?page=" + i, JsonRepositories.class).wrap(root);
repositories.putAll(map);
if (map.isEmpty()) break;
}
}
return Collections.unmodifiableMap(repositories);

View File

@@ -23,17 +23,22 @@
*/
package org.kohsuke.github;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -52,6 +57,7 @@ public class GHRepository {
private String description, homepage, url, name, owner;
private boolean has_issues, has_wiki, fork, _private, has_downloads;
private int watchers,forks;
private String created_at, pushed_at;
public String getDescription() {
return description;
@@ -108,6 +114,23 @@ public class GHRepository {
return watchers;
}
public Date getPushedAt() {
return parseDate(pushed_at);
}
public Date getCreatedAt() {
return parseDate(created_at);
}
private Date parseDate(String timestamp) {
try {
return new SimpleDateFormat(TIME_FORMAT).parse(timestamp);
} catch (ParseException e) {
throw new IllegalStateException("Unable to parse the timestamp: "+pushed_at);
}
}
/**
* Gets the collaborators on this repository.
* This set always appear to include the owner.
@@ -154,12 +177,62 @@ public class GHRepository {
}
/**
* Forks this repository.
* Forks this repository as your repository.
*
* @return
* Newly forked repository that belong to you.
*/
public GHRepository fork() throws IOException {
return new Poster(root).withCredential().to("/repos/fork/" + owner + "/" + name, JsonRepository.class).wrap(root);
}
/**
* Forks this repository into an organization.
*
* @return
* Newly forked repository that belong to you.
*/
public GHRepository forkTo(GHOrganization org) throws IOException {
WebClient wc = root.createWebClient();
HtmlPage pg = (HtmlPage)wc.getPage(getUrl());
for (HtmlForm f : pg.getForms()) {
if (!f.getActionAttribute().endsWith("/fork")) continue;
try {
if (org.getLogin().equals(f.getInputByName("organization").getValueAttribute())) {
// found it
f.submit((HtmlButton)f.getElementsByTagName("button").get(0));
return org.refreshRepository(name);
}
} catch (ElementNotFoundException e) {
// continue
}
}
throw new IllegalArgumentException("Either you don't have the privilege to fork into "+org.getLogin()+" or there's a bug in HTML scraping");
}
/**
* Rename this repository.
*/
public void renameTo(String newName) throws IOException {
WebClient wc = root.createWebClient();
HtmlPage pg = (HtmlPage)wc.getPage(getUrl()+"/admin");
for (HtmlForm f : pg.getForms()) {
if (!f.getActionAttribute().endsWith("/rename")) continue;
try {
f.getInputByName("name").setValueAttribute(newName);
f.submit((HtmlButton)f.getElementsByTagName("button").get(0));
name = newName;
return;
} catch (ElementNotFoundException e) {
// continue
}
}
throw new IllegalArgumentException("Either you don't have the privilege to rename "+owner+'/'+name+" or there's a bug in HTML scraping");
}
private void verifyMine() throws IOException {
if (!root.login.equals(owner))
throw new IOException("Operation not applicable to a repository owned by someone else: "+owner);
@@ -269,4 +342,6 @@ public class GHRepository {
public String toString() {
return "Repository:"+owner+":"+name;
}
private static final String TIME_FORMAT = "yyyy/MM/dd HH:mm:ss ZZZZ";
}

View File

@@ -22,6 +22,10 @@ public class AppTest extends TestCase {
public void testApp() throws IOException {
GitHub gitHub = GitHub.connect();
// tryRenaming(gitHub);
// tryOrgFork(gitHub);
// testOrganization(gitHub);
// testPostCommitHook(gitHub);
@@ -42,6 +46,15 @@ public class AppTest extends TestCase {
// System.out.println(hub.getUser("kohsuke").getRepository("hudson").getCollaborators());
}
private void tryRenaming(GitHub gitHub) throws IOException {
gitHub.getUser("kohsuke").getRepository("test").renameTo("test2");
}
private void tryOrgFork(GitHub gitHub) throws IOException {
GHOrganization o = gitHub.getOrganization("HudsonLabs");
System.out.println(gitHub.getUser("rtyler").getRepository("memcache-ada").forkTo(o).getUrl());
}
private void tryTeamCreation(GitHub gitHub) throws IOException {
GHOrganization o = gitHub.getOrganization("HudsonLabs");
GHTeam t = o.createTeam("auto team", Permission.PUSH);