diff --git a/plugins/gogs/hook/self_hook.tpl b/plugins/gogs/hook/self_hook.tpl new file mode 100644 index 000000000..05a7907cf --- /dev/null +++ b/plugins/gogs/hook/self_hook.tpl @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/plugins/gogs/index.py b/plugins/gogs/index.py index b513110b3..4e58b824b 100755 --- a/plugins/gogs/index.py +++ b/plugins/gogs/index.py @@ -764,6 +764,168 @@ def projectScriptSelf(): user = args['user'] name = args['name'] + '.git' + custom_hooks = getRootPath() + '/' + user + '/' + \ + name + '/custom_hooks' + self_path = custom_hooks + '/self' + + if not os.path.exists(self_path): + os.mkdir(self_path) + + self_hook_file = custom_hooks + '/self_hook.sh' + self_hook_exist = False + if os.path.exists(self_hook_file): + self_hook_exist = True + + dlist = [] + if os.path.exists(self_path): + for filename in os.listdir(self_path): + tmp = {} + filePath = self_path + '/' + filename + if os.path.isfile(filePath): + tmp['path'] = filePath + tmp['name'] = os.path.basename(filePath) + dlist.append(tmp) + + dlist_sum = len(dlist) + # print(dlist) + rdata = {} + rdata['data'] = dlist + rdata['self_hook'] = self_hook_exist + rdata['list'] = mw.getPage( + {'count': dlist_sum, 'p': 1, 'row': 100, 'tojs': 'self_page'}) + + return mw.returnJson(True, 'ok', rdata) + + +def projectScriptSelf_Create(): + args = getArgs() + data = checkArgs(args, ['user', 'name', 'file']) + if not data[0]: + return data[1] + + user = args['user'] + name = args['name'] + '.git' + file = args['file'] + + self_path = path = getRootPath() + '/' + user + '/' + \ + name + '/custom_hooks/self' + + if not os.path.exists(self_path): + os.mkdir(self_path) + + abs_file = self_path + '/' + file + '.sh' + if os.path.exists(abs_file): + return mw.returnJson(False, '脚本已经存在!') + + mw.writeFile(abs_file, "#!/bin/bash\n") + + rdata = {} + rdata['abs_file'] = abs_file + return mw.returnJson(True, '创建文件成功!', rdata) + + +def projectScriptSelf_Del(): + args = getArgs() + data = checkArgs(args, ['user', 'name', 'file']) + if not data[0]: + return data[1] + + user = args['user'] + name = args['name'] + '.git' + file = args['file'] + + self_path = path = getRootPath() + '/' + user + '/' + \ + name + '/custom_hooks/self' + + if not os.path.exists(self_path): + os.mkdir(self_path) + + abs_file = self_path + '/' + file + # print(abs_file) + if not os.path.exists(abs_file): + return mw.returnJson(False, '脚本已经删除!') + + os.remove(abs_file) + return mw.returnJson(True, '脚本删除成功!') + + +def projectScriptSelf_Logs(): + args = getArgs() + data = checkArgs(args, ['user', 'name', 'file']) + if not data[0]: + return data[1] + + user = args['user'] + name = args['name'] + '.git' + file = args['file'] + + self_path = path = getRootPath() + '/' + user + '/' + \ + name + '/custom_hooks/self_logs' + + if not os.path.exists(self_path): + os.mkdir(self_path) + + logs_file = self_path + '/' + file + '.log' + if os.path.exists(logs_file): + rdata = {} + rdata['path'] = logs_file + return mw.returnJson(True, '脚本已经删除!', rdata) + + return mw.returnJson(False, '日志不存在!') + + +def projectScriptSelf_Rename(): + args = getArgs() + data = checkArgs(args, ['user', 'name', 'o_file', 'n_file']) + if not data[0]: + return data[1] + + user = args['user'] + name = args['name'] + '.git' + o_file = args['o_file'] + n_file = args['n_file'] + + self_path = path = getRootPath() + '/' + user + '/' + \ + name + '/custom_hooks/self' + + if not os.path.exists(self_path): + os.mkdir(self_path) + + o_file_abs = self_path + '/' + o_file + '.sh' + if not os.path.exists(o_file_abs): + return mw.returnJson(False, '原文件已经不存在了!') + + n_file_abs = self_path + '/' + n_file + '.sh' + + os.rename(o_file_abs, n_file_abs) + return mw.returnJson(True, '重命名成功!') + + +def projectScriptSelf_Enable(): + args = getArgs() + data = checkArgs(args, ['user', 'name', 'enable']) + if not data[0]: + return data[1] + + user = args['user'] + name = args['name'] + '.git' + enable = args['enable'] + + custom_path = getRootPath() + '/' + user + '/' + \ + name + '/custom_hooks' + self_file = custom_path + '/self_hook.sh' + self_hook_tpl = getPluginDir() + '/hook/self_hook.tpl' + + if enable == '1': + content = mw.readFile(self_hook_tpl) + mw.writeFile(self_file, content) + mw.execShell("chmod 777" + self_file) + return mw.returnJson(True, '开启成功!') + else: + if os.path.exists(self_file): + os.remove(self_file) + return mw.returnJson(True, '关闭成功!') + def getRsaPublic(): path = getHomeDir() @@ -842,6 +1004,16 @@ if __name__ == "__main__": print(projectScriptRun()) elif func == 'project_script_self': print(projectScriptSelf()) + elif func == 'project_script_self_create': + print(projectScriptSelf_Create()) + elif func == 'project_script_self_del': + print(projectScriptSelf_Del()) + elif func == 'project_script_self_logs': + print(projectScriptSelf_Logs()) + elif func == 'project_script_self_rename': + print(projectScriptSelf_Rename()) + elif func == 'project_script_self_enable': + print(projectScriptSelf_Enable()) elif func == 'get_rsa_public': print(getRsaPublic()) elif func == 'get_total_statistics': diff --git a/plugins/gogs/js/gogs.js b/plugins/gogs/js/gogs.js index 89d0d0efa..a621635f8 100755 --- a/plugins/gogs/js/gogs.js +++ b/plugins/gogs/js/gogs.js @@ -305,7 +305,7 @@ function gogsRepoListPage(page, search){ option += '编辑' + ' | '; option += '日志' + ' | '; option += '手动' + ' | '; - option += '自定义'; + option += '自定义'; } else{ option += '加载脚本'; } @@ -321,9 +321,6 @@ function gogsRepoListPage(page, search){ ''; } - - - $('#repo_list tbody').html(body); $('#repo_list_page').html(rdata['data']['list']); @@ -402,14 +399,9 @@ function gogsRepoListPage(page, search){ gogsPost('project_script_run', {'user':user,'name':name}, function(data){ var data = $.parseJSON(data.data); - layer.msg(data.msg,{icon:data.code?2:1,time:2000,shade: [0.3, '#000']}); + layer.msg(data.msg,{icon:data.status?2:1,time:2000,shade: [0.3, '#000']}); }); }); - - - - - //--------- }); @@ -519,6 +511,199 @@ function projectScriptDebug(user,name){ }); } + +function projectScriptSelfRender(user, name){ + gogsPost('project_script_self', {'user':user,'name':name}, function(data){ + var rdata = $.parseJSON(data.data); + + var data = rdata['data']['data']; + + if (rdata['data']['self_hook']){ + $('#open_script').prop('checked',true); + } + + var body = ''; + if(data.length == 0 ){ + body += '无脚本数据'; + } else{ + for (var i = 0; i < data.length; i++) { + body += ''+ + '' + data[i]["name"]+''+ + '' + '已使用'+''+ + '' + + '删除' + ' | ' + + '编辑' + ' | ' + + '日志' + ' | ' + + '重命名' + + ''; + } + + } + + $('#gogs_self_table tbody').html(body); + $('#gogs_self_table .page').html(rdata['data']['list']); + + $('#gogs_self_table .del').click(function(){ + var i = $(this).data('index'); + var file = data[i]["name"]; + gogsPost('project_script_self_del', {'user':user,'name':name,'file':file}, function(data){ + var data = $.parseJSON(data.data); + showMsg(data.msg ,function(){ + projectScriptSelfRender(user, name); + },{icon:data.code?2:1,time:2000,shade: [0.3, '#000']},2000); + }); + }); + + $('#gogs_self_table .edit').click(function(){ + var i = $(this).data('index'); + var path = data[i]["path"]; + onlineEditFile(0,path); + }); + + $('#gogs_self_table .logs').click(function(){ + var i = $(this).data('index'); + var file = data[i]["name"]; + gogsPost('project_script_self_logs', {'user':user,'name':name,'file':file}, function(data){ + var rdata = $.parseJSON(data.data); + if (rdata['status']){ + onlineEditFile(0, rdata['path']); + } else { + layer.msg(rdata.msg,{icon:data.status?2:1,time:2000,shade: [0.3, '#000']}); + } + }); + }); + + + $('#gogs_self_table .rename').click(function(){ + var i = $(this).data('index'); + var file = data[i]["name"]; + file = file.split('.sh')[0]; + + layer.open({ + type: 1, + shift: 5, + closeBtn: 1, + area: '320px', + title: '重命名', + btn:['设置','关闭'], + content: '
\ +
\ + \ +
\ +
', + success:function(){ + $("#newFileName").focus().keyup(function(e){ + if(e.keyCode == 13) $(".layui-layer-btn0").click(); + }); + }, + yes:function(){ + var n_file = $("#newFileName").val(); + var o_file = file; + + gogsPost('project_script_self_rename', {'user':user,'name':name,'o_file':o_file,'n_file':n_file}, function(data){ + var data = $.parseJSON(data.data); + showMsg(data.msg ,function(){ + $(".layui-layer-btn1").click(); + projectScriptSelfRender(user, name); + },{icon:data.code?2:1,time:2000,shade: [0.3, '#000']},2000); + }); + } + }); + //----- + }); + //------ + }); +} + +//新建文件 +function createScriptFile(type, user, name, file) { + if (type == 1) { + gogsPost('project_script_self_create', {'user':user,'name':name,'file': file }, function(data){ + var rdata = $.parseJSON(data.data); + if(!rdata['status']){ + layer.msg(rdata.msg,{icon:2,time:2000,shade: [0.3, '#000']}); + return; + } + + showMsg(rdata.msg, function(){ + $(".layui-layer-btn1").click(); + onlineEditFile(0,rdata['data']['abs_file']); + projectScriptSelfRender(user, name); + }, {icon:1,shade: [0.3, '#000']},2000); + }); + return; + } + layer.open({ + type: 1, + shift: 5, + closeBtn: 1, + area: '320px', + title: '新建自定义脚本', + btn:['新建','关闭'], + content: '
\ +
\ + \ +
\ +
', + success:function(){ + $("#newFileName").focus().keyup(function(e){ + if(e.keyCode == 13) $(".layui-layer-btn0").click(); + }); + }, + yes:function(){ + var file = $("#newFileName").val();; + createScriptFile(1, user, name, file); + } + }); +} + +function projectScriptSelf(user, name){ + layer.open({ + type: 1, + title: '项目('+user+'/'+name+')自定义脚本', + area: '500px', + content:"
\ + \ +
\ + 开启自定义脚本\ + \ + \ +
\ +
\ + \ + \ + \ +
脚本文件名状态操作
\ + \ +
\ +
", + success:function(){ + projectScriptSelfRender(user, name); + + $('#create_script').click(function(){ + createScriptFile(0, user, name); + }); + + $('#script_hook_enable').click(function(){ + var enable = $('#open_script').prop('checked'); + var enable_option = '0'; + if (!enable){ + enable_option = '1'; + } + gogsPost('project_script_self_enable', {'user':user,'name':name,'enable':enable_option}, function(data){ + var data = $.parseJSON(data.data); + showMsg(data.msg ,function(){ + projectScriptSelfRender(user, name); + },{icon:data.status?1:2,shade: [0.3, '#000']},2000); + }); + + }); + } + }); +} + function getRsaPublic(){ gogsPost('get_rsa_public', {}, function(data){ var rdata = $.parseJSON(data.data);