3333import org .gitlab .api .models .GitlabProject ;
3434import org .gitlab .api .models .GitlabProjectHook ;
3535import org .gitlab .api .models .GitlabUser ;
36+ import org .springframework .beans .factory .annotation .Value ;
37+ import org .springframework .core .env .Environment ;
3638
3739import com .dkaedv .glghproxy .Application ;
40+ import com .dkaedv .glghproxy .Constants ;
3841import com .fasterxml .jackson .core .JsonProcessingException ;
3942
4043public class GitlabToGithubConverter {
@@ -64,7 +67,7 @@ public static List<RepositoryBranch> convertBranches(List<GitlabBranch> glbranch
6467 }
6568
6669 public static RepositoryCommit convertCommit (GitlabCommit glcommit , List <GitlabCommitDiff > gldiffs ,
67- GitlabUser gluser ) {
70+ GitlabUser gluser , Environment env ) {
6871 RepositoryCommit repoCommit = new RepositoryCommit ();
6972
7073 repoCommit .setSha (glcommit .getId ());
@@ -81,9 +84,14 @@ public static RepositoryCommit convertCommit(GitlabCommit glcommit, List<GitlabC
8184
8285 repoCommit .setCommit (commit );
8386
84- User user = new User ();
85- user .setEmail (glcommit .getAuthorEmail ());
86- user .setLogin (gluser != null ? gluser .getUsername () : null );
87+ User user = null ;
88+ if (gluser == null ) {
89+ user = new User ();
90+ user .setEmail (glcommit .getAuthorEmail ());
91+ user .setAvatarUrl (env .getProperty (Constants .KEY_FALLBACK_AVATAR_URL ));
92+ } else {
93+ user = convertUser (gluser , env );
94+ }
8795 repoCommit .setAuthor (user );
8896 repoCommit .setCommitter (user );
8997
@@ -200,30 +208,30 @@ public static List<Repository> convertRepositories(List<GitlabProject> projects,
200208 }
201209
202210 public static List <PullRequest > convertMergeRequests (List <GitlabMergeRequest > glmergerequests , String gitlabUrl ,
203- String namespace , String repo ) {
211+ String namespace , String repo , Environment env ) {
204212 List <PullRequest > pulls = new ArrayList <>(glmergerequests .size ());
205213
206214 for (GitlabMergeRequest glmr : glmergerequests ) {
207- pulls .add (convertMergeRequest (glmr , gitlabUrl , namespace , repo ));
215+ pulls .add (convertMergeRequest (glmr , gitlabUrl , namespace , repo , env ));
208216 }
209217
210218 return pulls ;
211219 }
212220
213221 public static PullRequest convertMergeRequest (GitlabMergeRequest glmr , String gitlabUrl , String namespace ,
214- String repo ) {
222+ String repo , Environment env ) {
215223 PullRequest pull = new PullRequest ();
216224
217- pull .setAssignee (convertUser (glmr .getAssignee ()));
218- pull .setUser (convertUser (glmr .getAuthor ()));
225+ pull .setAssignee (convertUser (glmr .getAssignee (), env ));
226+ pull .setUser (convertUser (glmr .getAuthor (), env ));
219227 pull .setCreatedAt (glmr .getCreatedAt ());
220228 pull .setBody (glmr .getDescription ());
221229 pull .setId (glmr .getId ());
222230 pull .setMilestone (convertMilestone (glmr .getMilestone ()));
223231 pull .setNumber (glmr .getIid ());
224232 pull .setHead (createPullRequestMarker (glmr .getSourceBranch (), namespace , repo ));
225233 pull .setBase (createPullRequestMarker (glmr .getTargetBranch (), namespace , repo ));
226- convertMergeRequestState (pull , glmr );
234+ convertMergeRequestState (pull , glmr , env );
227235 pull .setTitle (glmr .getTitle ());
228236
229237 if (glmr .getUpdatedAt () != null ) {
@@ -242,7 +250,7 @@ public static PullRequest convertMergeRequest(GitlabMergeRequest glmr, String gi
242250 return pull ;
243251 }
244252
245- private static void convertMergeRequestState (PullRequest pull , GitlabMergeRequest glmr ) {
253+ private static void convertMergeRequestState (PullRequest pull , GitlabMergeRequest glmr , Environment env ) {
246254 if ("can_be_merged" .equals (glmr .getMergeStatus ())) {
247255 pull .setMergeable (true );
248256 }
@@ -261,9 +269,9 @@ private static void convertMergeRequestState(PullRequest pull, GitlabMergeReques
261269 pull .setMergedAt (glmr .getUpdatedAt ());
262270
263271 if (glmr .getAssignee () != null ) {
264- pull .setMergedBy (convertUser (glmr .getAssignee ()));
272+ pull .setMergedBy (convertUser (glmr .getAssignee (), env ));
265273 } else {
266- pull .setMergedBy (convertUser (glmr .getAuthor ()));
274+ pull .setMergedBy (convertUser (glmr .getAuthor (), env ));
267275 }
268276 } else {
269277 throw new RuntimeException ("Unknown MR state: " + glmr .getState ());
@@ -302,7 +310,7 @@ private static Milestone convertMilestone(GitlabMilestone glmilestone) {
302310 return milestone ;
303311 }
304312
305- public static User convertUser (GitlabUser gluser ) {
313+ public static User convertUser (GitlabUser gluser , Environment env ) {
306314 if (gluser == null ) {
307315 return null ;
308316 }
@@ -311,9 +319,12 @@ public static User convertUser(GitlabUser gluser) {
311319 user .setId (gluser .getId ());
312320 user .setLogin (gluser .getUsername ());
313321 String avatarUrl = gluser .getAvatarUrl ();
314- if (avatarUrl != null && avatarUrl . length () > 0 ) {
322+ if (avatarUrl != null ) {
315323 user .setAvatarUrl (avatarUrl );
316324 }
325+ if (user .getAvatarUrl () == null || user .getAvatarUrl ().length () == 0 ) {
326+ user .setAvatarUrl (env .getProperty (Constants .KEY_FALLBACK_AVATAR_URL ));
327+ }
317328 user .setBio (gluser .getBio ());
318329 user .setEmail (gluser .getEmail ());
319330 user .setName (gluser .getName ());
@@ -323,30 +334,30 @@ public static User convertUser(GitlabUser gluser) {
323334 return user ;
324335 }
325336
326- public static List <RepositoryCommit > convertCommits (List <GitlabCommit > glcommits ) {
337+ public static List <RepositoryCommit > convertCommits (List <GitlabCommit > glcommits , Environment env ) {
327338 List <RepositoryCommit > commits = new ArrayList <>(glcommits .size ());
328339
329340 for (GitlabCommit glcommit : glcommits ) {
330- commits .add (convertCommit (glcommit , null , null ));
341+ commits .add (convertCommit (glcommit , null , null , env ));
331342 }
332343
333344 return commits ;
334345 }
335346
336- public static List <Comment > convertComments (List <GitlabNote > glnotes ) {
347+ public static List <Comment > convertComments (List <GitlabNote > glnotes , Environment env ) {
337348 List <Comment > comments = new ArrayList <>(glnotes .size ());
338349
339350 for (GitlabNote glnote : glnotes ) {
340- comments .add (convertComment (glnote ));
351+ comments .add (convertComment (glnote , env ));
341352 }
342353
343354 return comments ;
344355 }
345356
346- private static Comment convertComment (GitlabNote glnote ) {
357+ private static Comment convertComment (GitlabNote glnote , Environment env ) {
347358 Comment comment = new Comment ();
348359
349- comment .setUser (convertUser (glnote .getAuthor ()));
360+ comment .setUser (convertUser (glnote .getAuthor (), env ));
350361 comment .setBody (glnote .getBody ());
351362 comment .setCreatedAt (glnote .getCreatedAt ());
352363 comment .setId (glnote .getId ());
@@ -388,25 +399,25 @@ private static String convertToJson(Object o) {
388399 }
389400
390401 public static List <Event > convertMergeRequestsToEvents (List <GitlabMergeRequest > glmergerequests , String gitlabUrl ,
391- String namespace , String repo ) {
402+ String namespace , String repo , Environment env ) {
392403 List <Event > events = new ArrayList <>(glmergerequests .size ());
393404
394405 for (GitlabMergeRequest glmergerequest : glmergerequests ) {
395- events .add (convertMergeRequestToEvent (glmergerequest , gitlabUrl , namespace , repo ));
406+ events .add (convertMergeRequestToEvent (glmergerequest , gitlabUrl , namespace , repo , env ));
396407 }
397408
398409 return events ;
399410 }
400411
401412 public static Event convertMergeRequestToEvent (GitlabMergeRequest glmergerequest , String gitlabUrl ,
402- String namespace , String repo ) {
413+ String namespace , String repo , Environment env ) {
403414 Event event = new Event ();
404415
405416 event .setType (Event .TYPE_PULL_REQUEST );
406417 event .setCreatedAt (glmergerequest .getUpdatedAt ());
407418
408419 PullRequestPayload payload = new PullRequestPayload ();
409- payload .setPullRequest (convertMergeRequest (glmergerequest , gitlabUrl , namespace , repo ));
420+ payload .setPullRequest (convertMergeRequest (glmergerequest , gitlabUrl , namespace , repo , env ));
410421 payload .setNumber (payload .getPullRequest ().getNumber ());
411422
412423 event .setPayload (payload );
0 commit comments