Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf2e1330f0c9a2b3d2bca541993507a374140030 (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
/*******************************************************************************
 * 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.github.internal;

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.Repository;

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

	/** BUNDLE_ID */
	public static final String BUNDLE_ID = "org.eclipse.mylyn.github.core";

	/** CONNECTOR_KIND */
	public static final String CONNECTOR_KIND = "github";

	/** HTTP_WWW_GITHUB_ORG */
	public static final String HTTP_WWW_GITHUB_ORG = "http://www.github.org";

	/** HTTP_GITHUB_COM */
	public static final String HTTP_GITHUB_COM = "http://github.com";

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

	/**
	 * 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);
	}

	/**
	 * 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 Repository getRepository(String repositoryUrl) {
		return Repository.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