Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 426e491b2fb73a1a2234966f51d47a6e226e7a7c (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
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
/*******************************************************************************
 * Copyright (c) 2011 Red Hat and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     David Green <david.green@tasktop.com> - initial contribution
 *     Christian Trutz <christian.trutz@gmail.com> - initial contribution
 *     Chris Aniszczyk <caniszczyk@gmail.com> - initial contribution
 *******************************************************************************/
package org.eclipse.mylyn.internal.github.core;

import java.io.IOException;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
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.RequestException;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.tasks.core.TaskRepository;

/**
 * GitHub class
 */
public class GitHub {

	/** BUNDLE_ID */
	public static final String BUNDLE_ID = "org.eclipse.mylyn.github.core"; //$NON-NLS-1$

	/** CONNECTOR_KIND */
	public static final String CONNECTOR_KIND = "github"; //$NON-NLS-1$

	/** HTTP_WWW_GITHUB_ORG */
	public static final String HTTP_WWW_GITHUB_ORG = "http://www.github.org"; //$NON-NLS-1$

	/** HTTP_GITHUB_COM */
	public static final String HTTP_GITHUB_COM = "http://github.com"; //$NON-NLS-1$

	/** URL_PATTERN */
	public static final Pattern URL_PATTERN = Pattern.compile("(?:" //$NON-NLS-1$
			+ Pattern.quote(HTTP_WWW_GITHUB_ORG) + "|" //$NON-NLS-1$
			+ Pattern.quote(HTTP_GITHUB_COM) + ")/([^/]+)/([^/]+)"); //$NON-NLS-1$

	/** USER_AGENT */
	public static final String USER_AGENT = "GitHubEclipse/1.3.0"; //$NON-NLS-1$

	/** REPOSITORY_SEGMENTS */
	public static final String REPOSITORY_SEGMENTS = "/user/repository"; //$NON-NLS-1$

	/**
	 * Configure client with standard configuration
	 *
	 * @param client
	 * @return given client
	 */
	public static GitHubClient configureClient(GitHubClient client) {
		return client.setUserAgent(USER_AGENT);
	}

	/**
	 * Set credentials on client from task repository
	 *
	 * @param client
	 * @param repository
	 * @return specified client
	 */
	public static GitHubClient addCredentials(GitHubClient client,
			TaskRepository repository) {
		AuthenticationCredentials credentials = repository
				.getCredentials(AuthenticationType.REPOSITORY);
		if (credentials != null)
			client.setCredentials(credentials.getUserName(),
					credentials.getPassword());
		return client;
	}

	/**
	 * Create status of severity with message
	 *
	 * @param severity
	 * @param message
	 * @return status
	 */
	public static IStatus createStatus(int severity, String message) {
		return new Status(severity, BUNDLE_ID, message);
	}

	/**
	 * Create status of severity with message and throwable
	 *
	 * @param severity
	 * @param message
	 * @param e
	 * @return status
	 */
	public static IStatus createStatus(int severity, String message, Throwable e) {
		return new Status(severity, BUNDLE_ID, message, e);
	}

	/**
	 * Create error status from message
	 *
	 * @param message
	 * @return status
	 */
	public static IStatus createErrorStatus(String message) {
		return createStatus(IStatus.ERROR, message);
	}

	/**
	 * Create error status from message and throwable
	 *
	 * @param message
	 * @param t
	 * @return status
	 */
	public static IStatus createErrorStatus(String message, Throwable t) {
		return createStatus(IStatus.ERROR, message, t);
	}

	/**
	 * Create error status from throwable
	 *
	 * @param e
	 * @return status
	 */
	public static IStatus createErrorStatus(Throwable e) {
		return createStatus(IStatus.ERROR,
				"Unexpected error: " + e.getLocalizedMessage(), e); //$NON-NLS-1$
	}

	/**
	 * Create error status from {@link IOException} that wraps it in a
	 * {@link GitHubException} if it is a {@link RequestException}
	 *
	 * @param e
	 * @return status
	 */
	public static IStatus createWrappedStatus(IOException e) {
		return createErrorStatus(GitHubException.wrap(e));
	}

	/**
	 * Get log
	 *
	 * @return log
	 */
	public static ILog getLog() {
		return Platform.getLog(Platform.getBundle(BUNDLE_ID));
	}

	/**
	 * Log message and throwable as error status
	 *
	 * @param message
	 * @param t
	 */
	public static void logError(String message, Throwable t) {
		getLog().log(createErrorStatus(message, t));
	}

	/**
	 * Log throwable as error status
	 *
	 * @param t
	 */
	public static void logError(Throwable t) {
		getLog().log(createErrorStatus(t.getMessage(), t));
	}

	/**
	 * Get repository for url
	 *
	 * @param repositoryUrl
	 * @return repository or null if not present in url
	 */
	public static RepositoryId getRepository(String repositoryUrl) {
		return RepositoryId.createFromUrl(repositoryUrl);
	}

	/**
	 * Create url with github.com host
	 *
	 * @param user
	 * @param project
	 * @return url
	 *
	 * @see #createGitHubUrlAlternate(String, String)
	 */
	public static String createGitHubUrl(String user, String project) {
		return HTTP_GITHUB_COM + '/' + user + '/' + project;
	}

	/**
	 * Create url with github.org host
	 *
	 * @param user
	 * @param project
	 * @return url
	 *
	 * @see #createGitHubUrl(String, String)
	 */
	public static String createGitHubUrlAlternate(String user, String project) {
		return HTTP_WWW_GITHUB_ORG + '/' + user + '/' + project;
	}
}

Back to the top