Skip to content

Commit 2fcfb2f

Browse files
address review comments in supporting updating content with sha
1 parent f68a850 commit 2fcfb2f

File tree

3 files changed

+160
-13
lines changed

3 files changed

+160
-13
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.kohsuke.github;
2+
3+
public class GHContentUpdateRequest {
4+
private final String path;
5+
private final String branch;
6+
private final String sha;
7+
private final byte[] content;
8+
private final String commitMessage;
9+
10+
public static GHContentUpdateRequest.Builder getBuilder() {
11+
return new GHContentUpdateRequest.Builder();
12+
}
13+
14+
public GHContentUpdateRequest(String path, String branch, String sha, byte[] content, String commitMessage) {
15+
this.path = path;
16+
this.branch = branch;
17+
this.sha = sha;
18+
this.content = content;
19+
this.commitMessage = commitMessage;
20+
}
21+
22+
private GHContentUpdateRequest(Builder builder) {
23+
this.path = builder.path;
24+
this.branch = builder.branch;
25+
this.sha = builder.sha;
26+
this.content = builder.content;
27+
this.commitMessage = builder.commitMessage;
28+
}
29+
30+
public static Builder newGHContentUpdateRequest() {
31+
return new Builder();
32+
}
33+
34+
public String getPath() {
35+
return path;
36+
}
37+
38+
public String getBranch() {
39+
return branch;
40+
}
41+
42+
public String getSha() {
43+
return sha;
44+
}
45+
46+
public byte[] getContent() {
47+
return content;
48+
}
49+
50+
public String getCommitMessage() {
51+
return commitMessage;
52+
}
53+
54+
public static final class Builder {
55+
private String path;
56+
private String branch;
57+
private String sha;
58+
private byte[] content;
59+
private String commitMessage;
60+
61+
private Builder() {
62+
}
63+
64+
public GHContentUpdateRequest build() {
65+
return new GHContentUpdateRequest(this);
66+
}
67+
68+
public Builder path(String path) {
69+
this.path = path;
70+
return this;
71+
}
72+
73+
public Builder branch(String branch) {
74+
this.branch = branch;
75+
return this;
76+
}
77+
78+
public Builder sha(String sha) {
79+
this.sha = sha;
80+
return this;
81+
}
82+
83+
public Builder content(byte[] content) {
84+
this.content = content;
85+
return this;
86+
}
87+
public Builder content(String content) {
88+
this.content = content.getBytes();
89+
return this;
90+
}
91+
92+
public Builder commitMessage(String commitMessage) {
93+
this.commitMessage = commitMessage;
94+
return this;
95+
}
96+
}
97+
}

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,32 +1394,59 @@ public GHContent getReadme() throws IOException {
13941394
return requester.to(getApiTailUrl("readme"), GHContent.class).wrap(this);
13951395
}
13961396

1397-
public GHContentUpdateResponse createContent(String content, String commitMessage, String path,String sha1) throws IOException {
1398-
return createContent(content, commitMessage, path, null,sha1);
1397+
public GHContentUpdateResponse createContent(GHContentUpdateRequest updateRequest) throws IOException {
1398+
return createContent(updateRequest.getContent(), updateRequest.getCommitMessage(), updateRequest.getPath(), updateRequest.getBranch(), updateRequest.getSha());
13991399
}
14001400

1401-
public GHContentUpdateResponse createContent(String content, String commitMessage, String path, String branch, String sha1) throws IOException {
1401+
/**
1402+
* Use {@link GHContentUpdateRequest}.
1403+
*/
1404+
@Deprecated
1405+
public GHContentUpdateResponse createContent(String content, String commitMessage, String path) throws IOException {
1406+
return createContent(content.getBytes(), commitMessage, path, null, null);
1407+
}
1408+
1409+
/**
1410+
* Use {@link GHContentUpdateRequest}.
1411+
*/
1412+
@Deprecated
1413+
public GHContentUpdateResponse createContent(String content, String commitMessage, String path, String branch) throws IOException {
14021414
final byte[] payload;
14031415
try {
14041416
payload = content.getBytes("UTF-8");
14051417
} catch (UnsupportedEncodingException ex) {
14061418
throw (IOException) new IOException("UTF-8 encoding is not supported").initCause(ex);
14071419
}
1408-
return createContent(payload, commitMessage, path, branch,sha1);
1420+
return createContent(payload, commitMessage, path, branch, null);
14091421
}
14101422

1411-
public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path,String sha1) throws IOException {
1412-
return createContent(contentBytes, commitMessage, path, null,sha1);
1423+
/**
1424+
* Use {@link GHContentUpdateRequest}.
1425+
*/
1426+
@Deprecated
1427+
public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path) throws IOException {
1428+
return createContent(contentBytes, commitMessage, path, null, null);
14131429
}
14141430

1415-
public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch,
1416-
String sha1) throws IOException {
1431+
/**
1432+
* Use {@link GHContentUpdateRequest}.
1433+
*/
1434+
@Deprecated
1435+
public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch) throws IOException {
1436+
return createContent(contentBytes, commitMessage, path, branch, null);
1437+
}
1438+
1439+
private GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch, String sha1) throws IOException {
14171440
Requester requester = new Requester(root)
1418-
.with("path", path)
1419-
.with("sha", sha1)
1420-
.with("message", commitMessage)
1421-
.with("content", Base64.encodeBase64String(contentBytes))
1422-
.method("PUT");
1441+
.with("path", path)
1442+
.with("message", commitMessage)
1443+
.with("content", Base64.encodeBase64String(contentBytes))
1444+
.method("PUT");
1445+
1446+
if (sha1 != null) {
1447+
requester.with("sha", sha1);
1448+
}
1449+
14231450

14241451
if (branch != null) {
14251452
requester.with("branch", branch);

src/test/java/org/kohsuke/github/PullRequestTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,36 @@ public void testSquashMerge() throws Exception {
105105
String name = rnd.next();
106106
GHRef masterRef = getRepository().getRef("heads/master");
107107
GHRef branchRef = getRepository().createRef("refs/heads/" + name, masterRef.getObject().getSha());
108+
108109
getRepository().createContent(name, name, name, name);
109110
Thread.sleep(1000);
110111
GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash");
111112
Thread.sleep(1000);
112113
p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH);
113114
branchRef.delete();
114115
}
116+
@Test
117+
public void testUpdateContentSquashMerge() throws Exception {
118+
String name = rnd.next();
119+
GHRef masterRef = getRepository().getRef("heads/master");
120+
GHRef branchRef = getRepository().createRef("refs/heads/" + name, masterRef.getObject().getSha());
121+
122+
GHContentUpdateResponse response = getRepository().createContent(name, name, name, name);
123+
Thread.sleep(1000);
124+
125+
GHContentUpdateRequest updateRequest = GHContentUpdateRequest.getBuilder()
126+
.content(name + name)
127+
.path(name)
128+
.branch(name)
129+
.commitMessage(name)
130+
.sha(response.getContent().getSha())
131+
.build();
132+
getRepository().createContent(updateRequest);
133+
GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash");
134+
Thread.sleep(1000);
135+
p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH);
136+
branchRef.delete();
137+
}
115138

116139
@Test
117140
// Requires push access to the test repo to pass

0 commit comments

Comments
 (0)