Skip to content

Commit 56aae03

Browse files
committed
Support for GitHub Enterprise #50
1 parent c9d1e31 commit 56aae03

11 files changed

Lines changed: 127 additions & 32 deletions

File tree

src/main/java/com/junichi11/netbeans/modules/github/issues/GitHubCache.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,23 @@ public synchronized List<Repository> getForks(boolean force) {
359359
* Get the user for the OAuth Token.
360360
*
361361
* @param oAuthToken OAuth token
362+
* @param hostname hostname (for GitHub Enterprize)
362363
* @return User if it can be got, otherwise {@code null}
363364
*/
364365
@CheckForNull
365-
public static synchronized User getUser(String oAuthToken) {
366+
public static synchronized User getUser(String oAuthToken, String hostname) {
366367
if (StringUtils.isEmpty(oAuthToken)) {
367368
return null;
368369
}
369370

370371
User user = USERS.get(oAuthToken);
371372
if (user == null) {
372-
GitHubClient client = new GitHubClient().setOAuth2Token(oAuthToken);
373+
GitHubClient client;
374+
if (hostname == null || hostname.isEmpty()) {
375+
client = new GitHubClient().setOAuth2Token(oAuthToken);
376+
} else {
377+
client = new GitHubClient(hostname).setOAuth2Token(oAuthToken);
378+
}
373379
UserService userService = new UserService(client);
374380
try {
375381
user = userService.getUser();

src/main/java/com/junichi11/netbeans/modules/github/issues/GitHubIssues.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepository;
5353
import com.junichi11.netbeans.modules.github.issues.repository.GitHubRepositoryProvider;
5454
import java.awt.Color;
55+
import org.eclipse.egit.github.core.client.IGitHubConstants;
5556
import org.netbeans.modules.bugtracking.issuetable.IssueNode;
5657
import org.netbeans.modules.bugtracking.spi.BugtrackingSupport;
5758
import org.openide.util.RequestProcessor;
@@ -78,11 +79,13 @@ public final class GitHubIssues {
7879
public static final Color OPEN_STATE_COLOR = Color.decode("#6cc644"); // NOI18N
7980
public static final Color CLOSED_STATE_COLOR = Color.decode("#bd2c00"); // NOI18N
8081
public static final Color MERGED_STATE_COLOR = Color.decode("#6e5494"); // NOI18N
82+
83+
public static final String DEFAULT_HOSTNAME = IGitHubConstants.HOST_API;
8184
// url formats
8285
/**
83-
* https://github.com/[owner]/[repository]/raw/[sha]/[file name]
86+
* https://[hostname]/[owner]/[repository]/raw/[sha]/[file name]
8487
*/
85-
public static final String RAW_URL_FORMAT = "https://github.com/%s/%s/raw/%s/%s"; // NOI18N
88+
public static final String RAW_URL_FORMAT = "https://%s/%s/%s/raw/%s/%s"; // NOI18N
8689

8790
private static final GitHubIssues INSTANCE = new GitHubIssues();
8891

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/FileChangedPanel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,18 @@ public class FileChangedPanel extends javax.swing.JPanel {
7979
private static final long serialVersionUID = 5842222768556582282L;
8080
private static final Logger LOGGER = Logger.getLogger(FileChangedPanel.class.getName());
8181

82+
private final String hostname;
8283
private final CommitFile file;
8384
private final PullRequestMarker marker;
8485
private Component diffView;
8586

8687
/**
8788
* Creates new form FileChendedPanel
8889
*/
89-
public FileChangedPanel(CommitFile file, PullRequestMarker marker) {
90+
public FileChangedPanel(CommitFile file, PullRequestMarker marker, String hostname) {
9091
this.file = file;
9192
this.marker = marker;
93+
this.hostname = hostname;
9294
initComponents();
9395
String filename = file.getFilename();
9496
fileNameLabel.setText(filename);
@@ -200,7 +202,7 @@ public void run() {
200202
try {
201203
handle.start();
202204
URL pullRequestRawUrl = new URL(rawUrl);
203-
URL baseRawUrl = new URL(String.format(GitHubIssues.RAW_URL_FORMAT, owner.getLogin(), name, baseSha, filename));
205+
URL baseRawUrl = new URL(String.format(GitHubIssues.RAW_URL_FORMAT, hostname, owner.getLogin(), name, baseSha, filename));
204206
InputStream prInputStream;
205207
InputStream baseInputStream;
206208
try {

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/FilesChangedPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public void setDetails(PullRequest pullRequest) {
130130
diffUrl = pullRequest.getDiffUrl();
131131
}
132132

133-
public void addFile(CommitFile file, PullRequestMarker marker) {
133+
public void addFile(CommitFile file, PullRequestMarker marker, String hostname) {
134134
assert EventQueue.isDispatchThread();
135135
if (file == null) {
136136
return;
137137
}
138-
FileChangedPanel fileChangedPanel = new FileChangedPanel(file, marker);
138+
FileChangedPanel fileChangedPanel = new FileChangedPanel(file, marker, hostname);
139139
filesPanel.add(fileChangedPanel);
140140
}
141141

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/GitHubIssuePanel.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import org.eclipse.egit.github.core.PullRequestMarker;
101101
import org.eclipse.egit.github.core.RepositoryCommit;
102102
import org.eclipse.egit.github.core.User;
103+
import org.eclipse.egit.github.core.client.IGitHubConstants;
103104
import org.eclipse.egit.github.core.service.IssueService;
104105
import org.netbeans.api.annotations.common.CheckForNull;
105106
import org.openide.DialogDescriptor;
@@ -438,8 +439,12 @@ public void update() {
438439
List<CommitFile> pullRequestsFiles = repository.getPullRequestsFiles(issue.getNumber());
439440
filesChangedPanel.setDisplayName(String.format("[Diff] #%s - %s", id, summary)); // NOI18N
440441
filesChangedPanel.removeAllFiles();
442+
String hostname = repository.getHostname();
441443
for (CommitFile file : pullRequestsFiles) {
442-
filesChangedPanel.addFile(file, base);
444+
if (hostname.equals(GitHubIssues.DEFAULT_HOSTNAME)) {
445+
hostname = IGitHubConstants.HOST_DEFAULT;
446+
}
447+
filesChangedPanel.addFile(file, base, hostname);
443448
}
444449
filesChangedPanel.setDetails(pullRequest);
445450
filesChangedcollapsibleSectionPanel.setLabel(Bundle.GitHubIssuePanel_files_changed_count(pullRequestsFiles.size()));

src/main/java/com/junichi11/netbeans/modules/github/issues/repository/GitHubRepository.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public class GitHubRepository {
116116

117117
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
118118
private Set<GitHubQuery> queries = null;
119+
private static final String PROPERTY_HOSTNAME = "github.issues.hostname"; // NOI18N
119120
private static final String PROPERTY_OAUTH_TOKEN = "github.issues.oauth.token"; // NOI18N
120121
private static final String PROPERTY_REPOSITORY_AUTHOR = "github.issues.repository.author"; // NOI18N
121122
private static final String PROPERTY_REPOSITORY_NAME = "github.issues.repository.name"; // NOI18N
@@ -192,7 +193,7 @@ public GitHubClient createGitHubClient() {
192193
} else {
193194
oauthToken = getOAuthToken();
194195
}
195-
GitHubClient client = new GitHubClient().setOAuth2Token(oauthToken);
196+
GitHubClient client = new GitHubClient(getHostname()).setOAuth2Token(oauthToken);
196197
return client;
197198
} catch (IOException ex) {
198199
// show repository panel
@@ -226,6 +227,15 @@ public String getUserName() {
226227
return repositoryInfo.getUsername();
227228
}
228229

230+
/**
231+
* Get hostname.
232+
*
233+
* @return hostname
234+
*/
235+
public String getHostname() {
236+
return getPropertyValue(PROPERTY_HOSTNAME);
237+
}
238+
229239
/**
230240
* Get OAuth token.
231241
*
@@ -995,13 +1005,14 @@ public Repository getRepository() {
9951005
}
9961006

9971007
public void setRepositoryInfo(GitHubRepositoryInfo githubRepositoryInfo) {
998-
String url = String.format("https://github.com/%s/%s/issues/", githubRepositoryInfo.getRepositoryAuthor(), githubRepositoryInfo.getRepositoryName()); // NOI18N
1008+
String url = String.format("https://%s/%s/%s/issues/", githubRepositoryInfo.getHostname(), githubRepositoryInfo.getRepositoryAuthor(), githubRepositoryInfo.getRepositoryName()); // NOI18N
9991009
repositoryInfo = createRepositoryInfo(githubRepositoryInfo, url, githubRepositoryInfo.getUserName(), null, null, null);
10001010
setProperties(githubRepositoryInfo);
10011011
}
10021012

10031013
private void setProperties(GitHubRepositoryInfo githubRepositoryInfo) {
10041014
if (repositoryInfo != null) {
1015+
repositoryInfo.putValue(PROPERTY_HOSTNAME, githubRepositoryInfo.getHostname());
10051016
repositoryInfo.putValue(PROPERTY_OAUTH_TOKEN, githubRepositoryInfo.getOAuthToken());
10061017
repositoryInfo.putValue(PROPERTY_BOOLEAN_PROPERTY_FILE, String.valueOf(githubRepositoryInfo.isPropertyFile()));
10071018
repositoryInfo.putValue(PROPERTY_REPOSITORY_AUTHOR, githubRepositoryInfo.getRepositoryAuthor());
@@ -1042,12 +1053,17 @@ private void fireUnsubmittedIssueChanged() {
10421053
propertyChangeSupport.firePropertyChange(RepositoryProvider.EVENT_UNSUBMITTED_ISSUES_CHANGED, null, null);
10431054
}
10441055

1045-
public static List<Repository> getRepositories(String oauthToken) {
1046-
return getRepositories(oauthToken, false);
1056+
public static List<Repository> getRepositories(String oauthToken, String hostname) {
1057+
return getRepositories(oauthToken, false, hostname);
10471058
}
10481059

1049-
public static List<Repository> getRepositories(String oauthToken, boolean withParent) {
1050-
GitHubClient client = new GitHubClient().setOAuth2Token(oauthToken);
1060+
public static List<Repository> getRepositories(String oauthToken, boolean withParent, String hostname) {
1061+
GitHubClient client;
1062+
if (hostname == null || hostname.isEmpty()) {
1063+
client = new GitHubClient().setOAuth2Token(oauthToken);
1064+
} else {
1065+
client = new GitHubClient(hostname).setOAuth2Token(oauthToken);
1066+
}
10511067
RepositoryService repositoryService = new RepositoryService(client);
10521068
List<Repository> repositories = new ArrayList<>();
10531069
try {

src/main/java/com/junichi11/netbeans/modules/github/issues/repository/GitHubRepositoryController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public String getErrorMessage() {
9797
public void applyChanges() {
9898
GitHubRepositoryPanel p = getPanel();
9999
GitHubRepositoryInfo gitHubRepositoryInfo = new GitHubRepositoryInfo()
100+
.setHostname(p.getHostname())
100101
.setDisplayName(p.getDisplayName())
101102
.setUserName(p.getUserName())
102103
.setOAuthToken(p.getOAuthToken())

src/main/java/com/junichi11/netbeans/modules/github/issues/repository/GitHubRepositoryInfo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@
4747
*/
4848
public final class GitHubRepositoryInfo {
4949

50+
private String hostname;
5051
private String displayName;
5152
private String oauthToken;
5253
private String userName;
5354
private String repositoryAuthor;
5455
private String repositoryName;
5556
private boolean isPropertyFile;
5657

58+
public GitHubRepositoryInfo setHostname(String hostname) {
59+
this.hostname = hostname;
60+
return this;
61+
}
62+
5763
public GitHubRepositoryInfo setDisplayName(String displayName) {
5864
this.displayName = displayName;
5965
return this;
@@ -84,6 +90,10 @@ public GitHubRepositoryInfo setRepositoryName(String repositoryName) {
8490
return this;
8591
}
8692

93+
String getHostname() {
94+
return hostname;
95+
}
96+
8797
String getDisplayName() {
8898
return displayName;
8999
}

src/main/java/com/junichi11/netbeans/modules/github/issues/repository/ui/GitHubRepositoryPanel.form

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@
1616
<Layout>
1717
<DimensionLayout dim="0">
1818
<Group type="103" groupAlignment="0" attributes="0">
19+
<Group type="102" attributes="0">
20+
<Component id="propertyFileCheckBox" min="-2" max="-2" attributes="0"/>
21+
<EmptySpace pref="110" max="32767" attributes="0"/>
22+
<Component id="connectButton" min="-2" max="-2" attributes="0"/>
23+
</Group>
1924
<Group type="102" attributes="0">
2025
<Group type="103" groupAlignment="0" attributes="0">
2126
<Component id="oauthTokenLabel" alignment="0" min="-2" max="-2" attributes="0"/>
2227
<Component id="displayNameLabel" alignment="0" min="-2" max="-2" attributes="0"/>
2328
<Component id="repositoryLabel" alignment="0" min="-2" max="-2" attributes="0"/>
2429
<Component id="userNameLabel" alignment="0" min="-2" max="-2" attributes="0"/>
30+
<Component id="hostnameLabel" alignment="0" min="-2" max="-2" attributes="0"/>
2531
</Group>
26-
<EmptySpace min="-2" max="-2" attributes="0"/>
32+
<EmptySpace max="-2" attributes="0"/>
2733
<Group type="103" groupAlignment="0" attributes="0">
28-
<Group type="102" attributes="0">
34+
<Component id="hostnameTextField" max="32767" attributes="0"/>
35+
<Group type="102" alignment="0" attributes="0">
2936
<Component id="repositoryAuthorTextField" max="32767" attributes="0"/>
3037
<EmptySpace min="-2" max="-2" attributes="0"/>
3138
<Component id="repositorySeparationLabel" min="-2" max="-2" attributes="0"/>
@@ -41,16 +48,16 @@
4148
</Group>
4249
</Group>
4350
</Group>
44-
<Group type="102" alignment="0" attributes="0">
45-
<Component id="propertyFileCheckBox" min="-2" max="-2" attributes="0"/>
46-
<EmptySpace pref="110" max="32767" attributes="0"/>
47-
<Component id="connectButton" min="-2" max="-2" attributes="0"/>
48-
</Group>
4951
</Group>
5052
</DimensionLayout>
5153
<DimensionLayout dim="1">
5254
<Group type="103" groupAlignment="0" attributes="0">
5355
<Group type="102" alignment="1" attributes="0">
56+
<Group type="103" groupAlignment="3" attributes="0">
57+
<Component id="hostnameLabel" alignment="3" min="-2" max="-2" attributes="0"/>
58+
<Component id="hostnameTextField" alignment="3" min="-2" max="-2" attributes="0"/>
59+
</Group>
60+
<EmptySpace max="-2" attributes="0"/>
5461
<Group type="103" groupAlignment="3" attributes="0">
5562
<Component id="oauthTokenLabel" alignment="3" min="-2" max="-2" attributes="0"/>
5663
<Component id="oauthTokenTextField" alignment="3" min="-2" max="-2" attributes="0"/>
@@ -183,5 +190,19 @@
183190
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="addRepositoryButtonActionPerformed"/>
184191
</Events>
185192
</Component>
193+
<Component class="javax.swing.JLabel" name="hostnameLabel">
194+
<Properties>
195+
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
196+
<ResourceString bundle="com/junichi11/netbeans/modules/github/issues/repository/ui/Bundle.properties" key="GitHubRepositoryPanel.hostnameLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
197+
</Property>
198+
</Properties>
199+
</Component>
200+
<Component class="javax.swing.JTextField" name="hostnameTextField">
201+
<Properties>
202+
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
203+
<ResourceString bundle="com/junichi11/netbeans/modules/github/issues/repository/ui/Bundle.properties" key="GitHubRepositoryPanel.hostnameTextField.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
204+
</Property>
205+
</Properties>
206+
</Component>
186207
</SubComponents>
187208
</Form>

0 commit comments

Comments
 (0)