def test_rm_local(self): try: source = os.path.join(self.root_dir, 'new.file') with open(source, 'w') as f: f.write('test') self.assertTrue(os.path.exists(source)) with app.app_context(): execute_git_command(['rm-local', 'new.file']) self.assertFalse(os.path.exists(source)) finally: try: os.unlink(source) except Exception: pass
def git_command_output(*args: str) -> List[str]: "Execute a git command and return the output as a list of string" r = execute_git_command(list(args)) # Check exit code if r.headers.get('X-Git-Return-Code') != '0': return [] # Return stdout lines return r.data.decode('utf-8').split('\n')
def test_ls_local_logs(self): path = os.path.dirname(os.path.realpath(__file__)) configuration.conf.set(PLUGIN_NAME, 'git_init_repo', 'False') configuration.conf.set(PLUGIN_NAME, 'root_directory', path) with app.app_context(): r = execute_git_command(['ls-local', '-l', '~logs']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertIsNotNone(t)
def test_ls_local_airflow_hone(self): with app.app_context(): r = execute_git_command(['ls-local', '-l', '~airflow_home']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertIsNotNone(t) for line in t.split('\n'): i = line.split() self.assertTrue(i[1] in ['tree', 'blob']) self.assertTrue(i[2].startswith('/~airflow_home/'))
def get_tags_node(path: Optional[str] = None) -> List[Dict[str, Any]]: " Get tree tags node " result = [] r = execute_git_command(['tag']) if r.headers.get('X-Git-Return-Code') != '0': return result for line in r.data.decode('utf-8').split('\n'): if line: name = line.strip() result.append({'id': name, 'leaf': True, 'icon': 'fa-tags'}) return result
def test_ls_local_folder(self): with app.app_context(): r = execute_git_command(['ls-local', '-l', 'folder']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertIsNotNone(t) for line in t.split('\n'): i = line.split() self.assertEqual(i[1], 'blob') self.assertTrue(i[2].startswith('/folder/')) self.assertEqual(i[3], '2') self.assertTrue(i[4] in ['1', '2', '3'])
def test_ls_local_airflow_hone(self): path = os.path.dirname(os.path.realpath(__file__)) configuration.conf.set(PLUGIN_NAME, 'git_init_repo', 'False') configuration.conf.set(PLUGIN_NAME, 'root_directory', path) with app.app_context(): r = execute_git_command(['ls-local', '-l', '~airflow_home']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertIsNotNone(t) for line in t.split('\n'): i = line.split() self.assertTrue(i[1] in ['tree', 'blob']) self.assertTrue(i[2].startswith('/~airflow_home/'))
def get_local_branches_node( path: Optional[str] = None) -> List[Dict[str, Any]]: " Get tree local branches node " result = [] r = execute_git_command(['branch']) if r.headers.get('X-Git-Return-Code') != '0': return result for line in r.data.decode('utf-8').split('\n'): if line: name = line.strip() if name.startswith('*'): name = name[1:].strip() result.append({'id': name, 'leaf': True, 'icon': 'fa-code-fork'}) return result
def _download(self, session, path): " Send the contents of a file to the client " try: if path.startswith('~git/'): # Download git blob - path = '~git/<hash>/<name>' _, path, filename = path.split('/', 3) response = execute_git_command(["cat-file", "-p", path]) response.headers[ 'Content-Disposition'] = 'attachment; filename="%s"' % filename try: content_type = mimetypes.guess_type(filename)[0] if content_type: response.headers['Content-Type'] = content_type except Exception: pass return response else: # Download file fullpath = git_absolute_path(path) return send_file(fullpath, as_attachment=True) except Exception as ex: logging.error(ex) abort(HTTP_404_NOT_FOUND)
def test_ls_local_logs(self): with app.app_context(): r = execute_git_command(['ls-local', '-l', '~logs']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertIsNotNone(t)
def test_mounts(self): with app.app_context(): r = execute_git_command(['mounts']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertEqual(t, 'airflow_home\nlogs')
def test_ls_tree(self): with app.app_context(): r = execute_git_command(['ls-tree', 'HEAD', '-l']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertIsNotNone(t)
def test_invalid_command(self): with app.app_context(): r = execute_git_command(['invalid-command']) t = r.data.decode('utf-8') self.assertEqual(r.status_code, 200) self.assertTrue('Command not supported' in t)
def _git_repo_post(self, path): " Execute a GIT command (invoked by the HTTP POST method) " git_args = request.form.getlist('args[]') return execute_git_command(git_args)
def _git_repo_get(self, path): " Get a file from GIT (invoked by the HTTP GET method) " return execute_git_command(["cat-file", "-p", path])