Skip to content

Commit 4560be4

Browse files
committed
Fix names in response JSON to be lowercase with underscores instead of Camel case and improve Pull Request sync
1 parent 637d9a1 commit 4560be4

3 files changed

Lines changed: 91 additions & 25 deletions

File tree

src/main/java/com/dkaedv/glghproxy/Application.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
7+
8+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
59

610
@SpringBootApplication
711
public class Application {
812

913
public static void main(String[] args) {
1014
SpringApplication.run(Application.class, args);
1115
}
16+
17+
@Bean
18+
public Jackson2ObjectMapperBuilder jacksonBuilder() {
19+
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
20+
builder
21+
.indentOutput(true)
22+
.simpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
23+
.propertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy());
24+
25+
return builder;
26+
}
1227
}

src/main/java/com/dkaedv/glghproxy/controller/ReposController.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import java.util.List;
55
import java.util.Vector;
66

7-
import org.apache.catalina.connector.Request;
87
import org.apache.commons.logging.Log;
98
import org.apache.commons.logging.LogFactory;
109
import org.eclipse.egit.github.core.Comment;
11-
import org.eclipse.egit.github.core.Commit;
1210
import org.eclipse.egit.github.core.PullRequest;
1311
import org.eclipse.egit.github.core.RepositoryBranch;
1412
import org.eclipse.egit.github.core.RepositoryCommit;
@@ -24,6 +22,7 @@
2422
import org.gitlab.api.models.GitlabProjectHook;
2523
import org.gitlab.api.models.GitlabUser;
2624
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Value;
2726
import org.springframework.http.HttpStatus;
2827
import org.springframework.http.converter.HttpMessageNotReadableException;
2928
import org.springframework.stereotype.Controller;
@@ -40,6 +39,7 @@
4039
import com.dkaedv.glghproxy.converter.GitlabToGithubConverter;
4140
import com.dkaedv.glghproxy.githubentity.HookRequest;
4241
import com.dkaedv.glghproxy.gitlabclient.GitlabSessionProvider;
42+
import com.fasterxml.jackson.databind.ObjectMapper;
4343

4444
@Controller
4545
@RequestMapping("/api/v3/repos")
@@ -49,6 +49,12 @@ public class ReposController {
4949
@Autowired
5050
private GitlabSessionProvider gitlab;
5151

52+
@Value("${gitlabUrl}")
53+
private String gitlabUrl;
54+
55+
@Autowired
56+
ObjectMapper objectMapper;
57+
5258
@RequestMapping("/{namespace}/{repo}/branches")
5359
@ResponseBody
5460
public List<RepositoryBranch> getBranches(
@@ -108,8 +114,10 @@ public List<PullRequest> getPulls(
108114

109115
GitlabAPI api = gitlab.connect(authorization);
110116
List<GitlabMergeRequest> glmergerequests = api.getMergeRequests(namespace + "/" + repo);
111-
112-
return GitlabToGithubConverter.convertMergeRequests(glmergerequests);
117+
118+
List<PullRequest> mergeRequests = GitlabToGithubConverter.convertMergeRequests(glmergerequests, gitlabUrl, namespace, repo);
119+
//LOG.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(mergeRequests));
120+
return mergeRequests;
113121
}
114122

115123

@@ -123,13 +131,23 @@ public List<RepositoryCommit> getCommitsOnPullRequest(
123131
) throws IOException {
124132

125133
GitlabAPI api = gitlab.connect(authorization);
126-
GitlabProject project = api.getProject(namespace + "/" + repo);
127-
GitlabMergeRequest mergeRequest = api.getMergeRequest(project, id);
134+
GitlabMergeRequest mergeRequest = findMergeRequestByProjectAndIid(namespace, repo, id, api);
128135
List<GitlabCommit> commits = api.getCommits(mergeRequest);
129136

130137
return GitlabToGithubConverter.convertCommits(commits);
131138
}
132139

140+
private GitlabMergeRequest findMergeRequestByProjectAndIid(String namespace, String repo, Integer id, GitlabAPI api) throws IOException {
141+
List<GitlabMergeRequest> mergeRequests = api.getMergeRequests(namespace + "/" + repo);
142+
for (GitlabMergeRequest mergeRequest : mergeRequests) {
143+
if (mergeRequest.getIid().equals(id)) {
144+
return mergeRequest;
145+
}
146+
}
147+
148+
return null;
149+
}
150+
133151
/**
134152
* In Github, each Merge Request is automatically also an issue. Therefore we return its comments here.
135153
*/
@@ -143,8 +161,7 @@ public List<Comment> getCommentsOnPullRequest(
143161
) throws IOException {
144162

145163
GitlabAPI api = gitlab.connect(authorization);
146-
GitlabProject project = api.getProject(namespace + "/" + repo);
147-
GitlabMergeRequest mergeRequest = api.getMergeRequest(project, id);
164+
GitlabMergeRequest mergeRequest = findMergeRequestByProjectAndIid(namespace, repo, id, api);
148165
List<GitlabNote> notes = api.getNotes(mergeRequest);
149166

150167
return GitlabToGithubConverter.convertComments(notes);

src/main/java/com/dkaedv/glghproxy/converter/GitlabToGithubConverter.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Vector;
66

77
import org.apache.commons.lang3.StringUtils;
8+
import org.apache.commons.logging.Log;
9+
import org.apache.commons.logging.LogFactory;
810
import org.eclipse.egit.github.core.Comment;
911
import org.eclipse.egit.github.core.Commit;
1012
import org.eclipse.egit.github.core.CommitFile;
@@ -28,8 +30,12 @@
2830
import org.gitlab.api.models.GitlabProjectHook;
2931
import org.gitlab.api.models.GitlabUser;
3032

31-
public class GitlabToGithubConverter {
33+
import com.fasterxml.jackson.core.JsonProcessingException;
34+
import com.fasterxml.jackson.databind.ObjectMapper;
3235

36+
public class GitlabToGithubConverter {
37+
private final static Log LOG = LogFactory.getLog(GitlabToGithubConverter.class);
38+
3339
public static RepositoryBranch convertBranch(GitlabBranch glbranch) {
3440
RepositoryBranch branch = new RepositoryBranch();
3541
branch.setName(glbranch.getName());
@@ -162,17 +168,17 @@ public static List<Repository> convertRepositories(List<GitlabProject> projects)
162168
return repos;
163169
}
164170

165-
public static List<PullRequest> convertMergeRequests(List<GitlabMergeRequest> glmergerequests) {
171+
public static List<PullRequest> convertMergeRequests(List<GitlabMergeRequest> glmergerequests, String gitlabUrl, String namespace, String repo) {
166172
List<PullRequest> pulls = new Vector<PullRequest>();
167173

168174
for (GitlabMergeRequest glmr : glmergerequests) {
169-
pulls.add(convertMergeRequest(glmr));
175+
pulls.add(convertMergeRequest(glmr, gitlabUrl, namespace, repo));
170176
}
171177

172178
return pulls;
173179
}
174180

175-
private static PullRequest convertMergeRequest(GitlabMergeRequest glmr) {
181+
static PullRequest convertMergeRequest(GitlabMergeRequest glmr, String gitlabUrl, String namespace, String repo) {
176182
PullRequest pull = new PullRequest();
177183

178184
pull.setAssignee(convertUser(glmr.getAssignee()));
@@ -182,9 +188,9 @@ private static PullRequest convertMergeRequest(GitlabMergeRequest glmr) {
182188
pull.setId(glmr.getId());
183189
pull.setMilestone(convertMilestone(glmr.getMilestone()));
184190
pull.setNumber(glmr.getIid());
185-
pull.setHead(createPullRequestMarker(glmr.getSourceBranch()));
186-
pull.setBase(createPullRequestMarker(glmr.getTargetBranch()));
187-
convertMergeRequestState(pull, glmr.getState());
191+
pull.setHead(createPullRequestMarker(glmr.getSourceBranch(), namespace, repo));
192+
pull.setBase(createPullRequestMarker(glmr.getTargetBranch(), namespace, repo));
193+
convertMergeRequestState(pull, glmr);
188194
pull.setTitle(glmr.getTitle());
189195

190196
if (glmr.getUpdatedAt() != null) {
@@ -193,33 +199,51 @@ private static PullRequest convertMergeRequest(GitlabMergeRequest glmr) {
193199
pull.setUpdatedAt(glmr.getCreatedAt());
194200
}
195201

196-
if (pull.isMerged()) {
197-
pull.setMergedAt(glmr.getUpdatedAt());
198-
pull.setMergedBy(convertUser(glmr.getAssignee()));
199-
}
202+
pull.setHtmlUrl(gitlabUrl + "/" + namespace + "/" + repo + "/merge_requests/" + glmr.getIid());
203+
204+
//LOG.info("Converted merge request " + convertToJson(glmr) + " to pull request " + convertToJson(pull));
200205

201206
return pull;
202207
}
203208

204-
private static void convertMergeRequestState(PullRequest pull, String state) {
205-
if ("opened".equals(state)) {
209+
private static void convertMergeRequestState(PullRequest pull, GitlabMergeRequest glmr) {
210+
if ("opened".equals(glmr.getState())) {
206211
pull.setState("open");
207212
pull.setMerged(false);
208-
} else if ("closed".equals(state)) {
213+
} else if ("closed".equals(glmr.getState())) {
209214
pull.setState("closed");
210215
pull.setMerged(false);
211-
} else if ("merged".equals(state)) {
216+
pull.setClosedAt(glmr.getUpdatedAt());
217+
} else if ("merged".equals(glmr.getState())) {
212218
pull.setState("closed");
213219
pull.setMerged(true);
220+
pull.setClosedAt(glmr.getUpdatedAt());
221+
pull.setMergedAt(glmr.getUpdatedAt());
222+
223+
if (glmr.getAssignee() != null) {
224+
pull.setMergedBy(convertUser(glmr.getAssignee()));
225+
} else {
226+
pull.setMergedBy(convertUser(glmr.getAuthor()));
227+
}
214228
} else {
215-
throw new RuntimeException("Unknown MR state: " + state);
229+
throw new RuntimeException("Unknown MR state: " + glmr.getState());
216230
}
217231
}
218232

219-
private static PullRequestMarker createPullRequestMarker(String branch) {
233+
private static PullRequestMarker createPullRequestMarker(String branch, String namespace, String reponame) {
220234
PullRequestMarker marker = new PullRequestMarker();
221235
marker.setLabel(branch);
222236
marker.setRef(branch);
237+
238+
239+
Repository repo = new Repository();
240+
repo.setName(reponame);
241+
User owner = new User();
242+
owner.setLogin(namespace);
243+
repo.setOwner(owner);
244+
245+
marker.setRepo(repo);
246+
223247
return marker;
224248
}
225249

@@ -251,6 +275,8 @@ public static User convertUser(GitlabUser gluser) {
251275
user.setBio(gluser.getBio());
252276
user.setEmail(gluser.getEmail());
253277
user.setName(gluser.getName());
278+
user.setCreatedAt(gluser.getCreatedAt());
279+
user.setType(User.TYPE_USER);
254280

255281
return user;
256282
}
@@ -310,4 +336,12 @@ public static RepositoryHook convertHook(GitlabProjectHook glhook) {
310336

311337
return hook;
312338
}
339+
340+
private static String convertToJson(Object o) {
341+
try {
342+
return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(o);
343+
} catch (JsonProcessingException e) {
344+
throw new RuntimeException(e);
345+
}
346+
}
313347
}

0 commit comments

Comments
 (0)