Skip to content

Commit 59a85f6

Browse files
authored
add GitDataClient::updateReference (#253)
* add GitDataClient::updateReference * fix test
1 parent d99d19b commit 59a85f6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main/java/com/spotify/github/v3/clients/GitDataClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ public CompletableFuture<Reference> createTagReference(final String tag, final S
188188
return createReference(format("refs/tags/%s", tag), sha);
189189
}
190190

191+
/**
192+
* Update a git reference.
193+
*
194+
* @param ref reference name
195+
* @param sha The SHA1 value to set the reference to
196+
* @param force Indicates whether to force the update or to make sure the update is a fast-forward update. Setting
197+
* this to false will make sure you're not overwriting work.
198+
*/
199+
public CompletableFuture<Reference> updateReference(final String ref, final String sha, final boolean force) {
200+
final String path = format(REFERENCE_URI, owner, repo, ref);
201+
final ImmutableMap<String, String> body =
202+
of(
203+
"sha", sha,
204+
"force", Boolean.toString(force));
205+
return github.patch(path, github.json().toJsonUnchecked(body), Reference.class);
206+
}
207+
191208
/**
192209
* Create an annotated tag. First it would create a tag reference and then create annotated tag
193210
*

src/test/java/com/spotify/github/v3/clients/GitDataClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ public void createTagReference() throws Exception {
180180
assertThat(reference.object().sha(), is("5926dd300de5fee31d445c57be223f00e128a634"));
181181
}
182182

183+
@Test
184+
public void updateReference() throws Exception {
185+
final CompletableFuture<Reference> fixture =
186+
completedFuture(json.fromJson(getFixture("branch.json"), Reference.class));
187+
final ImmutableMap<String, String> body =
188+
of(
189+
"sha", "aa218f56b14c9653891f9e74264a383fa43fefbd",
190+
"force", "false");
191+
when(github.patch(
192+
"/repos/someowner/somerepo/git/refs/featureA",
193+
github.json().toJsonUnchecked(body),
194+
Reference.class))
195+
.thenReturn(fixture);
196+
final Reference reference =
197+
gitDataClient.updateReference("featureA", "aa218f56b14c9653891f9e74264a383fa43fefbd", false).get();
198+
assertThat(reference.ref(), is("refs/heads/featureA"));
199+
assertThat(reference.object().sha(), is("aa218f56b14c9653891f9e74264a383fa43fefbd"));
200+
}
201+
183202
@Test
184203
public void createAnnotateTag() throws Exception {
185204
final String now = Instant.now().toString();

0 commit comments

Comments
 (0)