55import java .util .Vector ;
66
77import org .apache .commons .lang3 .StringUtils ;
8+ import org .apache .commons .logging .Log ;
9+ import org .apache .commons .logging .LogFactory ;
810import org .eclipse .egit .github .core .Comment ;
911import org .eclipse .egit .github .core .Commit ;
1012import org .eclipse .egit .github .core .CommitFile ;
2830import org .gitlab .api .models .GitlabProjectHook ;
2931import 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