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

Commit

Permalink
Change authorType from enum to string (#152)
Browse files Browse the repository at this point in the history
* Change authorType from enum to string

* Increment version
  • Loading branch information
chzhanpeng authored Jul 6, 2020
1 parent 556ef11 commit 0ae00e6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>api</artifactId>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<version>3.2.5-SNAPSHOT</version>
<version>3.2.6-SNAPSHOT</version>
<description>Hygieia Rest API Layer</description>
<url>https://github.com/Hygieia/api</url>

Expand Down Expand Up @@ -58,7 +58,7 @@
</parent>

<properties>
<com.capitalone.dashboard.core.version>3.7.9</com.capitalone.dashboard.core.version>
<com.capitalone.dashboard.core.version>3.7.11</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,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;
Expand Down Expand Up @@ -176,12 +175,21 @@ protected List<Commit> getCommits(List<Map> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -343,11 +346,14 @@ 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 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);
Expand All @@ -369,7 +375,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"));
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);
Expand Down Expand Up @@ -408,7 +417,10 @@ 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"));
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +76,7 @@ public class GitHubSyncServiceImpl implements GitHubSyncService {
List<GitRequest> pullRequests;
List<GitRequest> issues;
Map<String, String> ldapMap;
Map<String, AuthorType> authorTypeMap;
Map<String, String> authorTypeMap;
private final List<Pattern> commitExclusionPatterns = new ArrayList<>();


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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!!!
Expand All @@ -895,7 +900,10 @@ private List<Comment> 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);
Expand Down Expand Up @@ -933,6 +941,10 @@ private List<Commit> 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);
}
Expand Down Expand Up @@ -1015,7 +1027,10 @@ private List<Review> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +42,7 @@ public abstract class GitHubV3 {
protected final ApiSettings apiSettings;

private Map<String, String> ldapMap;
private Map<String, AuthorType> authorTypeMap;
private Map<String, String> authorTypeMap;

public GitHubV3(CollectorService collectorService,
RestClient restClient,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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!!!
Expand Down

0 comments on commit 0ae00e6

Please sign in to comment.