From f709a4ea6b1d59e8c0a4d6f388104ec006de6c46 Mon Sep 17 00:00:00 2001 From: Kamil Musial Date: Tue, 18 Nov 2025 17:46:31 +0100 Subject: [PATCH] feat(DEX-37): remove unused Notion integration - Remove update_notion/ and push_tests/ directories - Remove utils/notion.js utility class - Remove .github/workflows/notion_update.yml workflow - Remove @notionhq/client and glob dependencies from package.json - Update package name to 'jira-github-actions' - Add meaningful package description - Clean codebase for Jira-only integration This removes 9 packages and eliminates failing Notion API calls in production. The codebase now focuses solely on Jira integration. --- .github/workflows/notion_update.yml | 33 --- package-lock.json | 107 ++------ package.json | 8 +- push_tests/action.yml | 18 -- push_tests/index.js | 40 --- update_notion/action.yml | 14 -- update_notion/index.js | 110 --------- utils/notion.js | 364 ---------------------------- 8 files changed, 19 insertions(+), 675 deletions(-) delete mode 100644 .github/workflows/notion_update.yml delete mode 100644 push_tests/action.yml delete mode 100644 push_tests/index.js delete mode 100644 update_notion/action.yml delete mode 100644 update_notion/index.js delete mode 100644 utils/notion.js diff --git a/.github/workflows/notion_update.yml b/.github/workflows/notion_update.yml deleted file mode 100644 index dfc9daf..0000000 --- a/.github/workflows/notion_update.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: Update Notion Statuses - -on: - push: - branches: - - main - - dev - - staging - -jobs: - notion_update_job: - runs-on: ubuntu-latest - name: Update Notion Task - steps: - # To use this repository's private action, - # you must check out the repository - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup Node.js - uses: actions/setup-node@v1 - with: - node-version: '16.x' - - run: npm ci - - - name: Update Notion Task - uses: ./update_notion # Uses an action in the root directory - id: update_notion - env: - GITHUB_TOKEN: ${{ github.token }} - with: - NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }} - NOTION_DATABASE_TOKEN: ${{ secrets.NOTION_DATABASE_TOKEN }} diff --git a/package-lock.json b/package-lock.json index fad2bed..6d199e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,20 +1,18 @@ { - "name": "notion-scripts", + "name": "jira-github-actions", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "notion-scripts", + "name": "jira-github-actions", "version": "1.0.0", "license": "ISC", "dependencies": { "@actions/core": "^1.9.1", "@actions/github": "^5.0.0", - "@notionhq/client": "^0.4.9", "@octokit/rest": "^18.12.0", - "dotenv": "^17.2.3", - "glob": "^7.2.0" + "dotenv": "^17.2.3" }, "devDependencies": { "eslint": "^8.3.0", @@ -100,18 +98,6 @@ "deprecated": "Use @eslint/object-schema instead", "dev": true }, - "node_modules/@notionhq/client": { - "version": "0.4.9", - "resolved": "https://registry.npmjs.org/@notionhq/client/-/client-0.4.9.tgz", - "integrity": "sha512-GR76TrbETlXDsCTr4TotQ/XQzi05g1ydT9iJwyB+N5Brgq6rwYaJva87y90wdI+UwiWquC1DoEHWRe7zeGClUQ==", - "dependencies": { - "@types/node-fetch": "^2.5.10", - "node-fetch": "^2.6.1" - }, - "engines": { - "node": ">=12" - } - }, "node_modules/@octokit/auth-token": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/@octokit/auth-token/-/auth-token-2.5.0.tgz", @@ -232,20 +218,6 @@ "@octokit/openapi-types": "^11.2.0" } }, - "node_modules/@types/node": { - "version": "16.11.11", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.11.11.tgz", - "integrity": "sha512-KB0sixD67CeecHC33MYn+eYARkqTheIRNuu97y2XMjR7Wu3XibO1vaY6VBV6O/a89SPI81cEUIYT87UqUWlZNw==" - }, - "node_modules/@types/node-fetch": { - "version": "2.5.12", - "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.12.tgz", - "integrity": "sha512-MKgC4dlq4kKNa/mYrwpKfzQMB5X3ee5U6fSprkKpToBqBmX4nFZL9cW5jl6sWn+xpRJ7ypWh2yyqqr8UUCstSw==", - "dependencies": { - "@types/node": "*", - "form-data": "^3.0.0" - } - }, "node_modules/acorn": { "version": "8.6.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.6.0.tgz", @@ -359,15 +331,11 @@ "node": ">=8" } }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true }, "node_modules/before-after-hook": { "version": "2.2.2", @@ -378,6 +346,7 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, "dependencies": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -481,17 +450,6 @@ "integrity": "sha512-hUewv7oMjCp+wkBv5Rm0v87eJhq4woh5rSR+42YSQJKecCqgIqNkZ6lAlQms/BwHPJA5NKMRlpxPRv0n8HQW6g==", "dev": true }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/commander": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", @@ -504,7 +462,8 @@ "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true }, "node_modules/cross-spawn": { "version": "7.0.3", @@ -543,14 +502,6 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/deprecation": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/deprecation/-/deprecation-2.3.1.tgz", @@ -854,23 +805,11 @@ "integrity": "sha512-8/sOawo8tJ4QOBX8YlQBMxL8+RLZfxMQOif9o0KUKTNTjMYElWPE0r/m5VNFxTRd0NSw8qSy8dajrwX4RYI1Hw==", "dev": true }, - "node_modules/form-data": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", - "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true }, "node_modules/functional-red-black-tree": { "version": "1.0.1", @@ -895,6 +834,7 @@ "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", "deprecated": "Glob versions prior to v9 are no longer supported", + "dev": true, "dependencies": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -1030,6 +970,7 @@ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", "deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.", + "dev": true, "dependencies": { "once": "^1.3.0", "wrappy": "1" @@ -1038,7 +979,8 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true }, "node_modules/is-extglob": { "version": "2.1.1", @@ -1506,25 +1448,6 @@ "node": ">=8.6" } }, - "node_modules/mime-db": { - "version": "1.51.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.51.0.tgz", - "integrity": "sha512-5y8A56jg7XVQx2mbv1lu49NR4dokRnhZYTtL+KGfaa27uq4pSTXkwQkFJl4pkRMyNFz/EtYDSkiiEHx3F7UN6g==", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/mime-types": { - "version": "2.1.34", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.34.tgz", - "integrity": "sha512-6cP692WwGIs9XXdOO4++N+7qjqv0rqxxVvJ3VHPh/Sc9mVZcQP+ZGhkKiTvWMQRr2tbHkJP/Yn7Y0npb3ZBs4A==", - "dependencies": { - "mime-db": "1.51.0" - }, - "engines": { - "node": ">= 0.6" - } - }, "node_modules/mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -1538,6 +1461,7 @@ "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, "dependencies": { "brace-expansion": "^1.1.7" }, @@ -1677,6 +1601,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true, "engines": { "node": ">=0.10.0" } diff --git a/package.json b/package.json index 15fb117..0d5be18 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "notion-scripts", + "name": "jira-github-actions", "version": "1.0.0", - "description": "", + "description": "GitHub Actions for automating Jira issue management", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", @@ -21,10 +21,8 @@ "dependencies": { "@actions/core": "^1.9.1", "@actions/github": "^5.0.0", - "@notionhq/client": "^0.4.9", "@octokit/rest": "^18.12.0", - "dotenv": "^17.2.3", - "glob": "^7.2.0" + "dotenv": "^17.2.3" }, "devDependencies": { "eslint": "^8.3.0", diff --git a/push_tests/action.yml b/push_tests/action.yml deleted file mode 100644 index d3c2ffc..0000000 --- a/push_tests/action.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: 'Push tests to Notion' -description: 'Github Action to push feature files to Notion DB' -inputs: - NOTION_API_KEY: # id of input - description: 'API Key to get access to the notion board' - required: true - default: '' - NOTION_TESTS_DATABASE: # id of input - description: 'Notion Test Database where the feature files are to be pushed' - required: true - default: '' - TEST_FILE_PATH: # id of input - description: 'Path from where the test files are to be read' - required: true - default: '' -runs: - using: 'node16' - main: 'index.js' diff --git a/push_tests/index.js b/push_tests/index.js deleted file mode 100644 index c817849..0000000 --- a/push_tests/index.js +++ /dev/null @@ -1,40 +0,0 @@ -const glob = require('glob') -const core = require('@actions/core') -const Notion = require('./../utils/notion') - -async function _getFeatureFiles (featureFilePath) { - return new Promise((resolve, reject) => { - glob(featureFilePath, (err, files) => { - if (err) { - reject(err) - } - - resolve(files) - }) - }) -} - -(async () => { - - const TEST_FILE_PATH = core.getInput('TEST_FILE_PATH') - const NOTION_TESTS_DATABASE = core.getInput('NOTION_TESTS_DATABASE') - const NOTION_API_KEY = core.getInput('NOTION_API_KEY') - - const notionUtil = new Notion({ - apiKey: NOTION_API_KEY, - testDatabase: NOTION_TESTS_DATABASE, - }) - - const testFilePaths = TEST_FILE_PATH.split(',').map((p) => p.trim()) - for (let index = 0; index < testFilePaths.length; index++) { - const eachFilePath = testFilePaths[index] - const files = await _getFeatureFiles(eachFilePath) - - await Promise.all(files.map(async filePath => { - notionUtil.createOrUpdateFeatureBlock(filePath) - })) - } -})().catch((error) => { - core.setFailed(error.message) -}) - diff --git a/update_notion/action.yml b/update_notion/action.yml deleted file mode 100644 index a2abca3..0000000 --- a/update_notion/action.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: 'Update Notion Task Status' -description: 'Github Action to Update Notion Task when a PR has been merged to a particular stage' -inputs: - NOTION_API_KEY: # id of input - description: 'API Key to get access to the notion board' - required: true - default: '' - NOTION_DATABASE_TOKEN: # id of input - description: 'Token to identify the task board in which the change is going to happen' - required: true - default: '' -runs: - using: 'node16' - main: 'index.js' diff --git a/update_notion/index.js b/update_notion/index.js deleted file mode 100644 index a2684fc..0000000 --- a/update_notion/index.js +++ /dev/null @@ -1,110 +0,0 @@ -const core = require('@actions/core') -const { Octokit } = require('@octokit/rest') - -const Notion = require('./../utils/notion') - -try { - const { - GITHUB_REF, - } = process.env - - const allowedBranches = [ - 'refs/heads/master', - 'refs/heads/main', - 'refs/heads/staging', - 'refs/heads/dev', - ] - - if (allowedBranches.indexOf(GITHUB_REF) !== -1) { - const branchName = GITHUB_REF.split('/').pop() - _updateNotionStatuses(branchName) - } -} catch (error) { - core.setFailed(error.message) -} - -function extractPrNumber (commitMessage) { - const match = commitMessage - .match(/#[0-9]{1,5}/) - - if (match && match.length) { - return match[0].substring(1) - } - return undefined -} - -/** - * Function to update the status of the tasks based on - * the recent commit is getting merged to which branch - * - * @param {'main' | 'dev' | 'staging' | 'master' } branch Name of the branch to which the commit is getting merged to - */ -async function _updateNotionStatuses (branch) { - - const { - GITHUB_REPOSITORY, - GITHUB_TOKEN, - } = process.env - - const octokit = new Octokit({ - auth: GITHUB_TOKEN, - }) - - const NOTION_DATABASE_TOKEN = core.getInput('NOTION_DATABASE_TOKEN') - const NOTION_API_KEY = core.getInput('NOTION_API_KEY') - - const notionUtil = new Notion({ - apiKey: NOTION_API_KEY, - databaseToken: NOTION_DATABASE_TOKEN, - }) - - const [ githubOwner, repositoryName ] = GITHUB_REPOSITORY.split('/') - - // Get most recent commit to branch - const { data } = await octokit.rest.repos.getCommit({ - owner: githubOwner, - repo: repositoryName, - ref: branch, - perPage: 1, - page: 1, - }) - - const { - commit: { - message: commitMessage, - }, - } = data - - switch (branch) { - case 'master': - case 'main': - if (commitMessage.includes('from coursedog/staging')) { - // Update all tasks that are in Completed Staging to Completed Prod - notionUtil.updateByStatus('Completed (Staging)', 'Completed (Production)') - } else if (commitMessage.match(/#+[0-9]/)) { - // direct from open PR to staging - const prNumber = extractPrNumber(commitMessage) - if (!prNumber) return - notionUtil.updateByPR(`${repositoryName}/pull/${prNumber}`, 'Completed (Production)') - } - break - case 'staging': - if (commitMessage.match(/#+[0-9]/)) { - const prNumber = extractPrNumber(commitMessage) - if (!prNumber) return - notionUtil.updateByPR(`${repositoryName}/pull/${prNumber}`, 'Completed (Staging)') - } - break - case 'dev': - if (commitMessage.match(/#+[0-9]/)) { - // direct from open PR to dev - const prNumber = extractPrNumber(commitMessage) - if (!prNumber) return - notionUtil.updateByPR(`${repositoryName}/pull/${prNumber}`, 'Completed (Staging)') - } - break - - default: - break - } -} diff --git a/utils/notion.js b/utils/notion.js deleted file mode 100644 index 8405247..0000000 --- a/utils/notion.js +++ /dev/null @@ -1,364 +0,0 @@ -const fs = require('fs') -const { Client }= require('@notionhq/client') - -class Notion { - constructor ({ apiKey, databaseToken, testDatabase }) { - // Retrieving all the input parameters relevant to notion - this.notionDatabaseToken = databaseToken - this.testDatabase = testDatabase - - // Initializing the notion client - this.client = new Client({ auth: apiKey }) - this.labels = { - GITHUB_PR: 'GitHub PR', - STATUS: 'Status', - DATE_COMPLETED: 'Date Completed', - } - } - - /** - * Function to update the status of a particular task from one to the other one - * - * @param {String} originalStatus Status from which the task is to be moved - * @param {String} newStatus Status to which the status is to be moved - */ - async updateByStatus (originalStatus, newStatus) { - console.log('Updating all Notion tasks from', originalStatus, 'to', newStatus) - const databaseId = this.notionDatabaseToken - const { - GITHUB_REPOSITORY, - } = process.env - const repositoryName = GITHUB_REPOSITORY.split('/').pop() - - const listOfTasks = await this.client.databases.query({ - database_id: databaseId, - filter: { - and: [ - { - property: this.labels.STATUS, - select: { - equals: originalStatus, - }, - }, - { - property: this.labels.GITHUB_PR, - text: { - contains: repositoryName, - }, - }, - ], - }, - }) - for (let i = 0; i < listOfTasks.results.length; i++) { - const updatedTasks = await this.client.pages.update({ - page_id: listOfTasks.results[i].id, - properties: { - Status: { - select: { - name: newStatus, - }, - }, - }, - }) - console.log('Updated', updatedTasks) - } - } - - /** - * Function to promote the task by the Github PR field - * - * @param {String} pr PR URL for which the status is to be updated - * @param {String} newStatus New status to which the task is to be promoted - */ - async updateByPR (pr, newStatus) { - console.log('Updating one Notion task with PR', pr, 'to', newStatus) - const databaseId = this.notionDatabaseToken - const listOfTasks = await this.client.databases.query({ - database_id: databaseId, - filter: { - and: [ - { - property: this.labels.GITHUB_PR, - text: { - contains: pr, - }, - }, - { - property: this.labels.STATUS, - select: { - does_not_equal: 'Completed (old)', - }, - }, - { - property: this.labels.STATUS, - select: { - does_not_equal: 'Completed (Production)', - }, - }, - ], - }, - }) - for (let i = 0; i < listOfTasks.results.length; i++) { - const updatedTasks = await this.client.pages.update({ - page_id: listOfTasks.results[i].id, - properties: { - [this.labels.STATUS]: { - select: { - name: newStatus, - }, - }, - [this.labels.DATE_COMPLETED]: { - date: { - start: new Date().toISOString(), - }, - }, - }, - }) - console.log('Updated', updatedTasks) - } - } - - /** - * Function to create a new feature block - * - * @param {String} object.content Content of the feature block - * @param {String} object.name Name of the page block which is to be used - * @param {String} object.product Product of the feature block - * @param {String} object.fileUrl URL of the file where the feature file is present - */ - async _createFeatureBlock ({ - content, - name, - product, - fileUrl, - }) { - return this.client.pages.create({ - parent: { - database_id: this.testDatabase, - }, - properties: { - Name: { - title: [ - { - type: 'text', - text: { - content: name, - }, - }, - ], - }, - File: { - url: fileUrl, - }, - Product: { - multi_select: [ - { name: product }, - ], - }, - Status: { - select: { - name: 'Implemented', - }, - }, - }, - children: [ - { - object: 'block', - type: 'code', - code: { - text: content, - language: 'gherkin', - }, - }, - ], - }) - } - - /** - * Function to update an existing feature block - * - * @param {Object} card Page object fetched from notion which is to be updated - * @param {String} object.content Content of the feature block - * @param {String} object.name Name of the page block which is to be used - * @param {String} object.product Product of the feature block - * @param {String} object.fileUrl URL of the file where the feature file is present - */ - async _updateFeatureBlock (card, { - content, - name, - product, - fileUrl, - }) { - await this.client.pages.update({ - page_id: card.id, - properties: { - Name: { - title: [ - { - type: 'text', - text: { - content: name, - }, - }, - ], - }, - File: { - url: fileUrl, - }, - Product: { - multi_select: [ - { name: product }, - ], - }, - Status: { - select: { - name: 'Implemented', - }, - }, - }, - }) - - const pageBlocks = await this.client.blocks.children.list({ - block_id: card.id, - page_size: 100, - }) - // NOTE: We find the first block that is unsupported (likely the divider) starting from the bottom - const dividerIndex = pageBlocks.results - .reverse() - .findIndex(block => block.type === 'unsupported') - - const blocksToDelete = pageBlocks.results.slice(0, dividerIndex) - - // NOTE: Delete all blocks after the last unsupported block (likely the divider) - if (blocksToDelete.length) { - for (const block of blocksToDelete) { - await this.client.blocks.delete({ - block_id: block.id, - }) - } - } - - // NOTE: Restore the feature block - await this.client.blocks.children.append({ - block_id: card.id, - children: [ - { - object: 'block', - type: 'code', - code: { - text: content, - language: 'gherkin', - }, - }, - ], - }) - } - - /** - * Utility Function to return the product under which this .feature file belongs - * - * @param {String} filePath Path of the file for which the product is to be determined - * @returns {String} Name of the product which is to be used to tag the file - */ - __getProduct (filePath) { - - const { - GITHUB_REPOSITORY, - } = process.env - - const repositoryName = GITHUB_REPOSITORY.split('/').pop() - - if (repositoryName === 'coursedog-catalog') { - return 'Catalog' - } - if (repositoryName === 'coursedog-events') { - return 'Events' - } - - switch (true) { - case filePath.includes('/cm/'): - return 'Curriculum' - case filePath.includes('/em/'): - return 'Events' - case filePath.includes('/ca/'): - return 'Catalog' - case filePath.includes('/sm/'): - return 'Scheduling' - default: - return 'All' - } - } - - /** - * Function to create or update an existing feature block in notion by - * the path of the feature file - * - * @param {String} filePath Path of the file where the feature file exists - * @returns {Promise} Returns promise around whether - */ - async createOrUpdateFeatureBlock (filePath) { - const fileContent = fs.readFileSync(filePath, 'utf8') - - const lines = fileContent.split('\n') - const children = [] - let i; let j; const chunk = 7 - // break up the lines into multiple chunks to avoid - // Notion API limits - for (i = 0, j = lines.length; i < j; i += chunk) { - children.push(lines.slice(i, i + chunk)) - } - - const textChildren = children.map(group => ({ - type: 'text', - text: { - content: group.join('\n'), - }, - })) - - const firstLine = (fileContent.match(/(^.*)/) || [])[1] || '' - const product = this.__getProduct(filePath) - const { - GITHUB_REPOSITORY, - } = process.env - const fileUrl = `https://github.com/${GITHUB_REPOSITORY}/blob/dev/${filePath.substring(5)}` - - const existingCards = await this.client.databases.query({ - database_id: this.testDatabase, - filter: { - or: [ - { - property: 'File', - text: { - contains: fileUrl, - }, - }, - { - property: 'Name', - text: { - contains: firstLine, - }, - }, - ], - }, - }) - - if (existingCards.results.length) { - return existingCards.results.map(card => this._updateFeatureBlock(card, { - content: textChildren, - name: firstLine, - product, - fileUrl, - })) - } else { - return this._createFeatureBlock({ - content: textChildren, - name: firstLine, - product, - fileUrl, - }) - } - } - -} - -module.exports = Notion