-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsanaExport.java
179 lines (161 loc) · 8.07 KB
/
AsanaExport.java
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
package net.florianschoppmann.issuetracking;
import com.asana.Client;
import com.asana.models.Project;
import com.asana.models.Workspace;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import joptsimple.util.PathConverter;
import net.florianschoppmann.issuetracking.asana.AsyncAttachmentDownloader;
import net.florianschoppmann.issuetracking.asana.Export;
import net.florianschoppmann.issuetracking.conversion.AsanaToYouTrack;
import net.florianschoppmann.java.futures.Futures;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.http.HttpClient;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.xml.bind.JAXBException;
public final class AsanaExport {
private final Logger log = LoggerFactory.getLogger(getClass());
private final String workspaceName;
private final String projectName;
private final String youTrackProjectAbbrev;
private final Client asanaClient;
private final AsanaToYouTrack asanaToYouTrack;
private final Serialization serialization;
private final Map<String, String> emailToLoginNameMap;
private final Path attachmentBasePath;
private final AsanaToYouTrack.Options options;
private AsanaExport(String workspaceName, String projectName, String youTrackProjectAbbrev, Client asanaClient,
AsanaToYouTrack asanaToYouTrack, Serialization serialization, Map<String, String> emailToLoginNameMap,
Path attachmentBasePath, AsanaToYouTrack.Options options) {
this.workspaceName = workspaceName;
this.projectName = projectName;
this.youTrackProjectAbbrev = youTrackProjectAbbrev;
this.asanaClient = asanaClient;
this.asanaToYouTrack = asanaToYouTrack;
this.serialization = serialization;
this.emailToLoginNameMap = emailToLoginNameMap;
this.attachmentBasePath = attachmentBasePath;
this.options = options;
}
private void run() throws IOException, JAXBException {
Workspace workspace = null;
for (Workspace currentWorkspace : asanaClient.workspaces.findAll()) {
if (workspaceName.equals(currentWorkspace.name)) {
workspace = currentWorkspace;
break;
}
}
if (workspace == null) {
log.error("Could not find workspace '{}'.\n", workspaceName);
System.exit(1);
return;
}
Project project = null;
for (Project currentProject : asanaClient.projects.findByWorkspace(workspace.id)) {
if (projectName.equals(currentProject.name)) {
project = currentProject;
break;
}
}
if (project == null) {
log.error("Could not find project '{}'.\n", projectName);
System.exit(1);
return;
}
Files.createDirectories(attachmentBasePath);
AsanaToYouTrack.Result result = asanaToYouTrack.youTrackFromAsanaProject(project.id, attachmentBasePath,
youTrackProjectAbbrev, emailToLoginNameMap, options);
for (CompletableFuture<Path> download : result.getDownloads()) {
Futures.unwrapCompletionException(download).join();
}
serialization.writeResultXml(result.getIssues());
serialization.writeResultXml(result.getLinks());
serialization.writeResultXml(result.getAttachments());
serialization.writeResultXml(result.getIssueUpdates());
serialization.writeResultXml(result.getExportWarnings());
serialization.writeResultXml(result.getConversionWarnings());
var importSettings = new ImportSettings();
importSettings.youTrackProjectAbbrev = youTrackProjectAbbrev;
importSettings.importIssues = true;
importSettings.importLinks = true;
importSettings.importAttachments = true;
importSettings.updateIssues = true;
serialization.writeResultXml(importSettings);
}
private static Map<String, String> userMapping(Path userMappingFile) throws IOException {
return Files.lines(userMappingFile)
.filter(line -> line.contains("="))
.map(line -> line.split("="))
.collect(Collectors.toMap(array -> array[0].toLowerCase(), array -> array[1]));
}
private static void start(String workspaceName, String projectName, String youTrackProjectAbbrev,
Path userMappingFile, Path attachmentBasePath, boolean estimatesInBrackets, int startId,
String asanaAccessToken) throws IOException, JAXBException {
// Create all dependencies
Client asanaClient = Client.accessToken(asanaAccessToken);
Serialization serialization = Serialization.defaultSerialization(attachmentBasePath);
var executor = new ThreadPoolExecutor(4, 4, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
executor.allowCoreThreadTimeOut(true);
HttpClient httpClient = HttpClient.newBuilder()
.executor(executor)
.build();
var attachmentDownloader = new AsyncAttachmentDownloader(httpClient);
var export = new Export(asanaClient, attachmentDownloader);
var asanaToYouTrack = new AsanaToYouTrack(export);
var options = new AsanaToYouTrack.Options(estimatesInBrackets, startId);
var asanaDownloader = new AsanaExport(workspaceName, projectName, youTrackProjectAbbrev, asanaClient,
asanaToYouTrack, serialization, userMapping(userMappingFile), attachmentBasePath, options);
try {
asanaDownloader.run();
} finally {
executor.shutdown();
}
}
public static void main(String[] args) throws IOException, JAXBException {
final Logger log = LoggerFactory.getLogger(AsanaExport.class);
OptionParser parser = new OptionParser();
OptionSpec<Void> helpOption = parser.accepts("help").forHelp();
OptionSpec<String> workspaceOpt
= parser.accepts("workspace", "name of the Asana workspace").withRequiredArg().required();
OptionSpec<String> asanaProjectOpt
= parser.accepts("project", "name of the Asana project").withRequiredArg().required();
OptionSpec<String> youTrackAbbrevOpt = parser
.accepts("abbrev", "YouTrack project abbreviation, should not contain hyphen (-)")
.withRequiredArg().required();
OptionSpec<Path> userMappingOpt = parser.accepts("user-mapping",
"path to file with lines of form \"<Asana email>=<YouTrack login name>\"")
.withRequiredArg().withValuesConvertedBy(new PathConverter()).required();
OptionSpec<Path> outputOpt = parser.accepts("output", "path where to store output")
.withRequiredArg().withValuesConvertedBy(new PathConverter()).required();
OptionSpec<Void> noTimeEstimatesInBracketsOpt = parser
.accepts("no-estimates", "in task names, do not treat numbers in brackets as time estimates");
OptionSpec<Integer> startIdOpt = parser.accepts("start-id", "first number in project")
.withRequiredArg().ofType(Integer.class).defaultsTo(1);
OptionSet options = parser.parse(args);
if (options.has(helpOption)) {
parser.printHelpOn(System.out);
System.exit(0);
return;
}
String asanaAccessToken = System.getenv("ASANA_ACCESS_TOKEN");
if (asanaAccessToken == null) {
log.error(
"Environment variable ASANA_ACCESS_TOKEN must be defined and contain a valid access token for Asana.");
System.exit(1);
return;
}
start(options.valueOf(workspaceOpt), options.valueOf(asanaProjectOpt), options.valueOf(youTrackAbbrevOpt),
options.valueOf(userMappingOpt), options.valueOf(outputOpt), !options.has(noTimeEstimatesInBracketsOpt),
options.valueOf(startIdOpt), asanaAccessToken);
}
}