|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.Date; |
| 9 | + |
| 10 | +/** |
| 11 | + * Builder for deleting repository content with support for specifying author and committer information. |
| 12 | + * |
| 13 | + * <p> |
| 14 | + * Obtain an instance via {@link GHContent#createDelete()}. |
| 15 | + * |
| 16 | + * @see GHContent#createDelete() |
| 17 | + */ |
| 18 | +public final class GHContentDeleter { |
| 19 | + @JsonInclude(Include.NON_NULL) |
| 20 | + private static final class UserInfo { |
| 21 | + private final String date; |
| 22 | + private final String email; |
| 23 | + private final String name; |
| 24 | + |
| 25 | + private UserInfo(String name, String email, Instant date) { |
| 26 | + this.name = name; |
| 27 | + this.email = email; |
| 28 | + this.date = date != null ? GitHubClient.printInstant(date) : null; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private final GHContent content; |
| 33 | + private final Requester req; |
| 34 | + |
| 35 | + GHContentDeleter(GHContent content) { |
| 36 | + this.content = content; |
| 37 | + final GHRepository repository = content.getOwner(); |
| 38 | + this.req = repository.root() |
| 39 | + .createRequest() |
| 40 | + .method("DELETE") |
| 41 | + .inBody() |
| 42 | + .with("path", content.getPath()) |
| 43 | + .with("sha", content.getSha()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Configures the author of the commit. If not specified, the authenticated user is used as the author. |
| 48 | + * |
| 49 | + * @param name |
| 50 | + * the name of the author |
| 51 | + * @param email |
| 52 | + * the email of the author |
| 53 | + * @return this deleter |
| 54 | + */ |
| 55 | + public GHContentDeleter author(String name, String email) { |
| 56 | + return author(name, email, (Instant) null); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Configures the author of the commit. If not specified, the authenticated user is used as the author. |
| 61 | + * |
| 62 | + * @param name |
| 63 | + * the name of the author |
| 64 | + * @param email |
| 65 | + * the email of the author |
| 66 | + * @param date |
| 67 | + * the date of the authoring |
| 68 | + * @return this deleter |
| 69 | + * @deprecated use {@link #author(String, String, Instant)} instead |
| 70 | + */ |
| 71 | + @Deprecated |
| 72 | + public GHContentDeleter author(String name, String email, Date date) { |
| 73 | + return author(name, email, GitHubClient.toInstantOrNull(date)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Configures the author of the commit. If not specified, the authenticated user is used as the author. |
| 78 | + * |
| 79 | + * @param name |
| 80 | + * the name of the author |
| 81 | + * @param email |
| 82 | + * the email of the author |
| 83 | + * @param date |
| 84 | + * the timestamp for the authoring |
| 85 | + * @return this deleter |
| 86 | + */ |
| 87 | + public GHContentDeleter author(String name, String email, Instant date) { |
| 88 | + req.with("author", new UserInfo(name, email, date)); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets the branch to delete the content from. |
| 94 | + * |
| 95 | + * @param branch |
| 96 | + * the branch name |
| 97 | + * @return this deleter |
| 98 | + */ |
| 99 | + public GHContentDeleter branch(String branch) { |
| 100 | + req.with("branch", branch); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Commits the deletion. |
| 106 | + * |
| 107 | + * @return the response containing the commit information |
| 108 | + * @throws IOException |
| 109 | + * the io exception |
| 110 | + */ |
| 111 | + public GHContentUpdateResponse commit() throws IOException { |
| 112 | + final GHRepository repository = content.getOwner(); |
| 113 | + GHContentUpdateResponse response = req.withUrlPath(GHContent.getApiRoute(repository, content.getPath())) |
| 114 | + .fetch(GHContentUpdateResponse.class); |
| 115 | + |
| 116 | + response.getCommit().wrapUp(repository); |
| 117 | + return response; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Configures the committer of the commit. If not specified, the authenticated user is used as the committer. |
| 122 | + * |
| 123 | + * @param name |
| 124 | + * the name of the committer |
| 125 | + * @param email |
| 126 | + * the email of the committer |
| 127 | + * @return this deleter |
| 128 | + */ |
| 129 | + public GHContentDeleter committer(String name, String email) { |
| 130 | + return committer(name, email, (Instant) null); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Configures the committer of the commit. If not specified, the authenticated user is used as the committer. |
| 135 | + * |
| 136 | + * @param name |
| 137 | + * the name of the committer |
| 138 | + * @param email |
| 139 | + * the email of the committer |
| 140 | + * @param date |
| 141 | + * the date of the commit |
| 142 | + * @return this deleter |
| 143 | + * @deprecated use {@link #committer(String, String, Instant)} instead |
| 144 | + */ |
| 145 | + @Deprecated |
| 146 | + public GHContentDeleter committer(String name, String email, Date date) { |
| 147 | + return committer(name, email, GitHubClient.toInstantOrNull(date)); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Configures the committer of the commit. If not specified, the authenticated user is used as the committer. |
| 152 | + * |
| 153 | + * @param name |
| 154 | + * the name of the committer |
| 155 | + * @param email |
| 156 | + * the email of the committer |
| 157 | + * @param date |
| 158 | + * the timestamp of the commit |
| 159 | + * @return this deleter |
| 160 | + */ |
| 161 | + public GHContentDeleter committer(String name, String email, Instant date) { |
| 162 | + req.with("committer", new UserInfo(name, email, date)); |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Sets the commit message. |
| 168 | + * |
| 169 | + * @param message |
| 170 | + * the commit message |
| 171 | + * @return this deleter |
| 172 | + */ |
| 173 | + public GHContentDeleter message(String message) { |
| 174 | + req.with("message", message); |
| 175 | + return this; |
| 176 | + } |
| 177 | +} |
0 commit comments