From cbddf71ebee042d9ad8734fbba70f6f85d1ba4c2 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Fri, 10 Feb 2023 15:52:28 +0000 Subject: [PATCH] Include version information in `!draupnir status`. --- .github/workflows/docker-hub-develop.yml | 2 ++ .github/workflows/docker-hub-release.yml | 2 ++ .gitignore | 2 ++ Dockerfile | 1 + package.json | 10 +++++----- src/commands/StatusCommand.ts | 7 +++++++ src/config.ts | 19 +++++++++++++++++++ 7 files changed, 38 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker-hub-develop.yml b/.github/workflows/docker-hub-develop.yml index a1faa85..12fe84b 100644 --- a/.github/workflows/docker-hub-develop.yml +++ b/.github/workflows/docker-hub-develop.yml @@ -19,6 +19,8 @@ jobs: steps: - name: Check out uses: actions/checkout@v2 + - name: Prepare version file + run: git describe > src/version.txt - name: Log in to Docker Hub uses: docker/login-action@v1 with: diff --git a/.github/workflows/docker-hub-release.yml b/.github/workflows/docker-hub-release.yml index e9d3831..99c97f3 100644 --- a/.github/workflows/docker-hub-release.yml +++ b/.github/workflows/docker-hub-release.yml @@ -18,6 +18,8 @@ jobs: uses: actions/checkout@v2 - name: Get release tag run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + - name: Prepare version file + run: git describe > src/version.txt - name: Log in to Docker Hub uses: docker/login-action@v1 with: diff --git a/.gitignore b/.gitignore index 68e477c..90e3e66 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ venv/ /db +# version file generated from yarn build +src/version.txt # Logs logs *.log diff --git a/Dockerfile b/Dockerfile index d0891b0..ead54f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ RUN cd /tmp/src \ && mv lib/ /mjolnir/ \ && mv node_modules / \ && mv mjolnir-entrypoint.sh / \ + && mv package.json / \ && cd / \ && rm -rf /tmp/* diff --git a/package.json b/package.json index be7d601..a73a7a2 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { - "name": "mjolnir", + "name": "draupnir", "version": "1.80.0-beta.0", "description": "A moderation tool for Matrix", "main": "lib/index.js", - "repository": "git@github.com:matrix-org/mjolnir.git", - "author": "The Matrix.org Foundation C.I.C.", - "license": "Apache-2.0", + "repository": "git@github.com:Gnuxie/Draupnir.git", + "author": "Gnuxie", + "license": "SEE LICENSE IN LICENSE", "private": true, "scripts": { - "build": "tsc", + "build": "tsc && (git describe > src/version.txt || true)", "postbuild": "rm -rf lib/test/ && cp -r lib/src/* lib/ && rm -rf lib/src/", "lint": "tslint --project ./tsconfig.json -t stylish", "start:dev": "yarn build && node --async-stack-traces lib/index.js", diff --git a/src/commands/StatusCommand.ts b/src/commands/StatusCommand.ts index 8f141b2..acc1f84 100644 --- a/src/commands/StatusCommand.ts +++ b/src/commands/StatusCommand.ts @@ -30,6 +30,7 @@ import { RichReply } from "matrix-bot-sdk"; import { htmlEscape, parseDuration } from "../utils"; import { HumanizeDurationLanguage, HumanizeDuration } from "humanize-duration-ts"; import PolicyList from "../models/PolicyList"; +import { PACKAGE_JSON, SOFTWARE_VERSION } from "../config"; const HUMANIZE_LAG_SERVICE: HumanizeDurationLanguage = new HumanizeDurationLanguage(); const HUMANIZER: HumanizeDuration = new HumanizeDuration(HUMANIZE_LAG_SERVICE); @@ -102,6 +103,12 @@ async function showMjolnirStatus(roomId: string, event: any, mjolnir: Mjolnir) { const subscribedAndProtectedLists = mjolnir.policyListManager.lists.filter(list => mjolnir.explicitlyProtectedRooms.includes(list.roomId)); renderPolicyLists("Subscribed and protected policy lists", subscribedAndProtectedLists); + html += `Version: ${SOFTWARE_VERSION}
`; + text += `Version: ${SOFTWARE_VERSION}\n`; + + html += `Repository: ${PACKAGE_JSON['repository'] ?? 'Unknown'}`; + text += `Repository: ${PACKAGE_JSON['repository'] ?? 'Unknown'}`; + const reply = RichReply.createFor(roomId, event, text, html); reply["msgtype"] = "m.notice"; return mjolnir.client.sendMessage(roomId, reply); diff --git a/src/config.ts b/src/config.ts index 9efc49f..8de6ed1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -29,6 +29,7 @@ import * as fs from "fs"; import { load } from "js-yaml"; import { MatrixClient, LogService } from "matrix-bot-sdk"; import Config from "config"; +import path from "path"; /** * The configuration, as read from production.yaml @@ -283,3 +284,21 @@ export function getProvisionedMjolnirConfig(managementRoomId: string): IConfig { config.protectedRooms = []; return config; } + +export const SOFTWARE_VERSION = (() => { + try { + return fs.readFileSync(path.join(__dirname, 'version.txt'), 'utf-8') + } catch (e) { + LogService.error("config", "Could not read Draupnir version", e); + return "A version was either not provided when building Draupnir or could not be read." + } +})(); + +export const PACKAGE_JSON = (() => { + try { + return JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8')); + } catch (e) { + LogService.error("config", "Could not read Draupnir package.json", e); + return {}; + } +})();