From 0ae00e608a16b358ef9a98fbffe03845ae8b9423 Mon Sep 17 00:00:00 2001 From: Zhanpeng Chen Date: Mon, 6 Jul 2020 16:27:42 -0400 Subject: [PATCH] Change authorType from enum to string (#152) * Change authorType from enum to string * Increment version --- pom.xml | 4 +-- .../webhook/github/GitHubCommitV3.java | 16 +++++++--- .../webhook/github/GitHubPullRequestV3.java | 20 +++++++++--- .../webhook/github/GitHubSyncServiceImpl.java | 31 ++++++++++++++----- .../dashboard/webhook/github/GitHubV3.java | 7 ++--- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 89ded7ea..2817463b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ api jar ${project.groupId}:${project.artifactId} - 3.2.5-SNAPSHOT + 3.2.6-SNAPSHOT Hygieia Rest API Layer https://github.com/Hygieia/api @@ -58,7 +58,7 @@ - 3.7.9 + 3.7.11 0.13 4.3.0 19.0 diff --git a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3.java b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3.java index 7c0ab144..396822a1 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3.java @@ -1,6 +1,5 @@ package com.capitalone.dashboard.webhook.github; -import com.capitalone.dashboard.model.AuthorType; import com.capitalone.dashboard.repository.CollectorItemRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; @@ -176,12 +175,21 @@ protected List getCommits(List commitsObjectList, String repoUrl, commit.setScmAuthorLogin(authorLogin); if (senderObj != null && authorLogin.equalsIgnoreCase(restClient.getString(senderObj, "login"))) { - commit.setScmAuthorType(AuthorType.fromString(restClient.getString(senderObj, "type"))); - commit.setScmAuthorLDAPDN(restClient.getString(senderObj, "ldap_dn")); + String authorType = restClient.getString(senderObj, "type"); + if (!StringUtils.isEmpty(authorType)) { + commit.setScmAuthorType(authorType); + } + String authorLDAPDN = restClient.getString(senderObj, "ldap_dn"); + if (!StringUtils.isEmpty(authorLDAPDN)) { + commit.setScmAuthorLDAPDN(authorLDAPDN); + } } else { start = System.currentTimeMillis(); - commit.setScmAuthorType(getAuthorType(repoUrl, authorLogin, gitHubWebHookToken)); + String authorType = getAuthorType(repoUrl, authorLogin, gitHubWebHookToken); + if (!StringUtils.isEmpty(authorType)) { + commit.setScmAuthorType(authorType); + } String authorLDAPDN = getLDAPDN(repoUrl, authorLogin, gitHubWebHookToken); if (!StringUtils.isEmpty(authorLDAPDN)) { commit.setScmAuthorLDAPDN(authorLDAPDN); diff --git a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3.java b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3.java index e612b4f7..efec9004 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3.java @@ -256,7 +256,10 @@ protected GitRequest buildGitRequestFromPayload(String repoUrl, String branch, O pull.setScmMergeEventRevisionNumber(mergeSha); Object mergedBy = restClient.getAsObject(pullRequestObject,"merged_by"); pull.setMergeAuthor(restClient.getString(mergedBy, "login")); - pull.setMergeAuthorType(getAuthorType(repoUrl, pull.getMergeAuthor(), token)); + String mergeAuthorType = getAuthorType(repoUrl, pull.getMergeAuthor(), token); + if (!StringUtils.isEmpty(mergeAuthorType)) { + pull.setMergeAuthorType(mergeAuthorType); + } String mergeAuthorLDAPDN = getLDAPDN(repoUrl, pull.getMergeAuthor(), token); if (!StringUtils.isEmpty(mergeAuthorLDAPDN)) { pull.setMergeAuthorLDAPDN(mergeAuthorLDAPDN); @@ -343,11 +346,14 @@ protected List getReviews(String repoUrl, Object reviewObject, String to review.setBody(restClient.getString(node, "bodyText")); JSONObject authorObj = (JSONObject) node.get("author"); review.setAuthor(restClient.getString(authorObj, "login")); + String authorType = getAuthorType(repoUrl, review.getAuthor(), token); + if (!StringUtils.isEmpty(authorType)) { + review.setAuthorType(authorType); + } String authorLDAPDN = getLDAPDN(repoUrl, review.getAuthor(), token); if (!StringUtils.isEmpty(authorLDAPDN)) { review.setAuthorLDAPDN(authorLDAPDN); } - review.setAuthorType(getAuthorType(repoUrl, review.getAuthor(), token)); review.setCreatedAt(getTimeStampMills(restClient.getString(node, "createdAt"))); review.setUpdatedAt(getTimeStampMills(restClient.getString(node, "updatedAt"))); reviews.add(review); @@ -369,7 +375,10 @@ protected List getComments(String repoUrl, Object commentsObject, Strin Comment comment = new Comment(); comment.setBody(restClient.getString(node, "bodyText")); comment.setUser(restClient.getString((JSONObject) node.get("author"), "login")); - comment.setUserType(getAuthorType(repoUrl, comment.getUser(), token)); + String userType = getAuthorType(repoUrl, comment.getUser(), token); + if (!StringUtils.isEmpty(userType)) { + comment.setUserType(userType); + } String userLDAPDN = getLDAPDN(repoUrl, comment.getUser(), token); if (!StringUtils.isEmpty(userLDAPDN)) { comment.setUserLDAPDN(userLDAPDN); @@ -408,7 +417,10 @@ protected List getPRCommits(String repoUrl, Object commitsObject, GitReq JSONObject authorUserJSON = (JSONObject) author.get("user"); newCommit.setScmAuthor(restClient.getString(author, "name")); newCommit.setScmAuthorLogin((authorUserJSON == null) ? "unknown" : restClient.getString(authorUserJSON, "login")); - newCommit.setScmAuthorType(getAuthorType(repoUrl, newCommit.getScmAuthorLogin(), token)); + String authorType = getAuthorType(repoUrl, newCommit.getScmAuthorLogin(), token); + if (!StringUtils.isEmpty(authorType)) { + newCommit.setScmAuthorType(authorType); + } String authorLDAPDN = getLDAPDN(repoUrl, newCommit.getScmAuthorLogin(), token); if (!StringUtils.isEmpty(authorLDAPDN)) { newCommit.setScmAuthorLDAPDN(authorLDAPDN); diff --git a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubSyncServiceImpl.java b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubSyncServiceImpl.java index 750da4d4..13dc03f7 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubSyncServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubSyncServiceImpl.java @@ -3,7 +3,6 @@ import com.capitalone.dashboard.client.RestClient; import com.capitalone.dashboard.client.RestUserInfo; import com.capitalone.dashboard.misc.HygieiaException; -import com.capitalone.dashboard.model.AuthorType; import com.capitalone.dashboard.model.CollectionError; import com.capitalone.dashboard.model.CollectionMode; import com.capitalone.dashboard.model.Collector; @@ -77,7 +76,7 @@ public class GitHubSyncServiceImpl implements GitHubSyncService { List pullRequests; List issues; Map ldapMap; - Map authorTypeMap; + Map authorTypeMap; private final List commitExclusionPatterns = new ArrayList<>(); @@ -535,7 +534,10 @@ private GitHubPaging processPullRequest(JSONObject pullObject, GitHubRepo repo, if (mergeEvent != null) { pull.setScmMergeEventRevisionNumber(mergeEvent.getMergeSha()); pull.setMergeAuthor(mergeEvent.getMergeAuthor()); - pull.setMergeAuthorType(getAuthorType(repo, mergeEvent.getMergeAuthor())); + String mergeAuthorType = getAuthorType(repo, mergeEvent.getMergeAuthor()); + if (!StringUtils.isEmpty(mergeAuthorType)) { + pull.setMergeAuthorType(mergeAuthorType); + } String mergeAuthorLDAPDN = getLDAPDN(repo, mergeEvent.getMergeAuthor()); if (!StringUtils.isEmpty(mergeAuthorLDAPDN)) { pull.setMergeAuthorLDAPDN(mergeAuthorLDAPDN); @@ -728,7 +730,10 @@ private GitHubPaging processCommits(JSONObject refObject, GitHubRepo repo) { commit.setScmRevisionNumber(sha); commit.setScmAuthor(authorName); commit.setScmAuthorLogin(authorLogin); - commit.setScmAuthorType(getAuthorType(repo, authorLogin)); + String authorType = getAuthorType(repo, authorLogin); + if (!StringUtils.isEmpty(authorType)) { + commit.setScmAuthorType(authorType); + } String authorLDAPDN = getLDAPDN(repo, authorLogin); if (!StringUtils.isEmpty(authorLDAPDN)) { commit.setScmAuthorLDAPDN(authorLDAPDN); @@ -849,7 +854,7 @@ public void getUser(GitHubRepo repo, String user) { ldapMap.put(user, ldapDN); } if (StringUtils.isNotEmpty(authorTypeStr)) { - authorTypeMap.put(user, AuthorType.fromString(authorTypeStr)); + authorTypeMap.put(user, authorTypeStr); } } catch (MalformedURLException | HygieiaException | RestClientException e) { LOG.error("Error getting LDAP_DN For user " + user, e); @@ -868,7 +873,7 @@ public String getLDAPDN(GitHubRepo repo, String user) { return ldapMap.get(formattedUser); } - public AuthorType getAuthorType(GitHubRepo repo, String user) { + public String getAuthorType(GitHubRepo repo, String user) { if (StringUtils.isEmpty(user) || "unknown".equalsIgnoreCase(user)) return null; if (authorTypeMap == null) { authorTypeMap = new HashMap<>(); } //This is weird. Github does replace the _ in commit author with - in the user api!!! @@ -895,7 +900,10 @@ private List getComments(GitHubRepo repo, JSONObject commentsJSON) thro Comment comment = new Comment(); comment.setBody(str(node, "bodyText")); comment.setUser(str((JSONObject) node.get("author"), "login")); - comment.setUserType(getAuthorType(repo, comment.getUser())); + String userType = getAuthorType(repo, comment.getUser()); + if (!StringUtils.isEmpty(userType)) { + comment.setUserType(userType); + } String userLDAPDN = getLDAPDN(repo, comment.getUser()); if (!StringUtils.isEmpty(userLDAPDN)) { comment.setUserLDAPDN(userLDAPDN); @@ -933,6 +941,10 @@ private List getPRCommits(GitHubRepo repo, JSONObject commits, GitReques newCommit.setScmAuthor(str(author, "name")); newCommit.setScmAuthorLogin(authorUserJSON == null ? "unknown" : str(authorUserJSON, "login")); String authorLDAPDN = "unknown".equalsIgnoreCase(newCommit.getScmAuthorLogin()) ? null : getLDAPDN(repo, newCommit.getScmAuthorLogin()); + String authorType = getAuthorType(repo, newCommit.getScmAuthorLogin()); + if (!StringUtils.isEmpty(authorType)) { + newCommit.setScmAuthorType(authorType); + } if (!StringUtils.isEmpty(authorLDAPDN)) { newCommit.setScmAuthorLDAPDN(authorLDAPDN); } @@ -1015,7 +1027,10 @@ private List getReviews(GitHubRepo repo, JSONObject reviewObject) throws review.setBody(str(node, "bodyText")); JSONObject authorObj = (JSONObject) node.get("author"); review.setAuthor(str(authorObj, "login")); - review.setAuthorType(getAuthorType(repo, review.getAuthor())); + String authorType = getAuthorType(repo, review.getAuthor()); + if (!StringUtils.isEmpty(authorType)) { + review.setAuthorType(authorType); + } String authorLDAPDN = getLDAPDN(repo, review.getAuthor()); if (!StringUtils.isEmpty(authorLDAPDN)) { review.setAuthorLDAPDN(authorLDAPDN); diff --git a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubV3.java b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubV3.java index 93c5a069..61b86201 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubV3.java @@ -1,6 +1,5 @@ package com.capitalone.dashboard.webhook.github; -import com.capitalone.dashboard.model.AuthorType; import com.capitalone.dashboard.repository.CollectorItemRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; @@ -43,7 +42,7 @@ public abstract class GitHubV3 { protected final ApiSettings apiSettings; private Map ldapMap; - private Map authorTypeMap; + private Map authorTypeMap; public GitHubV3(CollectorService collectorService, RestClient restClient, @@ -164,7 +163,7 @@ protected void getUser(String repoUrl, String user, String token) { ldapMap.put(user, ldapDN); } if (StringUtils.isNotEmpty(authorTypeStr)) { - authorTypeMap.put(user, AuthorType.fromString(authorTypeStr)); + authorTypeMap.put(user, authorTypeStr); } long end = System.currentTimeMillis(); @@ -195,7 +194,7 @@ protected String getLDAPDN(String repoUrl, String user, String token) { return ldapMap.get(formattedUser); } - protected AuthorType getAuthorType(String repoUrl, String user, String token) { + protected String getAuthorType(String repoUrl, String user, String token) { if (StringUtils.isEmpty(user) || "unknown".equalsIgnoreCase(user)) return null; if (authorTypeMap == null) { authorTypeMap = new HashMap<>(); } //This is weird. Github does replace the _ in commit author with - in the user api!!!