Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create and update file to RepositoryClient #119

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/RepositoryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
import com.spotify.github.v3.repos.CommitItem;
import com.spotify.github.v3.repos.CommitStatus;
import com.spotify.github.v3.repos.Content;
import com.spotify.github.v3.repos.FileCommit;
import com.spotify.github.v3.repos.FolderContent;
import com.spotify.github.v3.repos.Languages;
import com.spotify.github.v3.repos.Repository;
import com.spotify.github.v3.repos.RepositoryInvitation;
import com.spotify.github.v3.repos.Status;
import com.spotify.github.v3.repos.requests.AuthenticatedUserRepositoriesFilter;
import com.spotify.github.v3.repos.requests.FileCreate;
import com.spotify.github.v3.repos.requests.FileUpdate;
import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
import java.lang.invoke.MethodHandles;
import java.util.Iterator;
Expand Down Expand Up @@ -394,6 +397,30 @@ public CompletableFuture<Content> getFileContent(final String path, final String
return github.request(getContentPath(path, "?ref=" + ref), Content.class);
}

/**
* Update file in the repository.
*
* @param path the path
* @param fileUpdate the file update
* @return the completable future
*/
public CompletableFuture<FileCommit> updateFile(final String path, final FileUpdate fileUpdate) {
final String requestBody = github.json().toJsonUnchecked(fileUpdate);
return github.put(getContentPath(path, ""), requestBody, FileCommit.class);
}

/**
* Create file in the repository.
*
* @param path the path
* @param fileCreate the file create
* @return the completable future
*/
public CompletableFuture<FileCommit> createFile(final String path, final FileCreate fileCreate) {
final String requestBody = github.json().toJsonUnchecked(fileCreate);
return github.put(getContentPath(path, ""), requestBody, FileCommit.class);
}

/**
* Get repository contents of a folder.
*
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/spotify/github/v3/repos/FileCommit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.repos;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import com.spotify.github.v3.git.Commit;
import org.immutables.value.Value;


@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableFileCommit.class)
@JsonDeserialize(as = ImmutableFileCommit.class)
public interface FileCommit {

Content content();

Commit commit();
}
55 changes: 55 additions & 0 deletions src/main/java/com/spotify/github/v3/repos/requests/FileCreate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/


package com.spotify.github.v3.repos.requests;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import com.spotify.github.v3.git.Author;
import org.immutables.value.Value;

import javax.annotation.Nullable;

@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableFileCreate.class)
@JsonDeserialize(as = ImmutableFileCreate.class)
public interface FileCreate {

/** The commit message. */
String message();

/** Committer commit user. */
@Nullable
Author committer();

/** The author of the file. Default: The committer or the authenticated user if you omit. */
@Nullable
Author author();

/** The branch name. Default: the repository’s default branch (usually master) */
@Nullable
String branch();

/** The new file content, using Base64 encoding.. */
String content();
}
58 changes: 58 additions & 0 deletions src/main/java/com/spotify/github/v3/repos/requests/FileUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/


package com.spotify.github.v3.repos.requests;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import com.spotify.github.v3.git.Author;
import org.immutables.value.Value;

import javax.annotation.Nullable;

@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableFileUpdate.class)
@JsonDeserialize(as = ImmutableFileUpdate.class)
public interface FileUpdate {

/** The commit message. */
String message();

/** Committer commit user. */
@Nullable
Author committer();

/** The author of the file. Default: The committer or the authenticated user if you omit. */
@Nullable
Author author();

/** The branch name. Default: the repository’s default branch (usually master) */
@Nullable
String branch();

/** The new file content, using Base64 encoding. */
String content();

/** The blob SHA of the file being replaced. */
String sha();
}
79 changes: 79 additions & 0 deletions src/test/java/com/spotify/github/v3/repos/FileCreateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*-
* -\-\-
* github-client
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.repos;

import com.google.common.io.Resources;
import com.spotify.github.GitHubInstant;
import com.spotify.github.jackson.Json;
import com.spotify.github.v3.git.ImmutableShaLink;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.time.Instant;

import static com.google.common.io.Resources.getResource;
import static java.nio.charset.Charset.defaultCharset;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;


public class FileCreateTest {

private String fixture;

@Before
public void setUp() throws Exception {
fixture =
Resources.toString(getResource(this.getClass(), "file_create.json"), defaultCharset());
}

@Test
public void testDeserialization() throws IOException {
final FileCommit fileCommit = Json.create().fromJson(fixture, FileCommit.class);
assertThat(fileCommit.content(), not(nullValue()));
assertThat(fileCommit.content().name(), is("hello.txt"));
assertThat(fileCommit.content().path(), is("notes/hello.txt"));
assertThat(fileCommit.content().sha(), is("a56507ed892d05a37c6d6128c260937ea4d287bd"));
assertThat(fileCommit.content().size(), is(9));
assertThat(fileCommit.content().url(), is(URI.create("https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt")));
assertThat(fileCommit.content().htmlUrl(), is(URI.create("https://github.com/octocat/Hello-World/blob/master/notes/hello.txt")));
assertThat(fileCommit.content().gitUrl(), is(URI.create("https://api.github.com/repos/octocat/Hello-World/git/blobs/a56507ed892d05a37c6d6128c260937ea4d287bd")));
assertThat(fileCommit.content().downloadUrl(), is(URI.create("https://raw.githubusercontent.com/octocat/HelloWorld/master/notes/hello.txt")));
assertThat(fileCommit.content().type(), is("file"));
assertThat(fileCommit.commit(), not(nullValue()));

assertThat(fileCommit.commit().sha().isPresent(), is(true));
assertThat(fileCommit.commit().sha().get(), is("18a43cd8e1e3a79c786e3d808a73d23b6d212b16"));
assertThat(fileCommit.commit().url(), is(URI.create("https://api.github.com/repos/octocat/Hello-World/git/commits/18a43cd8e1e3a79c786e3d808a73d23b6d212b16")));
assertThat(fileCommit.commit().author(), not(nullValue()));
assertThat(fileCommit.commit().author().date().isPresent(), is(true));
assertThat(fileCommit.commit().author().date().get().instant(), is(GitHubInstant.create(Instant.parse("2014-11-07T22:01:45Z")).instant()));
assertThat(fileCommit.commit().author().name(), is("Monalisa Octocat"));
assertThat(fileCommit.commit().author().email().isPresent(), is(true));
assertThat(fileCommit.commit().author().email().get(), is("[email protected]"));
assertThat(fileCommit.commit().message(), is("my commit message"));
assertThat(fileCommit.commit().tree(), is(ImmutableShaLink.builder().url(URI.create("https://api.github.com/repos/octocat/Hello-World/git/trees/9a21f8e2018f42ffcf369b24d2cd20bc25c9e66f")).sha("9a21f8e2018f42ffcf369b24d2cd20bc25c9e66f").build()));
}
}
79 changes: 79 additions & 0 deletions src/test/java/com/spotify/github/v3/repos/FileUpdateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*-
* -\-\-
* github-client
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.repos;

import com.google.common.io.Resources;
import com.spotify.github.GitHubInstant;
import com.spotify.github.jackson.Json;
import com.spotify.github.v3.git.ImmutableShaLink;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.time.Instant;

import static com.google.common.io.Resources.getResource;
import static java.nio.charset.Charset.defaultCharset;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;


public class FileUpdateTest {

private String fixture;

@Before
public void setUp() throws Exception {
fixture =
Resources.toString(getResource(this.getClass(), "file_update.json"), defaultCharset());
}

@Test
public void testDeserialization() throws IOException {
final FileCommit fileCommit = Json.create().fromJson(fixture, FileCommit.class);
assertThat(fileCommit.content(), not(nullValue()));
assertThat(fileCommit.content().name(), is("hello.txt"));
assertThat(fileCommit.content().path(), is("notes/hello.txt"));
assertThat(fileCommit.content().sha(), is("95b966ae1c166bd92f8ae7d1c313e738c731dfc3"));
assertThat(fileCommit.content().size(), is(9));
assertThat(fileCommit.content().url(), is(URI.create("https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt")));
assertThat(fileCommit.content().htmlUrl(), is(URI.create("https://github.com/octocat/Hello-World/blob/master/notes/hello.txt")));
assertThat(fileCommit.content().gitUrl(), is(URI.create("https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3")));
assertThat(fileCommit.content().downloadUrl(), is(URI.create("https://raw.githubusercontent.com/octocat/HelloWorld/master/notes/hello.txt")));
assertThat(fileCommit.content().type(), is("file"));
assertThat(fileCommit.commit(), not(nullValue()));

assertThat(fileCommit.commit().sha().isPresent(), is(true));
assertThat(fileCommit.commit().sha().get(), is("7638417db6d59f3c431d3e1f261cc637155684cd"));
assertThat(fileCommit.commit().url(), is(URI.create("https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd")));
assertThat(fileCommit.commit().author(), not(nullValue()));
assertThat(fileCommit.commit().author().date().isPresent(), is(true));
assertThat(fileCommit.commit().author().date().get().instant(), is(GitHubInstant.create(Instant.parse("2014-11-07T22:01:45Z")).instant()));
assertThat(fileCommit.commit().author().name(), is("Monalisa Octocat"));
assertThat(fileCommit.commit().author().email().isPresent(), is(true));
assertThat(fileCommit.commit().author().email().get(), is("[email protected]"));
assertThat(fileCommit.commit().message(), is("my commit message"));
assertThat(fileCommit.commit().tree(), is(ImmutableShaLink.builder().url(URI.create("https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb")).sha("691272480426f78a0138979dd3ce63b77f706feb").build()));
}
}
Loading