فهرست منبع

feat: made Git repository info available alongside Musare version, can be disabled on the frontend

Kristian Vos 2 سال پیش
والد
کامیت
8a4d6c8ad1
6فایلهای تغییر یافته به همراه98 افزوده شده و 8 حذف شده
  1. 15 0
      backend/index.js
  2. 4 2
      docker-compose.yml
  3. 11 1
      frontend/dist/config/template.json
  4. 7 2
      frontend/dist/index.tpl.html
  5. 1 1
      frontend/src/main.js
  6. 60 2
      frontend/webpack.common.js

+ 15 - 0
backend/index.js

@@ -2,6 +2,7 @@ import "./loadEnvVariables.js";
 
 import util from "util";
 import config from "config";
+import fs from "fs";
 
 import package_json from "./package.json";
 
@@ -35,6 +36,20 @@ const MUSARE_VERSION = package_json.version;
 
 const printVersion = () => {
 	console.log(`Musare version: ${MUSARE_VERSION}.`);
+
+	try {
+		const head_contents = fs.readFileSync("app/.parent_git/HEAD").toString().replaceAll("\n", "");
+		const branch = new RegExp("ref: refs/heads/([\.A-Za-z0-9_-]+)").exec(head_contents)[1];
+		const config_contents = fs.readFileSync("app/.parent_git/config").toString().replaceAll("\t", "").split("\n");
+		const remote = new RegExp("remote = (.+)").exec(config_contents[config_contents.indexOf(`[branch "${branch}"]`) + 1])[1];
+		const remote_url = new RegExp("url = (.+)").exec(config_contents[config_contents.indexOf(`[remote "${remote}"]`) + 1])[1];
+		const latest_commit = fs.readFileSync(`app/.parent_git/refs/heads/${branch}`).toString().replaceAll("\n", "");
+		const latest_commit_short = latest_commit.substr(0, 7);
+
+		console.log(`Git branch: ${remote}/${branch}. Remote url: ${remote_url}. Latest commit: ${latest_commit} (${latest_commit_short}).`);
+	} catch(e) {
+		console.log(`Could not get Git info: ${e.message}.`);
+	}
 }
 
 printVersion();

+ 4 - 2
docker-compose.yml

@@ -8,6 +8,7 @@ services:
     volumes:
       - ./backend:/opt/app
       - ./log:/opt/log
+      - ./.git:/opt/app/.parent_git:ro
     links:
       - mongo
       - redis
@@ -21,6 +22,7 @@ services:
     volumes:
       - ./frontend:/opt/app
       - /opt/app/node_modules/
+      - ./.git:/opt/app/.parent_git:ro
     environment:
       - FRONTEND_MODE=${FRONTEND_MODE}
     links:
@@ -46,7 +48,7 @@ services:
     image: redis
     ports:
       - "${REDIS_HOST}:${REDIS_PORT}:6379"
-    command: "--notify-keyspace-events Ex --requirepass ${REDIS_PASSWORD} --appendonly yes"
+    command: "--notify-keyspace-events Ex --requirepass ${REDIS_PASSWORD}
+      --appendonly yes"
     volumes:
       - .redis:/data
-

+ 11 - 1
frontend/dist/config/template.json

@@ -37,6 +37,16 @@
 	// 		"preventDefault": true
 	// 	}
 	// },
+	"debug": {
+		"git": {
+			"remote": false,
+			"remoteUrl": false,
+			"branch": true,
+			"latestCommit": true,
+			"latestCommitShort": true
+		},
+		"version": true
+	},
 	"skipConfigVersionCheck": false,
-	"configVersion": 8
+	"configVersion": 9
 }

+ 7 - 2
frontend/dist/index.tpl.html

@@ -39,9 +39,14 @@
 	<script type='text/javascript' src='/vendor/can-autoplay.min.js'></script>
 	<script type='text/javascript' src='/vendor/lofig.1.3.4.min.js'></script>
 
-	<!--Musare version: <%= htmlWebpackPlugin.options.musareVersion %>-->
+	<!--Musare version: <%= htmlWebpackPlugin.options.debug.version %>-->
 	<script>
-		const MUSARE_VERSION = "<%= htmlWebpackPlugin.options.musareVersion %>";
+		const MUSARE_VERSION = "<%= htmlWebpackPlugin.options.debug.version %>";
+		const MUSARE_GIT_REMOTE = "<%= htmlWebpackPlugin.options.debug.git.remote %>";
+		const MUSARE_GIT_REMOTE_URL = "<%= htmlWebpackPlugin.options.debug.git.remoteUrl %>";
+		const MUSARE_GIT_BRANCH = "<%= htmlWebpackPlugin.options.debug.git.branch %>";
+		const MUSARE_GIT_LATEST_COMMIT = "<%= htmlWebpackPlugin.options.debug.git.latestCommit %>";
+		const MUSARE_GIT_LATEST_COMMIT_SHORT = "<%= htmlWebpackPlugin.options.debug.git.latestCommitShort %>";
 	</script>
 </head>
 

+ 1 - 1
frontend/src/main.js

@@ -9,7 +9,7 @@ import store from "./store";
 
 import AppComponent from "./App.vue";
 
-const REQUIRED_CONFIG_VERSION = 8;
+const REQUIRED_CONFIG_VERSION = 9;
 
 const handleMetadata = attrs => {
 	document.title = `Musare | ${attrs.title}`;

+ 60 - 2
frontend/webpack.common.js

@@ -1,8 +1,66 @@
+process.env.NODE_CONFIG_DIR = `${__dirname}/dist/config/`;
 const path = require("path");
+const fs = require("fs");
+const config = require("config");
 const { VueLoaderPlugin } = require("vue-loader");
 const HtmlWebpackPlugin = require("html-webpack-plugin");
 const ESLintPlugin = require("eslint-webpack-plugin");
-const package_json = require("./package.json");
+const packageJson = require("./package.json");
+
+console.log(`Musare version: ${packageJson.version}.`);
+
+const debug = {
+	git: {
+		remote: "",
+		remoteUrl: "",
+		branch: "",
+		latestCommit: "",
+		latestCommitShort: ""
+	},
+	version: ""
+};
+
+if (config.get("debug.version")) debug.version = packageJson.version;
+
+try {
+	const headContents = fs
+		.readFileSync(".parent_git/HEAD")
+		.toString()
+		.replace(/\n/g, "");
+	const branch = new RegExp("ref: refs/heads/([.A-Za-z0-9_-]+)").exec(
+		headContents
+	)[1];
+	const configContents = fs
+		.readFileSync(".parent_git/config")
+		.toString()
+		.replace(/\t/g, "")
+		.split("\n");
+	const remote = new RegExp("remote = (.+)").exec(
+		configContents[configContents.indexOf(`[branch "${branch}"]`) + 1]
+	)[1];
+	const remoteUrl = new RegExp("url = (.+)").exec(
+		configContents[configContents.indexOf(`[remote "${remote}"]`) + 1]
+	)[1];
+	const latestCommit = fs
+		.readFileSync(`.parent_git/refs/heads/${branch}`)
+		.toString()
+		.replace(/\n/g, "");
+	const latestCommitShort = latestCommit.substr(0, 7);
+
+	console.log(
+		`Git branch: ${remote}/${branch}. Remote url: ${remoteUrl}. Latest commit: ${latestCommit} (${latestCommitShort}).`
+	);
+
+	if (config.get("debug.git.remote")) debug.git.remote = remote;
+	if (config.get("debug.git.remoteUrl")) debug.git.remoteUrl = remoteUrl;
+	if (config.get("debug.git.branch")) debug.git.branch = branch;
+	if (config.get("debug.git.latestCommit"))
+		debug.git.latestCommit = latestCommit;
+	if (config.get("debug.git.latestCommitShort"))
+		debug.git.latestCommitShort = latestCommitShort;
+} catch (e) {
+	console.log(`Could not get Git info: ${e.message}.`);
+}
 
 module.exports = {
 	entry: "./src/main.js",
@@ -23,7 +81,7 @@ module.exports = {
 			template: "dist/index.tpl.html",
 			inject: "body",
 			filename: "index.html",
-			musareVersion: package_json.version
+			debug
 		}),
 		new ESLintPlugin()
 	],