Эх сурвалжийг харах

HUE-5858 [assist] Supporting file open from git browser in assist

krish 8 жил өмнө
parent
commit
bbc3323

+ 1 - 1
desktop/core/src/desktop/conf.py

@@ -430,7 +430,7 @@ VCS = UnspecifiedConfigSection(
       REMOTE_URL = Config(
         key="remote_url",
         help=_("Base URL to Interface Remote Server"),
-        default='https://github.com/cloudera/hue/',
+        default='https://github.com/cloudera/hue/tree/master',
         type=coerce_string,
       ),
       API_URL = Config(

+ 17 - 5
desktop/core/src/desktop/lib/vcs/apis/github_readonly_api.py

@@ -15,6 +15,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+
+import binascii
 import logging
 import re
 import urllib
@@ -58,14 +60,24 @@ class GithubReadOnlyApi(Api):
     https://developer.github.com/v3/repos/contents/#get-contents
     """
     response = {'status': -1}
+    response['fileType'] = filetype = request.GET.get('fileType', 'dir')
     filepath = request.GET.get('path', '/')
     filepath = self._clean_path(filepath)
 
     if self._remote_url:
       owner, repo, branch = self.parse_github_url(self._remote_url)
-      content = self._get_contents(owner, repo, filepath)
-      response['files'] = _massage_content(content)
-      response['status'] = 0
+      blob = self._get_contents(owner, repo, filepath)
+      if filetype == 'dir':
+        response['files'] = _massage_content(blob)
+        response['status'] = 0
+      elif filetype == 'file':
+        try:
+          response['content'] = blob['content'].decode('base64')
+          response['status'] = 0
+        except binascii.Error, e:
+          raise GithubClientException('Failed to decode file contents, check if file content is properly base64-encoded: %s' % e)
+        except KeyError, e:
+          raise GithubClientException('Failed to find expected content object in blob object: %s' % e)
     else:
       return HttpResponseBadRequest(_('url param is required'))
     return JsonResponse(response)
@@ -105,9 +117,9 @@ class GithubReadOnlyApi(Api):
       raise GithubClientException('Could not find GitHub object, check owner, repo or path: %s' % e)
 
 
-def _massage_content(content):
+def _massage_content(blob):
   response = []
-  for file in content:
+  for file in blob:
     file['stats'] = {
       'size': file.get('size', 0),
       'path': file.get('path', '')

+ 11 - 6
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -393,10 +393,11 @@ var ApiHelper = (function () {
    * @param {Number} [options.timeout]
    *
    * @param {string[]} options.pathParts
+   * @param {string} options.fileType
    */
-  ApiHelper.prototype.fetchGitPath = function (options) {
+  ApiHelper.prototype.fetchGitContents = function (options) {
     var self = this;
-    var url = GIT_API_PREFIX + '?path=' + options.pathParts.join("/");
+    var url = GIT_API_PREFIX + '?path=' + options.pathParts.join("/") + '&fileType=' + options.fileType;
     var fetchFunction = function (storeInCache) {
       if (options.timeout === 0) {
         self.assistErrorCallback(options)({ status: -1 });
@@ -407,11 +408,15 @@ var ApiHelper = (function () {
         url: url,
         timeout: options.timeout,
         success: function (data) {
-          if (!data.error && !self.successResponseIsError(data) && typeof data.files !== 'undefined' && data.files !== null) {
-            if (data.files.length > 2) {
-              storeInCache(data);
+          if (!data.error && !self.successResponseIsError(data)) {
+            if (data.fileType === 'dir' && typeof data.files !== 'undefined' && data.files !== null) {
+              if (data.files.length > 2) {
+                storeInCache(data);
+              }
+              options.successCallback(data);
+            } else if (data.fileType === 'file' && typeof data.content !== 'undefined' && data.content !== null) {
+              options.successCallback(data);
             }
-            options.successCallback(data);
           } else {
             self.assistErrorCallback(options)(data);
           }

+ 26 - 3
desktop/core/src/desktop/static/desktop/js/assist/assistGitEntry.js

@@ -40,6 +40,8 @@ var AssistGitEntry = (function () {
     }
     self.path += self.definition.name;
 
+    self.fileContent = ko.observable('');
+
     self.entries = ko.observableArray([]);
 
     self.loaded = false;
@@ -61,7 +63,27 @@ var AssistGitEntry = (function () {
 
   AssistGitEntry.prototype.dblClick = function () {
     var self = this;
-    huePubSub.publish('assist.dblClickGitItem', self);
+    if (self.definition.type !== 'file') {
+      return;
+    }
+    self.hasErrors(false);
+
+    var successCallback = function(data) {
+      self.fileContent(data.content);
+      huePubSub.publish('assist.dblClickGitItem', self);
+    };
+
+    var errorCallback = function () {
+      self.hasErrors(true);
+      self.loading(false);
+    };
+
+    self.apiHelper.fetchGitContents({
+      pathParts: self.getHierarchy(),
+      fileType: self.definition.type,
+      successCallback: successCallback,
+      errorCallback: errorCallback
+    })
   };
 
   AssistGitEntry.prototype.loadEntries = function(callback) {
@@ -98,8 +120,9 @@ var AssistGitEntry = (function () {
       }
     };
 
-    self.apiHelper.fetchGitPath({
+    self.apiHelper.fetchGitContents({
       pathParts: self.getHierarchy(),
+      fileType: self.definition.type,
       successCallback: successCallback,
       errorCallback: errorCallback
     })
@@ -146,7 +169,7 @@ var AssistGitEntry = (function () {
 
   AssistGitEntry.prototype.toggleOpen = function () {
     var self = this;
-    if (self.definition.type != 'dir') {
+    if (self.definition.type !== 'dir') {
       return;
     }
     self.open(!self.open());

+ 6 - 0
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -3741,6 +3741,12 @@
         }
       });
 
+      huePubSub.subscribe("assist.dblClickGitItem", function(assistGitEntry) {
+        if ($el.data("last-active-editor")) {
+          editor.session.setValue(assistGitEntry.fileContent());
+        }
+      });
+
       huePubSub.subscribe("assist.dblClickS3Item", function(assistS3Entry) {
         if ($el.data("last-active-editor")) {
           editor.session.insert(editor.getCursorPosition(), "'S3A://" + assistS3Entry.path + "'");