Skip to content

Commit

Permalink
add support (most of) the release-related endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Nov 5, 2013
1 parent d82af9f commit 178c9ff
Show file tree
Hide file tree
Showing 7 changed files with 557 additions and 8 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>3.1.0.201310021548-r</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/org/kohsuke/github/GHAsset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.Date;

public class GHAsset {
GitHub root;
GHRepository owner;
private String url;
private String id;
private String name;
private String label;
private String state;
private String content_type;
private long size;
private long download_count;
private Date created_at;
private Date updated_at;

public String getContentType() {
return content_type;
}

public void setContentType(String contentType) throws IOException {
edit("content_type", contentType);
this.content_type = contentType;
}

public Date getCreatedAt() {
return created_at;
}

public long getDownloadCount() {
return download_count;
}

public String getId() {
return id;
}

public String getLabel() {
return label;
}

public void setLabel(String label) throws IOException {
edit("label", label);
this.label = label;
}

public String getName() {
return name;
}

public GHRepository getOwner() {
return owner;
}

public GitHub getRoot() {
return root;
}

public long getSize() {
return size;
}

public String getState() {
return state;
}

public Date getUpdatedAt() {
return updated_at;
}

public String getUrl() {
return url;
}

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

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


private String getApiRoute() {
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/releases/assets/" + id;
}

GHAsset wrap(GHRelease release) {
this.owner = release.getOwner();
this.root = owner.root;
return this;
}

public static GHAsset[] wrap(GHAsset[] assets, GHRelease release) {
for (GHAsset aTo : assets) {
aTo.wrap(release);
}
return assets;
}
}
187 changes: 187 additions & 0 deletions src/main/java/org/kohsuke/github/GHRelease.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package org.kohsuke.github;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import static java.lang.String.format;

public class GHRelease {
GitHub root;
GHRepository owner;

private String url;
private String html_url;
private String assets_url;
private String upload_url;
private long id;
private String tag_name;
private String target_commitish;
private String name;
private String body;
private boolean draft;
private boolean prerelease;
private Date created_at;
private Date published_at;

public String getAssetsUrl() {
return assets_url;
}

public void setAssetsUrl(String assets_url) {
this.assets_url = assets_url;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public Date getCreatedAt() {
return created_at;
}

public void setCreatedAt(Date created_at) {
this.created_at = created_at;
}

public boolean isDraft() {
return draft;
}

public void setDraft(boolean draft) {
this.draft = draft;
}

public String getHtmlUrl() {
return html_url;
}

public void setHtmlUrl(String html_url) {
this.html_url = html_url;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public GHRepository getOwner() {
return owner;
}

public void setOwner(GHRepository owner) {
this.owner = owner;
}

public boolean isPrerelease() {
return prerelease;
}

public void setPrerelease(boolean prerelease) {
this.prerelease = prerelease;
}

public Date getPublished_at() {
return published_at;
}

public void setPublished_at(Date published_at) {
this.published_at = published_at;
}

public GitHub getRoot() {
return root;
}

public void setRoot(GitHub root) {
this.root = root;
}

public String getTagName() {
return tag_name;
}

public void setTagName(String tag_name) {
this.tag_name = tag_name;
}

public String getTargetCommitish() {
return target_commitish;
}

public void setTargetCommitish(String target_commitish) {
this.target_commitish = target_commitish;
}

public String getUploadUrl() {
return upload_url;
}

public void setUploadUrl(String upload_url) {
this.upload_url = upload_url;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

GHRelease wrap(GHRepository owner) {
this.owner = owner;
this.root = owner.root;
return this;
}

static GHRelease[] wrap(GHRelease[] releases, GHRepository owner) {
for (GHRelease release : releases) {
release.wrap(owner);
}
return releases;
}

/**
* Because github relies on SNI (http://en.wikipedia.org/wiki/Server_Name_Indication) this method will only work on
* Java 7 or greater. Options for fixing this for earlier JVMs can be found here
* http://stackoverflow.com/questions/12361090/server-name-indication-sni-on-java but involve more complicated
* handling of the HTTP requests to github's API.
*
* @throws IOException
*/
public GHAsset uploadAsset(File file, String contentType) throws IOException {
Requester builder = new Requester(owner.root);

String url = format("https://uploads.github.com%sreleases/%d/assets?name=%s",
owner.getApiTailUrl(""), getId(), file.getName());
return builder.contentType(contentType)
.with(new FileInputStream(file))
.to(url, GHAsset.class).wrap(this);
}

public GHAsset[] getAssets() throws IOException {
Requester builder = new Requester(owner.root);

GHAsset[] assets = (GHAsset[]) builder
.method("GET")
.to(owner.getApiTailUrl(format("releases/%d/assets", id)), GHAsset[].class);
return GHAsset.wrap(assets, this);
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/kohsuke/github/GHReleaseBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.kohsuke.github;

import java.io.IOException;

public class GHReleaseBuilder {
private final GHRepository repo;
private final Requester builder;

public GHReleaseBuilder(GHRepository ghRepository, String tag) {
this.repo = ghRepository;
this.builder = new Requester(repo.root);
builder.with("tag_name", tag);
}

/**
* @param body The release notes body.
*/
public GHReleaseBuilder body(String body) {
if (body != null) {
builder.with("body", body);
}
return this;
}

/**
* Specifies the commitish value that determines where the Git tag is created from. Can be any branch or
* commit SHA.
*
* @param commitish Defaults to the repository’s default branch (usually "master"). Unused if the Git tag
* already exists.
* @return
*/
public GHReleaseBuilder commitish(String commitish) {
if (commitish != null) {
builder.with("target_commitish", commitish);
}
return this;
}

/**
* Optional.
*
* @param draft {@code true} to create a draft (unpublished) release, {@code false} to create a published one.
* Default is {@code false}.
*/
public GHReleaseBuilder draft(boolean draft) {
builder.with("draft", draft);
return this;
}

/**
* @param name the name of the release
*/
public GHReleaseBuilder name(String name) {
if (name != null) {
builder.with("name", name);
}
return this;
}

/**
* Optional
*
* @param prerelease {@code true} to identify the release as a prerelease. {@code false} to identify the release
* as a full release. Default is {@code false}.
*/
public GHReleaseBuilder prerelease(boolean prerelease) {
builder.with("prerelease", prerelease);
return this;
}

public GHRelease create() throws IOException {
return builder.to(repo.getApiTailUrl("releases"), GHRelease.class).wrap(repo);
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ public List<GHIssue> getIssues(GHIssueState state) throws IOException {
return Arrays.asList(GHIssue.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this));
}

public GHReleaseBuilder createRelease(String tag) {
return new GHReleaseBuilder(this,tag);
}

public List<GHRelease> getReleases() throws IOException {
return Arrays.asList(GHRelease.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/releases",
GHRelease[].class), this));
}

protected String getOwnerName() {
return owner.login;
}
Expand Down
Loading

0 comments on commit 178c9ff

Please sign in to comment.