diff --git a/lib/data.js b/lib/data.js index 5907a0c..58b4dcc 100644 --- a/lib/data.js +++ b/lib/data.js @@ -51,4 +51,25 @@ exports.SyncConfig = function (config) { if (fs.readFileSync(pathJoin("data/config.json")) + "" != JSON.stringify(config) + "") { fs.writeFileSync(pathJoin("data/config.json"), JSON.stringify(config)) } -} \ No newline at end of file +} + +exports.AddCommitToQueue = function (data) { + if (!fs.existsSync(pathJoin("data/commit-queue.json"))) { + fs.writeFileSync(pathJoin("data/commit-queue.json"), "[]") + } + let all = fs.readFileSync(pathJoin("data/commit-queue.json")) + all = JSON.parse(all); + all.push(data); + fs.writeFileSync(pathJoin("data/commit-queue.json"), JSON.stringify(all)) +} + +exports.PopCommitByQueue = function () { + if (!fs.existsSync(pathJoin("data/commit-queue.json"))) { + fs.writeFileSync(pathJoin("data/commit-queue.json"), "[]") + } + let all = fs.readFileSync(pathJoin("data/commit-queue.json")) + all = JSON.parse(all); + var data = all.pop(); + fs.writeFileSync(pathJoin("data/commit-queue.json"), JSON.stringify(all)) + return data; +} \ No newline at end of file diff --git a/lib/http.js b/lib/http.js index af81f27..3d6479e 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,14 +1,61 @@ + +const { Notification } = require('electron') + var http = require("http"); +var querystring = require("querystring"); +var url = require("url"); +const path = require('path') +const libdata = require(path.join(__dirname, 'data.js')); exports.start = function () { http.createServer(function (request, response) { - let body = []; - request.on('data', (chunk) => { - body.push(chunk); - }).on('end', () => { - body = Buffer.concat(body).toString(); - // at this point, `body` has the entire request body stored in it as a string - - console.log("body is " + body); - }); + + var pathname = url.parse(request.url).pathname; + if (HttpMap[pathname]) { + HttpMap[pathname](request, response) + } else { + response.end("请求未定义"); + } }).listen(37188); +} + +HttpMap = { + "/commit": function (request, response) { + let query = querystring.parse(url.parse(request.url).query); + let branch = query["branch"]; + let commit = query["commit"]; + if (!branch || !commit) { + console.log([branch, commit]); + response.end("error"); + return; + } + let branchSplit = branch.split("-"); + let commitSplit = commit.split(" "); + if (branchSplit.length < 2 || commitSplit.length < 2) { + console.log([branch, commit, "error"]); + response.end("error"); + return; + } + let id = branchSplit[0]; + let hash = commitSplit.shift(); + commit = commitSplit.join(" "); + if (commit[0] == '-' || commit[0] == '#') { + console.log([branch, commit, "skip commit"]); + (new Notification({ + title: "有一条新的提交被忽略", + body: `分支:${branch} 提交内容:${commit}` + })).show() + + response.end("skip commit"); + return; + } + libdata.AddCommitToQueue({ + id: id, + branch: branch, + hash: hash, + commit: commit, + }); + + console.log([branch, commit, "success"]); + response.end("success"); + } } \ No newline at end of file diff --git a/lib/tray.js b/lib/tray.js index eeb9a64..384487d 100644 --- a/lib/tray.js +++ b/lib/tray.js @@ -1,5 +1,5 @@ -const { ipcMain, app, Menu, Tray, BrowserWindow, dialog } = require('electron') +const { ipcMain, app, Menu, Tray, BrowserWindow, dialog, Notification } = require('electron') const path = require('path') const fs = require('fs'); @@ -10,13 +10,23 @@ const git = require(path.join(__dirname, 'git.js')); const mainwin = require(path.join(__dirname, 'main.js')); let tray = null +ipcMain.on('new-notification', (event, title, body) => { + (new Notification({ + title: title, + body: body + })).show() +}); + ipcMain.on('new-task-notification', (event) => { - tray.displayBalloon({ + (new Notification({ title: "可能有新的任务", - content: `当前总任务数${libdata.GetConfig().DaibanCount}` - }) + body: `当前总任务数${libdata.GetConfig().DaibanCount}` + })).show() }); +ipcMain.on('test', (event, a, b, c) => { + console.log(event, a, b, c) +}) let isUpdate = false; ipcMain.on('put-in-tray', (event) => { @@ -56,7 +66,8 @@ ipcMain.on('put-in-tray', (event) => { label: "切换到该任务分支", click: (function () { let project_name = i; - let title = data[project_name][k]["title"]; + let id = data[project_name][k]["id"]; + let title = `${id}-${data[project_name][k]["title"]}`; return function () { if (!project_map[project_name] || project_map[project_name] == "") { dialog.showErrorBox(`无法切换分支`, `该项目找不到所在路径(${project_name})`); @@ -66,6 +77,7 @@ ipcMain.on('put-in-tray', (event) => { const simpleGit = require('simple-git')(workingDirPath); git.autoCheckoutLocalBranch(simpleGit, title, function () { console.log("autoCheckoutLocalBranch") + fs.copyFileSync(path.join(__dirname, '../res/post-commit'), path.join(workingDirPath, ".git//hooks/post-commit")) if (ot[project_name] && ot[project_name] != "" && project_map[project_name] && project_map[project_name] != "") { var exec = require('child_process').execFile; exec("cmd.exe", ["/c", "start", "", ot[project_name], project_map[project_name]], function (err, data) { diff --git a/lib/web/main.js b/lib/web/main.js index f2648f7..f34a914 100644 --- a/lib/web/main.js +++ b/lib/web/main.js @@ -27,6 +27,31 @@ window.addEventListener('DOMContentLoaded', () => { } }, 5 * 1000) + function CommitByQueue() { + setTimeout(() => { + let item = libdata2.PopCommitByQueue(); + console.log("PopCommitByQueue") + if (item && item.id) { + console.log(item) + let data = { + sm: item.commit, + name: "评论", + mid: item.id, + modenum: "work" + }; + $.ajax({ + url: "http://oa.ouxuan.net/index.php?a=pinglun&m=flowopt&d=flow&ajaxbool=true", + async: false, + data: data, + success: function () { + ipc.send('new-notification', "有一条新的评论被同步", `分支:${item.branch} 评论内容:${item.commit}`) + } + }); + } + CommitByQueue(); + }, 1000); + } + CommitByQueue(); let m = get("m"); diff --git a/main.js b/main.js index 0ba177b..cae2c15 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow, Notification } = require('electron') const path = require('path') const main = require(path.join(__dirname, "lib/main.js")) @@ -22,7 +22,17 @@ app.whenReady().then(function () { app.dock.hide(); } - // mhttp.start(); + mhttp.start(); + if (Notification.isSupported()) { + (new Notification({ + title: "启动", + body: "ouxuan.oa已经启动" + })).show() + + } else { + + dialog.showErrorBox(`警告`, `桌面通知无法被支持`); + } // tray.createTrayWindow() }) diff --git a/res/post-commit b/res/post-commit new file mode 100644 index 0000000..1d93fa8 --- /dev/null +++ b/res/post-commit @@ -0,0 +1,10 @@ +#!/bin/sh + +branch=`git symbolic-ref --short -q HEAD` +commit=`git log -1 --oneline $branch` + +branchencode=`echo -n "$branch" | xxd -ps | tr -d '\n' | sed -r 's/(..)/%\1/g'` +commitencode=`echo -n "$commit" | xxd -ps | tr -d '\n' | sed -r 's/(..)/%\1/g'` +curl "http://127.0.0.1:37188/commit?branch=${branchencode}&commit=${commitencode}" +echo 分支[$branch][$commit] +exit 0