Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/org/kohsuke/github/GHContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ static String getApiRoute(GHRepository repository, String path) {
public GHContent() {
}

/**
* Creates a builder that can be used to delete this file.
*
* <p>
* Unlike the {@link #delete(String)} convenience methods, this builder supports setting the author and committer of
* the resulting commit.
*
* @return a content deleter
* @see GHContentDeleter
*/
public GHContentDeleter createDelete() {
return new GHContentDeleter(this);
}

/**
* Creates a builder that can be used to update this file's content.
*
* <p>
* Unlike the {@link #update(String, String)} convenience methods, this builder supports setting the author and
* committer of the resulting commit.
*
* @return a content updater
* @see GHContentUpdater
*/
public GHContentUpdater createUpdate() {
return new GHContentUpdater(this);
}

/**
* Delete gh content update response.
*
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/org/kohsuke/github/GHContentBuilder.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Base64;
import java.util.Date;

// TODO: Auto-generated Javadoc
/**
Expand All @@ -15,6 +20,19 @@
* @see GHRepository#createContent() GHRepository#createContent()
*/
public final class GHContentBuilder {
@JsonInclude(Include.NON_NULL)
private static final class UserInfo {
private final String date;
private final String email;
private final String name;

private UserInfo(String name, String email, Instant date) {
this.name = name;
this.email = email;
this.date = date != null ? GitHubClient.printInstant(date) : null;
}
}

private String path;
private final GHRepository repo;
private final Requester req;
Expand All @@ -30,6 +48,52 @@ public final class GHContentBuilder {
this.req = repo.root().createRequest().method("PUT");
}

/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @return the gh content builder
*/
public GHContentBuilder author(String name, String email) {
return author(name, email, (Instant) null);
}

/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @param date
* the date of the authoring
* @return the gh content builder
* @deprecated use {@link #author(String, String, Instant)} instead
*/
@Deprecated
public GHContentBuilder author(String name, String email, Date date) {
return author(name, email, GitHubClient.toInstantOrNull(date));
}

/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @param date
* the timestamp for the authoring
* @return the gh content builder
*/
public GHContentBuilder author(String name, String email, Instant date) {
req.with("author", new UserInfo(name, email, date));
return this;
}

/**
* Branch gh content builder.
*
Expand Down Expand Up @@ -59,6 +123,52 @@ public GHContentUpdateResponse commit() throws IOException {
return response;
}

/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @return the gh content builder
*/
public GHContentBuilder committer(String name, String email) {
return committer(name, email, (Instant) null);
}

/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @param date
* the date of the commit
* @return the gh content builder
* @deprecated use {@link #committer(String, String, Instant)} instead
*/
@Deprecated
public GHContentBuilder committer(String name, String email, Date date) {
return committer(name, email, GitHubClient.toInstantOrNull(date));
}

/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @param date
* the timestamp of the commit
* @return the gh content builder
*/
public GHContentBuilder committer(String name, String email, Instant date) {
req.with("committer", new UserInfo(name, email, date));
return this;
}

/**
* Content gh content builder.
*
Expand Down
177 changes: 177 additions & 0 deletions src/main/java/org/kohsuke/github/GHContentDeleter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

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

/**
* Builder for deleting repository content with support for specifying author and committer information.
*
* <p>
* Obtain an instance via {@link GHContent#createDelete()}.
*
* @see GHContent#createDelete()
*/
public final class GHContentDeleter {
@JsonInclude(Include.NON_NULL)
private static final class UserInfo {
private final String date;
private final String email;
private final String name;

private UserInfo(String name, String email, Instant date) {
this.name = name;
this.email = email;
this.date = date != null ? GitHubClient.printInstant(date) : null;
}
}

private final GHContent content;
private final Requester req;

GHContentDeleter(GHContent content) {
this.content = content;
final GHRepository repository = content.getOwner();
this.req = repository.root()
.createRequest()
.method("DELETE")
.inBody()
.with("path", content.getPath())
.with("sha", content.getSha());
}

/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @return this deleter
*/
public GHContentDeleter author(String name, String email) {
return author(name, email, (Instant) null);
}

/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @param date
* the date of the authoring
* @return this deleter
* @deprecated use {@link #author(String, String, Instant)} instead
*/
@Deprecated
public GHContentDeleter author(String name, String email, Date date) {
return author(name, email, GitHubClient.toInstantOrNull(date));
}

/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @param date
* the timestamp for the authoring
* @return this deleter
*/
public GHContentDeleter author(String name, String email, Instant date) {
req.with("author", new UserInfo(name, email, date));
return this;
}

/**
* Sets the branch to delete the content from.
*
* @param branch
* the branch name
* @return this deleter
*/
public GHContentDeleter branch(String branch) {
req.with("branch", branch);
return this;
}

/**
* Commits the deletion.
*
* @return the response containing the commit information
* @throws IOException
* the io exception
*/
public GHContentUpdateResponse commit() throws IOException {
final GHRepository repository = content.getOwner();
GHContentUpdateResponse response = req.withUrlPath(GHContent.getApiRoute(repository, content.getPath()))
.fetch(GHContentUpdateResponse.class);

response.getCommit().wrapUp(repository);
return response;
}

/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @return this deleter
*/
public GHContentDeleter committer(String name, String email) {
return committer(name, email, (Instant) null);
}

/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @param date
* the date of the commit
* @return this deleter
* @deprecated use {@link #committer(String, String, Instant)} instead
*/
@Deprecated
public GHContentDeleter committer(String name, String email, Date date) {
return committer(name, email, GitHubClient.toInstantOrNull(date));
}

/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @param date
* the timestamp of the commit
* @return this deleter
*/
public GHContentDeleter committer(String name, String email, Instant date) {
req.with("committer", new UserInfo(name, email, date));
return this;
}

/**
* Sets the commit message.
*
* @param message
* the commit message
* @return this deleter
*/
public GHContentDeleter message(String message) {
req.with("message", message);
return this;
}
}
Loading
Loading