Skip to content

Commit

Permalink
v1.0.3 - refactor GitlabApi
Browse files Browse the repository at this point in the history
  • Loading branch information
batou9150 committed Jun 3, 2021
1 parent dac5167 commit e919007
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
4 changes: 3 additions & 1 deletion gitlabui-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-

from gitlabui import app
import os

if __name__ == "__main__":
app.run(debug=True, use_debugger=True, use_reloader=True, passthrough_errors=True, host='0.0.0.0')
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, use_debugger=True, use_reloader=True, passthrough_errors=True, host='0.0.0.0', port=port)
37 changes: 23 additions & 14 deletions gitlabui/gitlab.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from requests import request
import re
import os
import json
import os
import re
import sys
import urllib.parse

from requests import request


class GitlabApi:
Expand Down Expand Up @@ -30,6 +32,9 @@ def patch(self, path, **kwargs):
def delete(self, path, **kwargs):
return self.request('delete', path, **kwargs)

def get_project(self, id_or_name):
return self.get('/projects/' + urllib.parse.quote_plus(id_or_name)).json()

def get_projects(self, search=None, opts=None):

if os.path.exists(self.db):
Expand Down Expand Up @@ -90,20 +95,24 @@ def get_latest_tag(self, p):
p['tag_created_at'] = res[0]['commit']['created_at'][0:10]
return p

def search(self, search, filepath, ref, project_search=None, project_opts=None):
def get_repository_file_content(self, p, filepath, ref):
import base64
import urllib.parse
url = '/projects/' + str(p['id']) + '/repository/files/' + urllib.parse.quote_plus(filepath) + '?ref=' + ref
file = self.get(url).json()
if 'content' in file:
return base64.b64decode(file['content']).decode('utf8')
else:
return None

def search(self, search, filepath, ref, project_search=None, project_opts=None):
res = []
for p in self.get_projects(project_search, project_opts):
url = '/projects/' + str(p['id']) + '/repository/files/' + urllib.parse.quote_plus(filepath) + '?ref=' + ref
file = self.get(url).json()
if 'content' in file:
content = base64.b64decode(file['content']).decode('utf8')
if re.search(search, content, flags=re.IGNORECASE):
p['match'] = [line
for line in content.splitlines()
if re.search(search, line, flags=re.IGNORECASE)]
res.append(p)
content = self.get_repository_file_content(p, filepath, ref)
if content and re.search(search, content, flags=re.IGNORECASE):
p['match'] = [line
for line in content.splitlines()
if re.search(search, line, flags=re.IGNORECASE)]
res.append(p)
return res

def version(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='gitlabui',
version='1.0.2',
version='1.0.3',
author='batou9150',
packages=find_packages(),
include_package_data=True,
Expand Down

0 comments on commit e919007

Please sign in to comment.