Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.

Added support for hosted github domains #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ configure(allprojects) {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/ebr-maven-external" }
mavenLocal()
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public interface UserOperations {
* @return list of users the given user is following
*/
List<GitHubUser> getFollowing(String user);

/**
* Public operation to return the repos being watched by given user.
*
* @param user GitHub user
* @return list of repos being watched by given user
*/
List<GitHubRepo> getWatching(String user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.social.github.api.impl;

import org.springframework.social.MissingAuthorizationException;
import org.springframework.social.github.connect.GitHubHostUtils;

/**
* <p>
Expand All @@ -26,11 +27,17 @@
*/
class AbstractGitHubOperations {
private final boolean isAuthorized;
private String gitHubHost;

public AbstractGitHubOperations(boolean isAuthorized) {
this.isAuthorized = isAuthorized;
}

public AbstractGitHubOperations(boolean isAuthorized, String gitHubHost) {
this.isAuthorized = isAuthorized;
this.gitHubHost = gitHubHost;
}

protected void requireAuthorization() {
if (!isAuthorized) {
throw new MissingAuthorizationException("github");
Expand All @@ -40,9 +47,7 @@ protected void requireAuthorization() {
// Using String here instead of URI so I can include braces in the path. See, e.g., RepoTemplate. [WLW]
protected String buildUri(String path) {
// return URIBuilder.fromUri(API_URL_BASE + path).build();
return API_URL_BASE + path;
return GitHubHostUtils.getGitHubApi(gitHubHost) + path;
}

// GitHub API v3
private static final String API_URL_BASE = "https://api.github.com/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public GistTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
super(isAuthorizedForUser);
this.restTemplate = restTemplate;
}

/**
* @param restTemplate A RestTemplate
* @param isAuthorizedForUser Boolean indicating whether the RestTemplate is authorized for a user
* @param gitHubHost github host
*/
public GistTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser, String gitHubHost) {
super(isAuthorizedForUser,gitHubHost);
this.restTemplate = restTemplate;
}

public List<GitHubGist> getUserGists(String user) {
return asList(restTemplate.getForObject(buildUri("users/{user}/gists"), GitHubGist[].class, user));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,70 @@
* <p>
* The central class for interacting with GitHub.
* </p>
*
* @author Craig Walls
* @author Willie Wheeler ([email protected])
* @author Andy Wilkinson
*/
public class GitHubTemplate extends AbstractOAuth2ApiBinding implements GitHub {
private GistOperations gistOperations;
private RepoOperations repoOperations;
private UserOperations userOperations;

/**
* No-arg constructor to support cases in which you want to call the GitHub
* API without requiring authorization. This is useful for public operations,
* such as getting the list of watchers for a public repository.
*/
public GitHubTemplate() {
super();
initSubApis();
}

/**
* Constructs a GitHubTemplate with the minimal amount of information
* required to sign requests with an OAuth <code>Authorization</code>
* header.
*
* @param accessToken
* An access token granted to the application after OAuth
* authentication.
*/
public GitHubTemplate(String accessToken) {
super(accessToken);
initSubApis();
}

@Override
protected OAuth2Version getOAuth2Version() {
return OAuth2Version.BEARER;
}

public GistOperations gistOperations() {
return gistOperations;
}

public RepoOperations repoOperations() {
return repoOperations;
}

public UserOperations userOperations() {
return userOperations;
}

public RestOperations restOperations() {
return getRestTemplate();
}
private GistOperations gistOperations;

private RepoOperations repoOperations;

private UserOperations userOperations;

/**
* No-arg constructor to support cases in which you want to call the GitHub API without requiring authorization.
* This is useful for public operations, such as getting the list of watchers for a public repository.
*/
public GitHubTemplate() {
super();
initSubApis(null);
}

/**
* Constructs a GitHubTemplate with the minimal amount of information required to sign requests with an OAuth
* <code>Authorization</code> header.
*
* @param accessToken An access token granted to the application after OAuth authentication.
*/
public GitHubTemplate(String accessToken) {
super(accessToken);
initSubApis(null);
}

/**
* Constructs a GitHubTemplate with the minimal amount of information required to sign requests with an OAuth
* <code>Authorization</code> header.
*
* @param accessToken An access token granted to the application after OAuth authentication.
* @param gitHubHost github host
*/
public GitHubTemplate(String accessToken, String gitHubHost) {
super(accessToken);
initSubApis(gitHubHost);
}

@Override
protected OAuth2Version getOAuth2Version() {
return OAuth2Version.BEARER;
}

public GistOperations gistOperations() {
return gistOperations;
}

public RepoOperations repoOperations() {
return repoOperations;
}

public UserOperations userOperations() {
return userOperations;
}

public RestOperations restOperations() {
return getRestTemplate();
}

@Override
protected MappingJackson2HttpMessageConverter getJsonMessageConverter() {
Expand All @@ -94,11 +105,11 @@ protected MappingJackson2HttpMessageConverter getJsonMessageConverter() {
}

// internal helpers
private void initSubApis() {
this.gistOperations = new GistTemplate(getRestTemplate(), isAuthorized());
this.repoOperations = new RepoTemplate(getRestTemplate(), isAuthorized());
this.userOperations = new UserTemplate(getRestTemplate(), isAuthorized());
}

private void initSubApis(String githubHost) {
this.gistOperations = new GistTemplate(getRestTemplate(), isAuthorized(), githubHost);
this.repoOperations = new RepoTemplate(getRestTemplate(), isAuthorized(), githubHost);
this.userOperations = new UserTemplate(getRestTemplate(), isAuthorized(), githubHost);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public RepoTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
this.restTemplate = restTemplate;
}

/**
* @param restTemplate A RestTemplate
* @param isAuthorizedForUser Boolean indicating whether the RestTemplate is authorized for a user
* @param gitHubHost github host
*/
public RepoTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser, String gitHubHost) {
super(isAuthorizedForUser,gitHubHost);
this.restTemplate = restTemplate;
}

public GitHubRepo getRepo(String user, String repo) {
return restTemplate.getForObject(buildRepoUri(""), GitHubRepo.class, user, repo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Locale;
import java.util.Map;

import org.springframework.social.github.api.GitHubRepo;
import org.springframework.social.github.api.GitHubUser;
import org.springframework.social.github.api.GitHubUserProfile;
import org.springframework.social.github.api.UserOperations;
Expand All @@ -37,54 +38,67 @@
*
* @author Willie Wheeler ([email protected])
* @author Andy Wilkinson
* @author Vinayak Hulawale
*/
public class UserTemplate extends AbstractGitHubOperations implements UserOperations {

private final RestTemplate restTemplate;

/**
* @param restTemplate A RestTemplate
* @param isAuthorizedForUser Boolean indicating whether the RestTemplate is authorized for a user
*/
public UserTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
super(isAuthorizedForUser);
this.restTemplate = restTemplate;
}

public List<GitHubUser> getFollowers(String user) {
return asList(restTemplate.getForObject(buildUserUri("/followers"), GitHubUser[].class, user));
}

public List<GitHubUser> getFollowing(String user) {
return asList(restTemplate.getForObject(buildUserUri("/following"), GitHubUser[].class, user));
}

public String getProfileId() {
return getUserProfile().getLogin();
}

public GitHubUserProfile getUserProfile() {
return restTemplate.getForObject(buildUri("user"), GitHubUserProfile.class);
}

public String getProfileUrl() {
return "https://github.com/" + getUserProfile().getLogin();
}



private String buildUserUri(String path) {
return buildUri("users/{user}" + path);
}

private Date toDate(String dateString, DateFormat dateFormat) {
try {
return dateFormat.parse(dateString);
} catch (ParseException e) {
return null;
}
}

private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z", Locale.ENGLISH);

private final RestTemplate restTemplate;

/**
* @param restTemplate A RestTemplate
* @param isAuthorizedForUser Boolean indicating whether the RestTemplate is authorized for a user
*/
public UserTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
super(isAuthorizedForUser);
this.restTemplate = restTemplate;
}

/**
* @param restTemplate A RestTemplate
* @param isAuthorizedForUser Boolean indicating whether the RestTemplate is authorized for a user
* @param gitHubHost github host
*/
public UserTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser, String gitHubHost) {
super(isAuthorizedForUser, gitHubHost);
this.restTemplate = restTemplate;
}

public List<GitHubUser> getFollowers(String user) {
return asList(restTemplate.getForObject(buildUserUri("/followers"), GitHubUser[].class, user));
}

public List<GitHubUser> getFollowing(String user) {
return asList(restTemplate.getForObject(buildUserUri("/following"), GitHubUser[].class, user));
}

public List<GitHubRepo> getWatching(String user) {
return asList(restTemplate.getForObject(buildUserUri("/subscriptions"), GitHubRepo[].class, user));
}

public String getProfileId() {
return getUserProfile().getLogin();
}

public GitHubUserProfile getUserProfile() {
return restTemplate.getForObject(buildUri("user"), GitHubUserProfile.class);
}

public String getProfileUrl() {
return buildUri("") + getUserProfile().getLogin();
}

private String buildUserUri(String path) {
return buildUri("users/{user}" + path);
}

private Date toDate(String dateString, DateFormat dateFormat) {
try {
return dateFormat.parse(dateString);
} catch (ParseException e) {
return null;
}
}

private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z", Locale.ENGLISH);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.social.github.connect.GitHubConnectionFactory;
import org.springframework.social.github.security.GitHubAuthenticationService;
import org.springframework.social.security.provider.SocialAuthenticationService;
import org.springframework.util.StringUtils;

/**
* Implementation of {@link AbstractConnectionFactoryBeanDefinitionParser} that creates a {@link GitHubConnectionFactory}.
Expand All @@ -50,6 +51,9 @@ protected Class<? extends SocialAuthenticationService<?>> getAuthenticationServi
@Override
protected BeanDefinition getConnectionFactoryBeanDefinition(String appId, String appSecret, Map<String, Object> allAttributes) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(GitHubConnectionFactory.class).addConstructorArgValue(appId).addConstructorArgValue(appSecret);
if(StringUtils.hasText((String)allAttributes.get("app-host"))) {
builder.addConstructorArgValue((String)allAttributes.get("app-host"));
}
return builder.getBeanDefinition();
}

Expand Down
Loading