Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8f788cc5d5254718fe917005b74916a7eadb824 (plain) (blame)
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
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.mylyn.internal.github.ui.pr;

import java.text.MessageFormat;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.NoSuchPageException;
import org.eclipse.egit.github.core.service.IssueService;
import org.eclipse.egit.github.core.service.PullRequestService;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.core.GitHubException;
import org.eclipse.mylyn.internal.github.core.issue.IssueConnector;
import org.eclipse.mylyn.internal.github.core.pr.PullRequestConnector;
import org.eclipse.mylyn.internal.github.ui.GitHubUi;
import org.eclipse.mylyn.internal.github.ui.HttpRepositorySettingsPage;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

/**
 * Pull request task repository settings page.
 */
public class PullRequestRepositorySettingsPage extends
		HttpRepositorySettingsPage {

	private boolean syncLabel = true;
	private boolean editingUrl = false;

	/**
	 * Create pull request repository settings page
	 *
	 * @param taskRepository
	 */
	public PullRequestRepositorySettingsPage(final TaskRepository taskRepository) {
		super(Messages.PullRequestRepositorySettingsPage_Title,
				Messages.PullRequestRepositorySettingsPage_Description,
				taskRepository);
	}

	@Override
	public String getConnectorKind() {
		return PullRequestConnector.KIND;
	}

	/**
	 * Sync server URL combo with repository label editor based on default label
	 * format
	 */
	protected void syncRepositoryLabel() {
		if (syncLabel) {
			String url = serverUrlCombo.getText();
			RepositoryId repo = GitHub.getRepository(url);
			if (repo != null)
				repositoryLabelEditor.setStringValue(PullRequestConnector
						.getRepositoryLabel(repo));
		}
	}

	@Override
	protected void createAdditionalControls(Composite parent) {
		// Set the URL now, because serverURL is definitely instantiated .
		if (serverUrlCombo.getText().length() == 0) {
			String fullUrlText = GitHub.HTTP_GITHUB_COM
					+ GitHub.REPOSITORY_SEGMENTS;
			serverUrlCombo.setText(fullUrlText);
			serverUrlCombo.setFocus();
			// select the user/project part of the URL so that the user can just
			// start typing to replace the text.
			serverUrlCombo.setSelection(new Point(GitHub.HTTP_GITHUB_COM
					.length() + 1, fullUrlText.length()));

			syncRepositoryLabel();

			serverUrlCombo.addModifyListener(new ModifyListener() {

				public void modifyText(ModifyEvent e) {
					editingUrl = true;
					try {
						syncRepositoryLabel();
					} finally {
						editingUrl = false;
					}
				}
			});

			repositoryLabelEditor.getTextControl(compositeContainer)
					.addModifyListener(new ModifyListener() {

						public void modifyText(ModifyEvent e) {
							if (!editingUrl)
								syncLabel = false;
						}
					});
		} else
			serverUrlCombo.setText(PullRequestConnector.stripPulls(repository
					.getRepositoryUrl()));

		if (getRepository() == null)
			setAnonymous(false);
	}

	@Override
	protected Validator getValidator(final TaskRepository repository) {
		Validator validator = new Validator() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				monitor.beginTask(
						Messages.PullRequestRepositorySettingsPage_TaskValidating,
						100);
				monitor.subTask(Messages.PullRequestRepositorySettingsPage_TaskContacting);
				try {
					GitHubClient client = IssueConnector
							.createClient(repository);
					PullRequestService service = new PullRequestService(client);
					RepositoryId repo = GitHub.getRepository(repository
							.getRepositoryUrl());
					monitor.worked(50);
					service.pagePullRequests(repo, IssueService.STATE_OPEN, 1)
							.next();
				} catch (NoSuchPageException e) {
					String message = MessageFormat
							.format(Messages.PullRequestRepositorySettingsPage_ValidateError,
									GitHubException.wrap(e.getCause())
											.getLocalizedMessage());
					setStatus(GitHubUi.createErrorStatus(message));
					return;
				} finally {
					monitor.done();
				}

				setStatus(new Status(
						IStatus.OK,
						GitHubUi.BUNDLE_ID,
						Messages.PullRequestRepositorySettingsPage_ValidateSuccess));
			}
		};
		return validator;
	}

	/**
	 * @see org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage#applyTo(org.eclipse.mylyn.tasks.core.TaskRepository)
	 */
	public void applyTo(TaskRepository repository) {
		repository.setProperty(IRepositoryConstants.PROPERTY_CATEGORY,
				TaskRepository.CATEGORY_REVIEW);
		super.applyTo(repository);
	}

	public String getRepositoryUrl() {
		return PullRequestConnector.appendPulls(super.getRepositoryUrl());
	}
}

Back to the top