Skip to content

Commit

Permalink
feat: Add RepoUpdate endpoint (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi347 committed Mar 18, 2024
1 parent 020311e commit 2c49183
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 173 deletions.
75 changes: 45 additions & 30 deletions src/main/java/com/spotify/github/v3/clients/RepositoryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@
import com.spotify.github.v3.repos.CommitStatus;
import com.spotify.github.v3.repos.CommitWithFolderContent;
import com.spotify.github.v3.repos.Content;
import com.spotify.github.v3.repos.requests.FileCreate;
import com.spotify.github.v3.repos.requests.FileUpdate;
import com.spotify.github.v3.repos.requests.*;
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.RepositoryCreateStatus;

import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.util.Iterator;
Expand All @@ -79,7 +77,8 @@ public class RepositoryClient {
public static final String STATUS_URI_TEMPLATE = "/repos/%s/%s/statuses/%s";
private static final String COMMITS_URI_TEMPLATE = "/repos/%s/%s/commits";
private static final String COMMIT_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s";
private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s/pulls";
private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE =
"/repos/%s/%s/commits/%s/pulls";
private static final String COMMIT_STATUS_URI_TEMPLATE = "/repos/%s/%s/commits/%s/status";
private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s";
private static final String COMPARE_COMMIT_TEMPLATE = "/repos/%s/%s/compare/%s...%s";
Expand Down Expand Up @@ -160,6 +159,18 @@ public CompletableFuture<Repository> getRepository() {
return github.request(path, Repository.class);
}

/**
* Update Repository properties
* https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#update-a-repository
*
* @return repository information
*/
public CompletableFuture<Repository> updateRepository(final RepositoryUpdate repoUpdate) {
final String path = String.format(REPOSITORY_URI_TEMPLATE, owner, repo);
final String data = github.json().toJsonUnchecked(repoUpdate);
return github.patch(path, data, Repository.class);
}

/**
* List all repositories in this organization.
*
Expand Down Expand Up @@ -199,13 +210,13 @@ public CompletableFuture<Boolean> isCollaborator(final String user) {
/**
* Add a collaborator to the repo.
*
* @param user the GitHub username to add
* @param user the GitHub username to add
* @param permission the permission level for the user; one of RepositoryPermission, or a custom
* role
* role
* @return
*/
public CompletableFuture<Optional<RepositoryInvitation>> addCollaborator(final String user,
final String permission) {
public CompletableFuture<Optional<RepositoryInvitation>> addCollaborator(
final String user, final String permission) {
final String path = String.format(REPOSITORY_COLLABORATOR, owner, repo, user);
final String data = github.json().toJsonUnchecked(Map.of("permission", permission));
return github
Expand All @@ -216,12 +227,12 @@ public CompletableFuture<Optional<RepositoryInvitation>> addCollaborator(final S
// not called.
if (response.code() == NO_CONTENT) {
/*
GitHub returns a 204 when:
- an existing collaborator is added as a collaborator
- an organization member is added as an individual collaborator
- an existing team member (whose team is also a repository collaborator) is
added as a collaborator
*/
GitHub returns a 204 when:
- an existing collaborator is added as a collaborator
- an organization member is added as an individual collaborator
- an existing team member (whose team is also a repository collaborator) is
added as a collaborator
*/
return Optional.empty();
}
final RepositoryInvitation invitation =
Expand Down Expand Up @@ -284,18 +295,22 @@ public CompletableFuture<Optional<InputStream>> downloadZipball(final String ref
return downloadRepository(REPOSITORY_DOWNLOAD_ZIPBALL, Optional.of(ref));
}

private CompletableFuture<Optional<InputStream>> downloadRepository(final String path, final Optional<String> maybeRef) {
private CompletableFuture<Optional<InputStream>> downloadRepository(
final String path, final Optional<String> maybeRef) {
final var repoRef = maybeRef.orElse("");
final var repoPath = String.format(path, owner, repo, repoRef);
return github.request(repoPath).thenApply(response -> {
var body = response.body();
return github
.request(repoPath)
.thenApply(
response -> {
var body = response.body();

if (body == null) {
return Optional.empty();
}
if (body == null) {
return Optional.empty();
}

return Optional.of(body.byteStream());
});
return Optional.of(body.byteStream());
});
}

/**
Expand Down Expand Up @@ -457,7 +472,8 @@ public CompletableFuture<Content> getFileContent(final String path, final String
* @param request file creation request
* @return commit with content
*/
public CompletableFuture<CommitWithFolderContent> createFileContent(final String path, final FileCreate request) {
public CompletableFuture<CommitWithFolderContent> createFileContent(
final String path, final FileCreate request) {
final String contentPath = getContentPath(path, "");
final String requestBody = github.json().toJsonUnchecked(request);
return github.put(contentPath, requestBody, CommitWithFolderContent.class);
Expand All @@ -470,7 +486,8 @@ public CompletableFuture<CommitWithFolderContent> createFileContent(final String
* @param request file update request
* @return commit with content
*/
public CompletableFuture<CommitWithFolderContent> updateFileContent(final String path, final FileUpdate request) {
public CompletableFuture<CommitWithFolderContent> updateFileContent(
final String path, final FileUpdate request) {
final String contentPath = getContentPath(path, "");
final String requestBody = github.json().toJsonUnchecked(request);
return github.put(contentPath, requestBody, CommitWithFolderContent.class);
Expand Down Expand Up @@ -546,9 +563,8 @@ public CompletableFuture<Branch> getBranch(final String branch) {
}

/**
* List some branches in repository.
* Doesn't return more than 30 branches.
* Use {@link RepositoryClient#listAllBranches} instead to get all branches.
* List some branches in repository. Doesn't return more than 30 branches. Use {@link
* RepositoryClient#listAllBranches} instead to get all branches.
*
* @return list of 30 branches in repository
*/
Expand All @@ -557,7 +573,7 @@ public CompletableFuture<List<Branch>> listBranches() {
return github.request(path, LIST_BRANCHES);
}

/**
/**
* List all branches in repository
*
* @return list of all branches in repository
Expand All @@ -567,7 +583,6 @@ public Iterator<AsyncPage<Branch>> listAllBranches() {
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_BRANCHES));
}


/**
* Delete a comment for a given id.
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/spotify/github/v3/repos/RepositoryBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public interface RepositoryBase extends UpdateTracking {
@Nullable
URI htmlUrl();

/** Allow auto merges */
@Nullable
Boolean allowAutoMerge();

/** Allow squash merges */
@Nullable
Boolean allowSquashMerge();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*-
* -\-\-
* 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.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import java.util.Optional;
import org.immutables.value.Value;

@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableRepositoryUpdate.class)
@JsonDeserialize(as = ImmutableRepositoryUpdate.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public interface RepositoryUpdate {

/** Description */
Optional<String> description();

/** Allow auto merges */
Optional<Boolean> allowAutoMerge();

/**
* Either true to allow private forks, or false to prevent private forks.
*
* <p>Default: false
*/
Optional<Boolean> allowForking();

/** Allow squash merges */
Optional<Boolean> allowSquashMerge();

/** Allow merge commits */
Optional<Boolean> allowMergeCommit();

/** Allow rebase merges */
Optional<Boolean> allowRebaseMerge();

/**
* Either true to always allow a pull request head branch that is behind its base branch to be
* updated even if it is not required to be up to date before merging, or false otherwise.
*
* <p>Default: false
*/
Optional<Boolean> allowUpdateBranch();

/** Updates the default branch for this repository. */
Optional<String> defaultBranch();

/**
* Either true to allow automatically deleting head branches when pull requests are merged, or
* false to prevent automatic deletion.
*
* <p>Default: false
*/
Optional<Boolean> deleteBranchOnMerge();

/** Homepage URL */
Optional<String> homepage();

/** Does it have downloads */
Optional<Boolean> hasDownloads();

/** Does it have issues */
Optional<Boolean> hasIssues();

/** Does it have wiki */
Optional<Boolean> hasWiki();

/** Does it have pages */
Optional<Boolean> hasPages();

/** Does it have projects */
Optional<Boolean> hasProjects();

/**
* Whether to archive this repository. false will unarchive a previously archived repository.
*
* <p>Default: false
*/
@JsonProperty("archived")
Optional<Boolean> isArchived();

/** Is it private */
@JsonProperty("private")
Optional<Boolean> isPrivate();

/**
* Either true to make this repo available as a template repository or false to prevent it.
* Default: false
*/
Optional<Boolean> isTemplate();

/**
* The default value for a squash merge commit message:
*
* <p>PR_BODY - default to the pull request's body. COMMIT_MESSAGES - default to the branch's
* commit messages. BLANK - default to a blank commit message. Can be one of: PR_BODY,
* COMMIT_MESSAGES, BLANK
*/
Optional<String> squashMergeCommitMessage();

/**
* squash_merge_commit_title string The default value for a squash merge commit title:
*
* <p>PR_TITLE - default to the pull request's title. COMMIT_OR_PR_TITLE - default to the commit's
* title (if only one commit) or the pull request's title (when more than one commit). Can be one
* of: PR_TITLE, COMMIT_OR_PR_TITLE
*/
Optional<String> squashMergeCommitTitle();

/**
* The default value for a merge commit message.
*
* <p>PR_TITLE - default to the pull request's title. PR_BODY - default to the pull request's
* body. BLANK - default to a blank commit message.
*/
Optional<String> mergeCommitMessage();

/**
* The default value for a merge commit title.
*
* <p>PR_TITLE - default to the pull request's title. MERGE_MESSAGE - default to the classic title
* for a merge message (e.g., Merge pull request #123 from branch-name). Can be one of: PR_TITLE,
* MERGE_MESSAGE
*/
Optional<String> mergeCommitTitle();

/**
* The id of the team that will be granted access to this repository. This is only valid when
* creating a repository in an organization. Default: false
*/
Optional<Integer> teamId();

/** The visibility of the repo. Can be one of `public`, `private`, `internal` */
Optional<String> visibility();

/**
* Either true to require contributors to sign off on web-based commits, or false to not require
* contributors to sign off on web-based commits.
*
* <p>Default: false
*/
Optional<Boolean> webCommitSignoffRequired();
}
Loading

0 comments on commit 2c49183

Please sign in to comment.