Skip to content

Commit 76c0aaa

Browse files
author
Derek Wasinger
committed
added commit multiple files
1 parent 707d185 commit 76c0aaa

File tree

11 files changed

+201
-47
lines changed

11 files changed

+201
-47
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.redhat.labs.exception;
2+
3+
public class EncodingException extends RuntimeException {
4+
5+
private static final long serialVersionUID = -1311593717227266372L;
6+
7+
public EncodingException() {
8+
super();
9+
}
10+
11+
public EncodingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
12+
super(message, cause, enableSuppression, writableStackTrace);
13+
}
14+
15+
public EncodingException(String message, Throwable cause) {
16+
super(message, cause);
17+
}
18+
19+
public EncodingException(String message) {
20+
super(message);
21+
}
22+
23+
public EncodingException(Throwable cause) {
24+
super(cause);
25+
}
26+
27+
}

src/main/java/com/redhat/labs/omp/models/gitlab/Action.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.redhat.labs.omp.models.gitlab;
22

3+
import java.io.UnsupportedEncodingException;
4+
import java.nio.charset.StandardCharsets;
5+
36
import javax.json.bind.annotation.JsonbProperty;
47
import javax.validation.constraints.NotBlank;
58

9+
import com.redhat.labs.omp.utils.EncodingUtils;
10+
611
import lombok.AllArgsConstructor;
712
import lombok.Builder;
813
import lombok.Data;
@@ -16,7 +21,7 @@ public class Action {
1621

1722
@NotBlank
1823
@JsonbProperty("action")
19-
private String action;
24+
private FileAction action;
2025
@NotBlank
2126
@JsonbProperty("file_path")
2227
private String filePath;
@@ -31,4 +36,49 @@ public class Action {
3136
@JsonbProperty("execute_filemode")
3237
private Boolean executeFileMode;
3338

39+
public void encodeActionAttributes() throws UnsupportedEncodingException {
40+
41+
// encode file path
42+
if (null != filePath) {
43+
String encodedFilePath = EncodingUtils.urlEncode(this.filePath);
44+
this.filePath = encodedFilePath;
45+
}
46+
47+
// encode previous path
48+
if (null != previousPath) {
49+
String encodedFilePath = EncodingUtils.urlEncode(this.previousPath);
50+
this.previousPath = encodedFilePath;
51+
}
52+
53+
// encode contents
54+
if (null != content) {
55+
byte[] encodedContents = EncodingUtils.base64Encode(this.filePath.getBytes());
56+
this.content = new String(encodedContents, StandardCharsets.UTF_8);
57+
}
58+
59+
}
60+
61+
public void decodeActionAttributes() throws UnsupportedEncodingException {
62+
63+
// decode file path
64+
if (null != filePath) {
65+
String decodedFilePath = EncodingUtils.urlDecode(this.filePath);
66+
this.filePath = decodedFilePath;
67+
}
68+
69+
// decode previous path
70+
if (null != previousPath) {
71+
String decodedFilePath = EncodingUtils.urlDecode(this.previousPath);
72+
this.previousPath = decodedFilePath;
73+
}
74+
75+
// decode contents
76+
if (null != content) {
77+
byte[] decodedContents = EncodingUtils.base64Decode(this.content);
78+
this.content = new String(decodedContents, StandardCharsets.UTF_8);
79+
80+
}
81+
82+
}
83+
3484
}

src/main/java/com/redhat/labs/omp/models/gitlab/Commit.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import javax.json.bind.annotation.JsonbProperty;
44

5-
import com.redhat.labs.omp.models.FileAction;
6-
75
public class Commit extends File {
86

97
@JsonbProperty("action")

src/main/java/com/redhat/labs/omp/models/gitlab/CommitMultiple.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.redhat.labs.omp.models.gitlab;
22

3+
import java.io.UnsupportedEncodingException;
34
import java.util.List;
45

6+
import javax.json.bind.annotation.JsonbProperty;
7+
import javax.validation.constraints.NotBlank;
8+
import javax.validation.constraints.NotNull;
9+
10+
import com.redhat.labs.exception.EncodingException;
11+
512
import lombok.AllArgsConstructor;
613
import lombok.Builder;
714
import lombok.Data;
@@ -13,16 +20,50 @@
1320
@AllArgsConstructor
1421
public class CommitMultiple {
1522

23+
@NotNull
1624
private Integer id;
25+
@NotBlank
1726
private String branch;
27+
@NotBlank
28+
@JsonbProperty("commit_message")
1829
private String commitMessage;
30+
@JsonbProperty("start_branch")
1931
private String startBranch;
32+
@JsonbProperty("start_sha")
2033
private String startSha;
34+
@JsonbProperty("start_project")
2135
private Integer startProject;
36+
@NotNull
2237
private List<Action> actions;
38+
@JsonbProperty("author_email")
2339
private String authorEmail;
40+
@JsonbProperty("author_name")
2441
private String authorName;
2542
private Boolean stats;
2643
private Boolean force;
2744

45+
public void encodeActions() {
46+
47+
this.actions.stream().forEach(action -> {
48+
try {
49+
action.encodeActionAttributes();
50+
} catch (UnsupportedEncodingException e) {
51+
throw new EncodingException("failed to encode action attributes. " + action, e);
52+
}
53+
});
54+
55+
}
56+
57+
public void decodeActions() {
58+
59+
this.actions.stream().forEach(action -> {
60+
try {
61+
action.decodeActionAttributes();
62+
} catch (UnsupportedEncodingException e) {
63+
throw new EncodingException("failed to decode action attributes. " + action, e);
64+
}
65+
});
66+
67+
}
68+
2869
}

src/main/java/com/redhat/labs/omp/models/gitlab/File.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.redhat.labs.omp.models.gitlab;
22

3+
import java.io.UnsupportedEncodingException;
4+
import java.nio.charset.StandardCharsets;
5+
36
import javax.json.bind.annotation.JsonbProperty;
47
import javax.validation.constraints.NotBlank;
58

9+
import com.redhat.labs.omp.utils.EncodingUtils;
10+
611
import lombok.AllArgsConstructor;
712
import lombok.Builder;
813
import lombok.Data;
@@ -37,4 +42,37 @@ public class File {
3742
@JsonbProperty("author_name")
3843
private String authorName;
3944

45+
public void encodeFileAttributes() throws UnsupportedEncodingException {
46+
47+
// encode file path
48+
if (null != filePath) {
49+
String encodedFilePath = EncodingUtils.urlEncode(this.filePath);
50+
this.filePath = encodedFilePath;
51+
}
52+
53+
// encode contents
54+
if (null != content) {
55+
byte[] encodedContents = EncodingUtils.base64Encode(this.filePath.getBytes());
56+
this.content = new String(encodedContents, StandardCharsets.UTF_8);
57+
}
58+
59+
}
60+
61+
public void decodeFileAttributes() throws UnsupportedEncodingException {
62+
63+
// decode file path
64+
if (null != filePath) {
65+
String decodedFilePath = EncodingUtils.urlDecode(this.filePath);
66+
this.filePath = decodedFilePath;
67+
}
68+
69+
// decode contents
70+
if (null != content) {
71+
byte[] decodedContents = EncodingUtils.base64Decode(this.content);
72+
this.content = new String(decodedContents, StandardCharsets.UTF_8);
73+
74+
}
75+
76+
}
77+
4078
}

src/main/java/com/redhat/labs/omp/models/FileAction.java renamed to src/main/java/com/redhat/labs/omp/models/gitlab/FileAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.redhat.labs.omp.models;
1+
package com.redhat.labs.omp.models.gitlab;
22

33
public enum FileAction {
44
//refer to https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions

src/main/java/com/redhat/labs/omp/models/gitlab/request/CreateCommitFileRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.redhat.labs.omp.models.gitlab.request;
22

3-
import com.redhat.labs.omp.models.FileAction;
4-
53
import javax.json.bind.annotation.JsonbProperty;
64
import javax.json.bind.annotation.JsonbTransient;
5+
6+
import com.redhat.labs.omp.models.gitlab.FileAction;
7+
78
import java.io.Serializable;
89
import java.io.UnsupportedEncodingException;
910
import java.net.URLDecoder;

src/main/java/com/redhat/labs/omp/resources/EngagementResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.slf4j.LoggerFactory;
2424

2525
import com.redhat.labs.omp.models.CreateResidencyGroupStructure;
26-
import com.redhat.labs.omp.models.FileAction;
2726
import com.redhat.labs.omp.models.Engagement;
27+
import com.redhat.labs.omp.models.gitlab.FileAction;
2828
import com.redhat.labs.omp.models.gitlab.request.CommitMultipleFilesInRepsitoryRequest;
2929
import com.redhat.labs.omp.models.gitlab.request.CreateCommitFileRequest;
3030
import com.redhat.labs.omp.models.gitlab.response.GetMultipleFilesResponse;

src/main/java/com/redhat/labs/omp/resources/FileResource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.apache.http.HttpStatus;
1919
import org.jboss.resteasy.annotations.jaxrs.PathParam;
2020

21+
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
2122
import com.redhat.labs.omp.models.gitlab.File;
2223
import com.redhat.labs.omp.service.FileService;
2324

@@ -42,6 +43,24 @@ public Response post(@PathParam("projectId") Integer projectId, @Valid File file
4243

4344
}
4445

46+
@POST
47+
@Path("/commit/multiple")
48+
public Response postMultiple(@PathParam("projectId") Integer projectId, @Valid CommitMultiple commit) {
49+
50+
Response response = Response.serverError().build();
51+
52+
try {
53+
if(fileService.createFiles(projectId, commit)) {
54+
response = Response.status(HttpStatus.SC_CREATED).entity(commit).build();
55+
}
56+
} catch(Exception e) {
57+
// return server error
58+
}
59+
60+
return response;
61+
62+
}
63+
4564
@PUT
4665
public File put(@PathParam("projectId") Integer projectId, @Valid File file) {
4766

src/main/java/com/redhat/labs/omp/service/FileService.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import org.apache.http.HttpStatus;
1111
import org.eclipse.microprofile.rest.client.inject.RestClient;
1212

13+
import com.redhat.labs.exception.EncodingException;
1314
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
1415
import com.redhat.labs.omp.models.gitlab.File;
1516
import com.redhat.labs.omp.rest.client.GitLabService;
16-
import com.redhat.labs.omp.utils.EncodingUtils;
1717

1818
@ApplicationScoped
1919
public class FileService {
@@ -29,20 +29,20 @@ public Optional<File> createFile(Integer projectId, String filePath, File file)
2929

3030
try {
3131
// encode before sending to gitlab
32-
EncodingUtils.encodeFile(file);
32+
file.encodeFileAttributes();
3333

3434
// create new file
3535
File createdFile = gitLabService.createFile(projectId, filePath, file);
3636

3737
// decode file after creation
38-
EncodingUtils.decodeFile(createdFile);
38+
file.decodeFileAttributes();
3939

4040
if (null != createdFile) {
4141
optional = Optional.of(createdFile);
4242
}
4343

4444
} catch (UnsupportedEncodingException e) {
45-
return optional;
45+
throw new EncodingException("failed to encode or decode file attributes.", e);
4646
}
4747

4848
return optional;
@@ -52,8 +52,18 @@ public Optional<File> createFile(Integer projectId, String filePath, File file)
5252
// create multiple files
5353
public boolean createFiles(Integer projectId, CommitMultiple commit) {
5454

55-
Response response = gitLabService.commitMultipleFiles(projectId, commit);
55+
Response response = null;
5656

57+
// encode actions in commit
58+
commit.encodeActions();
59+
60+
// call gitlab api to commit
61+
response = gitLabService.commitMultipleFiles(projectId, commit);
62+
63+
// decode actions in commit
64+
commit.decodeActions();
65+
66+
// should get a 201 back if commit created
5767
if (HttpStatus.SC_CREATED == response.getStatus()) {
5868
return true;
5969
}
@@ -70,20 +80,20 @@ public Optional<File> updateFile(Integer projectId, String filePath, File file)
7080
try {
7181

7282
// encode file
73-
EncodingUtils.encodeFile(file);
83+
file.encodeFileAttributes();
7484

7585
// update file
7686
File updatedFile = gitLabService.updateFile(projectId, filePath, file);
7787

7888
// decode file
79-
EncodingUtils.decodeFile(updatedFile);
89+
file.decodeFileAttributes();
8090

8191
if (null != updatedFile) {
8292
optional = Optional.of(updatedFile);
8393
}
8494

8595
} catch (UnsupportedEncodingException e) {
86-
return optional;
96+
throw new EncodingException("failed to encode or decode file attributes.", e);
8797
}
8898

8999
return optional;
@@ -131,15 +141,13 @@ public Optional<File> getFile(Integer projectId, String filePath, String ref) {
131141
if (null != file) {
132142

133143
// decode file
134-
EncodingUtils.decodeFile(file);
135-
144+
file.decodeFileAttributes();
136145
optional = Optional.of(file);
137146

138147
}
139148

140149
} catch (UnsupportedEncodingException e) {
141-
// TODO: This should throw exception
142-
return optional;
150+
throw new EncodingException("failed to encode or decode file attributes.", e);
143151
}
144152

145153
return optional;

0 commit comments

Comments
 (0)