From 30fc6610864ce3a85d78aecbdd0938e18f16cdfb Mon Sep 17 00:00:00 2001 From: nireeshT Date: Mon, 18 Oct 2021 16:55:46 -0400 Subject: [PATCH] scmAuthorName fix (#246) * scmAuthorname fix * getLDAPDN fix * scmAuthorname fix --- pom.xml | 6 ++--- .../dashboard/settings/ApiSettings.java | 11 ++++++++ .../webhook/github/GitHubCommitV3.java | 13 ++++++++-- .../webhook/github/GitHubHookServiceImpl.java | 10 ++++--- .../webhook/github/GitHubIssueV3.java | 6 ++++- .../webhook/github/GitHubPullRequestV3.java | 9 +++++-- .../dashboard/webhook/github/GitHubV3.java | 26 ++++++++++++++++++- .../webhook/github/GitHubCommitV3Test.java | 4 ++- .../webhook/github/GitHubIssueV3Test.java | 4 ++- .../github/GitHubPullRequestV3Test.java | 4 ++- 10 files changed, 78 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index ef6143d2..6fc7744b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ api jar ${project.groupId}:${project.artifactId} - 3.4.24 + 3.4.25 Hygieia Rest API Layer https://github.com/Hygieia/api @@ -59,9 +59,9 @@ - 3.15.16 + 3.15.20 4.2.18.RELEASE - 8.5.57 + 8.5.70 1.9.4 1.14 4.1 diff --git a/src/main/java/com/capitalone/dashboard/settings/ApiSettings.java b/src/main/java/com/capitalone/dashboard/settings/ApiSettings.java index 35fa1318..a54e2f39 100644 --- a/src/main/java/com/capitalone/dashboard/settings/ApiSettings.java +++ b/src/main/java/com/capitalone/dashboard/settings/ApiSettings.java @@ -67,6 +67,9 @@ public class ApiSettings { @Value("${encryptRemoteCreatePayload:true}") private boolean encryptRemoteCreatePayload; + @Value("${optimizeUserCallsToGithub:true}") + private boolean optimizeUserCallsToGithub; + private String hygieia_ui_url=""; public Map getFunctional() { @@ -256,4 +259,12 @@ public String getContextSecurityAuthentication() { public void setContextSecurityAuthentication(String contextSecurityAuthentication) { this.contextSecurityAuthentication = contextSecurityAuthentication; } + + public boolean isOptimizeUserCallsToGithub() { + return optimizeUserCallsToGithub; + } + + public void setOptimizeUserCallsToGithub(boolean optimizeUserCallsToGithub) { + this.optimizeUserCallsToGithub = optimizeUserCallsToGithub; + } } 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 cf28772f..327c068b 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3.java @@ -1,8 +1,10 @@ package com.capitalone.dashboard.webhook.github; import com.capitalone.dashboard.model.GitHubCollector; +import com.capitalone.dashboard.model.UserEntitlements; import com.capitalone.dashboard.repository.BaseCollectorRepository; import com.capitalone.dashboard.repository.CollectorItemRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; import com.capitalone.dashboard.model.webhook.github.GitHubParsed; @@ -47,9 +49,10 @@ public GitHubCommitV3(CollectorService collectorService, CommitRepository commitRepository, GitRequestRepository gitRequestRepository, CollectorItemRepository collectorItemRepository, + UserEntitlementsRepository userEntitlementsRepository, ApiSettings apiSettings, BaseCollectorRepository collectorRepository) { - super(collectorService, restClient, apiSettings, collectorItemRepository, collectorRepository); + super(collectorService, restClient, apiSettings, collectorItemRepository, userEntitlementsRepository, collectorRepository); this.commitRepository = commitRepository; this.gitRequestRepository = gitRequestRepository; @@ -174,6 +177,9 @@ protected List getCommits(List commitListPayload, String repoUrl, String authorLogin = (userObject == null) ? "unknown" : restClient.getString(userObject, "login"); commit.setScmAuthorLogin(authorLogin); + String scmAuthorName = userObject == null ? null : restClient.getString(userObject, "name"); + commit.setScmAuthorName(scmAuthorName); + if (senderObj != null && authorLogin.equalsIgnoreCase(restClient.getString(senderObj, "login"))) { String authorType = restClient.getString(senderObj, "type"); if (!StringUtils.isEmpty(authorType)) { @@ -198,7 +204,10 @@ protected List getCommits(List commitListPayload, String repoUrl, long end = System.currentTimeMillis(); LOG.debug("Time to fetch LDAPDN = "+(end-start)); } - + // if ldap dn is null set it from ldapMap + if(StringUtils.isEmpty(commit.getScmAuthorLDAPDN())){ + commit.setScmAuthorLDAPDN(getLDAPDN(repoUrl, StringUtils.isEmpty(scmAuthorName) ? authorLogin : scmAuthorName, gitHubWebHookToken)); + } // Set the Committer details. This in the case of a merge commit is the user who merges the PR. // In the case of a regular commit, it is usually set to a default "name": "GitHub Enterprise", and login is null Object committerObject = restClient.getAsObject(node, "committer"); diff --git a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubHookServiceImpl.java b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubHookServiceImpl.java index 275e71df..948b0f1b 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubHookServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubHookServiceImpl.java @@ -4,6 +4,7 @@ import com.capitalone.dashboard.model.GitHubCollector; import com.capitalone.dashboard.repository.BaseCollectorRepository; import com.capitalone.dashboard.repository.CollectorItemRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; import com.capitalone.dashboard.repository.CommitRepository; @@ -28,6 +29,7 @@ public class GitHubHookServiceImpl implements GitHubHookService { private final GitRequestRepository gitRequestRepository; private final CollectorItemRepository collectorItemRepository; private final BaseCollectorRepository collectorRepository; + private final UserEntitlementsRepository userEntitlementsRepository; private final CollectorService collectorService; protected final ApiSettings apiSettings; protected final RestClient restClient; @@ -37,6 +39,7 @@ public GitHubHookServiceImpl(CommitRepository commitRepository, GitRequestRepository gitRequestRepository, CollectorService collectorService, CollectorItemRepository collectorItemRepository, + UserEntitlementsRepository userEntitlementsRepository, ApiSettings apiSettings, RestClient restClient, BaseCollectorRepository collectorRepository) { @@ -44,6 +47,7 @@ public GitHubHookServiceImpl(CommitRepository commitRepository, this.gitRequestRepository = gitRequestRepository; this.collectorItemRepository = collectorItemRepository; this.collectorRepository = collectorRepository; + this.userEntitlementsRepository = userEntitlementsRepository; this.collectorService = collectorService; this.apiSettings = apiSettings; this.restClient = restClient; @@ -67,15 +71,15 @@ public String createFromGitHubv3(JSONObject request) throws ParseException, Hygi switch (payloadType) { case Push: - gitHubv3 = new GitHubCommitV3(collectorService, restClient, commitRepository, gitRequestRepository, collectorItemRepository, apiSettings, collectorRepository); + gitHubv3 = new GitHubCommitV3(collectorService, restClient, commitRepository, gitRequestRepository, collectorItemRepository, userEntitlementsRepository, apiSettings, collectorRepository); break; case PullRequest: - gitHubv3 = new GitHubPullRequestV3(collectorService, restClient, gitRequestRepository, commitRepository, collectorItemRepository, apiSettings, collectorRepository); + gitHubv3 = new GitHubPullRequestV3(collectorService, restClient, gitRequestRepository, commitRepository, collectorItemRepository, userEntitlementsRepository, apiSettings, collectorRepository); break; case Issues: - gitHubv3 = new GitHubIssueV3(collectorService, restClient, gitRequestRepository, collectorItemRepository, apiSettings, collectorRepository); + gitHubv3 = new GitHubIssueV3(collectorService, restClient, gitRequestRepository, collectorItemRepository, userEntitlementsRepository , apiSettings, collectorRepository); break; default: diff --git a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3.java b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3.java index 5e704bbf..8c33d8b5 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3.java @@ -3,6 +3,7 @@ import com.capitalone.dashboard.model.GitHubCollector; import com.capitalone.dashboard.repository.BaseCollectorRepository; import com.capitalone.dashboard.repository.CollectorItemRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; import com.capitalone.dashboard.model.webhook.github.GitHubParsed; @@ -20,14 +21,17 @@ public class GitHubIssueV3 extends GitHubV3 { private final GitRequestRepository gitRequestRepository; + public GitHubIssueV3(CollectorService collectorService, RestClient restClient, GitRequestRepository gitRequestRepository, CollectorItemRepository collectorItemRepository, + UserEntitlementsRepository userEntitlementsRepository, ApiSettings apiSettings, BaseCollectorRepository collectorRepository) { - super(collectorService, restClient, apiSettings, collectorItemRepository, collectorRepository); + super(collectorService, restClient, apiSettings, collectorItemRepository, userEntitlementsRepository, collectorRepository); this.gitRequestRepository = gitRequestRepository; + } @Override 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 3c077b22..938b2ba6 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3.java @@ -4,6 +4,7 @@ import com.capitalone.dashboard.model.PullRequestEvent; import com.capitalone.dashboard.repository.BaseCollectorRepository; import com.capitalone.dashboard.repository.CollectorItemRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; import com.capitalone.dashboard.model.webhook.github.GitHubParsed; @@ -49,9 +50,10 @@ public GitHubPullRequestV3(CollectorService collectorService, GitRequestRepository gitRequestRepository, CommitRepository commitRepository, CollectorItemRepository collectorItemRepository, + UserEntitlementsRepository userEntitlementsRepository, ApiSettings apiSettings, BaseCollectorRepository collectorRepository) { - super(collectorService, restClient, apiSettings, collectorItemRepository, collectorRepository); + super(collectorService, restClient, apiSettings, collectorItemRepository, userEntitlementsRepository, collectorRepository); this.gitRequestRepository = gitRequestRepository; this.commitRepository = commitRepository; @@ -422,11 +424,14 @@ 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")); + String scmAuthorName = authorUserJSON == null ? null : restClient.getString(authorUserJSON, "name"); + newCommit.setScmAuthorName(scmAuthorName); + String authorType = getAuthorType(repoUrl, newCommit.getScmAuthorLogin(), token); if (!StringUtils.isEmpty(authorType)) { newCommit.setScmAuthorType(authorType); } - String authorLDAPDN = getLDAPDN(repoUrl, newCommit.getScmAuthorLogin(), token); + String authorLDAPDN = getLDAPDN(repoUrl, StringUtils.isEmpty(scmAuthorName) ? newCommit.getScmAuthorLogin() : scmAuthorName, token); if (!StringUtils.isEmpty(authorLDAPDN)) { newCommit.setScmAuthorLDAPDN(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 5322988b..ef29bf6f 100644 --- a/src/main/java/com/capitalone/dashboard/webhook/github/GitHubV3.java +++ b/src/main/java/com/capitalone/dashboard/webhook/github/GitHubV3.java @@ -1,9 +1,12 @@ package com.capitalone.dashboard.webhook.github; +import com.capitalone.dashboard.model.AuthType; import com.capitalone.dashboard.model.GitHubCollector; +import com.capitalone.dashboard.model.UserEntitlements; import com.capitalone.dashboard.repository.BaseCollectorRepository; import com.capitalone.dashboard.repository.CollectorItemRepository; import com.capitalone.dashboard.model.webhook.github.GitHubRepo; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.client.RestClient; import com.capitalone.dashboard.model.webhook.github.GitHubParsed; @@ -44,14 +47,19 @@ public abstract class GitHubV3 { protected final ApiSettings apiSettings; protected final CollectorItemRepository collectorItemRepository; private final BaseCollectorRepository collectorRepository; + private UserEntitlementsRepository userEntitlementsRepository; + private static final String ENTITLEMENT_TYPE = "distinguishedName"; + private Map authorTypeMap; + private Map ldapMap; - private Map authorTypeMap; + public GitHubV3(CollectorService collectorService, RestClient restClient, ApiSettings apiSettings, CollectorItemRepository collectorItemRepository, + UserEntitlementsRepository userEntitlementsRepository, BaseCollectorRepository collectorRepository ) { this.collectorService = collectorService; @@ -61,6 +69,7 @@ public GitHubV3(CollectorService collectorService, this.collectorRepository = collectorRepository; ldapMap = new HashMap<>(); authorTypeMap = new HashMap<>(); + this.userEntitlementsRepository = userEntitlementsRepository; } public GitHubCollector getCollector() { @@ -181,6 +190,16 @@ protected void getUser(String repoUrl, String user, String token) { String formattedUser = user.replace("_", "-"); int retryCount = 0; ResponseEntity response; + + if(apiSettings.isOptimizeUserCallsToGithub()) { + UserEntitlements entitlements = userEntitlementsRepository.findTopByAuthTypeAndEntitlementTypeAndUsername(AuthType.LDAP, + ENTITLEMENT_TYPE, StringUtils.lowerCase(user)); + String ldapDN = (entitlements == null) ? "" : entitlements.getEntitlements(); + ldapMap.put(user, ldapDN); + authorTypeMap.put(user, "User"); + return; + } + while(true) { try { long start = System.currentTimeMillis(); @@ -218,6 +237,11 @@ protected void getUser(String repoUrl, String user, String token) { protected String getLDAPDN(String repoUrl, String user, String token) { if (StringUtils.isEmpty(user) || "unknown".equalsIgnoreCase(user)) return null; if (ldapMap == null) { ldapMap = new HashMap<>(); } + + if(apiSettings.isOptimizeUserCallsToGithub()) { + return ldapMap.get(user); + } + //This is weird. Github does replace the _ in commit author with - in the user api!!! String formattedUser = user.replace("_", "-"); if(ldapMap.containsKey(formattedUser)) { diff --git a/src/test/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3Test.java b/src/test/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3Test.java index 9ea506c0..f1b68107 100644 --- a/src/test/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3Test.java +++ b/src/test/java/com/capitalone/dashboard/webhook/github/GitHubCommitV3Test.java @@ -15,6 +15,7 @@ import com.capitalone.dashboard.repository.CollectorItemRepository; import com.capitalone.dashboard.repository.CommitRepository; import com.capitalone.dashboard.repository.GitRequestRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.service.CollectorService; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.webhook.settings.GitHubWebHookSettings; @@ -61,6 +62,7 @@ public class GitHubCommitV3Test { @Mock private CommitRepository commitRepository; @Mock private GitRequestRepository gitRequestRepository; @Mock private CollectorItemRepository collectorItemRepository; + @Mock private UserEntitlementsRepository userEntitlementsRepository; @Mock private BaseCollectorRepository collectorRepository; @Mock private ApiSettings apiSettings; @Mock private RestOperationsSupplier restOperationsSupplier; @@ -72,7 +74,7 @@ public class GitHubCommitV3Test { public void init() { RestClient restClientTemp = new RestClient(restOperationsSupplier); restClient = Mockito.spy(restClientTemp); - gitHubCommitV3 = new GitHubCommitV3 (collectorService, restClient, commitRepository, gitRequestRepository, collectorItemRepository, apiSettings, collectorRepository); + gitHubCommitV3 = new GitHubCommitV3 (collectorService, restClient, commitRepository, gitRequestRepository, collectorItemRepository, userEntitlementsRepository, apiSettings, collectorRepository); } @Test diff --git a/src/test/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3Test.java b/src/test/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3Test.java index 17a2943d..2f034e8a 100644 --- a/src/test/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3Test.java +++ b/src/test/java/com/capitalone/dashboard/webhook/github/GitHubIssueV3Test.java @@ -12,6 +12,7 @@ import com.capitalone.dashboard.repository.BaseCollectorRepository; import com.capitalone.dashboard.repository.CollectorItemRepository; import com.capitalone.dashboard.repository.GitRequestRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.service.CollectorService; import com.capitalone.dashboard.settings.ApiSettings; import org.apache.commons.logging.Log; @@ -43,6 +44,7 @@ public class GitHubIssueV3Test { @Mock private GitRequestRepository gitRequestRepository; @Mock private CollectorItemRepository collectorItemRepository; @Mock private BaseCollectorRepository collectorRepository; + @Mock private UserEntitlementsRepository userEntitlementsRepository; @Mock private ApiSettings apiSettings; @Mock private RestOperationsSupplier restOperationsSupplier; @@ -51,7 +53,7 @@ public class GitHubIssueV3Test { @Before public void init() { RestClient restClient = new RestClient(restOperationsSupplier); - gitHubIssueV3 = new GitHubIssueV3 (collectorService, restClient, gitRequestRepository, collectorItemRepository, apiSettings, collectorRepository); + gitHubIssueV3 = new GitHubIssueV3 (collectorService, restClient, gitRequestRepository, collectorItemRepository, userEntitlementsRepository, apiSettings, collectorRepository); } @Test diff --git a/src/test/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3Test.java b/src/test/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3Test.java index 862f33bf..1c33711d 100644 --- a/src/test/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3Test.java +++ b/src/test/java/com/capitalone/dashboard/webhook/github/GitHubPullRequestV3Test.java @@ -17,6 +17,7 @@ import com.capitalone.dashboard.repository.CollectorItemRepository; import com.capitalone.dashboard.repository.CommitRepository; import com.capitalone.dashboard.repository.GitRequestRepository; +import com.capitalone.dashboard.repository.UserEntitlementsRepository; import com.capitalone.dashboard.service.CollectorService; import com.capitalone.dashboard.settings.ApiSettings; import org.apache.commons.logging.Log; @@ -57,6 +58,7 @@ public class GitHubPullRequestV3Test { @Mock private CollectorItemRepository collectorItemRepository; @Mock private BaseCollectorRepository collectorRepository; @Mock private CommitRepository commitRepository; + @Mock private UserEntitlementsRepository userEntitlementsRepository; @Mock private ApiSettings apiSettings; @Mock private RestOperationsSupplier restOperationsSupplier; @@ -67,7 +69,7 @@ public class GitHubPullRequestV3Test { @Before public void init() { restClient = new RestClient(restOperationsSupplier); - gitHubPullRequestV3 = new GitHubPullRequestV3 (collectorService, restClient, gitRequestRepository, commitRepository, collectorItemRepository, apiSettings, collectorRepository); + gitHubPullRequestV3 = new GitHubPullRequestV3 (collectorService, restClient, gitRequestRepository, commitRepository, collectorItemRepository, userEntitlementsRepository, apiSettings, collectorRepository); payLoadJsonObject = makePullRequestPayloadObject(); }