From 386b01be3fe82cbac0863ad72db84b204162dba1 Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Fri, 31 Jan 2020 19:32:45 +0300 Subject: [PATCH] Cannot read property 'branch' of null #831 (#832) --- server/Dockerfile | 1 - server/build/webpack.base.conf.js | 7 +++++++ server/jest.config.js | 5 ++++- server/package.json | 1 + server/src/config.ts | 29 +++++++++-------------------- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/server/Dockerfile b/server/Dockerfile index 572789a..560ca42 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -15,7 +15,6 @@ RUN npm run build FROM node:8-alpine # Note: context starts in directory above (see docker-compose file) -COPY .git /var/www/.git COPY server/package.json /var/www/server/ WORKDIR /var/www/server diff --git a/server/build/webpack.base.conf.js b/server/build/webpack.base.conf.js index 9cd3d37..0e01075 100644 --- a/server/build/webpack.base.conf.js +++ b/server/build/webpack.base.conf.js @@ -2,6 +2,8 @@ const path = require('path'); const fs = require('fs'); const webpack = require('webpack'); +const GitRevisionPlugin = require('git-revision-webpack-plugin'); +const gitRevisionPlugin = new GitRevisionPlugin(); function resolve(p) { @@ -23,6 +25,11 @@ module.exports = { minimize: false }, plugins: [ + new webpack.DefinePlugin({ + 'GIT_VERSION': JSON.stringify(gitRevisionPlugin.version()), + 'GIT_COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()), + 'GIT_BRANCH': JSON.stringify(gitRevisionPlugin.branch()), + }) ], resolve: { extensions: [".ts", ".js"], diff --git a/server/jest.config.js b/server/jest.config.js index 4f0f1e2..c0f5ed7 100644 --- a/server/jest.config.js +++ b/server/jest.config.js @@ -4,7 +4,10 @@ module.exports = { "ts-jest": { "useBabelrc": true, "tsConfigFile": "tsconfig.jest.json" - } + }, + "GIT_VERSION": "test_version", + "GIT_COMMITHASH": "test_commit_hash", + "GIT_BRANCH": "test_branch" }, "transform": { "\\.ts": "ts-jest" diff --git a/server/package.json b/server/package.json index 13bec3e..3b62b9c 100644 --- a/server/package.json +++ b/server/package.json @@ -41,6 +41,7 @@ "es6-promise": "^4.2.4", "event-stream": "3.3.4", "file-loader": "^1.1.11", + "git-revision-webpack-plugin": "^3.0.4", "grafana-datasource-kit": "0.1.17", "jest": "^23.1.1", "koa": "^2.0.46", diff --git a/server/src/config.ts b/server/src/config.ts index a58b26a..061c9dd 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -9,6 +9,10 @@ import * as fs from 'fs'; import * as os from 'os'; +declare const GIT_BRANCH: string; +declare const GIT_COMMITHASH: string; +declare const GIT_VERSION: string; + let configFile = path.join(__dirname, '../../config.json'); let configExists = fs.existsSync(configFile); @@ -66,7 +70,11 @@ export const HASTIC_ALERTMANAGER_URL = getConfigField('HASTIC_ALERTMANAGER_URL', export const ANLYTICS_PING_INTERVAL = 500; // ms export const PACKAGE_VERSION = getPackageVersion(); -export const GIT_INFO = getGitInfo(); +export const GIT_INFO = { + branch: GIT_BRANCH, + commitHash: GIT_COMMITHASH, + version: GIT_VERSION +}; export const INSIDE_DOCKER = process.env.INSIDE_DOCKER !== undefined; export const PRODUCTION_MODE = process.env.NODE_ENV !== 'development'; @@ -117,25 +125,6 @@ function getPackageVersion() { } } -function getGitInfo() { - let gitRoot = path.join(__dirname, '../../.git'); - let gitHeadFile = path.join(gitRoot, 'HEAD'); - if(!fs.existsSync(gitHeadFile)) { - console.error(`Can't find git HEAD file ${gitHeadFile}`); - return null; - } - const ref = fs.readFileSync(gitHeadFile).toString(); - let branchPath = ref.indexOf(':') === -1 ? ref : ref.slice(5, -1); - let branch = branchPath.split('/').pop(); - const branchFilename = `${gitRoot}/${branchPath}`; - if(!fs.existsSync(branchFilename)) { - console.error(`Can't find git branch file ${branchFilename}`); - return null; - } - let commitHash = fs.readFileSync(branchFilename).toString().slice(0, 7); - return { branch, commitHash }; -} - function createZMQConnectionString() { let zmq =`tcp://${ZMQ_HOST}:${ZMQ_DEV_PORT}`; //debug mode let zmqConf = getConfigField('ZMQ_CONNECTION_STRING', null);