GitLab Viewer Publish and Deploy Project

GitLab Viewer Publish and Deploy Project!

目前為 2023-06-02 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitLab Viewer Publish and Deploy Project
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.08
  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: 'f940', value: 8},
  139. {label: 'f942', value: 9},
  140. {label: 'f941', value: 10},
  141. {label: 'f934', value: 11},
  142. {label: 'f933', value: 12},
  143. {label: 'f932', value: 13},
  144. {label: 'f9211', value: 14},
  145. {label: '骨架', value: 15},
  146. {label: 'Vue', value: 16},
  147. {label: 'React', value: 17}
  148. ],
  149. rules: {
  150. frame: [
  151. { required: true, message: '请选择框架类型', trigger: 'change' }
  152. ]
  153. },
  154. clickNodeEntry: null
  155. };
  156. },
  157. methods: {
  158. handleClose(done) {
  159. done();
  160. /*
  161. this.$confirm('确认关闭?')
  162. .then(_ => {
  163. done();
  164. })
  165. .catch(_ => {});*/
  166. },
  167. handleNodeClick(data) {
  168. console.log(data);
  169. if(data.type === 'folder') {
  170. return false;
  171. }
  172. var self = this;
  173. var entry = data.entry;
  174. self.clickNodeEntry = entry;
  175. if(self.projectFtpUrl) {
  176. window.open('http://192.168.219.170' + self.projectFtpUrl + data.entry);
  177. self.clickNodeEntry = null;
  178. } else {
  179. if(this.supportDeploy === false) {
  180. this.$message({
  181. type: 'error',
  182. message: '暂只支持查看 重构模板 和 F9x2.0 的项目'
  183. });
  184. self.clickNodeEntry = null;
  185. return false;
  186. }
  187.  
  188. this.$confirm('资源未部署,部署至 170 服务器后可查看,是否部署?')
  189. .then(_ => {
  190.  
  191. this.dialogDeployVisible = true;
  192. /*
  193. this.depDialogVisible = true;
  194. this.loading = true;
  195. // 部署
  196. this.getDeployInfo({ type: '1' }, (data)=> {
  197. this.loading = false;
  198. this.depDialogVisible = false;
  199.  
  200. this.data = data.custom.detail;
  201. this.projectFtpUrl = data.custom.ftpUrl;
  202.  
  203. this.$alert('部署成功!', '提示', {
  204. confirmButtonText: '确定',
  205. callback: action => {
  206. window.open('http://192.168.219.170' + self.projectFtpUrl + entry)
  207. }
  208. });
  209.  
  210. });*/
  211. })
  212. .catch(_ => {});
  213. }
  214. },
  215. // 部署
  216. doDeploy () {
  217. if(!this.isDownLoadPage()) {
  218. return;
  219. }
  220. if(!this.dialogDeployVisible) {
  221. this.dialogDeployVisible = true;
  222. return;
  223. }
  224.  
  225. let self = this;
  226.  
  227. this.$refs['form'].validate((valid) => {
  228. if (valid) {
  229. this.dialogDeployVisible = false;
  230. this.depDialogVisible = true;
  231. this.loading = true;
  232. // 部署
  233. this.getDeployInfo({ type: '1', frame: this.form.frame }, (data)=> {
  234. this.loading = false;
  235. this.depDialogVisible = false;
  236.  
  237. if(!data.custom.text){
  238.  
  239. this.data = data.custom.detail;
  240. this.projectFtpUrl = data.custom.ftpUrl;
  241. this.supportDeploy = data.custom.supportDeploy;
  242. // 从部署按钮直接过来的
  243. if(!this.clickNodeEntry) {
  244. this.$message({
  245. type: 'success',
  246. message: '部署成功!'
  247. });
  248. // 打开查看弹窗
  249. this.viewProject();
  250. } else {// 从点击目录结构过来的,可以调整点击的树节点页面
  251. this.$alert('部署成功!', '提示', {
  252. confirmButtonText: '确定',
  253. callback: action => {
  254. window.open('http://192.168.219.170' + self.projectFtpUrl + self.clickNodeEntry);
  255. self.clickNodeEntry = null;
  256. }
  257. });
  258. }
  259.  
  260. } else {
  261. this.$message({
  262. type: 'error',
  263. message: data.custom.text
  264. });
  265. }
  266. });
  267. } else {
  268. console.log('error submit!!');
  269. return false;
  270. }
  271. });
  272.  
  273. /*
  274. this.$confirm('此操作将把 GitLab 资源打包部署至 170 服务器,已部署过的项目会进行覆盖部署,是否继续?', '提示', {
  275. confirmButtonText: '确定',
  276. cancelButtonText: '取消',
  277. type: 'warning'
  278. }).then(() => {
  279. }).catch(() => {
  280. this.$message({
  281. type: 'info',
  282. message: '已取消部署'
  283. });
  284. });*/
  285. },
  286. // 发布项目案例库
  287. publish () {
  288. this.$confirm('此操作将把项目发布至 <a href="'+ this.projectLibUrl +'" target="_blank">项目案例库</a>,已发布过的项目案例库会有重复项,是否继续?', '提示', {
  289. confirmButtonText: '确定',
  290. cancelButtonText: '取消',
  291. dangerouslyUseHTMLString: true,
  292. type: 'warning'
  293. }).then(() => {
  294. const themes = document.querySelectorAll('.badge-secondary');
  295. let keys = [];
  296.  
  297. for(let i = 0, l = themes.length; i < l; i++) {
  298. if(themes[0].innerText == '智能设备' && i > 0) {
  299. keys.push(themes[i].innerText);
  300. } else if (themes[0].innerText !== '智能设备' && i > 1) {
  301. keys.push(themes[i].innerText);
  302. }
  303. }
  304. // 存在更多主题的情况
  305. const moreKeyEl = document.querySelector('.gl-w-full .text-nowrap');
  306. if(moreKeyEl) {
  307. const moreKeyElContent = moreKeyEl.getAttribute('data-content');
  308. const regex = />([^<]+)</g;
  309. const matches = moreKeyElContent.match(regex);
  310. const results = matches.filter(function(match) {
  311. return match.length > 3;
  312. });
  313. const moreKeyData = results.map(function(match) {
  314. return match.substring(2, match.length - 2);
  315. });
  316.  
  317. if(moreKeyData && moreKeyData.length) {
  318. keys = keys.concat(moreKeyData);
  319. }
  320. }
  321.  
  322. // 组织项目案例库所需参数
  323. const projectName = document.querySelector('.home-panel-title').innerText;
  324. const projectBU = themes[0] ? themes[0].innerText : null;
  325. const projectKeys = keys.join(' ');
  326. const entryUrl = this.projectEntryUrl;
  327. let projectType = themes[1] ? themes[1].innerText : null;
  328.  
  329. if(themes[0]) {
  330. if(themes[0].innerText == '智能设备') {
  331. projectType = themes[1] ? themes[0].innerText : null;
  332. } else {
  333. projectType = themes[1] ? themes[1].innerText : null;
  334. }
  335. }
  336.  
  337. const destUrl = this.projectLibUrl + '?projectName=' + projectName + '&projectBU=' + projectBU + '&projectType=' + projectType + '&projectKeys=' + projectKeys + '&entryUrl=' + entryUrl + '&git=' + window.location.href;
  338.  
  339. this.$message({
  340. type: 'success',
  341. message: destUrl
  342. });
  343.  
  344. window.open(destUrl);
  345.  
  346. }).catch(() => {
  347. this.$message({
  348. type: 'info',
  349. message: '已取消发布'
  350. });
  351. });
  352. },
  353. // 查看项目
  354. viewProject () {
  355. if(!this.isDownLoadPage()) {
  356. return;
  357. }
  358. this.dialogVisible = true;
  359. this.loadingTree = true;
  360. let self = this;
  361.  
  362. // 发送ajax请求,查看是否进行过部署
  363. this.getDeployInfo((data)=> {
  364. // 有部署信息,直接赋值,
  365. if(true) {
  366. self.projectIsDeployed = true;
  367. self.data = data.custom.detail;
  368. self.projectFtpUrl = data.custom.ftpUrl;
  369. self.supportDeploy = data.custom.supportDeploy;
  370. self.loadingTree = false;
  371.  
  372.  
  373. } else {
  374. // 无部署信息,仅查看文件目录
  375. getZipResource((data)=> {
  376. self.data = data;
  377. self.loadingTree = false;
  378. });
  379. }
  380. });
  381.  
  382. },
  383. // 项目部署信息
  384. getDeployInfo(params, callback) {
  385. const projectId = document.body.getAttribute('data-project-id');
  386. const downloadBtn = document.querySelector('.gl-button.btn-sm.btn-confirm');
  387.  
  388. const sourceUrl = downloadBtn.getAttribute('href');
  389. const downloadUrl = window.location.origin + sourceUrl;
  390. const files = document.body.getAttribute('data-find-file').split('/');
  391. const name = document.body.getAttribute('data-project') + '-' + files[files.length - 1];
  392. const author = document.querySelector('.current-user .gl-font-weight-bold').innerText.trim();
  393. const projectName = document.querySelector('.sidebar-context-title').innerText.trim();
  394. const deployManOA = document.querySelector('.current-user>a').getAttribute('data-user');
  395. const projectGitUrl = 'http://192.168.0.200' + document.body.getAttribute('data-find-file').split('/-/')[0];
  396.  
  397. if(typeof params == 'function') {
  398. callback = params;
  399. params = null;
  400. }
  401.  
  402. if(projectId && projectId.length && sourceUrl) {
  403. fetch('http://192.168.219.170:3008/api/getDeployInfo', {
  404. method: 'POST',
  405. // 允许跨域请求
  406. mode: 'cors',
  407. headers: {
  408. 'Accept': 'application/json',
  409. 'Content-Type': 'application/json'
  410. },
  411. body: JSON.stringify({//post请求参数
  412. params: {
  413. "type": (params && params.type !== undefined) ? params.type : '0',// 0 代表查看, 1代表部署
  414. "name": name, // 项目路径英文名
  415. "deployMan": author, // 部署人姓名
  416. "deployManOA": deployManOA, // 部署人账号
  417. "projectName": projectName, // 项目名称
  418. "downloadUrl": downloadUrl, // 下载地址
  419. "projectId": projectId, // 主键,gitlab上的项目id
  420. "projectGitUrl": projectGitUrl,
  421. "frame": (params && params.frame) ? params.frame : undefined
  422. }
  423. })
  424. })
  425. .then(response => response.text())
  426. .then((result) => {
  427. var data = JSON.parse(result);
  428. callback && callback(data);
  429. })
  430. .catch(error => {
  431. this.depDialogVisible = false;
  432. this.dialogVisible = false;
  433. this.$message({
  434. type: 'error',
  435. message: '系统故障,请联系管理员。'
  436. });
  437. console.error(error);
  438. });
  439. } else {
  440. this.$message({
  441. type: 'error',
  442. message: '本页不支持查看和部署,请至仓库页。'
  443. });
  444. console.error('部署信息请求参数error');
  445. }
  446. },
  447. // 设置入口
  448. setProjectEntry(){
  449. const firstNode = findFirstFileNode(this.data);
  450. if(firstNode) {
  451. this.projectEntryUrl = 'http://192.168.219.170' + this.projectFtpUrl + firstNode.entry;
  452. }
  453. },
  454. isDownLoadPage() {
  455. const downloadBtn = document.querySelector('.gl-button.btn-sm.btn-confirm');
  456. const sourceUrl = downloadBtn && downloadBtn.getAttribute('href');
  457.  
  458. if(downloadBtn && sourceUrl) {
  459. return true;
  460. } else {
  461. this.$message({
  462. type: 'error',
  463. message: '本页面不支持查看和部署,请移至仓库页。'
  464. });
  465. return false;
  466. }
  467. }
  468. },
  469. mounted() {
  470.  
  471. }
  472. };
  473.  
  474. const placeholder = document.createElement('div');
  475.  
  476. // 创建 Vue 实例并挂载到页面
  477. const vueInstance = new Vue({
  478. el: placeholder,
  479. components: {
  480. MyComponent
  481. },
  482. methods: {
  483. },
  484. template: `<my-component></my-component>`
  485. });
  486.  
  487. // 等待页面加载完成
  488. window.addEventListener('load', function() {
  489. // 将占位元素追加到 body 元素中
  490. document.body.appendChild(vueInstance.$el);
  491. });
  492.  
  493. // 将文件条目组织成嵌套结构
  494. function organizeFileEntries(fileEntries) {
  495. const root = {
  496. label: document.querySelector('.home-panel-title').innerText || document.getElementById('project_name_edit').value,
  497. type: 'folder',
  498. children: []
  499. };
  500.  
  501. // 创建嵌套结构
  502. fileEntries.forEach(entry => {
  503. const pathSegments = entry.name.split('/');
  504. let currentFolder = root;
  505.  
  506. // 遍历路径中的每个部分,创建相应的文件夹节点
  507. for (let i = 0; i < pathSegments.length - 1; i++) {
  508. const folderName = pathSegments[i];
  509. let folder = currentFolder.children.find(child => child.label === folderName);
  510.  
  511. if(isExcludeFolder(entry.name)) {
  512. continue;
  513. }
  514.  
  515. if (!folder) {
  516. folder = {
  517. label: folderName,
  518. type: 'folder',
  519. children: []
  520. };
  521. currentFolder.children.push(folder);
  522. }
  523.  
  524. currentFolder = folder;
  525. }
  526.  
  527. // 创建文件节点并添加到相应的文件夹中
  528. const fileName = pathSegments[pathSegments.length - 1];
  529.  
  530. if(fileName && fileName.length && isIncludeFile(fileName) && !isExcludeFolder(entry.name)) {
  531. const fileNode = {
  532. label: fileName,
  533. type: 'file',
  534. entry: entry.name
  535. };
  536. currentFolder.children.push(fileNode);
  537. }
  538. });
  539.  
  540. return [root];
  541. }
  542. // 是否排除的文件夹
  543. function isExcludeFolder(entry) {
  544. if(entry.indexOf('css') > -1 ||
  545. entry.indexOf('scss') > -1 ||
  546. entry.indexOf('js') > -1 ||
  547. entry.indexOf('images') > -1 ||
  548. entry.indexOf('fui') > -1 ||
  549. entry.indexOf('lib') > -1 ||
  550. entry.indexOf('test') > -1 ||
  551. entry.indexOf('font') > -1 ||
  552. entry.indexOf('frame/fui') > -1) {
  553. return true;
  554. } else {
  555. return false;
  556. }
  557. }
  558. // 是否包含的文件
  559. function isIncludeFile(fileName) {
  560. if(fileName.indexOf('.html') > -1) {
  561. return true;
  562. } else {
  563. return false;
  564. }
  565. }
  566.  
  567. function getZipResource(callback) {
  568. const downloadUrl = window.location.origin + document.querySelector('.gl-button.btn-sm.btn-confirm').getAttribute('href');
  569.  
  570. fetch(downloadUrl)
  571. .then(response => response.arrayBuffer())
  572. .then(data => {
  573. // 将 ZIP 文件的二进制数据传递给 JSZip 进行解析
  574. return JSZip.loadAsync(data);
  575. })
  576. .then(zip => {
  577. // 获取 ZIP 文件中的所有条目(文件和目录)
  578. const zipEntries = Object.values(zip.files);
  579. const treeData = organizeFileEntries(zipEntries)
  580.  
  581. callback && callback(treeData);
  582.  
  583. })
  584. .catch(error => {
  585. console.error(error);
  586. });
  587. }
  588.  
  589. // 树结构第一个节点数据
  590. function findFirstFileNode(tree) {
  591. // 遍历树的节点
  592. for (let i = 0; i < tree.length; i++) {
  593. const node = tree[i];
  594.  
  595. // 如果节点的类型为 file,则返回该节点
  596. if (node.type === 'file') {
  597. return node;
  598. }
  599.  
  600. // 如果节点有子节点,则递归调用该函数查找子节点中的第一个 file 节点
  601. if (node.children && node.children.length > 0) {
  602. const fileNode = findFirstFileNode(node.children);
  603. if (fileNode) {
  604. return fileNode;
  605. }
  606. }
  607. }
  608.  
  609. // 如果没有找到 file 节点,则返回 null
  610. return null;
  611. }
  612. })();