Skip to content

Commit

Permalink
Support for branches/tags/commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Fawcett authored and Andrew Fawcett committed Apr 17, 2017
1 parent f9fb186 commit 5de8c31
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS;


import java.net.HttpURLConnection;
import java.net.URL;


Expand Down Expand Up @@ -137,9 +135,12 @@ public String authorize(@RequestParam final String code, @RequestParam final S
String redirectUrl = state;
return "redirect:" + redirectUrl;
}

@RequestMapping(method = RequestMethod.GET, value = "/{owner}/{repo}")
public String confirm(HttpServletRequest request,@PathVariable("owner") String repoOwner, @PathVariable("repo") String repoName,
public String confirm(HttpServletRequest request,
@PathVariable("owner") String repoOwner,
@PathVariable("repo") String repoName,
@RequestParam(defaultValue="master", required=false) String ref,
HttpSession session ,Map<String, Object> map) throws Exception
{
try
Expand All @@ -150,7 +151,7 @@ public String confirm(HttpServletRequest request,@PathVariable("owner") String r
// Repository name
RepositoryId repoId = RepositoryId.create(repoOwner, repoName);
map.put("repositoryName", repoId.generateId());

map.put("ref", ref);

// Display user info
ForceServiceConnector forceConnector = new ForceServiceConnector(ForceServiceConnector.getThreadLocalConnectorConfig());
Expand Down Expand Up @@ -209,18 +210,27 @@ public String confirm(HttpServletRequest request,@PathVariable("owner") String r
scanRepository(
contentService,
repoId,
contentService.getContents(repoId),
ref,
contentService.getContents(repoId, null, ref),
repositoryContainer,
repositoryScanResult
);

ObjectMapper mapper = new ObjectMapper();
if(repositoryScanResult.pacakgeRepoDirectory!=null)
map.put("githubcontents", mapper.writeValueAsString(repositoryScanResult.pacakgeRepoDirectory));
else if(repositoryContainer.repositoryItems.size()>0)
map.put("githubcontents", mapper.writeValueAsString(repositoryContainer));
else
// Determine correct root to emit to the page
RepositoryItem githubcontents = null;
if(repositoryScanResult.pacakgeRepoDirectory!=null) {
githubcontents = repositoryScanResult.pacakgeRepoDirectory;
} else if(repositoryContainer.repositoryItems.size()>0) {
githubcontents = repositoryContainer;
}

// Serialize JSON to page
if(githubcontents!=null) {
githubcontents.ref = ref; // Remember branch/tag/commit reference
map.put("githubcontents", new ObjectMapper().writeValueAsString(githubcontents));
} else {
map.put("error", "No Salesforce files found in repository.");
}
}
catch (RequestException e)
{
Expand All @@ -242,11 +252,16 @@ else if(repositoryContainer.repositoryItems.size()>0)
}
return "githubdeploy";
}

@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = "/{owner}/{repo}")
public String deploy(@PathVariable("owner") String repoOwner, @PathVariable("repo") String repoName, @RequestBody String repoContentsJson,
HttpServletResponse response,Map<String,Object> map,HttpSession session) throws Exception
public String deploy(
@PathVariable("owner") String repoOwner,
@PathVariable("repo") String repoName,
@RequestBody String repoContentsJson,
HttpServletResponse response,
Map<String,Object> map,
HttpSession session) throws Exception
{
String accessToken = (String)session.getAttribute(GITHUB_TOKEN);

Expand Down Expand Up @@ -314,7 +329,7 @@ public String deploy(@PathVariable("owner") String repoOwner, @PathVariable("rep
ZipInputStream zipIS;
try
{
zipIS = contentService.getArchiveAsZip(repoId);
zipIS = contentService.getArchiveAsZip(repoId, repositoryContainer.ref);
}catch(RequestException e)
{
session.removeAttribute(GITHUB_TOKEN);
Expand Down Expand Up @@ -418,7 +433,7 @@ else if(repoPackagePath!=null && repoPath.equals(repoPackagePath))
objectMapper.getSerializationConfig().addMixInAnnotations(AsyncResult.class, AsyncResultMixIn.class);
return objectMapper.writeValueAsString(asyncResult);
}

@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/{owner}/{repo}/checkstatus/{asyncId}")
public String checkStatus(@PathVariable("asyncId") String asyncId) throws Exception
Expand All @@ -431,7 +446,7 @@ public String checkStatus(@PathVariable("asyncId") String asyncId) throws Except
objectMapper.getSerializationConfig().addMixInAnnotations(AsyncResult.class, AsyncResultMixIn.class);
return objectMapper.writeValueAsString(asyncResult);
}

@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/{owner}/{repo}/checkdeploy/{asyncId}")
public String checkDeploy(@PathVariable("asyncId") String asyncId) throws Exception
Expand Down Expand Up @@ -459,6 +474,7 @@ public abstract class AsyncResultMixIn
*/
public static class RepositoryItem
{
public String ref;
public RepositoryContents repositoryItem;
public ArrayList<RepositoryItem> repositoryItems;
public String metadataFolder;
Expand Down Expand Up @@ -497,13 +513,17 @@ public ContentsServiceEx(GitHubClient client) {
super(client);
}

public ZipInputStream getArchiveAsZip(IRepositoryIdProvider repository)
public ZipInputStream getArchiveAsZip(IRepositoryIdProvider repository, String ref)
throws Exception
{
// https://developer.github.com/v3/repos/contents/#get-archive-link
String id = getId(repository);
StringBuilder uri = new StringBuilder(SEGMENT_REPOS);
uri.append('/').append(id);
uri.append('/').append("zipball");
if(ref!=null) {
uri.append('/').append(ref);
}
GitHubRequest request = createRequest();
request.setUri(uri);
return new ZipInputStream(getClient().getStream(request));
Expand Down Expand Up @@ -538,9 +558,7 @@ public GitHubResponse get(GitHubRequest request) throws IOException

private GitHubRequest applyClientIdAndSecret(GitHubRequest request)
{
Map<String, String> params = request.getParams();
if(params==null)
params = new HashMap<String, String>();
Map<String, String> params = new HashMap<String, String>(request.getParams());
params.put("client_id", clientId);
params.put("client_secret", clientSecret);
request.setParams(params);
Expand All @@ -558,7 +576,7 @@ private GitHubRequest applyClientIdAndSecret(GitHubRequest request)
* @param repositoryContainer
* @throws Exception
*/
private static void scanRepository(ContentsService contentService, RepositoryId repoId, List<RepositoryContents> contents, RepositoryItem repositoryContainer, RepositoryScanResult repositoryScanResult)
private static void scanRepository(ContentsService contentService, RepositoryId repoId, String ref, List<RepositoryContents> contents, RepositoryItem repositoryContainer, RepositoryScanResult repositoryScanResult)
throws Exception
{
// Process files first
Expand Down Expand Up @@ -641,7 +659,7 @@ else if(folders.length>2)
RepositoryItem repositoryItem = new RepositoryItem();
repositoryItem.repositoryItem = repo;
repositoryItem.repositoryItems = new ArrayList<RepositoryItem>();
scanRepository(contentService, repoId, contentService.getContents(repoId, repo.getPath().replace(" ", "%20")), repositoryItem, repositoryScanResult);
scanRepository(contentService, repoId, ref, contentService.getContents(repoId, repo.getPath().replace(" ", "%20"), ref), repositoryItem, repositoryScanResult);
if(repositoryScanResult.packageRepoPath!=null && repo.getPath().equals(repositoryScanResult.packageRepoPath))
repositoryScanResult.pacakgeRepoDirectory = repositoryItem;
if(repositoryItem.repositoryItems.size()>0)
Expand Down
16 changes: 11 additions & 5 deletions src/main/webapp/WEB-INF/jsp/githubdeploy.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<dd class="slds-dl--horizontal__detail slds-tile__meta">
<p class="slds-truncate">${repositoryName}</p>
</dd>
<dt class="slds-dl--horizontal__label">
<p class="slds-truncate">Branch/Tag/Commit:</p>
</dt>
<dd class="slds-dl--horizontal__detail slds-tile__meta">
<p class="slds-truncate">${ref}</p>
</dd>
<c:if test="${repo != null}">
<dt class="slds-dl--horizontal__label">
<p class="slds-truncate">Description:</p>
Expand All @@ -71,7 +77,7 @@
</dt>
<dd class="slds-dl--horizontal__detail slds-tile__meta">
<p class="slds-truncate">
<a href="${repo.getHtmlUrl()}" target="_new">${repo.getHtmlUrl()}</a>
<a href="${repo.getHtmlUrl()}/tree/${ref}" target="_new">${repo.getHtmlUrl()}/tree/${ref}</a>
</p>
</dd>
</c:if>
Expand Down Expand Up @@ -149,14 +155,14 @@
render: function(container) {
if(container.repositoryItem!=null)
$('#githubcontents').append(
'<div><a target="_new" href="${repo.getHtmlUrl()}/blob/master/' +
'<div><a target="_new" href="${repo.getHtmlUrl()}/blob/${ref}/' +
container.repositoryItem.path + '">' + container.repositoryItem.path + '</a></div>');
for(fileIdx in container.repositoryItems)
if(container.repositoryItems[fileIdx].repositoryItem.type == 'dir')
GitHubDeploy.render(container.repositoryItems[fileIdx]);
else
$('#githubcontents').append(
'<div><a target="_new" href="${repo.getHtmlUrl()}/blob/master/' +
'<div><a target="_new" href="${repo.getHtmlUrl()}/blob/${ref}/' +
container.repositoryItems[fileIdx].repositoryItem.path + '">' +
container.repositoryItems[fileIdx].repositoryItem.path + '</a></div>');
},
Expand Down Expand Up @@ -200,7 +206,7 @@
checkStatus: function() {
$.ajax({
type: 'GET',
url: window.location + '/checkstatus/' + GitHubDeploy.asyncResult.id,
url: window.pathname + '/checkstatus/' + GitHubDeploy.asyncResult.id,
contentType : 'application/json; charset=utf-8',
dataType : 'json',
success: function(data, textStatus, jqXHR) {
Expand All @@ -224,7 +230,7 @@
$('#deploy').attr('disabled', null);
$.ajax({
type: 'GET',
url: window.location + '/checkdeploy/' + GitHubDeploy.asyncResult.id,
url: window.pathname + '/checkdeploy/' + GitHubDeploy.asyncResult.id,
contentType : 'application/json; charset=utf-8',
dataType : 'json',
success: function(data, textStatus, jqXHR) {
Expand Down
23 changes: 21 additions & 2 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
var appName = ''
function githubdeploy()
{
var ref = $('#ref').val();
var sfdeployurl =
$('#production').attr('checked') ?
'https://githubsfdeploy.herokuapp.com/app/githubdeploy' :
'https://githubsfdeploy-sandbox.herokuapp.com/app/githubdeploy';
sfdeployurl+= '/' + $('#owner').val() + '/' + $('#repo').val();
sfdeployurl+= '/' + $('#owner').val() + '/' + $('#repo').val() + (ref != '' ? '?ref=' + ref : '');
window.location = sfdeployurl;
}
function togglebuttoncode()
Expand All @@ -30,9 +31,10 @@ function updatebuttonhtml()
{
var repoOwner = $('#owner').val();
var repoName = $('#repo').val();
var ref = $('#ref').val();
var buttonhtml =
( $('#blogpaste').attr('checked') == 'checked' ?
'<a href="https://githubsfdeploy.herokuapp.com?owner=' + repoOwner +'&repo=' + repoName + '">\n' :
'<a href="https://githubsfdeploy.herokuapp.com?owner=' + repoOwner +'&repo=' + repoName + (ref!='' ? '&ref=' + ref : '') + '">\n' :
'<a href="https://githubsfdeploy.herokuapp.com">\n') +
' <img alt="Deploy to Salesforce"\n' +
' src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png">\n' +
Expand All @@ -44,23 +46,34 @@ function load()
// Default from URL
var owner = $.url().param('owner');
var repo = $.url().param('repo');
var ref = $.url().param('ref');
// Check for GitHub referrer?
if(owner==null && repo==null) {
var referrer = document.referrer;
// Note this is not passed from private repos or to http://localhost
// https://github.com/afawcett/githubdeploytest
if(referrer!=null && referrer.startsWith('https://github.com')) {
var parts = referrer.split('/');
if(parts.length >= 5) {
owner = parts[3];
repo = parts[4];
}
if(parts.length >= 7) {
// Branch/Tag/Release?
// https://github.com/afawcett/githubdeploytest/tree/BranchA
// https://github.com/afawcett/githubdeploytest/tree/Branch/B
if(parts[5] == 'tree') {
ref = referrer.substr(referrer.indexOf('/tree/')+6);
}
}
}
}
// Default fields
$('#owner').val(owner);
$('#repo').val(repo);
$('#ref').val(ref);
$('#login').focus();
Expand Down Expand Up @@ -122,6 +135,12 @@ function load()
<input id="repo" oninput="updatebuttonhtml();"/>
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label">Branch/Tag/Commit:</label>
<div class="slds-form-element__control">
<input id="ref" oninput="updatebuttonhtml();"/>
</div>
</div>
<div class="slds-form-element">
<div class="slds-form-element__control">
<label class="slds-checkbox">
Expand Down

0 comments on commit 5de8c31

Please sign in to comment.