Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Add author type (#150)
Browse files Browse the repository at this point in the history
* Add author type

* Remove unused imports

* Fix minor bugs

* dont set LDAPDN if it is not present

* Refactor code
  • Loading branch information
chzhanpeng authored Jun 23, 2020
1 parent ebd8bed commit 9a56b79
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 128 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</parent>

<properties>
<com.capitalone.dashboard.core.version>3.7.2</com.capitalone.dashboard.core.version>
<com.capitalone.dashboard.core.version>3.7.9</com.capitalone.dashboard.core.version>
<apache.rat.plugin.version>0.13</apache.rat.plugin.version>
<coveralls.maven.plugin.version>4.3.0</coveralls.maven.plugin.version>
<guava.version>19.0</guava.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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;
Expand Down Expand Up @@ -27,7 +28,6 @@
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -41,8 +41,6 @@ public class GitHubCommitV3 extends GitHubV3 {
private final GitRequestRepository gitRequestRepository;
private final CollectorItemRepository collectorItemRepository;

private Map<String, String> ldapDNMap = new HashMap<>();

public GitHubCommitV3(CollectorService collectorService,
RestClient restClient,
CommitRepository commitRepository,
Expand Down Expand Up @@ -94,19 +92,15 @@ public String process(JSONObject jsonObject) throws MalformedURLException, Hygie
}

Object senderObj = jsonObject.get("sender");
String senderLogin = restClient.getString(senderObj,"login");
String senderLDAPDN = restClient.getString(senderObj,"ldap_dn");

List<Commit> commitList = getCommits(commitsObjectList, repoUrl, branch, senderLogin, senderLDAPDN);
List<Commit> commitList = getCommits(commitsObjectList, repoUrl, branch, senderObj);

commitRepository.save(commitList);

return result;
}

protected List<Commit> getCommits(List<Map> commitsObjectList, String repoUrl,
String branch, String senderLogin,
String senderLDAPDN) throws MalformedURLException, HygieiaException, ParseException {
String branch, Object senderObj) throws MalformedURLException, HygieiaException, ParseException {
List<Commit> commitsList = new ArrayList<>();

GitHubParsed gitHubParsed = new GitHubParsed(repoUrl);
Expand Down Expand Up @@ -165,7 +159,6 @@ protected List<Commit> getCommits(List<Map> commitsObjectList, String repoUrl,
commit.setScmBranch(branch);

DateTime commitTimestamp = new DateTime(restClient.getString(cObj, "timestamp"));

commit.setScmCommitTimestamp(commitTimestamp.getMillis());

Object node = getCommitNode(gitHubParsed, commitId, gitHubWebHookToken);
Expand All @@ -181,25 +174,21 @@ protected List<Commit> getCommits(List<Map> commitsObjectList, String repoUrl,
Object userObject = restClient.getAsObject(authorObject, "user");
String authorLogin = (userObject == null) ? "unknown" : restClient.getString(userObject, "login");
commit.setScmAuthorLogin(authorLogin);
commit.setScmAuthorLDAPDN(senderLDAPDN);
if (!StringUtils.isEmpty(senderLDAPDN) && !senderLogin.equalsIgnoreCase(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"));
} else {
start = System.currentTimeMillis();

String key = repoUrl+authorLogin;
String userLDAP = ldapDNMap.get(key);
if (StringUtils.isEmpty(userLDAP)) {
userLDAP = getLDAPDN(repoUrl, authorLogin, gitHubWebHookToken);
if (!StringUtils.isEmpty(userLDAP)) {
ldapDNMap.put(key, userLDAP);
}
commit.setScmAuthorType(getAuthorType(repoUrl, authorLogin, gitHubWebHookToken));
String authorLDAPDN = getLDAPDN(repoUrl, authorLogin, gitHubWebHookToken);
if (!StringUtils.isEmpty(authorLDAPDN)) {
commit.setScmAuthorLDAPDN(authorLDAPDN);
}

String authorLDAPDNFetched = StringUtils.isEmpty(authorLogin) ? null : userLDAP;

end = System.currentTimeMillis();
LOG.debug("Time to fetch LDAPDN = "+(end-start));

commit.setScmAuthorLDAPDN(authorLDAPDNFetched);
}

// Set the Committer details. This in the case of a merge commit is the user who merges the PR.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.Objects;
import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Collections;

public class GitHubPullRequestV3 extends GitHubV3 {
Expand All @@ -44,8 +43,6 @@ public class GitHubPullRequestV3 extends GitHubV3 {
private final CollectorItemRepository collectorItemRepository;
private final CommitRepository commitRepository;

private Map<String, String> ldapDNMap = new HashMap<>();

public GitHubPullRequestV3(CollectorService collectorService,
RestClient restClient,
GitRequestRepository gitRequestRepository,
Expand Down Expand Up @@ -144,7 +141,7 @@ public String process(JSONObject prJsonObject) throws MalformedURLException, Hyg
return "Repo: <" + repoUrl + "> Branch: <" + branch + "> is not registered in Hygieia";
}

GitRequest pull = buildGitRequestFromPayload(repoUrl, branch, pullRequestObject);
GitRequest pull = buildGitRequestFromPayload(repoUrl, branch, pullRequestObject, token);

updateGitRequestWithGraphQLData(pull, repoUrl, branch, prData, token);

Expand Down Expand Up @@ -196,7 +193,7 @@ protected JSONObject buildGraphQLQuery(GitHubParsed gitHubParsed, Object pullReq
return query;
}

protected GitRequest buildGitRequestFromPayload(String repoUrl, String branch, Object pullRequestObject) throws HygieiaException, MalformedURLException {
protected GitRequest buildGitRequestFromPayload(String repoUrl, String branch, Object pullRequestObject, String token) throws HygieiaException, MalformedURLException {
GitRequest pull = new GitRequest();
GitHubParsed gitHubParsed = new GitHubParsed(repoUrl);

Expand Down Expand Up @@ -259,7 +256,11 @@ 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.setMergeAuthorLDAPDN(restClient.getString(mergedBy, "ldap_dn"));
pull.setMergeAuthorType(getAuthorType(repoUrl, pull.getMergeAuthor(), token));
String mergeAuthorLDAPDN = getLDAPDN(repoUrl, pull.getMergeAuthor(), token);
if (!StringUtils.isEmpty(mergeAuthorLDAPDN)) {
pull.setMergeAuthorLDAPDN(mergeAuthorLDAPDN);
}
}

setCollectorItemId(pull);
Expand Down Expand Up @@ -342,22 +343,11 @@ protected List<Review> 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 key = repoUrl+review.getAuthor();
String authorLDAPDN = ldapDNMap.get(key);
review.setAuthorLDAPDN(authorLDAPDN);
if (StringUtils.isEmpty(authorLDAPDN)) {
long start = System.currentTimeMillis();

authorLDAPDN = getLDAPDN(repoUrl, review.getAuthor(), token);

long end = System.currentTimeMillis();
LOG.info("Time to make the LDAP call = "+(end-start));

if (!StringUtils.isEmpty(authorLDAPDN)) {
ldapDNMap.put(key, authorLDAPDN);
review.setAuthorLDAPDN(authorLDAPDN);
}
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);
Expand All @@ -379,21 +369,10 @@ protected List<Comment> 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"));
String key = repoUrl+comment.getUser();
String userLDAP = ldapDNMap.get(key);
comment.setUserLDAPDN(userLDAP);
if (StringUtils.isEmpty(userLDAP)) {
long start = System.currentTimeMillis();

userLDAP = getLDAPDN(repoUrl, comment.getUser(), token);

long end = System.currentTimeMillis();
LOG.debug("Time to make the LDAP call = "+(end-start));

if (!StringUtils.isEmpty(userLDAP)) {
ldapDNMap.put(key, userLDAP);
comment.setUserLDAPDN(userLDAP);
}
comment.setUserType(getAuthorType(repoUrl, comment.getUser(), token));
String userLDAPDN = getLDAPDN(repoUrl, comment.getUser(), token);
if (!StringUtils.isEmpty(userLDAPDN)) {
comment.setUserLDAPDN(userLDAPDN);
}
comment.setCreatedAt(getTimeStampMills(restClient.getString(node, "createdAt")));
comment.setUpdatedAt(getTimeStampMills(restClient.getString(node, "updatedAt")));
Expand Down Expand Up @@ -429,47 +408,24 @@ protected List<Commit> 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"));

if (!"unknown".equalsIgnoreCase(newCommit.getScmAuthorLogin())) {
String key = repoUrl+newCommit.getScmAuthorLogin();
String authorLDAPDN = ldapDNMap.get(key);
newCommit.setScmAuthorType(getAuthorType(repoUrl, newCommit.getScmAuthorLogin(), token));
String authorLDAPDN = getLDAPDN(repoUrl, newCommit.getScmAuthorLogin(), token);
if (!StringUtils.isEmpty(authorLDAPDN)) {
newCommit.setScmAuthorLDAPDN(authorLDAPDN);
if (StringUtils.isEmpty(authorLDAPDN)) {
long start = System.currentTimeMillis();

authorLDAPDN = getLDAPDN(repoUrl, newCommit.getScmAuthorLogin(), token);

long end = System.currentTimeMillis();
LOG.debug("Time to make the LDAP call = "+(end-start));

if (!StringUtils.isEmpty(authorLDAPDN)) {
ldapDNMap.put(key, authorLDAPDN);
newCommit.setScmAuthorLDAPDN(authorLDAPDN);
}
}
}

int changedFiles = NumberUtils.toInt(restClient.getString(commit, "changedFiles"));
int deletions = NumberUtils.toInt(restClient.getString(commit, "deletions"));
int additions = NumberUtils.toInt(restClient.getString(commit, "additions"));
newCommit.setNumberOfChanges(changedFiles+deletions+additions);
newCommit.setNumberOfChanges((long) changedFiles + deletions + additions);

newCommit.setScmCommitTimestamp(getTimeStampMills(restClient.getString(author, "date")));
JSONObject statusObj = (JSONObject) commit.get("status");

if (statusObj != null) {
if (lastCommitTime <= newCommit.getScmCommitTimestamp()) {
lastCommitTime = newCommit.getScmCommitTimestamp();
lastCommitStatusObject = statusObj;
}
if (Objects.equals(newCommit.getScmRevisionNumber(), prHeadSha)) {
List<CommitStatus> commitStatuses = getCommitStatuses(statusObj);
List<CommitStatus> existingCommitStatusList = pull.getCommitStatuses();
if (!CollectionUtils.isEmpty(commitStatuses) && !CollectionUtils.isEmpty(existingCommitStatusList)) {
existingCommitStatusList.addAll(commitStatuses);
} else {
pull.setCommitStatuses(commitStatuses);
}
}
if (statusObj != null && lastCommitTime <= newCommit.getScmCommitTimestamp()) {
lastCommitTime = newCommit.getScmCommitTimestamp();
lastCommitStatusObject = statusObj;
setPRCommitStatus(statusObj, newCommit, pull);
}

// Relies mostly on an open pr to find commits from other repos, branches in the database.
Expand All @@ -493,6 +449,20 @@ protected List<Commit> getPRCommits(String repoUrl, Object commitsObject, GitReq
return prCommits;
}

private void setPRCommitStatus(JSONObject statusObj, Commit newCommit, GitRequest pull) {
String prHeadSha = pull.getHeadSha();
if (Objects.equals(newCommit.getScmRevisionNumber(), prHeadSha)) {
List<CommitStatus> commitStatuses = getCommitStatuses(statusObj);
List<CommitStatus> existingCommitStatusList = pull.getCommitStatuses();
if (!CollectionUtils.isEmpty(commitStatuses) && !CollectionUtils.isEmpty(existingCommitStatusList)) {
existingCommitStatusList.addAll(commitStatuses);
} else {
pull.setCommitStatuses(commitStatuses);
}
}

}

protected void updateMatchingCommitsInDb(Commit commit, GitRequest pull) {
long start = System.currentTimeMillis();

Expand Down
Loading

0 comments on commit 9a56b79

Please sign in to comment.