Skip to content

Commit

Permalink
fixes #395 so we can create new folders in the wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jul 16, 2013
1 parent 7bcb074 commit 412b036
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 50 deletions.
63 changes: 51 additions & 12 deletions hawtio-git/src/main/java/io/hawt/git/GitFacade.java
Expand Up @@ -325,6 +325,7 @@ public FileInfo call() throws Exception {
});
}


/**
* Provides a file/path completion hook so we can start typing the name of a file or directory
*/
Expand Down Expand Up @@ -433,11 +434,12 @@ public String call() throws Exception {
}


public void write(final String branch, final String path, final String commitMessage,
public CommitInfo write(final String branch, final String path, final String commitMessage,
final String authorName, final String authorEmail, final String contents) {
final PersonIdent personIdent = new PersonIdent(authorName, authorEmail);
gitOperation(personIdent, new Callable<RevCommit>() {
public RevCommit call() throws Exception {
return gitOperation(personIdent, new Callable<CommitInfo>() {
public CommitInfo call() throws Exception {
checkoutBranch(branch);
File file = getFile(path);
file.getParentFile().mkdirs();

Expand All @@ -448,7 +450,39 @@ public RevCommit call() throws Exception {
add.call();

CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
return commitThenPush(commit);
RevCommit revCommit = commitThenPush(commit);
return createCommitInfo(revCommit);
}
});
}



/**
* Creates a new file if it doesn't already exist
*
* @return the commit metadata for the newly created file or null if it already exists
*/
@Override
public CommitInfo createDirectory(final String branch, final String path, final String commitMessage,
final String authorName, final String authorEmail) {
final PersonIdent personIdent = new PersonIdent(authorName, authorEmail);
return gitOperation(personIdent, new Callable<CommitInfo>() {
public CommitInfo call() throws Exception {
checkoutBranch(branch);
File file = getFile(path);
if (file.exists()) {
return null;

}
file.mkdirs();
String filePattern = getFilePattern(path);
AddCommand add = git.add().addFilepattern(filePattern).addFilepattern(".");
add.call();

CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
RevCommit revCommit = commitThenPush(commit);
return createCommitInfo(revCommit);
}
});
}
Expand Down Expand Up @@ -579,21 +613,26 @@ public List<CommitInfo> history(String objectId, String path, int limit) {
List<RevCommit> commits = block.getCommits();
List<CommitInfo> results = new ArrayList<CommitInfo>();
for (RevCommit entry : commits) {
final Date date = getCommitDate(entry);
String author = entry.getAuthorIdent().getName();
boolean merge = entry.getParentCount() > 1;
String shortMessage = entry.getShortMessage();
String trimmedMessage = Strings.trimString(shortMessage, 78);
String name = entry.getName();
String commitHashText = getShortCommitHash(name);
results.add(new CommitInfo(commitHashText, name, author, date, merge, trimmedMessage, shortMessage));
CommitInfo commitInfo = createCommitInfo(entry);
results.add(commitInfo);
}
return results;
} catch (Exception e) {
throw new RuntimeIOException(e);
}
}

public CommitInfo createCommitInfo(RevCommit entry) {
final Date date = getCommitDate(entry);
String author = entry.getAuthorIdent().getName();
boolean merge = entry.getParentCount() > 1;
String shortMessage = entry.getShortMessage();
String trimmedMessage = Strings.trimString(shortMessage, 78);
String name = entry.getName();
String commitHashText = getShortCommitHash(name);
return new CommitInfo(commitHashText, name, author, date, merge, trimmedMessage, shortMessage);
}

/**
* Retrieves a Java Date from a Git commit.
*
Expand Down
14 changes: 12 additions & 2 deletions hawtio-git/src/main/java/io/hawt/git/GitFacadeMXBean.java
@@ -1,6 +1,7 @@
package io.hawt.git;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;

import java.io.IOException;
import java.util.List;
Expand All @@ -22,8 +23,17 @@ public interface GitFacadeMXBean {
*/
FileContents read(String branch, String path) throws IOException, GitAPIException;

void write(String branch, String path, String commitMessage,
String authorName, String authorEmail, String contents);
CommitInfo write(String branch, String path, String commitMessage,
String authorName, String authorEmail, String contents);

/**
* Creates a new file if it doesn't already exist
*
* @return the commit metadata for the newly created file or null if it already exists
*/
CommitInfo createDirectory(String branch, String path, String commitMessage,
String authorName, String authorEmail);


/**
* Renames the given oldPath to the newPath location for the given branch, commit message and user
Expand Down
12 changes: 12 additions & 0 deletions hawtio-web/src/main/webapp/app/git/js/git.ts
Expand Up @@ -27,6 +27,11 @@ module Git {
*/
write(branch:string, path:string, commitMessage:string, contents:string, fn);

/**
* Creates a new directory of the given name
*/
createDirectory(branch:string, path:string, commitMessage:string, fn);

/**
* Reverts to a specific version of the file
*/
Expand Down Expand Up @@ -97,6 +102,13 @@ module Git {
return this.jolokia.execute(this.mbean, "write", this.branch, path, commitMessage, authorName, authorEmail, contents, onSuccess(fn));
}

public createDirectory(branch:string, path:string, commitMessage:string, fn) {
var authorName = this.getUserName();
var authorEmail = this.getUserEmail();

return this.jolokia.execute(this.mbean, "createDirectory", branch, path, commitMessage, authorName, authorEmail, onSuccess(fn));
}

public revertTo(objectId:string, blobPath:string, commitMessage:string, fn) {
var authorName = this.getUserName();
var authorEmail = this.getUserEmail();
Expand Down
4 changes: 2 additions & 2 deletions hawtio-web/src/main/webapp/app/wiki/html/viewPage.html
Expand Up @@ -80,15 +80,15 @@

<div class="modal-large">
<div modal="addDialog.show" close="addDialog.close()" ng-options="addDialog.options">
<div class="modal-header"><h4>Create New Document</h4></div>
<div class="modal-header"><h4>Create Document</h4></div>
<div class="modal-body">
<div class="control-group">
<div hawtio-tree="createDocumentTree" hideRoot="true" onSelect="onCreateDocumentSelect"
activateNodes="createDocumentTreeActivations"></div>
</div>

<form class="form-inline" ng-submit="addAndCloseDialog()">
<label class="control-label" for="fileName">Document Name: </label>
<label class="control-label" for="fileName">Name: </label>

<input id="fileName" type="text" ng-model="newDocumentName"
placeholder="{{selectedCreateDocumentTemplate.exemplar}}"/>
Expand Down
Binary file added hawtio-web/src/main/webapp/app/wiki/img/folder.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions hawtio-web/src/main/webapp/app/wiki/js/helpers.ts
Expand Up @@ -11,6 +11,13 @@ module Wiki {
* The wizard tree for creating new content in the wiki
*/
export var documentTemplates = [
{
label: "Folder",
tooltip: "Create a new folder to contain documents",
folder: true,
icon: "/app/wiki/img/folder.gif",
exemplar: "New Folder"
},
{
label: "Markdown Document",
tooltip: "A basic markup document using the Markdown wiki markup, particularly useful for ReadMe files in directories",
Expand Down Expand Up @@ -80,6 +87,9 @@ module Wiki {
// var tooltip = value["tooltip"] || value["description"] || label;
var tooltip = template["tooltip"] || template["description"] || '';
node.tooltip = tooltip;
if (template["folder"]) {
node.isFolder = () => { return true; };
}
parent.children.push(node);

var children = template.children;
Expand Down
84 changes: 50 additions & 34 deletions hawtio-web/src/main/webapp/app/wiki/js/view.ts
@@ -1,5 +1,13 @@
module Wiki {

function goToLink(link, $timeout, $location) {
var href = Core.trimLeading(link, "#");
$timeout(() => {
console.log("About to navigate to: " + href);
$location.path(href);
}, 100);
}

export function ViewController($scope, $location, $routeParams, $http, $timeout, workspace:Workspace, marked, fileExtensionTypeRegistry, wikiRepository:GitWikiRepository, $compile) {
Wiki.initScope($scope, $routeParams, $location);

Expand Down Expand Up @@ -169,43 +177,51 @@ module Wiki {
name = name.substring(idx + 1);
}
var path = folder + "/" + name;
console.log("Creating file " + path);
notification("success", "Creating new document " + name);

$http.get(exemplarUri).success((contents) => {

// TODO lets check this page does not exist - if it does lets keep adding a new post fix...
wikiRepository.putPage($scope.branch, path, contents, commitMessage, (status) => {
console.log("Created file " + name);
Wiki.onComplete(status);

// lets navigate to the edit link
// load the directory and find the child item
$scope.git = wikiRepository.getPage($scope.branch, folder, $scope.objectId, (details) => {
// lets find the child entry so we can calculate its correct edit link
var link = null;
if (details && details.children) {
console.log("scanned the directory " + details.children.length + " children");
var child = details.children.find(c => c.name === fileName);
if (child) {
link = $scope.childLink(child);
} else {
console.log("Could not find name '" + fileName + "' in the list of file names " + JSON.stringify(details.children.map(c => c.name)));

if (template.folder) {
notification("success", "Creating new folder " + name);

wikiRepository.createDirectory($scope.branch, path, commitMessage, (status) => {
$scope.addDialog.close();
Core.$apply($scope);
var link = Wiki.viewLink($scope.branch, path, $location);
goToLink(link, $timeout, $location);
});
} else {
notification("success", "Creating new document " + name);

$http.get(exemplarUri).success((contents) => {

// TODO lets check this page does not exist - if it does lets keep adding a new post fix...
wikiRepository.putPage($scope.branch, path, contents, commitMessage, (status) => {
console.log("Created file " + name);
Wiki.onComplete(status);

// lets navigate to the edit link
// load the directory and find the child item
$scope.git = wikiRepository.getPage($scope.branch, folder, $scope.objectId, (details) => {
// lets find the child entry so we can calculate its correct edit link
var link = null;
if (details && details.children) {
console.log("scanned the directory " + details.children.length + " children");
var child = details.children.find(c => c.name === fileName);
if (child) {
link = $scope.childLink(child);
} else {
console.log("Could not find name '" + fileName + "' in the list of file names " + JSON.stringify(details.children.map(c => c.name)));
}
}
}
if (!link) {
console.log("WARNING: could not find the childLink so reverting to the wiki edit page!");
link = Wiki.editLink($scope.branch, path, $location);
}
var href = Core.trimLeading(link, "#");
Core.$apply($scope);
$timeout(() => {
console.log("About to navigate to: " + href);
$location.path(href);
}, 400);
if (!link) {
console.log("WARNING: could not find the childLink so reverting to the wiki edit page!");
link = Wiki.editLink($scope.branch, path, $location);
}
$scope.addDialog.close();
Core.$apply($scope);
goToLink(link, $timeout, $location);
});
});
});
});
}
$scope.addDialog.close();
};

Expand Down
5 changes: 5 additions & 0 deletions hawtio-web/src/main/webapp/app/wiki/js/wikiRepository.ts
Expand Up @@ -82,6 +82,11 @@ module Wiki {
this.git().write(branch, fullPath, commitMessage, contents, fn);
}

public createDirectory(branch:string, path:string, commitMessage:string, fn) {
var fullPath = this.getPath(path);
this.git().createDirectory(branch, fullPath, commitMessage, fn);
}

public revertTo(objectId:string, blobPath:string, commitMessage:string, fn) {
var fullPath = this.getLogPath(blobPath);
this.git().revertTo(objectId, fullPath, commitMessage, fn);
Expand Down

0 comments on commit 412b036

Please sign in to comment.