Skip to content

Commit

Permalink
Add deleteFile() to GHGist
Browse files Browse the repository at this point in the history
Related to hub4j#466 and hub4j#484
  • Loading branch information
bitwiseman committed Apr 26, 2020
1 parent 44dcbe7 commit 4b71a74
Show file tree
Hide file tree
Showing 16 changed files with 796 additions and 104 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/kohsuke/github/GHGistBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Collections;
import java.util.LinkedHashMap;

import javax.annotation.Nonnull;

/**
* Builder pattern for creating a new Gist.
*
Expand Down Expand Up @@ -59,7 +61,7 @@ public GHGistBuilder public_(boolean v) {
* the content
* @return Adds a new file.
*/
public GHGistBuilder file(String fileName, String content) {
public GHGistBuilder file(@Nonnull String fileName, @Nonnull String content) {
files.put(fileName, Collections.singletonMap("content", content));
return this;
}
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/org/kohsuke/github/GHGistUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Nonnull;

/**
* Builder pattern for updating a Gist.
Expand Down Expand Up @@ -32,16 +36,15 @@ public class GHGistUpdater {
* @throws IOException
* the io exception
*/
public GHGistUpdater addFile(String fileName, String content) throws IOException {
public GHGistUpdater addFile(@Nonnull String fileName, @Nonnull String content) throws IOException {
updateFile(fileName, content);
return this;
}

// // This method does not work.
// public GHGistUpdater deleteFile(String fileName) throws IOException {
// files.put(fileName, Collections.singletonMap("filename", null));
// return this;
// }
public GHGistUpdater deleteFile(@Nonnull String fileName) throws IOException {
files.put(fileName, null);
return this;
}

/**
* Rename file gh gist updater.
Expand All @@ -54,7 +57,7 @@ public GHGistUpdater addFile(String fileName, String content) throws IOException
* @throws IOException
* the io exception
*/
public GHGistUpdater renameFile(String fileName, String newFileName) throws IOException {
public GHGistUpdater renameFile(@Nonnull String fileName, @Nonnull String newFileName) throws IOException {
files.put(fileName, Collections.singletonMap("filename", newFileName));
return this;
}
Expand All @@ -70,11 +73,33 @@ public GHGistUpdater renameFile(String fileName, String newFileName) throws IOEx
* @throws IOException
* the io exception
*/
public GHGistUpdater updateFile(String fileName, String content) throws IOException {
public GHGistUpdater updateFile(@Nonnull String fileName, @Nonnull String content) throws IOException {
files.put(fileName, Collections.singletonMap("content", content));
return this;
}

/**
* Update file name and content
*
* @param fileName
* the file name
* @param newFileName
* the new file name
* @param content
* the content
* @return the gh gist updater
* @throws IOException
* the io exception
*/
public GHGistUpdater updateFile(@Nonnull String fileName, @Nonnull String newFileName, @Nonnull String content)
throws IOException {
Map<String, String> file = new HashMap<>();
file.put("content", content);
file.put("filename", newFileName);
files.put(fileName, file);
return this;
}

/**
* Description gh gist updater.
*
Expand Down
59 changes: 58 additions & 1 deletion src/test/java/org/kohsuke/github/GHGistTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.junit.Test;

import static org.hamcrest.Matchers.notNullValue;
import java.io.FileNotFoundException;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;

/**
Expand All @@ -17,9 +19,12 @@ public void lifecycleTest() throws Exception {
.description("Test Gist")
.file("abc.txt", "abc")
.file("def.txt", "def")
.file("ghi.txt", "ghi")
.create();

assertThat(gist.getCreatedAt(), is(notNullValue()));
assertThat(gist.getDescription(), equalTo("Test Gist"));
assertThat(gist.getFiles().size(), equalTo(3));

assertNotNull(gist.getUpdatedAt());
assertNotNull(gist.getCommentsUrl());
Expand All @@ -28,7 +33,57 @@ public void lifecycleTest() throws Exception {
assertNotNull(gist.getGitPushUrl());
assertNotNull(gist.getHtmlUrl());

String id = gist.getGistId();

GHGist gistUpdate = gitHub.getGist(id);
assertThat(gistUpdate.getGistId(), equalTo(gist.getGistId()));
assertThat(gistUpdate.getDescription(), equalTo(gist.getDescription()));
assertThat(gistUpdate.getFiles().size(), equalTo(3));

gistUpdate = gistUpdate.update().description("Gist Test").addFile("jkl.txt", "jkl").update();

assertThat(gistUpdate.getGistId(), equalTo(gist.getGistId()));
assertThat(gistUpdate.getDescription(), equalTo("Gist Test"));
assertThat(gistUpdate.getFiles().size(), equalTo(4));

gistUpdate = gistUpdate.update()
.renameFile("abc.txt", "ab.txt")
.deleteFile("def.txt")
.updateFile("ghi.txt", "gh")
.updateFile("jkl.txt", "klm.txt", "nop")
.update();

assertThat(gistUpdate.getGistId(), equalTo(gist.getGistId()));
assertThat(gistUpdate.getDescription(), equalTo("Gist Test"));
assertThat(gistUpdate.getFiles().size(), equalTo(3));

// verify delete works
assertThat(gistUpdate.getFile("def.txt"), nullValue());

// verify rename
assertThat(gistUpdate.getFile("ab.txt").getContent(), equalTo("abc"));

// verify updates
assertThat(gistUpdate.getFile("ghi.txt").getContent(), equalTo("gh"));
assertThat(gistUpdate.getFile("klm.txt").getContent(), equalTo("nop"));

try {
gist.getId();
fail("Newly created gists do not have numeric ids.");
} catch (NumberFormatException e) {
assertThat(e, notNullValue());
}

assertThat(gist.getGistId(), notNullValue());

gist.delete();

try {
gitHub.getGist(id);
fail("Gist should be deleted.");
} catch (FileNotFoundException e) {
assertThat(e, notNullValue());
}
}

@Test
Expand Down Expand Up @@ -63,6 +118,8 @@ public void gistFile() throws Exception {
GHGist gist = gitHub.getGist("9903708");

assertTrue(gist.isPublic());
assertThat(gist.getId(), equalTo(9903708L));
assertThat(gist.getGistId(), equalTo("9903708"));

assertEquals(1, gist.getFiles().size());
GHGistFile f = gist.getFile("keybase.md");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"url": "https://api.github.com/gists/9faa55cd67b21a789bb44e34e5e41f72",
"forks_url": "https://api.github.com/gists/9faa55cd67b21a789bb44e34e5e41f72/forks",
"commits_url": "https://api.github.com/gists/9faa55cd67b21a789bb44e34e5e41f72/commits",
"id": "9faa55cd67b21a789bb44e34e5e41f72",
"node_id": "MDQ6R2lzdDlmYWE1NWNkNjdiMjFhNzg5YmI0NGUzNGU1ZTQxZjcy",
"git_pull_url": "https://gist.github.com/9faa55cd67b21a789bb44e34e5e41f72.git",
"git_push_url": "https://gist.github.com/9faa55cd67b21a789bb44e34e5e41f72.git",
"html_url": "https://gist.github.com/9faa55cd67b21a789bb44e34e5e41f72",
"url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd",
"forks_url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/forks",
"commits_url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/commits",
"id": "934d422d84e3fd76e1aeea159b0dbedd",
"node_id": "MDQ6R2lzdDkzNGQ0MjJkODRlM2ZkNzZlMWFlZWExNTliMGRiZWRk",
"git_pull_url": "https://gist.github.com/934d422d84e3fd76e1aeea159b0dbedd.git",
"git_push_url": "https://gist.github.com/934d422d84e3fd76e1aeea159b0dbedd.git",
"html_url": "https://gist.github.com/934d422d84e3fd76e1aeea159b0dbedd",
"files": {
"abc.txt": {
"filename": "abc.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/9faa55cd67b21a789bb44e34e5e41f72/raw/f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f/abc.txt",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/934d422d84e3fd76e1aeea159b0dbedd/raw/f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f/abc.txt",
"size": 3,
"truncated": false,
"content": "abc"
Expand All @@ -21,19 +21,28 @@
"filename": "def.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/9faa55cd67b21a789bb44e34e5e41f72/raw/0c003832e7bfa9ca8b5c2035c9bd684a5f2623bc/def.txt",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/934d422d84e3fd76e1aeea159b0dbedd/raw/0c003832e7bfa9ca8b5c2035c9bd684a5f2623bc/def.txt",
"size": 3,
"truncated": false,
"content": "def"
},
"ghi.txt": {
"filename": "ghi.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/934d422d84e3fd76e1aeea159b0dbedd/raw/45a82e295c52473114c57c18f4f52368df2a3cfd/ghi.txt",
"size": 3,
"truncated": false,
"content": "ghi"
}
},
"public": false,
"created_at": "2019-09-08T06:47:42Z",
"updated_at": "2019-09-08T06:47:42Z",
"created_at": "2020-04-22T22:20:35Z",
"updated_at": "2020-04-22T22:20:35Z",
"description": "Test Gist",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/9faa55cd67b21a789bb44e34e5e41f72/comments",
"comments_url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/comments",
"owner": {
"login": "bitwiseman",
"id": 1958953,
Expand Down Expand Up @@ -77,14 +86,14 @@
"type": "User",
"site_admin": false
},
"version": "b25886c9892025e090e71bdf1bb9b20927cb4a19",
"committed_at": "2019-09-08T06:47:42Z",
"version": "b63367d179640c0c89b3933fb5c9e473934c572e",
"committed_at": "2020-04-22T22:20:34Z",
"change_status": {
"total": 2,
"additions": 2,
"total": 3,
"additions": 3,
"deletions": 0
},
"url": "https://api.github.com/gists/9faa55cd67b21a789bb44e34e5e41f72/b25886c9892025e090e71bdf1bb9b20927cb4a19"
"url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/b63367d179640c0c89b3933fb5c9e473934c572e"
}
],
"truncated": false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd",
"forks_url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/forks",
"commits_url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/commits",
"id": "934d422d84e3fd76e1aeea159b0dbedd",
"node_id": "MDQ6R2lzdDkzNGQ0MjJkODRlM2ZkNzZlMWFlZWExNTliMGRiZWRk",
"git_pull_url": "https://gist.github.com/934d422d84e3fd76e1aeea159b0dbedd.git",
"git_push_url": "https://gist.github.com/934d422d84e3fd76e1aeea159b0dbedd.git",
"html_url": "https://gist.github.com/934d422d84e3fd76e1aeea159b0dbedd",
"files": {
"abc.txt": {
"filename": "abc.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/934d422d84e3fd76e1aeea159b0dbedd/raw/f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f/abc.txt",
"size": 3,
"truncated": false,
"content": "abc"
},
"def.txt": {
"filename": "def.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/934d422d84e3fd76e1aeea159b0dbedd/raw/0c003832e7bfa9ca8b5c2035c9bd684a5f2623bc/def.txt",
"size": 3,
"truncated": false,
"content": "def"
},
"ghi.txt": {
"filename": "ghi.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/934d422d84e3fd76e1aeea159b0dbedd/raw/45a82e295c52473114c57c18f4f52368df2a3cfd/ghi.txt",
"size": 3,
"truncated": false,
"content": "ghi"
}
},
"public": false,
"created_at": "2020-04-22T22:20:35Z",
"updated_at": "2020-04-22T22:20:35Z",
"description": "Test Gist",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/comments",
"owner": {
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false
},
"forks": [],
"history": [
{
"user": {
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false
},
"version": "b63367d179640c0c89b3933fb5c9e473934c572e",
"committed_at": "2020-04-22T22:20:34Z",
"change_status": {
"total": 3,
"additions": 3,
"deletions": 0
},
"url": "https://api.github.com/gists/934d422d84e3fd76e1aeea159b0dbedd/b63367d179640c0c89b3933fb5c9e473934c572e"
}
],
"truncated": false
}
Loading

0 comments on commit 4b71a74

Please sign in to comment.