forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorConfigCsvParser.java
More file actions
143 lines (120 loc) · 5.3 KB
/
AuthorConfigCsvParser.java
File metadata and controls
143 lines (120 loc) · 5.3 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
package reposense.parser;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.csv.CSVRecord;
import reposense.model.Author;
import reposense.model.AuthorConfiguration;
import reposense.model.RepoLocation;
/**
* Container for the values parsed from {@code author-config.csv} file.
*/
public class AuthorConfigCsvParser extends CsvParser<AuthorConfiguration> {
public static final String AUTHOR_CONFIG_FILENAME = "author-config.csv";
/**
* Positions of the elements of a line in author-config.csv config file.
*/
private static final int LOCATION_POSITION = 0;
private static final int BRANCH_POSITION = 1;
private static final int GITHUB_ID_POSITION = 2;
private static final int EMAIL_POSITION = 3;
private static final int DISPLAY_NAME_POSITION = 4;
private static final int ALIAS_POSITION = 5;
private static final int IGNORE_GLOB_LIST_POSITION = 6;
public AuthorConfigCsvParser(Path csvFilePath) throws IOException {
super(csvFilePath);
}
/**
* Gets the list of positions that are mandatory for verification.
*/
@Override
protected int[] mandatoryPositions() {
return new int[] {
GITHUB_ID_POSITION,
};
}
/**
* Processes the csv file line by line and add created {@code AuthorConfiguration} into {@code results} but
* skips {@code author} already exists in a {@code AuthorConfiguration} that has same {@code location} and
* {@code branch}.
*/
@Override
protected void processLine(List<AuthorConfiguration> results, CSVRecord record)
throws ParseException {
String location = get(record, LOCATION_POSITION);
String branch = getOrDefault(record, BRANCH_POSITION, AuthorConfiguration.DEFAULT_BRANCH);
String gitHubId = get(record, GITHUB_ID_POSITION);
List<String> emails = getAsList(record, EMAIL_POSITION);
String displayName = get(record, DISPLAY_NAME_POSITION);
List<String> aliases = getAsList(record, ALIAS_POSITION);
List<String> ignoreGlobList = getAsList(record, IGNORE_GLOB_LIST_POSITION);
AuthorConfiguration config = findMatchingAuthorConfiguration(results, location, branch);
Author author = new Author(gitHubId);
if (config.containsAuthor(author)) {
logger.warning(String.format(
"Skipping author as %s already in repository %s %s",
author.getGitId(), config.getLocation(), config.getBranch()));
return;
}
config.addAuthor(author);
setEmails(config, author, emails);
setDisplayName(config, author, displayName);
setAliases(config, author, gitHubId, aliases);
setAuthorIgnoreGlobList(author, ignoreGlobList);
}
/**
* Gets an existing {@code AuthorConfiguration} from {@code results} if {@code location} and {@code branch} matches.
* Otherwise adds a newly created {@code AuthorConfiguration} into {@code results} and returns it.
*
* @throws InvalidLocationException if {@code location} is invalid.
*/
private static AuthorConfiguration findMatchingAuthorConfiguration(
List<AuthorConfiguration> results, String location, String branch) throws InvalidLocationException {
AuthorConfiguration config = new AuthorConfiguration(new RepoLocation(location), branch);
for (AuthorConfiguration authorConfig : results) {
if (authorConfig.getLocation().equals(config.getLocation())
&& authorConfig.getBranch().equals(config.getBranch())) {
return authorConfig;
}
}
results.add(config);
return config;
}
/**
* Associates {@code emails} to {@code author}, if provided and not empty.
*/
private static void setEmails(AuthorConfiguration config, Author author, List<String> emails) {
author.setEmails(new ArrayList<>(emails));
config.addAuthorEmailsAndAliasesMapEntry(author, author.getEmails());
}
/**
* Associates {@code displayName} to {@code author}, if provided and not empty.
* Otherwise, use github id from {@code author}.
*/
private static void setDisplayName(AuthorConfiguration config, Author author, String displayName) {
author.setDisplayName(!displayName.isEmpty() ? displayName : author.getGitId());
config.setAuthorDisplayName(author, !displayName.isEmpty() ? displayName : author.getGitId());
}
/**
* Associates {@code gitHubId} and additional {@code aliases} to {@code author}.
*/
private static void setAliases(AuthorConfiguration config, Author author, String gitHubId, List<String> aliases) {
config.addAuthorEmailsAndAliasesMapEntry(author, Arrays.asList(gitHubId));
if (aliases.isEmpty()) {
return;
}
config.addAuthorEmailsAndAliasesMapEntry(author, aliases);
author.setAuthorAliases(aliases);
}
/**
* Sets the list of globs to ignore for the {@code author} for file analysis.
*/
private static void setAuthorIgnoreGlobList(Author author, List<String> ignoreGlobList) {
if (ignoreGlobList.isEmpty()) {
return;
}
author.setIgnoreGlobList(ignoreGlobList);
}
}