-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCloneRepositoryWizard.java
More file actions
229 lines (208 loc) · 9.76 KB
/
CloneRepositoryWizard.java
File metadata and controls
229 lines (208 loc) · 9.76 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright 2011-2017 Amazon Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package com.amazonaws.eclipse.codecommit.wizard;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import com.amazonaws.eclipse.codecommit.CodeCommitAnalytics;
import com.amazonaws.eclipse.codecommit.CodeCommitPlugin;
import com.amazonaws.eclipse.codecommit.CodeCommitAnalytics.EventResult;
import com.amazonaws.eclipse.codecommit.credentials.GitCredential;
import com.amazonaws.eclipse.codecommit.credentials.GitCredentialsManager;
import com.amazonaws.eclipse.codecommit.pages.GitCredentialsConfigurationPage;
import com.amazonaws.eclipse.core.AwsToolkitCore;
import com.amazonaws.eclipse.core.egit.GitRepositoryInfo;
import com.amazonaws.eclipse.core.egit.RepositorySelection;
import com.amazonaws.eclipse.core.egit.UIText;
import com.amazonaws.eclipse.core.egit.jobs.CloneGitRepositoryJob;
import com.amazonaws.eclipse.core.egit.jobs.ImportProjectJob;
import com.amazonaws.eclipse.core.egit.ui.CloneDestinationPage;
import com.amazonaws.eclipse.core.egit.ui.SourceBranchPage;
import com.amazonaws.eclipse.core.exceptions.AwsActionException;
import com.amazonaws.eclipse.core.model.GitCredentialsDataModel;
import com.amazonaws.eclipse.core.regions.RegionUtils;
import com.amazonaws.eclipse.core.telemetry.AwsToolkitMetricType;
import com.amazonaws.eclipse.core.util.WorkbenchUtils;
import com.amazonaws.services.codecommit.AWSCodeCommit;
import com.amazonaws.services.codecommit.model.GetRepositoryRequest;
import com.amazonaws.services.codecommit.model.RepositoryMetadata;
public class CloneRepositoryWizard extends Wizard implements IImportWizard {
protected IWorkbench workbench;
//dummy
private final AWSCodeCommit client;
private final String repositoryName;
private final String currentProfile;
private final GitCredentialsDataModel dataModel = new GitCredentialsDataModel();
protected GitCredentialsConfigurationPage credentialConfigPage;
// a page for repository branch selection
protected SourceBranchPage sourceBranchPage;
// a page for selection of the clone destination
protected CloneDestinationPage cloneDestinationPage;
private GitRepositoryInfo gitRepositoryInfo;
public CloneRepositoryWizard(String accountId, String regionId, String repositoryName) throws URISyntaxException {
super();
if (accountId == null) {
accountId = AwsToolkitCore.getDefault().getCurrentAccountId();
}
if (regionId == null) {
regionId = RegionUtils.getCurrentRegion().getId();
}
this.client = AwsToolkitCore.getClientFactory(accountId).getCodeCommitClientByRegion(regionId);
this.repositoryName = repositoryName;
this.currentProfile = AwsToolkitCore.getDefault().getAccountManager().getAccountInfo(accountId).getAccountName();
setWindowTitle("Clone AWS CodeCommit Repository");
setNeedsProgressMonitor(true);
GitCredential credential = GitCredentialsManager.getGitCredential(currentProfile);
if (credential != null) {
dataModel.setUsername(credential.getUsername());
dataModel.setPassword(credential.getPassword());
}
dataModel.setUserAccount(accountId);
dataModel.setRegionId(regionId);
}
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
this.workbench = workbench;
}
@Override
public final void addPages() {
if (!CodeCommitPlugin.currentProfileGitCredentialsConfigured()) {
credentialConfigPage = new GitCredentialsConfigurationPage(dataModel);
addPage(credentialConfigPage);
}
sourceBranchPage = createSourceBranchPage();
cloneDestinationPage = createCloneDestinationPage();
addPage(sourceBranchPage);
addPage(cloneDestinationPage);
}
@Override
public boolean performFinish() {
try {
final File destinationFile = cloneDestinationPage.getDestinationFile();
getContainer().run(true, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
monitor.subTask("Cloning repository...");
GitCredentialsManager.getGitCredentials().put(currentProfile,
new GitCredential(dataModel.getUsername(), dataModel.getPassword()));
GitCredentialsManager.saveGitCredentials();
try {
new CloneGitRepositoryJob(CloneRepositoryWizard.this, sourceBranchPage, cloneDestinationPage, getTheGitRepositoryInfo())
.execute(monitor);
} catch (URISyntaxException e) {
throw new InvocationTargetException(e);
}
monitor.subTask("Importing project...");
IFile fileToOpen = new ImportProjectJob(repositoryName, destinationFile)
.execute(monitor);
if (fileToOpen != null) {
WorkbenchUtils.selectAndReveal(fileToOpen, workbench); // show in explorer
WorkbenchUtils.openFileInEditor(fileToOpen, workbench); // show in editor
}
monitor.done();
}
});
} catch (InvocationTargetException e) {
CodeCommitAnalytics.trackCloneRepository(EventResult.FAILED);
CodeCommitPlugin.getDefault().reportException("Failed to clone git repository.",
new AwsActionException(AwsToolkitMetricType.EXPLORER_CODECOMMIT_CLONE_REPO.getName(), e.getMessage(), e));
return false;
} catch (InterruptedException e) {
CodeCommitAnalytics.trackCloneRepository(EventResult.CANCELED);
CodeCommitPlugin.getDefault().reportException(
UIText.GitCreateProjectViaWizardWizard_AbortedMessage, e);
return false;
}
CodeCommitAnalytics.trackCloneRepository(EventResult.SUCCEEDED);
return true;
}
@Override
public boolean performCancel() {
CodeCommitAnalytics.trackCloneRepository(EventResult.CANCELED);
return super.performCancel();
}
private SourceBranchPage createSourceBranchPage() {
return new SourceBranchPage() {
@Override
public void setVisible(boolean visible) {
if (visible) {
try {
setSelection(getRepositorySelection());
setCredentials(getTheGitRepositoryInfo().getCredentials());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
super.setVisible(visible);
}
};
}
private CloneDestinationPage createCloneDestinationPage() {
return new CloneDestinationPage() {
@Override
public void setVisible(boolean visible) {
if (visible)
setSelection(getRepositorySelection(),
sourceBranchPage.getAvailableBranches(),
sourceBranchPage.getSelectedBranches(),
sourceBranchPage.getHEAD());
super.setVisible(visible);
}
};
}
/**
* @return the repository specified in the data model.
*/
private RepositorySelection getRepositorySelection() {
try {
return new RepositorySelection(new URIish(getTheGitRepositoryInfo().getCloneUri()), null);
} catch (URISyntaxException e) {
CodeCommitPlugin.getDefault().reportException(
UIText.GitImportWizard_errorParsingURI, e);
return null;
} catch (Exception e) {
CodeCommitPlugin.getDefault().reportException(e.getMessage(), e);
return null;
}
}
private GitRepositoryInfo getTheGitRepositoryInfo() throws URISyntaxException {
if (gitRepositoryInfo == null) {
RepositoryMetadata metadata = client.getRepository(
new GetRepositoryRequest()
.withRepositoryName(repositoryName))
.getRepositoryMetadata();
this.gitRepositoryInfo = createGitRepositoryInfo(
metadata.getCloneUrlHttp(), metadata.getRepositoryName(),
dataModel.getUsername(), dataModel.getPassword());
}
return this.gitRepositoryInfo;
}
private static GitRepositoryInfo createGitRepositoryInfo(String httpUrl, String repoName, String username, String password) throws URISyntaxException {
GitRepositoryInfo info = new GitRepositoryInfo(httpUrl);
info.setRepositoryName(repoName);
info.setShouldSaveCredentialsInSecureStore(true);
info.setCredentials(username, password);
return info;
}
}