From 567d3dac5572ae87768939b1c8d60753f6fd3126 Mon Sep 17 00:00:00 2001 From: Martin van Zijl Date: Tue, 13 Nov 2018 07:51:45 +1300 Subject: [PATCH 1/3] Add support for editing Gists. Fixes issue #466 NOTE: I could not get the deleteFile() method to work. --- src/main/java/org/kohsuke/github/GHGist.java | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHGist.java b/src/main/java/org/kohsuke/github/GHGist.java index c4c91737d6..10c1522176 100644 --- a/src/main/java/org/kohsuke/github/GHGist.java +++ b/src/main/java/org/kohsuke/github/GHGist.java @@ -6,6 +6,7 @@ import java.net.URL; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -159,6 +160,55 @@ public void delete() throws IOException { new Requester(root).method("DELETE").to("/gists/" + id); } + private String getApiRoute() { + return "/gists/" + id; + } + + private void edit(String key, Object value) throws IOException { + new Requester(root)._with(key, value).method("PATCH").to(getApiRoute()); + } + + public void setDescription(String description) throws IOException { + edit("description",description); + } + + /** + * Adds a file. + */ + public void addFile(String fileName, String content) throws IOException + { + updateFile(fileName, content); + } + +// /** +// * Deletes a file. +// */ +// public void deleteFile(String fileName) throws IOException { +// LinkedHashMap filesMap = new LinkedHashMap(); +// filesMap.put(fileName, Collections.singletonMap("filename", null)); +// edit("files",filesMap); +// } + + /** + * Replaces the content of a file. + */ + public void updateFile(String fileName, String content) throws IOException + { + LinkedHashMap filesMap = new LinkedHashMap(); + filesMap.put(fileName, Collections.singletonMap("content", content)); + edit("files",filesMap); + } + + /** + * Renames a file. + */ + public void renameFile(String fileName, String newFileName) throws IOException + { + LinkedHashMap filesMap = new LinkedHashMap(); + filesMap.put(fileName, Collections.singletonMap("filename", newFileName)); + edit("files",filesMap); + } + @Override public boolean equals(Object o) { if (this == o) return true; From 620c822dac60d62d6d04dfc39afe8ef9e48cec86 Mon Sep 17 00:00:00 2001 From: Martin van Zijl Date: Thu, 10 Oct 2019 07:08:05 +1300 Subject: [PATCH 2/3] Updates per review on GitHub: - Created class GHGistUpdater to do the updating. - Added unit tests. --- src/main/java/org/kohsuke/github/GHGist.java | 62 ++------ .../org/kohsuke/github/GHGistUpdater.java | 60 ++++++++ .../org/kohsuke/github/GHGistUpdaterTest.java | 73 +++++++++ ...-fde9aa5a-83fc-41d8-b556-16335859de73.json | 100 +++++++++++++ ...-d3ef0c04-12e0-4815-a871-ab7a397b00d7.json | 139 ++++++++++++++++++ .../mappings/gists-1-fde9aa.json | 48 ++++++ ...ef72c25fe4b3f673437603ab6d5d-2-d3ef0c.json | 47 ++++++ ...ef72c25fe4b3f673437603ab6d5d-3-94cd51.json | 33 +++++ 8 files changed, 512 insertions(+), 50 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHGistUpdater.java create mode 100644 src/test/java/org/kohsuke/github/GHGistUpdaterTest.java create mode 100644 src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists-fde9aa5a-83fc-41d8-b556-16335859de73.json create mode 100644 src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists_209fef72c25fe4b3f673437603ab6d5d-d3ef0c04-12e0-4815-a871-ab7a397b00d7.json create mode 100644 src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists-1-fde9aa.json create mode 100644 src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-2-d3ef0c.json create mode 100644 src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-3-94cd51.json diff --git a/src/main/java/org/kohsuke/github/GHGist.java b/src/main/java/org/kohsuke/github/GHGist.java index 10c1522176..1cccf952f7 100644 --- a/src/main/java/org/kohsuke/github/GHGist.java +++ b/src/main/java/org/kohsuke/github/GHGist.java @@ -6,7 +6,6 @@ import java.net.URL; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -116,6 +115,12 @@ private void wrapUp() { e.getValue().fileName = e.getKey(); } } + + String getApiRoute() { + // Needed since getId() returns the hidden field from GHObject. + return "/gists/" + id; + } + String getApiTailUrl(String tail) { return "/gists/" + id + '/' + tail; } @@ -160,55 +165,6 @@ public void delete() throws IOException { new Requester(root).method("DELETE").to("/gists/" + id); } - private String getApiRoute() { - return "/gists/" + id; - } - - private void edit(String key, Object value) throws IOException { - new Requester(root)._with(key, value).method("PATCH").to(getApiRoute()); - } - - public void setDescription(String description) throws IOException { - edit("description",description); - } - - /** - * Adds a file. - */ - public void addFile(String fileName, String content) throws IOException - { - updateFile(fileName, content); - } - -// /** -// * Deletes a file. -// */ -// public void deleteFile(String fileName) throws IOException { -// LinkedHashMap filesMap = new LinkedHashMap(); -// filesMap.put(fileName, Collections.singletonMap("filename", null)); -// edit("files",filesMap); -// } - - /** - * Replaces the content of a file. - */ - public void updateFile(String fileName, String content) throws IOException - { - LinkedHashMap filesMap = new LinkedHashMap(); - filesMap.put(fileName, Collections.singletonMap("content", content)); - edit("files",filesMap); - } - - /** - * Renames a file. - */ - public void renameFile(String fileName, String newFileName) throws IOException - { - LinkedHashMap filesMap = new LinkedHashMap(); - filesMap.put(fileName, Collections.singletonMap("filename", newFileName)); - edit("files",filesMap); - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -222,4 +178,10 @@ public boolean equals(Object o) { public int hashCode() { return id.hashCode(); } + + GHGist wrap(GHUser owner) { + this.owner = owner; + this.root = owner.root; + return this; + } } diff --git a/src/main/java/org/kohsuke/github/GHGistUpdater.java b/src/main/java/org/kohsuke/github/GHGistUpdater.java new file mode 100644 index 0000000000..bb878cb2e8 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHGistUpdater.java @@ -0,0 +1,60 @@ +package org.kohsuke.github; + +import java.io.IOException; +import java.util.Collections; +import java.util.LinkedHashMap; + +/** + * Builder pattern for updating a Gist. + * + * @author Martin van Zijl + */ +public class GHGistUpdater { + private final GHGist base; + private final Requester builder; + LinkedHashMap files; + + GHGistUpdater(GHGist base) { + this.base = base; + this.builder = new Requester(base.root); + + files = new LinkedHashMap<>(); + } + + public GHGistUpdater addFile(String fileName, 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 renameFile(String fileName, String newFileName) throws IOException + { + files.put(fileName, Collections.singletonMap("filename", newFileName)); + return this; + } + + public GHGistUpdater updateFile(String fileName, String content) throws IOException { + files.put(fileName, Collections.singletonMap("content", content)); + return this; + } + + public GHGistUpdater description(String desc) { + builder.with("description",desc); + return this; + } + + /** + * Updates the Gist based on the parameters specified thus far. + */ + public GHGist update() throws IOException { + builder._with("files", files); + return builder + .method("PATCH") + .to(base.getApiRoute(), GHGist.class).wrap(base.owner); + } +} diff --git a/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java b/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java new file mode 100644 index 0000000000..e6fe07430e --- /dev/null +++ b/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java @@ -0,0 +1,73 @@ +package org.kohsuke.github; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Martin van Zijl + */ +public class GHGistUpdaterTest extends AbstractGitHubWireMockTest { + + private GHGist gist; + + @Before + public void setUp() throws IOException { + GHGistBuilder builder = new GHGistBuilder(gitHub); + gist = builder.description("Test for the API") + .file("unmodified.txt", "Should be unmodified") + //.file("delete-me.txt", "To be deleted") + .file("rename-me.py", "print 'hello'") + .file("update-me.txt", "To be updated") + .public_(true) + .create(); + } + + @After + public void cleanUp() throws Exception { + // Cleanup is only needed when proxying + if (!mockGitHub.isUseProxy()) { + return; + } + + gist.delete(); + } + + @Test + public void testGitUpdater() throws Exception { + GHGistUpdater updater = new GHGistUpdater(gist); + GHGist updatedGist = updater.description("Description updated by API") + .addFile("new-file.txt", "Added by updater") + //.deleteFile("delete-me.txt") + .renameFile("rename-me.py", "renamed.py") + .updateFile("update-me.txt", "Content updated by API") + .update(); + + assertEquals("Description updated by API", updatedGist.getDescription()); + + Map files = updatedGist.getFiles(); + + // Check that the unmodified file stays intact. + assertTrue(files.containsKey("unmodified.txt")); + assertEquals("Should be unmodified", files.get("unmodified.txt").getContent()); + + // Check that the files are updated as expected. + //assertFalse("File was not deleted.", files.containsKey("delete-me.txt")); + + assertTrue(files.containsKey("new-file.txt")); + assertEquals("Added by updater", files.get("new-file.txt").getContent()); + + assertFalse(files.containsKey("rename-me.py")); + assertTrue(files.containsKey("renamed.py")); + assertEquals("print 'hello'", files.get("renamed.py").getContent()); + + assertEquals("Content updated by API", files.get("update-me.txt").getContent()); + } +} diff --git a/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists-fde9aa5a-83fc-41d8-b556-16335859de73.json b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists-fde9aa5a-83fc-41d8-b556-16335859de73.json new file mode 100644 index 0000000000..c0d3a5cb17 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists-fde9aa5a-83fc-41d8-b556-16335859de73.json @@ -0,0 +1,100 @@ +{ + "url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d", + "forks_url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/forks", + "commits_url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/commits", + "id": "209fef72c25fe4b3f673437603ab6d5d", + "node_id": "MDQ6R2lzdDIwOWZlZjcyYzI1ZmU0YjNmNjczNDM3NjAzYWI2ZDVk", + "git_pull_url": "https://gist.github.com/209fef72c25fe4b3f673437603ab6d5d.git", + "git_push_url": "https://gist.github.com/209fef72c25fe4b3f673437603ab6d5d.git", + "html_url": "https://gist.github.com/209fef72c25fe4b3f673437603ab6d5d", + "files": { + "rename-me.py": { + "filename": "rename-me.py", + "type": "application/x-python", + "language": "Python", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/b609dee383ab7aee026b182aa99086777a97cb77/rename-me.py", + "size": 13, + "truncated": false, + "content": "print 'hello'" + }, + "unmodified.txt": { + "filename": "unmodified.txt", + "type": "text/plain", + "language": "Text", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/92aaf920c4873e413416e552aaad16cd79d361b7/unmodified.txt", + "size": 20, + "truncated": false, + "content": "Should be unmodified" + }, + "update-me.txt": { + "filename": "update-me.txt", + "type": "text/plain", + "language": "Text", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/bd835e30ff8e75fb7f435c1868b747f83c3ba243/update-me.txt", + "size": 13, + "truncated": false, + "content": "To be updated" + } + }, + "public": true, + "created_at": "2019-10-09T18:06:38Z", + "updated_at": "2019-10-09T18:06:38Z", + "description": "Test for the API", + "comments": 0, + "user": null, + "comments_url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/comments", + "owner": { + "login": "martinvanzijl", + "id": 24422213, + "node_id": "MDQ6VXNlcjI0NDIyMjEz", + "avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/martinvanzijl", + "html_url": "https://github.com/martinvanzijl", + "followers_url": "https://api.github.com/users/martinvanzijl/followers", + "following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}", + "gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}", + "starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions", + "organizations_url": "https://api.github.com/users/martinvanzijl/orgs", + "repos_url": "https://api.github.com/users/martinvanzijl/repos", + "events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}", + "received_events_url": "https://api.github.com/users/martinvanzijl/received_events", + "type": "User", + "site_admin": false + }, + "forks": [], + "history": [ + { + "user": { + "login": "martinvanzijl", + "id": 24422213, + "node_id": "MDQ6VXNlcjI0NDIyMjEz", + "avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/martinvanzijl", + "html_url": "https://github.com/martinvanzijl", + "followers_url": "https://api.github.com/users/martinvanzijl/followers", + "following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}", + "gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}", + "starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions", + "organizations_url": "https://api.github.com/users/martinvanzijl/orgs", + "repos_url": "https://api.github.com/users/martinvanzijl/repos", + "events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}", + "received_events_url": "https://api.github.com/users/martinvanzijl/received_events", + "type": "User", + "site_admin": false + }, + "version": "3ef13d1c0c4fc4deef97527c758d1068d2ca4fe3", + "committed_at": "2019-10-09T18:06:37Z", + "change_status": { + "total": 3, + "additions": 3, + "deletions": 0 + }, + "url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/3ef13d1c0c4fc4deef97527c758d1068d2ca4fe3" + } + ], + "truncated": false +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists_209fef72c25fe4b3f673437603ab6d5d-d3ef0c04-12e0-4815-a871-ab7a397b00d7.json b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists_209fef72c25fe4b3f673437603ab6d5d-d3ef0c04-12e0-4815-a871-ab7a397b00d7.json new file mode 100644 index 0000000000..0aeeb7f2b7 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/__files/gists_209fef72c25fe4b3f673437603ab6d5d-d3ef0c04-12e0-4815-a871-ab7a397b00d7.json @@ -0,0 +1,139 @@ +{ + "url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d", + "forks_url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/forks", + "commits_url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/commits", + "id": "209fef72c25fe4b3f673437603ab6d5d", + "node_id": "MDQ6R2lzdDIwOWZlZjcyYzI1ZmU0YjNmNjczNDM3NjAzYWI2ZDVk", + "git_pull_url": "https://gist.github.com/209fef72c25fe4b3f673437603ab6d5d.git", + "git_push_url": "https://gist.github.com/209fef72c25fe4b3f673437603ab6d5d.git", + "html_url": "https://gist.github.com/209fef72c25fe4b3f673437603ab6d5d", + "files": { + "new-file.txt": { + "filename": "new-file.txt", + "type": "text/plain", + "language": "Text", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/be732fb7a4178a55e5794e0b142b6e251545d7e4/new-file.txt", + "size": 16, + "truncated": false, + "content": "Added by updater" + }, + "renamed.py": { + "filename": "renamed.py", + "type": "application/x-python", + "language": "Python", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/b609dee383ab7aee026b182aa99086777a97cb77/renamed.py", + "size": 13, + "truncated": false, + "content": "print 'hello'" + }, + "unmodified.txt": { + "filename": "unmodified.txt", + "type": "text/plain", + "language": "Text", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/92aaf920c4873e413416e552aaad16cd79d361b7/unmodified.txt", + "size": 20, + "truncated": false, + "content": "Should be unmodified" + }, + "update-me.txt": { + "filename": "update-me.txt", + "type": "text/plain", + "language": "Text", + "raw_url": "https://gist.githubusercontent.com/martinvanzijl/209fef72c25fe4b3f673437603ab6d5d/raw/401c618749d9904b511a478bf45934966158b5bb/update-me.txt", + "size": 22, + "truncated": false, + "content": "Content updated by API" + } + }, + "public": true, + "created_at": "2019-10-09T18:06:38Z", + "updated_at": "2019-10-09T18:06:39Z", + "description": "Description updated by API", + "comments": 0, + "user": null, + "comments_url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/comments", + "owner": { + "login": "martinvanzijl", + "id": 24422213, + "node_id": "MDQ6VXNlcjI0NDIyMjEz", + "avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/martinvanzijl", + "html_url": "https://github.com/martinvanzijl", + "followers_url": "https://api.github.com/users/martinvanzijl/followers", + "following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}", + "gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}", + "starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions", + "organizations_url": "https://api.github.com/users/martinvanzijl/orgs", + "repos_url": "https://api.github.com/users/martinvanzijl/repos", + "events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}", + "received_events_url": "https://api.github.com/users/martinvanzijl/received_events", + "type": "User", + "site_admin": false + }, + "forks": [], + "history": [ + { + "user": { + "login": "martinvanzijl", + "id": 24422213, + "node_id": "MDQ6VXNlcjI0NDIyMjEz", + "avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/martinvanzijl", + "html_url": "https://github.com/martinvanzijl", + "followers_url": "https://api.github.com/users/martinvanzijl/followers", + "following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}", + "gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}", + "starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions", + "organizations_url": "https://api.github.com/users/martinvanzijl/orgs", + "repos_url": "https://api.github.com/users/martinvanzijl/repos", + "events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}", + "received_events_url": "https://api.github.com/users/martinvanzijl/received_events", + "type": "User", + "site_admin": false + }, + "version": "a081e0cf18f30a48b337375327d1f04d4d61b2ab", + "committed_at": "2019-10-09T18:06:39Z", + "change_status": { + "total": 3, + "additions": 2, + "deletions": 1 + }, + "url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/a081e0cf18f30a48b337375327d1f04d4d61b2ab" + }, + { + "user": { + "login": "martinvanzijl", + "id": 24422213, + "node_id": "MDQ6VXNlcjI0NDIyMjEz", + "avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/martinvanzijl", + "html_url": "https://github.com/martinvanzijl", + "followers_url": "https://api.github.com/users/martinvanzijl/followers", + "following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}", + "gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}", + "starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions", + "organizations_url": "https://api.github.com/users/martinvanzijl/orgs", + "repos_url": "https://api.github.com/users/martinvanzijl/repos", + "events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}", + "received_events_url": "https://api.github.com/users/martinvanzijl/received_events", + "type": "User", + "site_admin": false + }, + "version": "3ef13d1c0c4fc4deef97527c758d1068d2ca4fe3", + "committed_at": "2019-10-09T18:06:37Z", + "change_status": { + "total": 3, + "additions": 3, + "deletions": 0 + }, + "url": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d/3ef13d1c0c4fc4deef97527c758d1068d2ca4fe3" + } + ], + "truncated": false +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists-1-fde9aa.json b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists-1-fde9aa.json new file mode 100644 index 0000000000..15895715f0 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists-1-fde9aa.json @@ -0,0 +1,48 @@ +{ + "id": "fde9aa5a-83fc-41d8-b556-16335859de73", + "name": "gists", + "request": { + "url": "/gists", + "method": "POST", + "bodyPatterns": [ + { + "equalToJson": "{\"public\":true,\"description\":\"Test for the API\",\"files\":{\"unmodified.txt\":{\"content\":\"Should be unmodified\"},\"rename-me.py\":{\"content\":\"print 'hello'\"},\"update-me.txt\":{\"content\":\"To be updated\"}}}", + "ignoreArrayOrder": true, + "ignoreExtraElements": true + } + ] + }, + "response": { + "status": 201, + "bodyFileName": "gists-fde9aa5a-83fc-41d8-b556-16335859de73.json", + "headers": { + "Date": "Wed, 09 Oct 2019 18:06:38 GMT", + "Content-Type": "application/json; charset=utf-8", + "Server": "GitHub.com", + "Status": "201 Created", + "X-RateLimit-Limit": "5000", + "X-RateLimit-Remaining": "4986", + "X-RateLimit-Reset": "1570647617", + "Cache-Control": "private, max-age=60, s-maxage=60", + "Vary": [ + "Accept, Authorization, Cookie, X-GitHub-OTP", + "Accept-Encoding" + ], + "ETag": "\"f60e43f46988893f64e50f632c072e41\"", + "Location": "https://api.github.com/gists/209fef72c25fe4b3f673437603ab6d5d", + "X-GitHub-Media-Type": "unknown, github.v3", + "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type", + "Access-Control-Allow-Origin": "*", + "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options": "deny", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy": "default-src 'none'", + "X-GitHub-Request-Id": "AF56:13F4:1234663:150EFE0:5D9E21AD" + } + }, + "uuid": "fde9aa5a-83fc-41d8-b556-16335859de73", + "persistent": true, + "insertionIndex": 1 +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-2-d3ef0c.json b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-2-d3ef0c.json new file mode 100644 index 0000000000..a7225467da --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-2-d3ef0c.json @@ -0,0 +1,47 @@ +{ + "id": "d3ef0c04-12e0-4815-a871-ab7a397b00d7", + "name": "gists_209fef72c25fe4b3f673437603ab6d5d", + "request": { + "url": "/gists/209fef72c25fe4b3f673437603ab6d5d", + "method": "PATCH", + "bodyPatterns": [ + { + "equalToJson": "{\"description\":\"Description updated by API\",\"files\":{\"new-file.txt\":{\"content\":\"Added by updater\"},\"rename-me.py\":{\"filename\":\"renamed.py\"},\"update-me.txt\":{\"content\":\"Content updated by API\"}}}", + "ignoreArrayOrder": true, + "ignoreExtraElements": true + } + ] + }, + "response": { + "status": 200, + "bodyFileName": "gists_209fef72c25fe4b3f673437603ab6d5d-d3ef0c04-12e0-4815-a871-ab7a397b00d7.json", + "headers": { + "Date": "Wed, 09 Oct 2019 18:06:40 GMT", + "Content-Type": "application/json; charset=utf-8", + "Server": "GitHub.com", + "Status": "200 OK", + "X-RateLimit-Limit": "5000", + "X-RateLimit-Remaining": "4985", + "X-RateLimit-Reset": "1570647617", + "Cache-Control": "private, max-age=60, s-maxage=60", + "Vary": [ + "Accept, Authorization, Cookie, X-GitHub-OTP", + "Accept-Encoding" + ], + "ETag": "W/\"428bc6a3a9c8f9c26b9023cf8ff0ac9f\"", + "X-GitHub-Media-Type": "unknown, github.v3", + "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type", + "Access-Control-Allow-Origin": "*", + "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options": "deny", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy": "default-src 'none'", + "X-GitHub-Request-Id": "AF56:13F4:1234695:150F007:5D9E21AE" + } + }, + "uuid": "d3ef0c04-12e0-4815-a871-ab7a397b00d7", + "persistent": true, + "insertionIndex": 2 +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-3-94cd51.json b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-3-94cd51.json new file mode 100644 index 0000000000..1341899dd2 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GHGistUpdaterTest/wiremock/testGitUpdater/mappings/gists_209fef72c25fe4b3f673437603ab6d5d-3-94cd51.json @@ -0,0 +1,33 @@ +{ + "id": "94cd5114-cc74-4003-ba1f-6bedcb561d20", + "name": "gists_209fef72c25fe4b3f673437603ab6d5d", + "request": { + "url": "/gists/209fef72c25fe4b3f673437603ab6d5d", + "method": "DELETE" + }, + "response": { + "status": 204, + "headers": { + "Date": "Wed, 09 Oct 2019 18:06:40 GMT", + "Server": "GitHub.com", + "Status": "204 No Content", + "X-RateLimit-Limit": "5000", + "X-RateLimit-Remaining": "4984", + "X-RateLimit-Reset": "1570647617", + "X-GitHub-Media-Type": "unknown, github.v3", + "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type", + "Access-Control-Allow-Origin": "*", + "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options": "deny", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy": "default-src 'none'", + "Vary": "Accept-Encoding", + "X-GitHub-Request-Id": "AF56:13F4:12346B2:150F02F:5D9E21B0" + } + }, + "uuid": "94cd5114-cc74-4003-ba1f-6bedcb561d20", + "persistent": true, + "insertionIndex": 3 +} \ No newline at end of file From 1ba630c221e1cad4936ba706f5853d0252a260b4 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Fri, 11 Oct 2019 11:21:11 -0700 Subject: [PATCH 3/3] Minor Gist updater improvement --- src/main/java/org/kohsuke/github/GHGist.java | 19 +++++++++++++------ .../org/kohsuke/github/GHGistUpdater.java | 2 +- .../org/kohsuke/github/GHGistUpdaterTest.java | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHGist.java b/src/main/java/org/kohsuke/github/GHGist.java index 1cccf952f7..8de2f02585 100644 --- a/src/main/java/org/kohsuke/github/GHGist.java +++ b/src/main/java/org/kohsuke/github/GHGist.java @@ -1,6 +1,7 @@ package org.kohsuke.github; import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.net.URL; @@ -116,13 +117,12 @@ private void wrapUp() { } } - String getApiRoute() { - // Needed since getId() returns the hidden field from GHObject. - return "/gists/" + id; - } - String getApiTailUrl(String tail) { - return "/gists/" + id + '/' + tail; + String result = "/gists/" + id; + if (!StringUtils.isBlank(tail)) { + result += StringUtils.prependIfMissing(tail, "/"); + } + return result; } public void star() throws IOException { @@ -165,6 +165,13 @@ public void delete() throws IOException { new Requester(root).method("DELETE").to("/gists/" + id); } + /** + * Updates this gist via a builder. + */ + public GHGistUpdater update() throws IOException { + return new GHGistUpdater(this); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/kohsuke/github/GHGistUpdater.java b/src/main/java/org/kohsuke/github/GHGistUpdater.java index bb878cb2e8..71bff34e22 100644 --- a/src/main/java/org/kohsuke/github/GHGistUpdater.java +++ b/src/main/java/org/kohsuke/github/GHGistUpdater.java @@ -55,6 +55,6 @@ public GHGist update() throws IOException { builder._with("files", files); return builder .method("PATCH") - .to(base.getApiRoute(), GHGist.class).wrap(base.owner); + .to(base.getApiTailUrl(""), GHGist.class).wrap(base.owner); } } diff --git a/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java b/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java index e6fe07430e..23bd4622f3 100644 --- a/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java +++ b/src/test/java/org/kohsuke/github/GHGistUpdaterTest.java @@ -42,7 +42,7 @@ public void cleanUp() throws Exception { @Test public void testGitUpdater() throws Exception { - GHGistUpdater updater = new GHGistUpdater(gist); + GHGistUpdater updater = gist.update(); GHGist updatedGist = updater.description("Description updated by API") .addFile("new-file.txt", "Added by updater") //.deleteFile("delete-me.txt")