Skip to content

Commit 2c49183

Browse files
authored
feat: Add RepoUpdate endpoint (#181)
1 parent 020311e commit 2c49183

File tree

4 files changed

+407
-173
lines changed

4 files changed

+407
-173
lines changed

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

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@
4444
import com.spotify.github.v3.repos.CommitStatus;
4545
import com.spotify.github.v3.repos.CommitWithFolderContent;
4646
import com.spotify.github.v3.repos.Content;
47-
import com.spotify.github.v3.repos.requests.FileCreate;
48-
import com.spotify.github.v3.repos.requests.FileUpdate;
47+
import com.spotify.github.v3.repos.requests.*;
4948
import com.spotify.github.v3.repos.FolderContent;
5049
import com.spotify.github.v3.repos.Languages;
5150
import com.spotify.github.v3.repos.Repository;
5251
import com.spotify.github.v3.repos.RepositoryInvitation;
5352
import com.spotify.github.v3.repos.Status;
54-
import com.spotify.github.v3.repos.requests.AuthenticatedUserRepositoriesFilter;
55-
import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
53+
5654
import java.io.InputStream;
5755
import java.lang.invoke.MethodHandles;
5856
import java.util.Iterator;
@@ -79,7 +77,8 @@ public class RepositoryClient {
7977
public static final String STATUS_URI_TEMPLATE = "/repos/%s/%s/statuses/%s";
8078
private static final String COMMITS_URI_TEMPLATE = "/repos/%s/%s/commits";
8179
private static final String COMMIT_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s";
82-
private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s/pulls";
80+
private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE =
81+
"/repos/%s/%s/commits/%s/pulls";
8382
private static final String COMMIT_STATUS_URI_TEMPLATE = "/repos/%s/%s/commits/%s/status";
8483
private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s";
8584
private static final String COMPARE_COMMIT_TEMPLATE = "/repos/%s/%s/compare/%s...%s";
@@ -160,6 +159,18 @@ public CompletableFuture<Repository> getRepository() {
160159
return github.request(path, Repository.class);
161160
}
162161

162+
/**
163+
* Update Repository properties
164+
* https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#update-a-repository
165+
*
166+
* @return repository information
167+
*/
168+
public CompletableFuture<Repository> updateRepository(final RepositoryUpdate repoUpdate) {
169+
final String path = String.format(REPOSITORY_URI_TEMPLATE, owner, repo);
170+
final String data = github.json().toJsonUnchecked(repoUpdate);
171+
return github.patch(path, data, Repository.class);
172+
}
173+
163174
/**
164175
* List all repositories in this organization.
165176
*
@@ -199,13 +210,13 @@ public CompletableFuture<Boolean> isCollaborator(final String user) {
199210
/**
200211
* Add a collaborator to the repo.
201212
*
202-
* @param user the GitHub username to add
213+
* @param user the GitHub username to add
203214
* @param permission the permission level for the user; one of RepositoryPermission, or a custom
204-
* role
215+
* role
205216
* @return
206217
*/
207-
public CompletableFuture<Optional<RepositoryInvitation>> addCollaborator(final String user,
208-
final String permission) {
218+
public CompletableFuture<Optional<RepositoryInvitation>> addCollaborator(
219+
final String user, final String permission) {
209220
final String path = String.format(REPOSITORY_COLLABORATOR, owner, repo, user);
210221
final String data = github.json().toJsonUnchecked(Map.of("permission", permission));
211222
return github
@@ -216,12 +227,12 @@ public CompletableFuture<Optional<RepositoryInvitation>> addCollaborator(final S
216227
// not called.
217228
if (response.code() == NO_CONTENT) {
218229
/*
219-
GitHub returns a 204 when:
220-
- an existing collaborator is added as a collaborator
221-
- an organization member is added as an individual collaborator
222-
- an existing team member (whose team is also a repository collaborator) is
223-
added as a collaborator
224-
*/
230+
GitHub returns a 204 when:
231+
- an existing collaborator is added as a collaborator
232+
- an organization member is added as an individual collaborator
233+
- an existing team member (whose team is also a repository collaborator) is
234+
added as a collaborator
235+
*/
225236
return Optional.empty();
226237
}
227238
final RepositoryInvitation invitation =
@@ -284,18 +295,22 @@ public CompletableFuture<Optional<InputStream>> downloadZipball(final String ref
284295
return downloadRepository(REPOSITORY_DOWNLOAD_ZIPBALL, Optional.of(ref));
285296
}
286297

287-
private CompletableFuture<Optional<InputStream>> downloadRepository(final String path, final Optional<String> maybeRef) {
298+
private CompletableFuture<Optional<InputStream>> downloadRepository(
299+
final String path, final Optional<String> maybeRef) {
288300
final var repoRef = maybeRef.orElse("");
289301
final var repoPath = String.format(path, owner, repo, repoRef);
290-
return github.request(repoPath).thenApply(response -> {
291-
var body = response.body();
302+
return github
303+
.request(repoPath)
304+
.thenApply(
305+
response -> {
306+
var body = response.body();
292307

293-
if (body == null) {
294-
return Optional.empty();
295-
}
308+
if (body == null) {
309+
return Optional.empty();
310+
}
296311

297-
return Optional.of(body.byteStream());
298-
});
312+
return Optional.of(body.byteStream());
313+
});
299314
}
300315

301316
/**
@@ -457,7 +472,8 @@ public CompletableFuture<Content> getFileContent(final String path, final String
457472
* @param request file creation request
458473
* @return commit with content
459474
*/
460-
public CompletableFuture<CommitWithFolderContent> createFileContent(final String path, final FileCreate request) {
475+
public CompletableFuture<CommitWithFolderContent> createFileContent(
476+
final String path, final FileCreate request) {
461477
final String contentPath = getContentPath(path, "");
462478
final String requestBody = github.json().toJsonUnchecked(request);
463479
return github.put(contentPath, requestBody, CommitWithFolderContent.class);
@@ -470,7 +486,8 @@ public CompletableFuture<CommitWithFolderContent> createFileContent(final String
470486
* @param request file update request
471487
* @return commit with content
472488
*/
473-
public CompletableFuture<CommitWithFolderContent> updateFileContent(final String path, final FileUpdate request) {
489+
public CompletableFuture<CommitWithFolderContent> updateFileContent(
490+
final String path, final FileUpdate request) {
474491
final String contentPath = getContentPath(path, "");
475492
final String requestBody = github.json().toJsonUnchecked(request);
476493
return github.put(contentPath, requestBody, CommitWithFolderContent.class);
@@ -546,9 +563,8 @@ public CompletableFuture<Branch> getBranch(final String branch) {
546563
}
547564

548565
/**
549-
* List some branches in repository.
550-
* Doesn't return more than 30 branches.
551-
* Use {@link RepositoryClient#listAllBranches} instead to get all branches.
566+
* List some branches in repository. Doesn't return more than 30 branches. Use {@link
567+
* RepositoryClient#listAllBranches} instead to get all branches.
552568
*
553569
* @return list of 30 branches in repository
554570
*/
@@ -557,7 +573,7 @@ public CompletableFuture<List<Branch>> listBranches() {
557573
return github.request(path, LIST_BRANCHES);
558574
}
559575

560-
/**
576+
/**
561577
* List all branches in repository
562578
*
563579
* @return list of all branches in repository
@@ -567,7 +583,6 @@ public Iterator<AsyncPage<Branch>> listAllBranches() {
567583
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_BRANCHES));
568584
}
569585

570-
571586
/**
572587
* Delete a comment for a given id.
573588
*

src/main/java/com/spotify/github/v3/repos/RepositoryBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public interface RepositoryBase extends UpdateTracking {
7979
@Nullable
8080
URI htmlUrl();
8181

82+
/** Allow auto merges */
83+
@Nullable
84+
Boolean allowAutoMerge();
85+
8286
/** Allow squash merges */
8387
@Nullable
8488
Boolean allowSquashMerge();
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 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.repos.requests;
22+
23+
import com.fasterxml.jackson.annotation.JsonInclude;
24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
26+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
27+
import com.spotify.github.GithubStyle;
28+
import java.util.Optional;
29+
import org.immutables.value.Value;
30+
31+
@Value.Immutable
32+
@GithubStyle
33+
@JsonSerialize(as = ImmutableRepositoryUpdate.class)
34+
@JsonDeserialize(as = ImmutableRepositoryUpdate.class)
35+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
36+
public interface RepositoryUpdate {
37+
38+
/** Description */
39+
Optional<String> description();
40+
41+
/** Allow auto merges */
42+
Optional<Boolean> allowAutoMerge();
43+
44+
/**
45+
* Either true to allow private forks, or false to prevent private forks.
46+
*
47+
* <p>Default: false
48+
*/
49+
Optional<Boolean> allowForking();
50+
51+
/** Allow squash merges */
52+
Optional<Boolean> allowSquashMerge();
53+
54+
/** Allow merge commits */
55+
Optional<Boolean> allowMergeCommit();
56+
57+
/** Allow rebase merges */
58+
Optional<Boolean> allowRebaseMerge();
59+
60+
/**
61+
* Either true to always allow a pull request head branch that is behind its base branch to be
62+
* updated even if it is not required to be up to date before merging, or false otherwise.
63+
*
64+
* <p>Default: false
65+
*/
66+
Optional<Boolean> allowUpdateBranch();
67+
68+
/** Updates the default branch for this repository. */
69+
Optional<String> defaultBranch();
70+
71+
/**
72+
* Either true to allow automatically deleting head branches when pull requests are merged, or
73+
* false to prevent automatic deletion.
74+
*
75+
* <p>Default: false
76+
*/
77+
Optional<Boolean> deleteBranchOnMerge();
78+
79+
/** Homepage URL */
80+
Optional<String> homepage();
81+
82+
/** Does it have downloads */
83+
Optional<Boolean> hasDownloads();
84+
85+
/** Does it have issues */
86+
Optional<Boolean> hasIssues();
87+
88+
/** Does it have wiki */
89+
Optional<Boolean> hasWiki();
90+
91+
/** Does it have pages */
92+
Optional<Boolean> hasPages();
93+
94+
/** Does it have projects */
95+
Optional<Boolean> hasProjects();
96+
97+
/**
98+
* Whether to archive this repository. false will unarchive a previously archived repository.
99+
*
100+
* <p>Default: false
101+
*/
102+
@JsonProperty("archived")
103+
Optional<Boolean> isArchived();
104+
105+
/** Is it private */
106+
@JsonProperty("private")
107+
Optional<Boolean> isPrivate();
108+
109+
/**
110+
* Either true to make this repo available as a template repository or false to prevent it.
111+
* Default: false
112+
*/
113+
Optional<Boolean> isTemplate();
114+
115+
/**
116+
* The default value for a squash merge commit message:
117+
*
118+
* <p>PR_BODY - default to the pull request's body. COMMIT_MESSAGES - default to the branch's
119+
* commit messages. BLANK - default to a blank commit message. Can be one of: PR_BODY,
120+
* COMMIT_MESSAGES, BLANK
121+
*/
122+
Optional<String> squashMergeCommitMessage();
123+
124+
/**
125+
* squash_merge_commit_title string The default value for a squash merge commit title:
126+
*
127+
* <p>PR_TITLE - default to the pull request's title. COMMIT_OR_PR_TITLE - default to the commit's
128+
* title (if only one commit) or the pull request's title (when more than one commit). Can be one
129+
* of: PR_TITLE, COMMIT_OR_PR_TITLE
130+
*/
131+
Optional<String> squashMergeCommitTitle();
132+
133+
/**
134+
* The default value for a merge commit message.
135+
*
136+
* <p>PR_TITLE - default to the pull request's title. PR_BODY - default to the pull request's
137+
* body. BLANK - default to a blank commit message.
138+
*/
139+
Optional<String> mergeCommitMessage();
140+
141+
/**
142+
* The default value for a merge commit title.
143+
*
144+
* <p>PR_TITLE - default to the pull request's title. MERGE_MESSAGE - default to the classic title
145+
* for a merge message (e.g., Merge pull request #123 from branch-name). Can be one of: PR_TITLE,
146+
* MERGE_MESSAGE
147+
*/
148+
Optional<String> mergeCommitTitle();
149+
150+
/**
151+
* The id of the team that will be granted access to this repository. This is only valid when
152+
* creating a repository in an organization. Default: false
153+
*/
154+
Optional<Integer> teamId();
155+
156+
/** The visibility of the repo. Can be one of `public`, `private`, `internal` */
157+
Optional<String> visibility();
158+
159+
/**
160+
* Either true to require contributors to sign off on web-based commits, or false to not require
161+
* contributors to sign off on web-based commits.
162+
*
163+
* <p>Default: false
164+
*/
165+
Optional<Boolean> webCommitSignoffRequired();
166+
}

0 commit comments

Comments
 (0)