GitLab Viewer Publish and Deploy Project

GitLab Viewer Publish and Deploy Project!

当前为 2023-06-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitLab Viewer Publish and Deploy Project
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.09
  5. // @description GitLab Viewer Publish and Deploy Project!
  6. // @author Sean
  7. // @match http://192.168.0.200/fe3project/*
  8. // @match http://192.168.0.200/frontend_pc/project/*
  9. // @icon http://192.168.0.200/assets/favicon-7901bd695fb93edb07975966062049829afb56cf11511236e61bcf425070e36e.png
  10. // @require https://cdn.bootcdn.net/ajax/libs/vue/2.7.14/vue.min.js
  11. // @require https://unpkg.com/element-ui/lib/index.js
  12. // @require https://cdn.bootcdn.net/ajax/libs/jszip/3.7.1/jszip.min.js
  13. // @resource myFontFile https://element.eleme.io/2.11/static/element-icons.535877f.woff
  14. // @grant GM_getResourceURL
  15. // @grant GM_addStyle
  16. // @grant GM_getResourceText
  17. // @resource ElementCSS https://unpkg.com/element-ui/lib/theme-chalk/index.css
  18. // @grant GM_xmlhttpRequest
  19. // @license MIT
  20. // @run-at document-end
  21. // ==/UserScript==
  22.  
  23. (function() {
  24. 'use strict';
  25.  
  26. const fontUrl = 'https://element.eleme.io/2.11/static/element-icons.535877f.woff';
  27.  
  28. // 添加样式规则,将字体应用到指定元素上
  29. GM_addStyle(`
  30. @font-face {
  31. font-family: element-icons;
  32. src: url(${fontUrl}) format("woff");
  33. }
  34. `);
  35.  
  36. GM_addStyle(GM_getResourceText('ElementCSS'));
  37.  
  38. let epointCss = ".epoint-tool {position: fixed; bottom: 0%; right:10px; transform: translateY(-50%);}";
  39. epointCss += ".el-row { padding: 3px 0;} .el-dialog__body .el-tree{min-height: 420px; max-height: 500px;overflow: auto;}";
  40. epointCss += ".deploy-body {height: 306px;}"
  41. epointCss += ".el-loading-spinner {margin-top: -60px;} .el-button--primary:focus {outline: 0 !important;}";
  42. // 添加注入样式
  43. let extraStyleElement = document.createElement("style");
  44. extraStyleElement.innerHTML = epointCss;
  45. document.head.appendChild(extraStyleElement);
  46.  
  47. const MyComponent = {
  48. template: `<div class="epoint-wrap">
  49. <div class="epoint-tool">
  50. <el-row><el-button type="primary" icon="el-icon-search" round @click="viewProject">查看</el-button></el-row>
  51. <el-row>
  52. <el-tooltip content="一键部署到170服务器" placement="top" effect="light">
  53. <el-button type="primary" icon="el-icon-s-unfold" round @click="doDeploy">部署</el-button>
  54. </el-tooltip>
  55. </el-row>
  56. <el-row>
  57. <el-tooltip content="一键发布到项目案例库" placement="top" effect="light">
  58. <el-button type="primary" icon="el-icon-upload" round @click="publish">发布</el-button>
  59. </el-tooltip>
  60. </el-row>
  61. </div>
  62. <el-dialog
  63. title="目录结构"
  64. width="900px"
  65. :append-to-body="true"
  66. :visible.sync="dialogVisible"
  67. :before-close="handleClose">
  68. <el-tree
  69. class="filter-tree"
  70. :data="data"
  71. :props="defaultProps"
  72. node-key="id"
  73. default-expand-all
  74. @node-click="handleNodeClick"
  75. v-loading="loadingTree"
  76. element-loading-background="rgba(255, 255, 255, 1)"
  77. element-loading-text="拼命加载中......"
  78. ref="tree">
  79. </el-tree>
  80. </el-dialog>
  81. <el-dialog
  82. title="部署"
  83. width="420px"
  84. :visible.sync="depDialogVisible">
  85. <div class="deploy-body"
  86. v-loading="loading"
  87. element-loading-text="正在打包部署至 170 服务器,请耐心等待"
  88. element-loading-spinner="el-icon-loading">
  89. </div>
  90. </el-dialog>
  91. <el-dialog title="部署提示" width="640px" :visible.sync="dialogDeployVisible">
  92. <el-alert
  93. title="此操作将把 GitLab 资源打包部署至 170 服务器,已部署过的项目会进行覆盖部署,是否继续?"
  94. type="warning" :closable="false" style="margin-bottom: 20px;">
  95. </el-alert>
  96. <el-form :model="form" ref="form" :rules="rules">
  97. <el-form-item label="框架类型" :label-width="formLabelWidth" prop="frame">
  98. <el-select v-model="form.frame" placeholder="请框架类型">
  99. <el-option v-for="item in framework" :key="item.value" :label="item.label" :value="item.value"></el-option>
  100. </el-select>
  101. </el-form-item>
  102. </el-form>
  103. <div slot="footer" class="dialog-footer">
  104. <el-button @click="dialogDeployVisible = false">取 消</el-button>
  105. <el-button type="primary" @click="doDeploy">确 定</el-button>
  106. </div>
  107. </el-dialog>
  108. </div>`,
  109. data() {
  110. return {
  111. dialogVisible: false, // 查看目录结构弹窗
  112. data: [], // 目录结构树的数据结构
  113. defaultProps: {
  114. children: 'children',
  115. label: 'label'
  116. },
  117. loadingTree: false,
  118. depDialogVisible: false, // 部署中弹窗
  119. loading: false,
  120. projectLibUrl: 'http://192.168.118.60:9999/webapp/pages/caselib/create.html', // 项目案例库地址
  121. projectIsDeployed: false, // 项目是否部署过
  122. projectFtpUrl: '', // ftp路径
  123. projectEntryUrl: 'http://192.168.219.170/showcase/', // 项目案例库发布表单的入口页面
  124. supportDeploy: true, // 项目是否支持部署
  125. dialogDeployVisible: false,
  126. formLabelWidth: '120px',
  127. form: {
  128. frame: ''
  129. },
  130. framework: [
  131. {label: '重构模板', value: 1},
  132. {label: 'f9x1.0', value: 2},
  133. {label: 'f9x2.0', value: 3},
  134. {label: 'f950', value: 4},
  135. {label: 'f950sp1', value: 5},
  136. {label: 'f950sp2', value: 6},
  137. {label: 'f950sp3', value: 7},
  138. {label: 'f951', value: 18},
  139. {label: 'f940', value: 8},
  140. {label: 'f942', value: 9},
  141. {label: 'f941', value: 10},
  142. {label: 'f934', value: 11},
  143. {label: 'f933', value: 12},
  144. {label: 'f932', value: 13},
  145. {label: 'f9211', value: 14},
  146. {label: '骨架', value: 15},
  147. {label: 'Vue', value: 16},
  148. {label: 'React', value: 17}
  149. ],
  150. rules: {
  151. frame: [
  152. { required: true, message: '请选择框架类型', trigger: 'change' }
  153. ]
  154. },
  155. clickNodeEntry: null
  156. };
  157. },
  158. methods: {
  159. handleClose(done) {
  160. done();
  161. /*
  162. this.$confirm('确认关闭?')
  163. .then(_ => {
  164. done();
  165. })
  166. .catch(_ => {});*/
  167. },
  168. handleNodeClick(data) {
  169. console.log(data);
  170. if(data.type === 'folder') {
  171. return false;
  172. }
  173. var self = this;
  174. var entry = data.entry;
  175. self.clickNodeEntry = entry;
  176. if(self.projectFtpUrl) {
  177. window.open('http://192.168.219.170' + self.projectFtpUrl + data.entry);
  178. self.clickNodeEntry = null;
  179. } else {
  180. if(this.supportDeploy === false) {
  181. this.$message({
  182. type: 'error',
  183. message: '暂只支持查看 重构模板 和 F9x2.0 的项目'
  184. });
  185. self.clickNodeEntry = null;
  186. return false;
  187. }
  188.  
  189. this.$confirm('资源未部署,部署至 170 服务器后可查看,是否部署?')
  190. .then(_ => {
  191.  
  192. this.dialogDeployVisible = true;
  193. /*
  194. this.depDialogVisible = true;
  195. this.loading = true;
  196. // 部署
  197. this.getDeployInfo({ type: '1' }, (data)=> {
  198. this.loading = false;
  199. this.depDialogVisible = false;
  200.  
  201. this.data = data.custom.detail;
  202. this.projectFtpUrl = data.custom.ftpUrl;
  203.  
  204. this.$alert('部署成功!', '提示', {
  205. confirmButtonText: '确定',
  206. callback: action => {
  207. window.open('http://192.168.219.170' + self.projectFtpUrl + entry)
  208. }
  209. });
  210.  
  211. });*/
  212. })
  213. .catch(_ => {});
  214. }
  215. },
  216. // 部署
  217. doDeploy () {
  218. if(!this.isDownLoadPage()) {
  219. return;
  220. }
  221. if(!this.dialogDeployVisible) {
  222. this.dialogDeployVisible = true;
  223. return;
  224. }
  225.  
  226. let self = this;
  227.  
  228. this.$refs['form'].validate((valid) => {
  229. if (valid) {
  230. this.dialogDeployVisible = false;
  231. this.depDialogVisible = true;
  232. this.loading = true;
  233. // 部署
  234. this.getDeployInfo({ type: '1', frame: this.form.frame }, (data)=> {
  235. this.loading = false;
  236. this.depDialogVisible = false;
  237.  
  238. if(!data.custom.text){
  239.  
  240. this.data = data.custom.detail;
  241. this.projectFtpUrl = data.custom.ftpUrl;
  242. this.supportDeploy = data.custom.supportDeploy;
  243. // 从部署按钮直接过来的
  244. if(!this.clickNodeEntry) {
  245. this.$message({
  246. type: 'success',
  247. message: '部署成功!'
  248. });
  249. // 打开查看弹窗
  250. this.viewProject();
  251. } else {// 从点击目录结构过来的,可以调整点击的树节点页面
  252. this.$alert('部署成功!', '提示', {
  253. confirmButtonText: '确定',
  254. callback: action => {
  255. window.open('http://192.168.219.170' + self.projectFtpUrl + self.clickNodeEntry);
  256. self.clickNodeEntry = null;
  257. }
  258. });
  259. }
  260.  
  261. } else {
  262. this.$message({
  263. type: 'error',
  264. message: data.custom.text
  265. });
  266. }
  267. });
  268. } else {
  269. console.log('error submit!!');
  270. return false;
  271. }
  272. });
  273.  
  274. /*
  275. this.$confirm('此操作将把 GitLab 资源打包部署至 170 服务器,已部署过的项目会进行覆盖部署,是否继续?', '提示', {
  276. confirmButtonText: '确定',
  277. cancelButtonText: '取消',
  278. type: 'warning'
  279. }).then(() => {
  280. }).catch(() => {
  281. this.$message({
  282. type: 'info',
  283. message: '已取消部署'
  284. });
  285. });*/
  286. },
  287. // 发布项目案例库
  288. publish () {
  289. this.$confirm('此操作将把项目发布至 <a href="'+ this.projectLibUrl +'" target="_blank">项目案例库</a>,已发布过的项目案例库会有重复项,是否继续?', '提示', {
  290. confirmButtonText: '确定',
  291. cancelButtonText: '取消',
  292. dangerouslyUseHTMLString: true,
  293. type: 'warning'
  294. }).then(() => {
  295. const themes = document.querySelectorAll('.badge-secondary');
  296. let keys = [];
  297.  
  298. for(let i = 0, l = themes.length; i < l; i++) {
  299. if(themes[0].innerText == '智能设备' && i > 0) {
  300. keys.push(themes[i].innerText);
  301. } else if (themes[0].innerText !== '智能设备' && i > 1) {
  302. keys.push(themes[i].innerText);
  303. }
  304. }
  305. // 存在更多主题的情况
  306. const moreKeyEl = document.querySelector('.gl-w-full .text-nowrap');
  307. if(moreKeyEl) {
  308. const moreKeyElContent = moreKeyEl.getAttribute('data-content');
  309. const regex = />([^<]+)</g;
  310. const matches = moreKeyElContent.match(regex);
  311. const results = matches.filter(function(match) {
  312. return match.length > 3;
  313. });
  314. const moreKeyData = results.map(function(match) {
  315. return match.substring(2, match.length - 2);
  316. });
  317.  
  318. if(moreKeyData && moreKeyData.length) {
  319. keys = keys.concat(moreKeyData);
  320. }
  321. }
  322.  
  323. // 组织项目案例库所需参数
  324. const projectName = document.querySelector('.home-panel-title').innerText;
  325. const projectBU = themes[0] ? themes[0].innerText : null;
  326. const projectKeys = keys.join(' ');
  327. const entryUrl = this.projectEntryUrl;
  328. let projectType = themes[1] ? themes[1].innerText : null;
  329.  
  330. if(themes[0]) {
  331. if(themes[0].innerText == '智能设备') {
  332. projectType = themes[1] ? themes[0].innerText : null;
  333. } else {
  334. projectType = themes[1] ? themes[1].innerText : null;
  335. }
  336. }
  337.  
  338. const destUrl = this.projectLibUrl + '?projectName=' + projectName + '&projectBU=' + projectBU + '&projectType=' + projectType + '&projectKeys=' + projectKeys + '&entryUrl=' + entryUrl + '&git=' + window.location.href;
  339.  
  340. this.$message({
  341. type: 'success',
  342. message: destUrl
  343. });
  344.  
  345. window.open(destUrl);
  346.  
  347. }).catch(() => {
  348. this.$message({
  349. type: 'info',
  350. message: '已取消发布'
  351. });
  352. });
  353. },
  354. // 查看项目
  355. viewProject () {
  356. if(!this.isDownLoadPage()) {
  357. return;
  358. }
  359. this.dialogVisible = true;
  360. this.loadingTree = true;
  361. let self = this;
  362.  
  363. // 发送ajax请求,查看是否进行过部署
  364. this.getDeployInfo((data)=> {
  365. // 有部署信息,直接赋值,
  366. if(true) {
  367. self.projectIsDeployed = true;
  368. self.data = data.custom.detail;
  369. self.projectFtpUrl = data.custom.ftpUrl;
  370. self.supportDeploy = data.custom.supportDeploy;
  371. self.loadingTree = false;
  372.  
  373.  
  374. } else {
  375. // 无部署信息,仅查看文件目录
  376. getZipResource((data)=> {
  377. self.data = data;
  378. self.loadingTree = false;
  379. });
  380. }
  381. });
  382.  
  383. },
  384. // 项目部署信息
  385. getDeployInfo(params, callback) {
  386. const projectId = document.body.getAttribute('data-project-id');
  387. const downloadBtn = document.querySelector('.gl-button.btn-sm.btn-confirm');
  388.  
  389. const sourceUrl = downloadBtn.getAttribute('href');
  390. const downloadUrl = window.location.origin + sourceUrl;
  391. const files = document.body.getAttribute('data-find-file').split('/');
  392. const name = document.body.getAttribute('data-project') + '-' + files[files.length - 1];
  393. const author = document.querySelector('.current-user .gl-font-weight-bold').innerText.trim();
  394. const projectName = document.querySelector('.sidebar-context-title').innerText.trim();
  395. const deployManOA = document.querySelector('.current-user>a').getAttribute('data-user');
  396. const projectGitUrl = 'http://192.168.0.200' + document.body.getAttribute('data-find-file').split('/-/')[0];
  397.  
  398. if(typeof params == 'function') {
  399. callback = params;
  400. params = null;
  401. }
  402.  
  403. if(projectId && projectId.length && sourceUrl) {
  404. fetch('http://192.168.219.170:3008/api/getDeployInfo', {
  405. method: 'POST',
  406. // 允许跨域请求
  407. mode: 'cors',
  408. headers: {
  409. 'Accept': 'application/json',
  410. 'Content-Type': 'application/json'
  411. },
  412. body: JSON.stringify({//post请求参数
  413. params: {
  414. "type": (params && params.type !== undefined) ? params.type : '0',// 0 代表查看, 1代表部署
  415. "name": name, // 项目路径英文名
  416. "deployMan": author, // 部署人姓名
  417. "deployManOA": deployManOA, // 部署人账号
  418. "projectName": projectName, // 项目名称
  419. "downloadUrl": downloadUrl, // 下载地址
  420. "projectId": projectId, // 主键,gitlab上的项目id
  421. "projectGitUrl": projectGitUrl,
  422. "frame": (params && params.frame) ? params.frame : undefined
  423. }
  424. })
  425. })
  426. .then(response => response.text())
  427. .then((result) => {
  428. var data = JSON.parse(result);
  429. callback && callback(data);
  430. })
  431. .catch(error => {
  432. this.depDialogVisible = false;
  433. this.dialogVisible = false;
  434. this.$message({
  435. type: 'error',
  436. message: '系统故障,请联系管理员。'
  437. });
  438. console.error(error);
  439. });
  440. } else {
  441. this.$message({
  442. type: 'error',
  443. message: '本页不支持查看和部署,请至仓库页。'
  444. });
  445. console.error('部署信息请求参数error');
  446. }
  447. },
  448. // 设置入口
  449. setProjectEntry(){
  450. const firstNode = findFirstFileNode(this.data);
  451. if(firstNode) {
  452. this.projectEntryUrl = 'http://192.168.219.170' + this.projectFtpUrl + firstNode.entry;
  453. }
  454. },
  455. isDownLoadPage() {
  456. const downloadBtn = document.querySelector('.gl-button.btn-sm.btn-confirm');
  457. const sourceUrl = downloadBtn && downloadBtn.getAttribute('href');
  458.  
  459. if(downloadBtn && sourceUrl) {
  460. return true;
  461. } else {
  462. this.$message({
  463. type: 'error',
  464. message: '本页面不支持查看和部署,请移至仓库页。'
  465. });
  466. return false;
  467. }
  468. }
  469. },
  470. mounted() {
  471.  
  472. }
  473. };
  474.  
  475. const placeholder = document.createElement('div');
  476.  
  477. // 创建 Vue 实例并挂载到页面
  478. const vueInstance = new Vue({
  479. el: placeholder,
  480. components: {
  481. MyComponent
  482. },
  483. methods: {
  484. },
  485. template: `<my-component></my-component>`
  486. });
  487.  
  488. // 等待页面加载完成
  489. window.addEventListener('load', function() {
  490. // 将占位元素追加到 body 元素中
  491. document.body.appendChild(vueInstance.$el);
  492. });
  493.  
  494. // 将文件条目组织成嵌套结构
  495. function organizeFileEntries(fileEntries) {
  496. const root = {
  497. label: document.querySelector('.home-panel-title').innerText || document.getElementById('project_name_edit').value,
  498. type: 'folder',
  499. children: []
  500. };
  501.  
  502. // 创建嵌套结构
  503. fileEntries.forEach(entry => {
  504. const pathSegments = entry.name.split('/');
  505. let currentFolder = root;
  506.  
  507. // 遍历路径中的每个部分,创建相应的文件夹节点
  508. for (let i = 0; i < pathSegments.length - 1; i++) {
  509. const folderName = pathSegments[i];
  510. let folder = currentFolder.children.find(child => child.label === folderName);
  511.  
  512. if(isExcludeFolder(entry.name)) {
  513. continue;
  514. }
  515.  
  516. if (!folder) {
  517. folder = {
  518. label: folderName,
  519. type: 'folder',
  520. children: []
  521. };
  522. currentFolder.children.push(folder);
  523. }
  524.  
  525. currentFolder = folder;
  526. }
  527.  
  528. // 创建文件节点并添加到相应的文件夹中
  529. const fileName = pathSegments[pathSegments.length - 1];
  530.  
  531. if(fileName && fileName.length && isIncludeFile(fileName) && !isExcludeFolder(entry.name)) {
  532. const fileNode = {
  533. label: fileName,
  534. type: 'file',
  535. entry: entry.name
  536. };
  537. currentFolder.children.push(fileNode);
  538. }
  539. });
  540.  
  541. return [root];
  542. }
  543. // 是否排除的文件夹
  544. function isExcludeFolder(entry) {
  545. if(entry.indexOf('css') > -1 ||
  546. entry.indexOf('scss') > -1 ||
  547. entry.indexOf('js') > -1 ||
  548. entry.indexOf('images') > -1 ||
  549. entry.indexOf('fui') > -1 ||
  550. entry.indexOf('lib') > -1 ||
  551. entry.indexOf('test') > -1 ||
  552. entry.indexOf('font') > -1 ||
  553. entry.indexOf('frame/fui') > -1) {
  554. return true;
  555. } else {
  556. return false;
  557. }
  558. }
  559. // 是否包含的文件
  560. function isIncludeFile(fileName) {
  561. if(fileName.indexOf('.html') > -1) {
  562. return true;
  563. } else {
  564. return false;
  565. }
  566. }
  567.  
  568. function getZipResource(callback) {
  569. const downloadUrl = window.location.origin + document.querySelector('.gl-button.btn-sm.btn-confirm').getAttribute('href');
  570.  
  571. fetch(downloadUrl)
  572. .then(response => response.arrayBuffer())
  573. .then(data => {
  574. // 将 ZIP 文件的二进制数据传递给 JSZip 进行解析
  575. return JSZip.loadAsync(data);
  576. })
  577. .then(zip => {
  578. // 获取 ZIP 文件中的所有条目(文件和目录)
  579. const zipEntries = Object.values(zip.files);
  580. const treeData = organizeFileEntries(zipEntries)
  581.  
  582. callback && callback(treeData);
  583.  
  584. })
  585. .catch(error => {
  586. console.error(error);
  587. });
  588. }
  589.  
  590. // 树结构第一个节点数据
  591. function findFirstFileNode(tree) {
  592. // 遍历树的节点
  593. for (let i = 0; i < tree.length; i++) {
  594. const node = tree[i];
  595.  
  596. // 如果节点的类型为 file,则返回该节点
  597. if (node.type === 'file') {
  598. return node;
  599. }
  600.  
  601. // 如果节点有子节点,则递归调用该函数查找子节点中的第一个 file 节点
  602. if (node.children && node.children.length > 0) {
  603. const fileNode = findFirstFileNode(node.children);
  604. if (fileNode) {
  605. return fileNode;
  606. }
  607. }
  608. }
  609.  
  610. // 如果没有找到 file 节点,则返回 null
  611. return null;
  612. }
  613. })();