diff --git a/.github/workflows/ci-nym-wallet-storybook.yml b/.github/workflows/ci-nym-wallet-storybook.yml index 212c748386..e669de8fff 100644 --- a/.github/workflows/ci-nym-wallet-storybook.yml +++ b/.github/workflows/ci-nym-wallet-storybook.yml @@ -51,25 +51,3 @@ jobs: REMOTE_USER: ${{ secrets.CI_WWW_REMOTE_USER }} TARGET: ${{ secrets.CI_WWW_REMOTE_TARGET }}/wallet-${{ env.GITHUB_REF_SLUG }} EXCLUDE: "/dist/, /node_modules/" - - - name: Matrix - Node Install - run: npm install - working-directory: .github/workflows/support-files - - - name: Matrix - Send Notification - env: - NYM_NOTIFICATION_KIND: nym-wallet - NYM_PROJECT_NAME: "nym-wallet" - NYM_CI_WWW_BASE: "${{ secrets.NYM_CI_WWW_BASE }}" - NYM_CI_WWW_LOCATION: "wallet-${{ env.GITHUB_REF_SLUG }}" - GIT_COMMIT_MESSAGE: "${{ github.event.head_commit.message }}" - GIT_BRANCH: "${GITHUB_REF##*/}" - IS_SUCCESS: "${{ job.status == 'success' }}" - MATRIX_SERVER: "${{ secrets.MATRIX_SERVER }}" - MATRIX_ROOM: "${{ secrets.MATRIX_ROOM }}" - MATRIX_USER_ID: "${{ secrets.MATRIX_USER_ID }}" - MATRIX_TOKEN: "${{ secrets.MATRIX_TOKEN }}" - MATRIX_DEVICE_ID: "${{ secrets.MATRIX_DEVICE_ID }}" - uses: docker://keybaseio/client:stable-node - with: - args: .github/workflows/support-files/notifications/entry_point.sh diff --git a/.github/workflows/nightly-build.yml b/.github/workflows/nightly-build.yml index 918603b449..e2d23a4d5f 100644 --- a/.github/workflows/nightly-build.yml +++ b/.github/workflows/nightly-build.yml @@ -10,8 +10,8 @@ jobs: strategy: fail-fast: false matrix: - rust: [stable, beta] - os: [ubuntu-22.04, windows-latest, macos-latest] + rust: [ stable, beta ] + os: [ ubuntu-22.04, windows-latest, macos-latest ] runs-on: ${{ matrix.os }} env: CARGO_TERM_COLOR: always @@ -93,38 +93,3 @@ jobs: with: command: clippy args: --workspace --all-targets -- -D warnings - - notification: - needs: build - runs-on: custom-linux - steps: - - name: Collect jobs status - uses: technote-space/workflow-conclusion-action@v3 - - name: Check out repository code - uses: actions/checkout@v6 - - name: install npm - uses: actions/setup-node@v4 - if: env.WORKFLOW_CONCLUSION == 'failure' - with: - node-version: 20 - - name: Matrix - Node Install - if: env.WORKFLOW_CONCLUSION == 'failure' - run: npm install - working-directory: .github/workflows/support-files - - name: Matrix - Send Notification - if: env.WORKFLOW_CONCLUSION == 'failure' - env: - NYM_NOTIFICATION_KIND: nightly - NYM_PROJECT_NAME: "Nym nightly build" - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - GIT_COMMIT_MESSAGE: "${{ github.event.head_commit.message }}" - GIT_BRANCH: "${GITHUB_REF##*/}" - IS_SUCCESS: "${{ env.WORKFLOW_CONCLUSION == 'success' }}" - MATRIX_SERVER: "${{ secrets.MATRIX_SERVER }}" - MATRIX_ROOM: "${{ secrets.MATRIX_ROOM_NIGHTLY }}" - MATRIX_USER_ID: "${{ secrets.MATRIX_USER_ID }}" - MATRIX_TOKEN: "${{ secrets.MATRIX_TOKEN }}" - MATRIX_DEVICE_ID: "${{ secrets.MATRIX_DEVICE_ID }}" - uses: docker://keybaseio/client:stable-node - with: - args: .github/workflows/support-files/notifications/entry_point.sh diff --git a/.github/workflows/nightly-nym-wallet-build.yml b/.github/workflows/nightly-nym-wallet-build.yml index c81b6bc712..b4d4f69a1f 100644 --- a/.github/workflows/nightly-nym-wallet-build.yml +++ b/.github/workflows/nightly-nym-wallet-build.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-22.04, macos-latest, windows-latest] + os: [ ubuntu-22.04, macos-latest, windows-latest ] runs-on: ${{ matrix.os }} env: CARGO_TERM_COLOR: always @@ -55,38 +55,3 @@ jobs: with: command: clippy args: ${{ env.MANIFEST_PATH }} --workspace --all-targets -- -D warnings - - notification: - needs: build - runs-on: custom-linux - steps: - - name: Collect jobs status - uses: technote-space/workflow-conclusion-action@v3 - - name: Check out repository code - uses: actions/checkout@v6 - - name: install npm - uses: actions/setup-node@v4 - if: env.WORKFLOW_CONCLUSION == 'failure' - with: - node-version: 20 - - name: Matrix - Node Install - if: env.WORKFLOW_CONCLUSION == 'failure' - run: npm install - working-directory: .github/workflows/support-files - - name: Matrix - Send Notification - if: env.WORKFLOW_CONCLUSION == 'failure' - env: - NYM_NOTIFICATION_KIND: nightly - NYM_PROJECT_NAME: "nym-wallet-nightly-build" - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - GIT_COMMIT_MESSAGE: "${{ github.event.head_commit.message }}" - GIT_BRANCH: "${GITHUB_REF##*/}" - IS_SUCCESS: "${{ env.WORKFLOW_CONCLUSION == 'success' }}" - MATRIX_SERVER: "${{ secrets.MATRIX_SERVER }}" - MATRIX_ROOM: "${{ secrets.MATRIX_ROOM_NIGHTLY }}" - MATRIX_USER_ID: "${{ secrets.MATRIX_USER_ID }}" - MATRIX_TOKEN: "${{ secrets.MATRIX_TOKEN }}" - MATRIX_DEVICE_ID: "${{ secrets.MATRIX_DEVICE_ID }}" - uses: docker://keybaseio/client:stable-node - with: - args: .github/workflows/support-files/notifications/entry_point.sh diff --git a/.github/workflows/nightly-security-audit.yml b/.github/workflows/nightly-security-audit.yml index 52decfd3d0..8d03483a9c 100644 --- a/.github/workflows/nightly-security-audit.yml +++ b/.github/workflows/nightly-security-audit.yml @@ -24,34 +24,3 @@ jobs: with: name: report path: .github/workflows/support-files/notifications/deny.message - notification: - needs: cargo-deny - runs-on: custom-linux - steps: - - name: Check out repository code - uses: actions/checkout@v6 - - name: Download report from previous job - uses: actions/download-artifact@v7 - with: - name: report - path: .github/workflows/support-files/notifications - - name: install npm - uses: actions/setup-node@v4 - with: - node-version: 20 - - name: Matrix - Node Install - run: npm install - working-directory: .github/workflows/support-files - - name: Matrix - Send Notification - env: - NYM_NOTIFICATION_KIND: security - NYM_PROJECT_NAME: "Daily security report" - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - MATRIX_SERVER: "${{ secrets.MATRIX_SERVER }}" - MATRIX_ROOM: "${{ secrets.MATRIX_ROOM_AUDIT }}" - MATRIX_USER_ID: "${{ secrets.MATRIX_USER_ID }}" - MATRIX_TOKEN: "${{ secrets.MATRIX_TOKEN }}" - MATRIX_DEVICE_ID: "${{ secrets.MATRIX_DEVICE_ID }}" - uses: docker://keybaseio/client:stable-node - with: - args: .github/workflows/support-files/notifications/entry_point.sh diff --git a/.github/workflows/support-files/README.md b/.github/workflows/support-files/README.md index 9cac90aecc..4fddc794f6 100644 --- a/.github/workflows/support-files/README.md +++ b/.github/workflows/support-files/README.md @@ -4,51 +4,23 @@ This is a collection of scripts and files to support GitHub Actions. ## Sending Notifications -These scripts send CI notifications to Matrix by creating messages from templates and env vars passed from GitHub Actions. - -### Adding notifications to a GitHub Action - -``` -jobs: - build: - ... - - name: Notifications - Node Install - run: npm install - working-directory: .github/workflows/support-files/notifications - - name: Notifications - Send - env: - NYM_NOTIFICATION_KIND: "my-component" - GIT_BRANCH: "${GITHUB_REF##*/}" - MATRIX_SERVER: "${{ secrets.MATRIX_SERVER }}" - MATRIX_ROOM: "${{ secrets.MATRIX_ROOM }}" - MATRIX_USER_ID: "${{ secrets.MATRIX_USER_ID }}" - MATRIX_TOKEN: "${{ secrets.MATRIX_TOKEN }}" - MATRIX_DEVICE_ID: "${{ secrets.MATRIX_DEVICE_ID }}" - IS_SUCCESS: "${{ job.status == 'success' }}" - uses: docker://keybaseio/client:stable-node - with: - args: .github/workflows/support-files/notifications/entry_point.sh -``` - -Notifications are run by adding the snippet above to a GitHub Action, and: - -1. Installing node packages needed at run time -2. Set the env vars as required: - - `NYM_NOTIFICATION_KIND` matches the directory in `.github/workflows/support-files/${NYM_NOTIFICATION_KIND}` to provide the templates and extra scripting in `index.js` - - Matrix credentials, room and other env vars for the status of the build and repo -3. Replacing the default entry point shell script on the `keybaseio/client:stable-node` docker image to run `.github/workflows/support-files/notifications/entry_point.sh` +These scripts send CI notifications to Matrix by creating messages from templates and env vars passed from GitHub +Actions. ### Running locally You will need: + - Node 16 LTS - npm -Copy `.github/workflows/support-files/.env.example` to `.github/workflows/support-files/.env` and valid Matrix credentials. +Copy `.github/workflows/support-files/.env.example` to `.github/workflows/support-files/.env` and valid Matrix +credentials. Then run `npm install` to get dependencies. -Start development mode for the notification type you want either by passing the value as an env var called `NYM_NOTIFICATION_KIND` or set the `.env` file values correctly. +Start development mode for the notification type you want either by passing the value as an env var called +`NYM_NOTIFICATION_KIND` or set the `.env` file values correctly. ```bash cd .github/workflows/support-files diff --git a/.github/workflows/support-files/notifications/entry_point.sh b/.github/workflows/support-files/notifications/entry_point.sh deleted file mode 100755 index 04607fb979..0000000000 --- a/.github/workflows/support-files/notifications/entry_point.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash - -# pass exit codes out to GitHub Actions -set -euxo pipefail - -# change to the directory that contains this script -cd "${0%/*}" - -# run the node script -node send_message.js \ No newline at end of file diff --git a/.github/workflows/support-files/notifications/send_message.js b/.github/workflows/support-files/notifications/send_message.js deleted file mode 100644 index 12eec724e1..0000000000 --- a/.github/workflows/support-files/notifications/send_message.js +++ /dev/null @@ -1,126 +0,0 @@ -require('dotenv').config(); - -const { sendMatrixMessage } = require('./send_message_to_matrix'); - -let context = { - kinds: ['nym-wallet', 'ts-packages', 'network-explorer', 'nightly', 'nym-connect','security','ci-docs','cd-docs','ci-dev','cd-dev'], -}; - -/** - * Validate that all required env and context vars are available - */ -function validateContext() { - if (!context.env.NYM_NOTIFICATION_KIND) { - throw new Error( - 'Please set env var NYM_NOTIFICATION_KIND with the project kind that matches a directory in ".github/workflows/support-files"', - ); - } - if (!context.kinds.includes(context.env.NYM_NOTIFICATION_KIND)) { - throw new Error(`Env var NYM_NOTIFICATION_KIND is not in ${context.kinds}`); - } - if (!context.env.NYM_PROJECT_NAME) { - throw new Error( - 'Please set env var NYM_PROJECT_NAME with the project name for displaying in notification messages', - ); - } - if (context.env.MATRIX_ROOM) { - if (!context.env.MATRIX_SERVER) { - throw new Error( - 'Matrix server is not defined. Please set env var MATRIX_SERVER', - ); - } - if (!context.env.MATRIX_USER_ID) { - throw new Error( - 'Matrix user id is not defined. Please set env var MATRIX_USER_ID', - ); - } - if (!context.env.MATRIX_TOKEN) { - throw new Error( - 'Matrix token is not defined. Please set env var MATRIX_TOKEN', - ); - } - if (!context.env.MATRIX_DEVICE_ID) { - throw new Error( - 'Matrix device id is not defined. Please set env var MATRIX_DEVICE_ID', - ); - } - } -} - -/** - * Creates a context that will be available in the templates for rendering notifications - */ -function createTemplateContext() { - const options = { dateStyle: 'full', timeStyle: 'long' }; - context.timestamp = new Date().toLocaleString(undefined, options); - - // add environment to template context and validate - context.env = process.env; - try { - validateContext(); - } catch (e) { - if(process.env.SHOW_DEBUG) { - // recursively print the context for easy debugging and rethrow the error - console.dir({ context }, { depth: null }); - } - throw e; - } - - context.kind = context.env.NYM_NOTIFICATION_KIND; - - if (!context.env.GIT_BRANCH_NAME) { - context.env.GIT_BRANCH_NAME = context.env.GITHUB_REF.split('/') - .slice(2) - .join('/'); - } - - context.status = process.env.IS_SUCCESS === 'true' ? 'success' : 'failure'; -} - -/** - * Uses the `kind` set in the context to process the context and generate a notification message - * @returns {Promise} A string notification message body - */ -async function processKindScript() { - const script = require(`../${context.kind}`); - if (!script.addToContextAndValidate) { - throw new Error( - `"./${context.kind}/index.js" does not export a method called "async addToContextAndValidate(context)"`, - ); - } - if (!script.getMessageBody) { - throw new Error( - `"./${context.kind}/index.js" does not export a method called "async getMessageBody(context)"`, - ); - } - - // call the script to modify and validate the context - await script.addToContextAndValidate(context); - - // let the script create a message body and return the result as a string for sending - return await script.getMessageBody(context); -} - -/** - * The main function, as async so that await syntax is available - */ -async function main() { - createTemplateContext(); - console.log(`Sending notification for kind "${context.kind}"...`); - const messageBody = await processKindScript(); - if(process.env.SHOW_DEBUG) { - console.log('-----------------------------------------'); - console.log(messageBody); - console.log('-----------------------------------------'); - } - if(context.env.MATRIX_ROOM) { - await sendMatrixMessage(context, messageBody, context.env.MATRIX_ROOM) - } - if(context.env.MATRIX_ROOM_OF_SHAME && context.env.IS_SUCCESS !== 'true') { - // when a job fails - await sendMatrixMessage(context, messageBody, context.env.MATRIX_ROOM_OF_SHAME) - } -} - -// call main function and let NodeJS handle the promise -main(); diff --git a/.github/workflows/support-files/notifications/send_message_to_matrix.js b/.github/workflows/support-files/notifications/send_message_to_matrix.js deleted file mode 100644 index 002af92350..0000000000 --- a/.github/workflows/support-files/notifications/send_message_to_matrix.js +++ /dev/null @@ -1,67 +0,0 @@ -const sdk = require('matrix-js-sdk'); -global.Olm = require('olm'); -const { LocalStorage } = require('node-localstorage'); -const localStorage = new LocalStorage('./scratch'); -const { - LocalStorageCryptoStore, -} = require('matrix-js-sdk/lib/crypto/store/localStorage-crypto-store'); -var showdown = require('showdown'); - -// hide all matrix client output -console.error = (error) => console.log('❌ error: ', error); -process.stderr.write = () => {}; -process.stdout.write = () => {}; - - -function createClient(context, room, message) { - const server = context.env.MATRIX_SERVER; - const token = context.env.MATRIX_TOKEN; - const deviceId = context.env.MATRIX_DEVICE_ID; - const userId = context.env.MATRIX_USER_ID; - - const client = sdk.createClient({ - baseUrl: server, - accessToken: token, - userId, - deviceId, - sessionStore: new sdk.WebStorageSessionStore(localStorage), - cryptoStore: new LocalStorageCryptoStore(localStorage), - }); - - client.on('sync', async function(state, prevState, res) { - if (state !== 'PREPARED') return; - client.setGlobalErrorOnUnknownDevices(false); - try { - await client.joinRoom(room); - await client.sendEvent( - room, - 'm.room.message', - { - msgtype: 'm.text', - format: 'org.matrix.custom.html', - body: message, - formatted_body: message, - }, - '', - ); - } catch (error) { - console.error('Job failed: ' + error.message); - } - client.stopClient(); - process.exit(0); - }); - - return client; -} - -async function sendMatrixMessage(contextArg, messageAsMarkdown, roomId) { - const converter = new showdown.Converter(); - const messageAsHtml = converter.makeHtml(messageAsMarkdown); - const client = createClient(contextArg, roomId, messageAsHtml); - await client.initCrypto(); - await client.startClient({ initialSyncLimit: 1 }); -} - -module.exports = { - sendMatrixMessage, -};