From 37ef540b118da772a3cd131d7f2260a6bab26564 Mon Sep 17 00:00:00 2001 From: jonycoo Date: Wed, 26 Jun 2024 17:24:50 +0200 Subject: [PATCH] Backup db (#589) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * backupdb2jira using axios * use native fetch instead * create Issue, store Backup in Issue. Added Error logging * remove push trigger, add dispatch and weekly trigger * put workflow to correct location * add test trigger * which dir * update action v4 * delete PR exec of backup * use correct format EJSON * correct dependencies for backupdb script * Test für Freitag morgen --------- Co-authored-by: Silas Meilbeck --- .github/workflows/backup2jira.yaml | 31 +++++++ actions/backup2jira/indexBackup.js | 142 +++++++++++++++++++++++++++++ actions/backup2jira/package.json | 7 ++ 3 files changed, 180 insertions(+) create mode 100644 .github/workflows/backup2jira.yaml create mode 100644 actions/backup2jira/indexBackup.js create mode 100644 actions/backup2jira/package.json diff --git a/.github/workflows/backup2jira.yaml b/.github/workflows/backup2jira.yaml new file mode 100644 index 000000000..9b8bec3a9 --- /dev/null +++ b/.github/workflows/backup2jira.yaml @@ -0,0 +1,31 @@ +name: Backup and Upload + +on: + workflow_dispatch: + schedule: + - cron: "0 0 * * 0" # Every Sunday at midnight (UTC) + - cron: "0 5 * * 5" # Every Friday at 5 AM (UTC) + +jobs: + backup-and-upload: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Install dependencies + run: | + cd actions/backup2jira + npm install + + - name: Run Backup and Upload + run: node actions/backup2jira/indexBackup.js + env: + DATABASE_URI: ${{ secrets.DATABASE_URI }} + JIRA_TOKEN: ${{ secrets.JIRA_TOKEN }} diff --git a/actions/backup2jira/indexBackup.js b/actions/backup2jira/indexBackup.js new file mode 100644 index 000000000..8aff22dde --- /dev/null +++ b/actions/backup2jira/indexBackup.js @@ -0,0 +1,142 @@ +require('dotenv').config(); +const { MongoClient } = require("mongodb"); +const { EJSON } = require('bson') + +// MongoDB URI and database details +const mongoURI = process.env.DATABASE_URI; +const dbName = "Seed"; +const collectionNames = ["Stories", "Repositories", "CustomBlocks", "User", "Workgroups"]; + +const jiraBaseUrl = "https://jira.adesso.de/rest/api/2"; +const jiraProjKey = 'CUC' +const bearerToken = process.env.JIRA_TOKEN + +async function connectToMongoDB() { + try { + const client = new MongoClient(mongoURI); + await client.connect(); + console.log("Connected to MongoDB"); + return client; + } catch (error) { + console.error("Error connecting to MongoDB:", error.message); + throw error; + } +} + +async function queryMongoData(client) { + try { + const db = client.db(dbName); + const data = []; + + for (const collectionName of collectionNames) { + const collection = db.collection(collectionName); + const collectionData = await collection.find().toArray(); + const collectionBSON = collectionData.map(o => EJSON.stringify(o)); // Serialize each (individual)object + //console.log(collectionData) + const file = new File(collectionBSON, `${collectionName}.json`); + data.push(file); + } + + console.log("Retrieved MongoDB data:", data); + return data; + } catch (error) { + console.error("Error querying MongoDB data:", error.message); + throw error; + } +} + +async function createJiraIssue(projectKey) { + try { + const attachmentUrl = `${jiraBaseUrl}/issue`; + + const today = new Date() + + // Append issue details to formData + const issueData = { fields: { + project: { key: projectKey }, + summary: `DB-Backup ${today.toLocaleDateString()}`, + description: `Backup of Database Collections for Project Seed-Test`, + // fixVersion: {id:1002}, + labels: ['DB-Backup'], + issuetype: { + name: "Service Request" + } + }}; + + // Make the POST request + const response = await fetch(attachmentUrl, { + method: 'POST', + headers: { + "Authorization": `Bearer ${bearerToken}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(issueData) + }); + + if(response.ok){ + const content = await response.json(); + console.log("Issue created: ", content.key); + return content.key; + } + throw Error(`creating Issue no ok ${response.status}`) + + } catch (error) { + console.error("Failed Creating Issue. Reason:", error.message); + } +} + + +/** + * + * @param {String} issueKey + * @param {Array} data + */ +async function sendToJiraIssue(issueKey, data) { + try { + const attachmentUrl = `${jiraBaseUrl}/issue/${issueKey}/attachments`; + + const formData = new FormData(); + // Create a FormData object and append the data + data.forEach(file => formData.append("file", file)) + + + // Make the POST request + const response = await fetch(attachmentUrl, { + method: 'POST', + headers: { + "Authorization": `Bearer ${bearerToken}`, // Include your bearer token + "X-Atlassian-Token": "nocheck" + }, + body: formData + }).then(async res => await res.json()); + delete response?.author + + if(response.errorMessages) throw Error(`add attachment no ok ${response.errorMessages}, Issue: ${issueKey}`); + return response; + + } catch (error) { + console.error("Error adding attachment:", error.message); + } +} + + +async function main() { + let client; + try { + client = await connectToMongoDB(); + const data = await queryMongoData(client); + const issueKey = await createJiraIssue(jiraProjKey); + const attachResult = await sendToJiraIssue(issueKey, data) + if(attachResult&&data) console.log("Successfully created DB Backup: ", issueKey); + throw Error("Something went wrong see earlier errors") + } catch (error) { + console.error("Error:", error.message); + } finally { + if (client) { + client.close(); + console.log("Disconnected from MongoDB"); + } + } +} + +main(); diff --git a/actions/backup2jira/package.json b/actions/backup2jira/package.json new file mode 100644 index 000000000..6be3eceae --- /dev/null +++ b/actions/backup2jira/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "bson": "^6.7.0", + "dotenv": "^16.4.5", + "mongodb": "^6.6.2" + } +}