-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for projects #545
Support for projects #545
Changes from all commits
b20c2ba
fc08711
5cc88a0
3972d11
20cfb26
41c5164
552edf8
dc2830d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2018 Martin van Zijl. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
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; | ||
|
||
/** | ||
* A GitHub project. | ||
* @see https://developer.github.com/v3/projects/ | ||
* @author Martin van Zijl | ||
*/ | ||
public class GHProject extends GHObject { | ||
protected GitHub root; | ||
protected GHObject owner; | ||
|
||
private String owner_url; | ||
private String html_url; | ||
private String node_id; | ||
private String name; | ||
private String body; | ||
private int number; | ||
private String state; | ||
private GHUser creator; | ||
|
||
@Override | ||
public URL getHtmlUrl() throws IOException { | ||
return GitHub.parseURL(html_url); | ||
} | ||
|
||
public GitHub getRoot() { | ||
return root; | ||
} | ||
|
||
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 URL getOwnerUrl() { | ||
return GitHub.parseURL(owner_url); | ||
} | ||
|
||
public String getNode_id() { | ||
return node_id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getBody() { | ||
return body; | ||
} | ||
|
||
public int getNumber() { | ||
return number; | ||
} | ||
|
||
public ProjectState getState() { | ||
return Enum.valueOf(ProjectState.class, state.toUpperCase(Locale.ENGLISH)); | ||
} | ||
|
||
public GHUser getCreator() { | ||
return creator; | ||
} | ||
|
||
public GHProject wrap(GHRepository repo) { | ||
this.owner = repo; | ||
this.root = repo.root; | ||
return this; | ||
} | ||
|
||
public GHProject wrap(GitHub root) { | ||
this.root = root; | ||
return this; | ||
} | ||
|
||
private void edit(String key, Object value) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of the builder/updater style instead? https://github.com/github-api/github-api/blob/master/src/main/java/org/kohsuke/github/GHRelease.java It would reduce round trips but would add more code. I don't have a strong opinion. The project doesn't seem to have one particular style it uses, I'd like to move toward a more consistent style. I'm wondering what you think. (Maybe @kohsuke can comment as well if he's around.) |
||
new Requester(root).withPreview(INERTIA)._with(key, value).method("PATCH").to(getApiRoute()); | ||
} | ||
|
||
protected String getApiRoute() { | ||
return "/projects/" + id; | ||
} | ||
|
||
public void setName(String name) throws IOException { | ||
edit("name", name); | ||
} | ||
|
||
public void setBody(String body) throws IOException { | ||
edit("body", body); | ||
} | ||
|
||
public enum ProjectState { | ||
OPEN, | ||
CLOSED | ||
} | ||
|
||
public void setState(ProjectState state) throws IOException { | ||
edit("state", state.toString().toLowerCase()); | ||
} | ||
|
||
public static enum ProjectStateFilter { | ||
ALL, | ||
OPEN, | ||
CLOSED | ||
} | ||
|
||
/** | ||
* Set the permission level that all members of the project's organization will have on this project. | ||
* Only applicable for organization-owned projects. | ||
*/ | ||
public void setOrganizationPermission(GHPermissionType permission) throws IOException { | ||
edit("organization_permission", permission.toString().toLowerCase()); | ||
} | ||
|
||
/** | ||
* Sets visibility of the project within the organization. | ||
* Only applicable for organization-owned projects. | ||
*/ | ||
public void setPublic(boolean isPublic) throws IOException { | ||
edit("public", isPublic); | ||
} | ||
|
||
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); | ||
} | ||
} |
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()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.