Skip to content

Commit bbf5805

Browse files
annagustavssonAnna Gustavssononalzengin
authored
Add logic for creating commit on new branch (#72)
* Add logic for creating commit on new branch * Implement changes according to review comments * Remove unused import + fix formatting of one line * Move logic from anonymous functions to private methods * Add test methods for getTree and getReference * Add test methods for setBlob, setTree and createBranch * Remove method getGitCommit and modify Commit interface * Fix checkstyle violations and rename handleGetGitCommit to handle GetCommit * Add GithubException if API response returns null * Some cleanup * Fix handleSetBlob method * Fix handleSetBlob method and wrap if statement in handleGetCommit in brackets * EOF newlines * Header updates * Did some refactoring and fixed some tests Co-authored-by: Anna Gustavsson <agustavsson@spotify.com> Co-authored-by: Önal Zengin <onalzengin@spotify.com>
1 parent 6f4080f commit bbf5805

File tree

18 files changed

+686
-50
lines changed

18 files changed

+686
-50
lines changed

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

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
import com.google.common.collect.ImmutableMap;
2929
import com.spotify.github.v3.git.Reference;
30+
import com.spotify.github.v3.git.ShaLink;
3031
import com.spotify.github.v3.git.Tag;
32+
import com.spotify.github.v3.git.Tree;
33+
import com.spotify.github.v3.git.TreeItem;
34+
import com.spotify.github.v3.repos.Commit;
3135
import java.time.Instant;
3236
import java.util.List;
3337
import java.util.concurrent.CompletableFuture;
@@ -42,6 +46,14 @@ public class GitDataClient {
4246
private static final String CREATE_REFERENCE_URI = "/repos/%s/%s/git/refs";
4347
private static final String CREATE_REFERENCE_TAG = "/repos/%s/%s/git/tags";
4448
private static final String LIST_MATCHING_REFERENCES_URI = "/repos/%s/%s/git/matching-refs/%s";
49+
50+
private static final String CREATE_COMMIT_URI_TEMPLATE = "/repos/%s/%s/git/commits";
51+
52+
private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s";
53+
private static final String TREE_URI_TEMPLATE = "/repos/%s/%s/git/trees";
54+
55+
private static final String BLOB_URI_TEMPLATE = "/repos/%s/%s/git/blobs";
56+
4557
private final GitHubClient github;
4658
private final String owner;
4759
private final String repo;
@@ -62,8 +74,7 @@ static GitDataClient create(final GitHubClient github, final String owner, final
6274
* @param ref search parameters
6375
*/
6476
public CompletableFuture<Void> deleteReference(final String ref) {
65-
final String path =
66-
format(REFERENCE_URI, owner, repo, ref.replaceAll("refs/", ""));
77+
final String path = format(REFERENCE_URI, owner, repo, ref.replaceAll("refs/", ""));
6778
return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER);
6879
}
6980

@@ -136,16 +147,12 @@ public CompletableFuture<List<Reference>> listReferences(final String ref) {
136147
return github.request(path, LIST_REFERENCES);
137148
}
138149

139-
/**
140-
* List references. (Replaced by listMatchingReferences for github enterprise version > 2.18)
141-
*
142-
*/
150+
/** List references. (Replaced by listMatchingReferences for github enterprise version > 2.18) */
143151
@Deprecated
144152
public CompletableFuture<List<Reference>> listReferences() {
145153
return listReferences("");
146154
}
147155

148-
149156
/**
150157
* Create a git reference.
151158
*
@@ -154,10 +161,10 @@ public CompletableFuture<List<Reference>> listReferences() {
154161
*/
155162
public CompletableFuture<Reference> createReference(final String ref, final String sha) {
156163
final String path = format(CREATE_REFERENCE_URI, owner, repo);
157-
final ImmutableMap<String, String> body = of(
158-
"ref", ref,
159-
"sha", sha
160-
);
164+
final ImmutableMap<String, String> body =
165+
of(
166+
"ref", ref,
167+
"sha", sha);
161168
return github.post(path, github.json().toJsonUnchecked(body), Reference.class);
162169
}
163170

@@ -212,4 +219,63 @@ public CompletableFuture<Tag> createAnnotatedTag(
212219
.thenCompose(
213220
reference -> github.post(tagPath, github.json().toJsonUnchecked(body), Tag.class));
214221
}
222+
223+
/**
224+
* Create a commit which references a tree
225+
*
226+
* @param message commit message
227+
* @param parents list of parent sha values, usually just one sha
228+
* @param treeSha sha value of the tree
229+
*/
230+
public CompletableFuture<Commit> createCommit(
231+
final String message, final List<String> parents, final String treeSha) {
232+
final String path = String.format(CREATE_COMMIT_URI_TEMPLATE, owner, repo);
233+
final String requestBody =
234+
github
235+
.json()
236+
.toJsonUnchecked(
237+
ImmutableMap.of("message", message, "parents", parents, "tree", treeSha));
238+
return github.post(path, requestBody, Commit.class);
239+
}
240+
241+
/**
242+
* Get a repository tree.
243+
*
244+
* @param sha commit sha
245+
* @return tree
246+
*/
247+
public CompletableFuture<Tree> getTree(final String sha) {
248+
final String path = String.format(TREE_SHA_URI_TEMPLATE, owner, repo, sha);
249+
return github.request(path, Tree.class);
250+
}
251+
252+
/**
253+
* Set a repository tree.
254+
*
255+
* @param tree list of tree items
256+
* @param baseTreeSha sha of existing tree used as base for new tree
257+
* @return tree
258+
*/
259+
public CompletableFuture<Tree> createTree(final List<TreeItem> tree, final String baseTreeSha) {
260+
final String path = String.format(TREE_URI_TEMPLATE, owner, repo);
261+
final String requestBody = github.json()
262+
.toJsonUnchecked(ImmutableMap.of("base_tree", baseTreeSha, "tree", tree));
263+
return github.post(path, requestBody, Tree.class);
264+
}
265+
266+
267+
/**
268+
* Post new content to the server.
269+
*
270+
* @param content the content to be posted
271+
*/
272+
public CompletableFuture<ShaLink> createBlob(final String content) {
273+
final String path = String.format(BLOB_URI_TEMPLATE, owner, repo);
274+
final String encoding = "utf-8|base64";
275+
final String requestBody = github.json()
276+
.toJsonUnchecked(ImmutableMap.of("content", content, "encoding", encoding));
277+
return github.post(path, requestBody, ShaLink.class);
278+
}
279+
280+
215281
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
package com.spotify.github.v3.clients;
2222

2323
import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER;
24+
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2425
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2526
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
26-
import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE;
27-
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2827
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
28+
import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE;
2929

3030
import com.google.common.base.Strings;
3131
import com.google.common.collect.ImmutableMap;
@@ -44,8 +44,8 @@
4444
import com.spotify.github.v3.repos.Languages;
4545
import com.spotify.github.v3.repos.Repository;
4646
import com.spotify.github.v3.repos.Status;
47-
import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
4847
import com.spotify.github.v3.repos.requests.AuthenticatedUserRepositoriesFilter;
48+
import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
4949
import java.lang.invoke.MethodHandles;
5050
import java.util.Iterator;
5151
import java.util.List;
@@ -161,9 +161,12 @@ public CompletableFuture<List<Repository>> listOrganizationRepositories() {
161161
* @param filter filter parameters
162162
* @return list of repositories for the authenticated user
163163
*/
164-
public Iterator<AsyncPage<Repository>> listAuthenticatedUserRepositories(final AuthenticatedUserRepositoriesFilter filter) {
164+
public Iterator<AsyncPage<Repository>> listAuthenticatedUserRepositories(
165+
final AuthenticatedUserRepositoriesFilter filter) {
165166
final String serial = filter.serialize();
166-
final String path = LIST_REPOSITORIES_FOR_AUTHENTICATED_USER + (Strings.isNullOrEmpty(serial) ? "" : "?" + serial);
167+
final String path =
168+
LIST_REPOSITORIES_FOR_AUTHENTICATED_USER
169+
+ (Strings.isNullOrEmpty(serial) ? "" : "?" + serial);
167170
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_REPOSITORY));
168171
}
169172

@@ -235,8 +238,8 @@ public CompletableFuture<CommitStatus> getCommitStatus(final String ref) {
235238
}
236239

237240
/**
238-
* List statuses for a specific ref. Statuses are returned in reverse chronological order.
239-
* The first status in the list will be the latest one.
241+
* List statuses for a specific ref. Statuses are returned in reverse chronological order. The
242+
* first status in the list will be the latest one.
240243
*
241244
* @param sha the commit sha to list the statuses for
242245
*/
@@ -284,9 +287,11 @@ public CompletableFuture<Commit> getCommit(final String sha) {
284287
/**
285288
* Get a repository tree.
286289
*
290+
* @deprecated Use {@link com.spotify.github.v3.clients.GitDataClient#getTree(String)} instead
287291
* @param sha commit sha
288292
* @return tree
289293
*/
294+
@Deprecated
290295
public CompletableFuture<Tree> getTree(final String sha) {
291296
final String path = String.format(TREE_SHA_URI_TEMPLATE, owner, repo, sha);
292297
return github.request(path, Tree.class);
@@ -429,7 +434,6 @@ public CompletableFuture<Languages> getLanguages() {
429434
* Perform a merge.
430435
*
431436
* @see "https://developer.github.com/enterprise/2.18/v3/repos/merging/"
432-
*
433437
* @param base branch name or sha
434438
* @param head branch name or sha
435439
* @return resulting merge commit, or empty if base already contains the head (nothing to merge)
@@ -442,7 +446,6 @@ public CompletableFuture<Optional<CommitItem>> merge(final String base, final St
442446
* Perform a merge.
443447
*
444448
* @see "https://developer.github.com/enterprise/2.18/v3/repos/merging/"
445-
*
446449
* @param base branch name that the head will be merged into
447450
* @param head branch name or sha to merge
448451
* @param commitMessage commit message to use for the merge commit
@@ -477,20 +480,17 @@ public CompletableFuture<Optional<CommitItem>> merge(
477480
});
478481
}
479482

480-
/**
483+
/**
481484
* Create a fork.
482485
*
483486
* @see "https://developer.github.com/v3/repos/forks/#create-a-fork"
484-
*
485487
* @param organization the organization where the fork will be created
486488
* @return resulting repository
487489
*/
488490
public CompletableFuture<Repository> createFork(final String organization) {
489491
final String path = String.format(FORK_TEMPLATE, owner, repo);
490492
final ImmutableMap<String, String> params =
491-
(organization == null)
492-
? ImmutableMap.of()
493-
: ImmutableMap.of("organization", organization);
493+
(organization == null) ? ImmutableMap.of() : ImmutableMap.of("organization", organization);
494494
final String body = github.json().toJsonUnchecked(params);
495495

496496
return github
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.git;
22+
23+
import com.fasterxml.jackson.annotation.JsonProperty;
24+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26+
import com.spotify.github.GithubStyle;
27+
import org.immutables.value.Value;
28+
import javax.annotation.Nullable;
29+
import java.net.URI;
30+
31+
@Value.Immutable
32+
@GithubStyle
33+
@JsonSerialize(as = ImmutableFileItem.class)
34+
@JsonDeserialize(as = ImmutableFileItem.class)
35+
36+
public interface FileItem {
37+
38+
/** Commit sha value. */
39+
String sha();
40+
41+
/** Commit node_id. */
42+
String filename();
43+
44+
/** Commit API URL. */
45+
@Nullable
46+
String status();
47+
48+
@Nullable
49+
Integer additions();
50+
51+
/** Author commit user. */
52+
@Nullable
53+
Integer deletions();
54+
55+
@Nullable
56+
Integer changes();
57+
58+
@Nullable
59+
@JsonProperty("blob_url")
60+
URI blobUrl();
61+
62+
@Nullable
63+
@JsonProperty("raw_url")
64+
URI rawUrl();
65+
66+
@Nullable
67+
@JsonProperty("contents_url")
68+
URI contentsUrl();
69+
70+
@Nullable
71+
String patch();
72+
73+
}
74+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.git;
22+
23+
import com.fasterxml.jackson.annotation.JsonProperty;
24+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26+
import com.spotify.github.GithubStyle;
27+
import org.immutables.value.Value;
28+
29+
import javax.annotation.Nullable;
30+
import java.net.URI;
31+
32+
33+
@Value.Immutable
34+
@GithubStyle
35+
@JsonSerialize(as = ImmutableParentItem.class)
36+
@JsonDeserialize(as = ImmutableParentItem.class)
37+
38+
public interface ParentItem {
39+
40+
@Nullable
41+
String sha();
42+
43+
@Nullable
44+
URI url();
45+
46+
@Nullable
47+
@JsonProperty("html_url")
48+
URI htmlUrl();
49+
}

0 commit comments

Comments
 (0)