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 27, 2020
1 parent 44dcbe7 commit 16e0099
Show file tree
Hide file tree
Showing 18 changed files with 1,052 additions and 108 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
50 changes: 38 additions & 12 deletions src/main/java/org/kohsuke/github/GHGistUpdater.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.kohsuke.github;

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 All @@ -12,7 +15,7 @@
public class GHGistUpdater {
private final GHGist base;
private final Requester builder;
LinkedHashMap<String, Object> files;
LinkedHashMap<String, Map<String, String>> files;

GHGistUpdater(GHGist base) {
this.base = base;
Expand All @@ -32,16 +35,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,8 +56,9 @@ public GHGistUpdater addFile(String fileName, String content) throws IOException
* @throws IOException
* the io exception
*/
public GHGistUpdater renameFile(String fileName, String newFileName) throws IOException {
files.put(fileName, Collections.singletonMap("filename", newFileName));
public GHGistUpdater renameFile(@Nonnull String fileName, @Nonnull String newFileName) throws IOException {
Map<String, String> file = files.computeIfAbsent(fileName, d -> new HashMap<>());
file.put("filename", newFileName);
return this;
}

Expand All @@ -70,8 +73,31 @@ public GHGistUpdater renameFile(String fileName, String newFileName) throws IOEx
* @throws IOException
* the io exception
*/
public GHGistUpdater updateFile(String fileName, String content) throws IOException {
files.put(fileName, Collections.singletonMap("content", content));
public GHGistUpdater updateFile(@Nonnull String fileName, @Nonnull String content) throws IOException {
Map<String, String> file = files.computeIfAbsent(fileName, d -> new HashMap<>());
file.put("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 = files.computeIfAbsent(fileName, d -> new HashMap<>());
file.put("content", content);
file.put("filename", newFileName);
files.put(fileName, file);
return this;
}

Expand Down
68 changes: 67 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,66 @@ 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"));

// rename and update on the same file in one update shoudl work.
gistUpdate = gistUpdate.update().renameFile("ab.txt", "a.txt").updateFile("ab.txt", "abcd").update();

assertThat(gistUpdate.getGistId(), equalTo(gist.getGistId()));
assertThat(gistUpdate.getFiles().size(), equalTo(3));

// verify rename and update
assertThat(gistUpdate.getFile("a.txt").getContent(), equalTo("abcd"));

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 +127,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/11a257b91982aafd6370089ef877a682",
"forks_url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/forks",
"commits_url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/commits",
"id": "11a257b91982aafd6370089ef877a682",
"node_id": "MDQ6R2lzdDExYTI1N2I5MTk4MmFhZmQ2MzcwMDg5ZWY4NzdhNjgy",
"git_pull_url": "https://gist.github.com/11a257b91982aafd6370089ef877a682.git",
"git_push_url": "https://gist.github.com/11a257b91982aafd6370089ef877a682.git",
"html_url": "https://gist.github.com/11a257b91982aafd6370089ef877a682",
"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/11a257b91982aafd6370089ef877a682/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/11a257b91982aafd6370089ef877a682/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/11a257b91982aafd6370089ef877a682/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-27T17:24:06Z",
"updated_at": "2020-04-27T17:24:06Z",
"description": "Test Gist",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/9faa55cd67b21a789bb44e34e5e41f72/comments",
"comments_url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/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": "580ab68864a16653f98107dad574acb502f35deb",
"committed_at": "2020-04-27T17:24:05Z",
"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/11a257b91982aafd6370089ef877a682/580ab68864a16653f98107dad574acb502f35deb"
}
],
"truncated": false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682",
"forks_url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/forks",
"commits_url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/commits",
"id": "11a257b91982aafd6370089ef877a682",
"node_id": "MDQ6R2lzdDExYTI1N2I5MTk4MmFhZmQ2MzcwMDg5ZWY4NzdhNjgy",
"git_pull_url": "https://gist.github.com/11a257b91982aafd6370089ef877a682.git",
"git_push_url": "https://gist.github.com/11a257b91982aafd6370089ef877a682.git",
"html_url": "https://gist.github.com/11a257b91982aafd6370089ef877a682",
"files": {
"abc.txt": {
"filename": "abc.txt",
"type": "text/plain",
"language": "Text",
"raw_url": "https://gist.githubusercontent.com/bitwiseman/11a257b91982aafd6370089ef877a682/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/11a257b91982aafd6370089ef877a682/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/11a257b91982aafd6370089ef877a682/raw/45a82e295c52473114c57c18f4f52368df2a3cfd/ghi.txt",
"size": 3,
"truncated": false,
"content": "ghi"
}
},
"public": false,
"created_at": "2020-04-27T17:24:06Z",
"updated_at": "2020-04-27T17:24:06Z",
"description": "Test Gist",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/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": "580ab68864a16653f98107dad574acb502f35deb",
"committed_at": "2020-04-27T17:24:05Z",
"change_status": {
"total": 3,
"additions": 3,
"deletions": 0
},
"url": "https://api.github.com/gists/11a257b91982aafd6370089ef877a682/580ab68864a16653f98107dad574acb502f35deb"
}
],
"truncated": false
}
Loading

0 comments on commit 16e0099

Please sign in to comment.