Skip to content

Commit be337eb

Browse files
authored
Add functionality to CommitAction to support fetching content from file. (#344)
* Added Encoding constant (#342). * Mods to use Encoding enum constant (#342). * Mods to support reading content from files when using CommitsApi.createCommit() (#342).
1 parent ebb10cb commit be337eb

File tree

7 files changed

+210
-43
lines changed

7 files changed

+210
-43
lines changed

src/main/java/org/gitlab4j/api/CommitsApi.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.gitlab4j.api.models.Comment;
1414
import org.gitlab4j.api.models.Commit;
1515
import org.gitlab4j.api.models.CommitAction;
16+
import org.gitlab4j.api.models.CommitAction.Action;
1617
import org.gitlab4j.api.models.CommitPayload;
1718
import org.gitlab4j.api.models.CommitRef;
1819
import org.gitlab4j.api.models.CommitRef.RefType;
@@ -23,6 +24,7 @@
2324

2425
/**
2526
* This class implements the client side API for the GitLab commits calls.
27+
* See <a href="https://docs.gitlab.com/ce/api/commits.html">Commits API at GitLab</a> for more information.
2628
*/
2729
public class CommitsApi extends AbstractApi {
2830

@@ -566,6 +568,23 @@ public Comment addComment(Object projectIdOrPath, String sha, String note) throw
566568
public Commit createCommit(Object projectIdOrPath, String branch, String commitMessage, String startBranch,
567569
String authorEmail, String authorName, List<CommitAction> actions) throws GitLabApiException {
568570

571+
// Validate the actions
572+
if (actions == null || actions.isEmpty()) {
573+
throw new GitLabApiException("actions cannot be null or empty.");
574+
}
575+
576+
for (CommitAction action : actions) {
577+
578+
// File content is required for create and update
579+
Action actionType = action.getAction();
580+
if (actionType == Action.CREATE || actionType == Action.UPDATE) {
581+
String content = action.getContent();
582+
if (content == null) {
583+
throw new GitLabApiException("Content cannot be null for create or update actions.");
584+
}
585+
}
586+
}
587+
569588
CommitPayload payload = new CommitPayload();
570589
payload.setBranch(branch);
571590
payload.setCommitMessage(commitMessage);
@@ -574,7 +593,8 @@ public Commit createCommit(Object projectIdOrPath, String branch, String commitM
574593
payload.setAuthorName(authorName);
575594
payload.setActions(actions);
576595

577-
Response response = post(Response.Status.CREATED, payload, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits");
596+
Response response = post(Response.Status.CREATED, payload,
597+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits");
578598
return (response.readEntity(Commit.class));
579599
}
580600
}

src/main/java/org/gitlab4j/api/Constants.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ public enum TokenType {
3939
ACCESS, OAUTH2_ACCESS, PRIVATE;
4040
}
4141

42+
/** Enum to specify encoding of file contents. */
43+
public enum Encoding {
44+
TEXT, BASE64;
45+
46+
private static JacksonJsonEnumHelper<Encoding> enumHelper = new JacksonJsonEnumHelper<>(Encoding.class);
47+
48+
@JsonCreator
49+
public static Encoding forValue(String value) {
50+
return enumHelper.forValue((value != null ? value.toLowerCase() : value));
51+
}
52+
53+
@JsonValue
54+
public String toValue() {
55+
return (enumHelper.toString(this));
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return (enumHelper.toString(this));
61+
}
62+
}
63+
4264
/** Enum to use for ordering the results of various API calls. */
4365
public enum SortOrder {
4466

src/main/java/org/gitlab4j/api/RepositoryFileApi.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/**
1818
* This class provides an entry point to all the GitLab API repository files calls.
19+
* See <a href="https://docs.gitlab.com/ce/api/repository_files.html">Repository Files API at GitLab</a> for more information.
1920
*/
2021
public class RepositoryFileApi extends AbstractApi {
2122

@@ -67,7 +68,10 @@ public RepositoryFile getFileInfo(Object projectIdOrPath, String filePath, Strin
6768
RepositoryFile file = new RepositoryFile();
6869
file.setBlobId(response.getHeaderString("X-Gitlab-Blob-Id"));
6970
file.setCommitId(response.getHeaderString("X-Gitlab-Commit-Id"));
70-
file.setEncoding(response.getHeaderString("X-Gitlab-Encoding"));
71+
72+
String encoding = response.getHeaderString("X-Gitlab-Encoding");
73+
file.setEncoding(Constants.Encoding.forValue(encoding));
74+
7175
file.setFileName(response.getHeaderString("X-Gitlab-File-Name"));
7276
file.setFilePath(response.getHeaderString("X-Gitlab-File-Path"));
7377
file.setLastCommitId(response.getHeaderString("X-Gitlab-Last-Commit-Id"));

src/main/java/org/gitlab4j/api/models/CommitAction.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package org.gitlab4j.api.models;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.gitlab4j.api.Constants.Encoding;
7+
import org.gitlab4j.api.GitLabApiException;
8+
import org.gitlab4j.api.utils.FileUtils;
39
import org.gitlab4j.api.utils.JacksonJson;
410
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
511

@@ -30,28 +36,6 @@ public String toString() {
3036
}
3137
}
3238

33-
public enum Encoding {
34-
35-
BASE64, TEXT;
36-
37-
private static JacksonJsonEnumHelper<Encoding> enumHelper = new JacksonJsonEnumHelper<>(Encoding.class);
38-
39-
@JsonCreator
40-
public static Encoding forValue(String value) {
41-
return enumHelper.forValue(value);
42-
}
43-
44-
@JsonValue
45-
public String toValue() {
46-
return (enumHelper.toString(this));
47-
}
48-
49-
@Override
50-
public String toString() {
51-
return (enumHelper.toString(this));
52-
}
53-
}
54-
5539
private Action action;
5640
private String filePath;
5741
private String previousPath;
@@ -151,6 +135,25 @@ public CommitAction withExecuteFilemode(Boolean executeFilemode) {
151135
return this;
152136
}
153137

138+
public CommitAction withFileContent(String filePath, Encoding encoding) throws GitLabApiException {
139+
File file = new File(filePath);
140+
return (withFileContent(file, filePath, encoding));
141+
}
142+
143+
public CommitAction withFileContent(File file, String filePath, Encoding encoding) throws GitLabApiException {
144+
145+
this.encoding = (encoding != null ? encoding : Encoding.TEXT);
146+
this.filePath = filePath;
147+
148+
try {
149+
content = FileUtils.getFileContentAsString(file, this.encoding);
150+
} catch (IOException e) {
151+
throw new GitLabApiException(e);
152+
}
153+
154+
return (this);
155+
}
156+
154157
@Override
155158
public String toString() {
156159
return (JacksonJson.toJsonString(this));

src/main/java/org/gitlab4j/api/models/RepositoryFile.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.util.Base64;
55

6+
import org.gitlab4j.api.Constants.Encoding;
67
import org.gitlab4j.api.utils.JacksonJson;
78

89
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -12,7 +13,7 @@ public class RepositoryFile {
1213
private String fileName; // file name only, Ex. class.rb
1314
private String filePath; // full path to file. Ex. lib/class.rb
1415
private Integer size;
15-
private String encoding;
16+
private Encoding encoding;
1617
private String content;
1718
private String ref;
1819
private String blobId;
@@ -43,11 +44,11 @@ public void setSize(Integer size) {
4344
this.size = size;
4445
}
4546

46-
public String getEncoding() {
47+
public Encoding getEncoding() {
4748
return encoding;
4849
}
4950

50-
public void setEncoding(String encoding) {
51+
public void setEncoding(Encoding encoding) {
5152
this.encoding = encoding;
5253
}
5354

@@ -104,7 +105,7 @@ public String getDecodedContentAsString() {
104105
return (null);
105106
}
106107

107-
if ("base64".equalsIgnoreCase(encoding)) {
108+
if (Encoding.BASE64.equals(encoding)) {
108109
return (new String(Base64.getDecoder().decode(content)));
109110
}
110111

@@ -124,7 +125,7 @@ public byte[] getDecodedContentAsBytes() {
124125
return (null);
125126
}
126127

127-
if ("base64".equalsIgnoreCase(encoding)) {
128+
if (encoding == Encoding.BASE64) {
128129
return (Base64.getDecoder().decode(content));
129130
}
130131

@@ -157,7 +158,7 @@ public void encodeAndSetContent(byte[] byteContent) {
157158
}
158159

159160
this.content = Base64.getEncoder().encodeToString(byteContent);
160-
encoding = "base64";
161+
encoding = Encoding.BASE64;
161162
}
162163

163164
@Override

src/main/java/org/gitlab4j/api/utils/FileUtils.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package org.gitlab4j.api.utils;
22

33
import java.io.File;
4+
import java.io.FileInputStream;
45
import java.io.IOException;
56
import java.io.Reader;
7+
import java.nio.file.Files;
8+
import java.util.Base64;
69
import java.util.Scanner;
710

811
import javax.ws.rs.core.Response;
912

13+
import org.gitlab4j.api.Constants.Encoding;
14+
1015
/**
1116
* This class provides static utility methods used throughout GitLab4J.
1217
*/
@@ -79,7 +84,7 @@ public static String readFileContents(File file) throws IOException {
7984

8085
/**
8186
* Reads the content of a Reader instance and returns it as a String.
82-
*
87+
*
8388
* @param reader the Reader instance to read the content from
8489
* @return the content of a Reader instance as a String
8590
* @throws IOException if any error occurs
@@ -95,4 +100,27 @@ public static String getReaderContentAsString(Reader reader) throws IOException
95100

96101
return (out.toString());
97102
}
103+
104+
/**
105+
* Reads the content of a File instance and returns it as a String of either text or base64 encoded text.
106+
*
107+
* @param file the File instance to read from
108+
* @param encoding whether to encode as Base64 or as Text, defaults to Text if null
109+
* @return the content of the File as a String
110+
* @throws IOException if any error occurs
111+
*/
112+
public static String getFileContentAsString(File file, Encoding encoding) throws IOException {
113+
114+
if (encoding == Encoding.BASE64) {
115+
116+
try (FileInputStream stream = new FileInputStream(file)) {
117+
byte data[] = new byte[(int) file.length()];
118+
stream.read(data);
119+
return (Base64.getEncoder().encodeToString(data));
120+
}
121+
122+
} else {
123+
return(new String (Files.readAllBytes(file.toPath())));
124+
}
125+
}
98126
}

0 commit comments

Comments
 (0)