diff --git a/src/main/java/com/spotify/github/v3/clients/GitDataClient.java b/src/main/java/com/spotify/github/v3/clients/GitDataClient.java index 4953f47b..6f7fda03 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitDataClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitDataClient.java @@ -188,6 +188,23 @@ public CompletableFuture createTagReference(final String tag, final S return createReference(format("refs/tags/%s", tag), sha); } + /** + * Update a git reference. + * + * @param ref reference name + * @param sha The SHA1 value to set the reference to + * @param force Indicates whether to force the update or to make sure the update is a fast-forward update. Setting + * this to false will make sure you're not overwriting work. + */ + public CompletableFuture updateReference(final String ref, final String sha, final boolean force) { + final String path = format(REFERENCE_URI, owner, repo, ref); + final ImmutableMap body = + of( + "sha", sha, + "force", Boolean.toString(force)); + return github.patch(path, github.json().toJsonUnchecked(body), Reference.class); + } + /** * Create an annotated tag. First it would create a tag reference and then create annotated tag * diff --git a/src/test/java/com/spotify/github/v3/clients/GitDataClientTest.java b/src/test/java/com/spotify/github/v3/clients/GitDataClientTest.java index 4f87af26..1ef3dfdd 100644 --- a/src/test/java/com/spotify/github/v3/clients/GitDataClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/GitDataClientTest.java @@ -180,6 +180,25 @@ public void createTagReference() throws Exception { assertThat(reference.object().sha(), is("5926dd300de5fee31d445c57be223f00e128a634")); } + @Test + public void updateReference() throws Exception { + final CompletableFuture fixture = + completedFuture(json.fromJson(getFixture("branch.json"), Reference.class)); + final ImmutableMap body = + of( + "sha", "aa218f56b14c9653891f9e74264a383fa43fefbd", + "force", "false"); + when(github.patch( + "/repos/someowner/somerepo/git/refs/featureA", + github.json().toJsonUnchecked(body), + Reference.class)) + .thenReturn(fixture); + final Reference reference = + gitDataClient.updateReference("featureA", "aa218f56b14c9653891f9e74264a383fa43fefbd", false).get(); + assertThat(reference.ref(), is("refs/heads/featureA")); + assertThat(reference.object().sha(), is("aa218f56b14c9653891f9e74264a383fa43fefbd")); + } + @Test public void createAnnotateTag() throws Exception { final String now = Instant.now().toString();