Skip to content

Commit 1dcd84d

Browse files
Lirickmarthast
andauthored
Add create plain reference functionality (#29)
Co-authored-by: marthast <marthast@spotify.com>
1 parent cb97d5f commit 1dcd84d

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,38 @@ public CompletableFuture<List<Reference>> listMatchingReferences(final String re
126126
}
127127

128128
/**
129-
* Create a git branch reference. It must not include the refs/heads.
129+
* Create a git reference.
130130
*
131-
* @param branch tag name
131+
* @param ref reference name
132132
* @param sha commit to branch from
133133
*/
134-
public CompletableFuture<Reference> createBranchReference(final String branch, final String sha) {
134+
public CompletableFuture<Reference> createReference(final String ref, final String sha) {
135135
final String path = format(CREATE_REFERENCE_URI, owner, repo);
136136
final ImmutableMap<String, String> body = of(
137-
"ref", format("refs/heads/%s", branch),
137+
"ref", ref,
138138
"sha", sha
139139
);
140140
return github.post(path, github.json().toJsonUnchecked(body), Reference.class);
141141
}
142142

143+
/**
144+
* Create a git branch reference. It must not include the refs/heads.
145+
*
146+
* @param branch tag name
147+
* @param sha commit to branch from
148+
*/
149+
public CompletableFuture<Reference> createBranchReference(final String branch, final String sha) {
150+
return createReference(format("refs/heads/%s", branch), sha);
151+
}
152+
143153
/**
144154
* Create a git tag reference. It must not include the refs/tags.
145155
*
146156
* @param tag tag name
147157
* @param sha commit to tag
148158
*/
149159
public CompletableFuture<Reference> createTagReference(final String tag, final String sha) {
150-
final String path = format(CREATE_REFERENCE_URI, owner, repo);
151-
final ImmutableMap<String, String> body = of("ref", format("refs/tags/%s", tag), "sha", sha);
152-
return github.post(path, github.json().toJsonUnchecked(body), Reference.class);
160+
return createReference(format("refs/tags/%s", tag), sha);
153161
}
154162

155163
/**

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ public void listMatchingReferences() throws Exception {
102102
}
103103

104104

105+
public void createReference() throws Exception {
106+
final CompletableFuture<Reference> fixture =
107+
completedFuture(json.fromJson(getFixture("reference.json"), Reference.class));
108+
final ImmutableMap<String, String> body = of(
109+
"ref", "featureA",
110+
"sha", "aa218f56b14c9653891f9e74264a383fa43fefbd"
111+
);
112+
when(github.post("/repos/someowner/somerepo/git/refs",
113+
github.json().toJsonUnchecked(body),
114+
Reference.class))
115+
.thenReturn(fixture);
116+
final Reference reference =
117+
gitDataClient.createReference("featureA", "aa218f56b14c9653891f9e74264a383fa43fefbd").get();
118+
assertThat(reference.ref(), is("featureA"));
119+
assertThat(reference.object().sha(), is("aa218f56b14c9653891f9e74264a383fa43fefbd"));
120+
}
121+
105122
@Test
106123
public void createBranchReference() throws Exception {
107124
final CompletableFuture<Reference> fixture =
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ref": "featureA",
3+
"node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==",
4+
"url": "https://api.github.com/repos/octocat/Hello-World/git/featureA",
5+
"object": {
6+
"type": "commit",
7+
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
8+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
9+
}
10+
}

0 commit comments

Comments
 (0)