mirror of https://github.com/openspug/spug
A api update
parent
883a351e1c
commit
908054b424
|
@ -4,4 +4,5 @@ from .views import *
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', AppView.as_view()),
|
path('', AppView.as_view()),
|
||||||
|
path('<int:a_id>/versions/', get_versions),
|
||||||
]
|
]
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
from django.conf import settings
|
||||||
|
from apps.app.models import App
|
||||||
|
from libs.gitlib import Git
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def parse_envs(text):
|
||||||
|
data = {}
|
||||||
|
if data:
|
||||||
|
for line in text.split('\n'):
|
||||||
|
fields = line.split('=', 1)
|
||||||
|
if len(fields) != 2 or fields[0].strip() == '':
|
||||||
|
raise Exception(f'解析自定义全局变量{line!r}失败,确认其遵循 key = value 格式')
|
||||||
|
data[fields[0].strip()] = fields[1].strip()
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_versions(app: App):
|
||||||
|
git_repo = app.extend_obj.git_repo
|
||||||
|
repo_dir = os.path.join(settings.REPOS_DIR, str(app.id))
|
||||||
|
return Git(git_repo, repo_dir).fetch_branches_tags()
|
|
@ -1,6 +1,7 @@
|
||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
from libs import JsonParser, Argument, json_response
|
from libs import JsonParser, Argument, json_response
|
||||||
from apps.app.models import App, AppExtend1, AppExtend2
|
from apps.app.models import App, AppExtend1, AppExtend2
|
||||||
|
from apps.app.utils import parse_envs, fetch_versions
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,12 +61,11 @@ class AppView(View):
|
||||||
return json_response(error=error)
|
return json_response(error=error)
|
||||||
|
|
||||||
|
|
||||||
def parse_envs(data):
|
def get_versions(request, a_id):
|
||||||
tmp = {}
|
app = App.objects.filter(pk=a_id).first()
|
||||||
if data:
|
if not app:
|
||||||
for line in data.split('\n'):
|
return json_response(error='未找到指定应用')
|
||||||
fields = line.split('=', 1)
|
if app.extend == '2':
|
||||||
if len(fields) != 2 or fields[0].strip() == '':
|
return json_response(error='该应用不支持此操作')
|
||||||
raise Exception(f'解析自定义全局变量{line!r}失败,确认其遵循 key = value 格式')
|
branches, tags = fetch_versions(app)
|
||||||
tmp[fields[0].strip()] = fields[1].strip()
|
return json_response({'branches': branches, 'tags': tags})
|
||||||
return tmp
|
|
||||||
|
|
|
@ -21,6 +21,3 @@ class RequestView(View):
|
||||||
form.host_ids = json.dumps(form.host_ids)
|
form.host_ids = json.dumps(form.host_ids)
|
||||||
DeployRequest.objects.create(**form)
|
DeployRequest.objects.create(**form)
|
||||||
return json_response(error=error)
|
return json_response(error=error)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
from git import Repo, RemoteReference, TagReference, InvalidGitRepositoryError
|
||||||
|
import shutil
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Git:
|
||||||
|
def __init__(self, git_repo, repo_dir):
|
||||||
|
self.repo = self._get_repo(git_repo, repo_dir)
|
||||||
|
|
||||||
|
def archive(self, filepath, commit):
|
||||||
|
with open(filepath, 'wb') as f:
|
||||||
|
self.repo.archive(f, commit)
|
||||||
|
|
||||||
|
def fetch_branches_tags(self):
|
||||||
|
self.repo.remotes.origin.fetch()
|
||||||
|
branches, tags = {}, {}
|
||||||
|
for ref in self.repo.references:
|
||||||
|
if isinstance(ref, RemoteReference):
|
||||||
|
if ref.remote_head != 'HEAD':
|
||||||
|
branches[ref.remote_head] = self._get_commits(f'origin/{ref.remote_head}')
|
||||||
|
elif isinstance(ref, TagReference):
|
||||||
|
tags[ref.name] = {
|
||||||
|
'id': ref.tag.hexsha,
|
||||||
|
'author': ref.tag.tagger.name,
|
||||||
|
'date': ref.tag.tagged_date,
|
||||||
|
'message': ref.tag.message.strip()
|
||||||
|
}
|
||||||
|
return branches, tags
|
||||||
|
|
||||||
|
def _get_repo(self, git_repo, repo_dir):
|
||||||
|
if os.path.exists(repo_dir):
|
||||||
|
try:
|
||||||
|
return Repo(repo_dir)
|
||||||
|
except InvalidGitRepositoryError:
|
||||||
|
if os.path.isdir(repo_dir):
|
||||||
|
shutil.rmtree(repo_dir)
|
||||||
|
else:
|
||||||
|
os.remove(repo_dir)
|
||||||
|
return Repo.clone_from(git_repo, repo_dir)
|
||||||
|
|
||||||
|
def _get_commits(self, branch, count=10):
|
||||||
|
commits = []
|
||||||
|
for commit in self.repo.iter_commits(branch):
|
||||||
|
if len(commits) == count:
|
||||||
|
break
|
||||||
|
commits.append({
|
||||||
|
'id': commit.hexsha,
|
||||||
|
'author': commit.author.name,
|
||||||
|
'date': commit.committed_date,
|
||||||
|
'message': commit.message.strip()
|
||||||
|
})
|
||||||
|
return commits
|
|
@ -93,6 +93,7 @@ TEMPLATES = [
|
||||||
|
|
||||||
SCHEDULE_KEY = 'spug:schedule'
|
SCHEDULE_KEY = 'spug:schedule'
|
||||||
MONITOR_KEY = 'spug:monitor'
|
MONITOR_KEY = 'spug:monitor'
|
||||||
|
REPOS_DIR = os.path.join(BASE_DIR, 'repos')
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/2.2/topics/i18n/
|
# https://docs.djangoproject.com/en/2.2/topics/i18n/
|
||||||
|
|
Loading…
Reference in New Issue