forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthor.java
More file actions
192 lines (159 loc) · 6.27 KB
/
Author.java
File metadata and controls
192 lines (159 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package reposense.model;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a Git Author.
*/
public class Author {
public static final String NAME_FAILED_TO_CLONE_OR_CHECKOUT = "FAILED TO CLONE OR CHECKOUT THIS REPOSITORY";
public static final String NAME_NO_AUTHOR_WITH_COMMITS_FOUND =
"NO AUTHOR WITH COMMITS FOUND WITHIN THIS PERIOD OF TIME";
private static final String UNKNOWN_AUTHOR_GIT_ID = "-";
private static final String STANDARD_GITHUB_EMAIL_DOMAIN = "@users.noreply.github.com";
private static final String MESSAGE_UNCOMMON_EMAIL_PATTERN = "The provided email, %s, uses uncommon pattern.";
private static final String MESSAGE_UNCOMMON_GLOB_PATTERN = "The provided ignore glob, %s, uses uncommon pattern.";
private static final String COMMON_EMAIL_REGEX =
"^([a-zA-Z0-9_\\-\\.\\+]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$";
private static final String COMMON_GLOB_REGEX = "^[-a-zA-Z0-9 _/\\\\*!{}\\[\\]!(),:.]*$";
public static final Author UNKNOWN_AUTHOR = new Author(UNKNOWN_AUTHOR_GIT_ID);
private final String gitId;
private transient List<String> emails;
private transient String displayName;
private transient List<String> authorAliases;
private transient List<String> ignoreGlobList;
private transient PathMatcher ignoreGlobMatcher;
public Author(String gitId) {
this.gitId = gitId;
this.emails = new ArrayList<>();
this.displayName = gitId;
this.authorAliases = new ArrayList<>();
this.ignoreGlobList = new ArrayList<>();
addStandardGitHubEmail(this.emails);
updateIgnoreGlobMatcher();
}
public Author(StandaloneAuthor sa) {
String gitId = sa.getGithubId();
List<String> emails = new ArrayList<>(sa.getEmails());
String displayName = !sa.getDisplayName().isEmpty() ? sa.getDisplayName() : sa.getGithubId();
List<String> authorAliases = sa.getAuthorNames();
List<String> ignoreGlobList = sa.getIgnoreGlobList();
this.gitId = gitId;
this.displayName = displayName;
this.authorAliases = authorAliases;
setEmails(emails);
setIgnoreGlobList(ignoreGlobList);
}
public Author(Author another) {
this.gitId = another.gitId;
this.emails = another.emails;
this.displayName = another.gitId;
this.authorAliases = another.authorAliases;
this.ignoreGlobList = another.authorAliases;
this.ignoreGlobMatcher = another.ignoreGlobMatcher;
}
/**
* Checks that all the strings in the {@code emails} only contains commonly used email patterns.
* @throws IllegalArgumentException if any of the values do not meet the criteria.
*/
private static void validateEmails(List<String> emails) throws IllegalArgumentException {
for (String email : emails) {
if (!email.matches(COMMON_EMAIL_REGEX)) {
throw new IllegalArgumentException(String.format(MESSAGE_UNCOMMON_EMAIL_PATTERN, email));
}
}
}
/**
* Checks that all the strings in the {@code ignoreGlobList} only contains commonly used glob patterns.
* @throws IllegalArgumentException if any of the values do not meet the criteria.
*/
private static void validateIgnoreGlobs(List<String> ignoreGlobList) throws IllegalArgumentException {
for (String glob : ignoreGlobList) {
if (!glob.matches(COMMON_GLOB_REGEX)) {
throw new IllegalArgumentException(String.format(MESSAGE_UNCOMMON_GLOB_PATTERN, glob));
}
}
}
public String getGitId() {
return gitId;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
validateEmails(emails);
this.emails = new ArrayList<>(emails);
addStandardGitHubEmail(this.emails);
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public List<String> getAuthorAliases() {
return authorAliases;
}
public void setAuthorAliases(List<String> authorAliases) {
this.authorAliases = authorAliases;
}
public List<String> getIgnoreGlobList() {
return ignoreGlobList;
}
public void setIgnoreGlobList(List<String> ignoreGlobList) {
validateIgnoreGlobs(ignoreGlobList);
this.ignoreGlobList = new ArrayList<>(ignoreGlobList);
updateIgnoreGlobMatcher();
}
public PathMatcher getIgnoreGlobMatcher() {
return ignoreGlobMatcher;
}
/**
* Validates and adds a list of ignore glob into the {@code Author} class instance variable,
* and updates the ignore glob matcher.
*/
public void appendIgnoreGlobList(List<String> ignoreGlobList) {
validateIgnoreGlobs(ignoreGlobList);
this.ignoreGlobList.addAll(ignoreGlobList);
updateIgnoreGlobMatcher();
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (this == other) {
return true;
}
// instanceof handles null
if (!(other instanceof Author)) {
return false;
}
Author otherAuthor = (Author) other;
return this.gitId.equalsIgnoreCase(otherAuthor.gitId);
}
@Override
public int hashCode() {
return gitId != null ? gitId.toLowerCase().hashCode() : 0;
}
@Override
public String toString() {
return gitId;
}
/**
* Updates the {@code PathMatcher} to the new ignore glob list set.
* Called after a new ignore glob list is set.
*/
private void updateIgnoreGlobMatcher() {
String globString = "glob:{" + String.join(",", ignoreGlobList) + "}";
ignoreGlobMatcher = FileSystems.getDefault().getPathMatcher(globString);
}
/**
* Adds the standard github email to {@code emails} if doesn't exist.
*/
private void addStandardGitHubEmail(List<String> emails) {
String standardGitHubEmail = getGitId() + STANDARD_GITHUB_EMAIL_DOMAIN;
if (!emails.contains(standardGitHubEmail)) {
emails.add(standardGitHubEmail);
}
}
}