Skip to content

Commit

Permalink
Expanded GHProject with columns and cards. Also added tests for proje…
Browse files Browse the repository at this point in the history
…cts, columns and cards
  • Loading branch information
gskjold authored and bitwiseman committed Oct 4, 2019
1 parent b20c2ba commit fc08711
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 35 deletions.
62 changes: 46 additions & 16 deletions src/main/java/org/kohsuke/github/GHProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
*/
package org.kohsuke.github;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;

import static org.kohsuke.github.Previews.INERTIA;

/**
Expand All @@ -33,12 +36,11 @@
* @author Martin van Zijl
*/
public class GHProject extends GHObject {
private GitHub root;
private GHRepository owner;
protected GitHub root;
protected GHObject owner;

private String owner_url;
private String html_url;
private String columns_url;
private String node_id;
private String name;
private String body;
Expand All @@ -55,20 +57,25 @@ public GitHub getRoot() {
return root;
}

public GHRepository getOwner() {
public GHObject getOwner() throws IOException {
if(owner == null) {
try {
if(owner_url.contains("/orgs/")) {
owner = root.retrieve().to(getOwnerUrl().getPath(), GHOrganization.class).wrapUp(root);
} else if(owner_url.contains("/users/")) {
owner = root.retrieve().to(getOwnerUrl().getPath(), GHUser.class).wrapUp(root);
} else if(owner_url.contains("/repos/")) {
owner = root.retrieve().to(getOwnerUrl().getPath(), GHRepository.class).wrap(root);
}
} catch (FileNotFoundException e) {
return null;
}
}
return owner;
}

public String getOwner_url() {
return owner_url;
}

public String getHtml_url() {
return html_url;
}

public String getColumns_url() {
return columns_url;
public URL getOwnerUrl() {
return GitHub.parseURL(owner_url);
}

public String getNode_id() {
Expand All @@ -87,8 +94,8 @@ public int getNumber() {
return number;
}

public String getState() {
return state;
public ProjectState getState() {
return Enum.valueOf(ProjectState.class, state.toUpperCase(Locale.ENGLISH));
}

public GHUser getCreator() {
Expand Down Expand Up @@ -156,4 +163,27 @@ public void setPublic(boolean isPublic) throws IOException {
public void delete() throws IOException {
new Requester(root).withPreview(INERTIA).method("DELETE").to(getApiRoute());
}

public PagedIterable<GHProjectColumn> listColumns() throws IOException {
final GHProject project = this;
return new PagedIterable<GHProjectColumn>() {
public PagedIterator<GHProjectColumn> _iterator(int pageSize) {
return new PagedIterator<GHProjectColumn>(root.retrieve().withPreview(INERTIA)
.asIterator(String.format("/projects/%d/columns", id), GHProjectColumn[].class, pageSize)) {
@Override
protected void wrapUp(GHProjectColumn[] page) {
for (GHProjectColumn c : page)
c.wrap(project);
}
};
}
};
}

public GHProjectColumn createColumn(String name) throws IOException {
return root.retrieve().method("POST")
.withPreview(INERTIA)
.with("name", name)
.to(String.format("/projects/%d/columns", id), GHProjectColumn.class).wrap(this);
}
}
123 changes: 123 additions & 0 deletions src/main/java/org/kohsuke/github/GHProjectCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.kohsuke.github;

import org.apache.commons.lang3.StringUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import static org.kohsuke.github.Previews.INERTIA;

/**
* @author Gunnar Skjold
*/
public class GHProjectCard extends GHObject {
private GitHub root;
private GHProject project;
private GHProjectColumn column;

private String note;
private GHUser creator;
private String content_url, project_url, column_url;
private boolean archived;

public URL getHtmlUrl() throws IOException {
return null;
}

public GHProjectCard wrap(GitHub root) {
this.root = root;
return this;
}

public GHProjectCard wrap(GHProjectColumn column) {
this.column = column;
this.project = column.project;
this.root = column.root;
return this;
}

public GitHub getRoot() {
return root;
}

public GHProject getProject() throws IOException {
if(project == null) {
try {
project = root.retrieve().to(getProjectUrl().getPath(), GHProject.class).wrap(root);
} catch (FileNotFoundException e) {
return null;
}
}
return project;
}

public GHProjectColumn getColumn() throws IOException {
if(column == null) {
try {
column = root.retrieve().to(getColumnUrl().getPath(), GHProjectColumn.class).wrap(root);
} catch (FileNotFoundException e) {
return null;
}
}
return column;
}

public GHIssue getContent() throws IOException {
if(StringUtils.isEmpty(content_url))
return null;
try {
if(content_url.contains("/pulls")) {
return root.retrieve().to(getContentUrl().getPath(), GHPullRequest.class).wrap(root);
} else {
return root.retrieve().to(getContentUrl().getPath(), GHIssue.class).wrap(root);
}
} catch (FileNotFoundException e) {
return null;
}
}

public String getNote() {
return note;
}

public GHUser getCreator() {
return creator;
}

public URL getContentUrl() {
return GitHub.parseURL(content_url);
}

public URL getProjectUrl() {
return GitHub.parseURL(project_url);
}

public URL getColumnUrl() {
return GitHub.parseURL(column_url);
}

public boolean isArchived() {
return archived;
}

public void setNote(String note) throws IOException {
edit("note", note);
}

public void setArchived(boolean archived) throws IOException {
edit("archived", archived);
}

private void edit(String key, Object value) throws IOException {
new Requester(root).withPreview(INERTIA)._with(key, value).method("PATCH").to(getApiRoute());
}

protected String getApiRoute() {
return String.format("/projects/columns/cards/%d", id);
}

public void delete() throws IOException {
new Requester(root).withPreview(INERTIA).method("DELETE").to(getApiRoute());
}
}
104 changes: 104 additions & 0 deletions src/main/java/org/kohsuke/github/GHProjectColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.kohsuke.github;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import static org.kohsuke.github.Previews.INERTIA;

/**
* @author Gunnar Skjold
*/
public class GHProjectColumn extends GHObject {
protected GitHub root;
protected GHProject project;

private String name;
private String project_url;

@Override
public URL getHtmlUrl() throws IOException {
return null;
}

public GHProjectColumn wrap(GitHub root) {
this.root = root;
return this;
}

public GHProjectColumn wrap(GHProject project) {
this.project = project;
this.root = project.root;
return this;
}

public GitHub getRoot() {
return root;
}

public GHProject getProject() throws IOException {
if(project == null) {
try {
project = root.retrieve().to(getProjectUrl().getPath(), GHProject.class).wrap(root);
} catch (FileNotFoundException e) {
return null;
}
}
return project;
}

public String getName() {
return name;
}

public URL getProjectUrl() {
return GitHub.parseURL(project_url);
}

public void setName(String name) throws IOException {
edit("name", name);
}

private void edit(String key, Object value) throws IOException {
new Requester(root).withPreview(INERTIA)._with(key, value).method("PATCH").to(getApiRoute());
}

protected String getApiRoute() {
return String.format("/projects/columns/%d", id);
}

public void delete() throws IOException {
new Requester(root).withPreview(INERTIA).method("DELETE").to(getApiRoute());
}

public PagedIterable<GHProjectCard> listCards() throws IOException {
final GHProjectColumn column = this;
return new PagedIterable<GHProjectCard>() {
public PagedIterator<GHProjectCard> _iterator(int pageSize) {
return new PagedIterator<GHProjectCard>(root.retrieve().withPreview(INERTIA)
.asIterator(String.format("/projects/columns/%d/cards", id), GHProjectCard[].class, pageSize)) {
@Override
protected void wrapUp(GHProjectCard[] page) {
for (GHProjectCard c : page)
c.wrap(column);
}
};
}
};
}

public GHProjectCard createCard(String note) throws IOException {
return root.retrieve().method("POST")
.withPreview(INERTIA)
.with("note", note)
.to(String.format("/projects/columns/%d/cards", id), GHProjectCard.class).wrap(this);
}

public GHProjectCard createCard(GHIssue issue) throws IOException {
return root.retrieve().method("POST")
.withPreview(INERTIA)
.with("content_type", issue instanceof GHPullRequest ? "PullRequest" : "Issue")
.with("content_id", issue.getId())
.to(String.format("/projects/columns/%d/cards", id), GHProjectCard.class).wrap(this);
}
}
36 changes: 17 additions & 19 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,23 @@

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;

import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*;
import static java.net.HttpURLConnection.*;
import static java.util.logging.Level.*;
import static org.kohsuke.github.Previews.*;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.logging.Level.FINE;
import static org.kohsuke.github.Previews.DRAX;
import static org.kohsuke.github.Previews.INERTIA;

/**
* Root of the GitHub API.
Expand Down Expand Up @@ -746,7 +736,15 @@ public boolean isCredentialValid() {
}

public GHProject getProject(long id) throws IOException {
return retrieve().withPreview(INERTIA).to("/projects/"+id,GHProject.class).wrap(this);
return retrieve().withPreview(INERTIA).to("/projects/"+id, GHProject.class).wrap(this);
}

public GHProjectColumn getProjectColumn(long id) throws IOException {
return retrieve().withPreview(INERTIA).to("/projects/columns/"+id, GHProjectColumn.class).wrap(this);
}

public GHProjectCard getProjectCard(long id) throws IOException {
return retrieve().withPreview(INERTIA).to("/projects/columns/cards/"+id, GHProjectCard.class).wrap(this);
}

private static class GHApiInfo {
Expand Down
Loading

0 comments on commit fc08711

Please sign in to comment.