Skip to content

Commit

Permalink
Fixed build process and added workflows to test dev and prod builds (#…
Browse files Browse the repository at this point in the history
…2792)

* Fixed build process

* Moved import fixes to fix-imports dir

* Fixed development errors

* updated pr workflow to test dev and prod

* Enabled introspection

* Updated to ES module

* Fix import sample data errors

* Fixed PR workflow

* Fixed PR workflow

* Skip ssl verification for prod

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* improve code coverage and updated pr workflow

* Added scripts for certs

* Updated .gitignore

* Added cert.pem and key.pem to gitignore

* Updated PR workflow

* Added empty certs dir

* Remove .gitkeep

* Updated certs dir

* Added ssl version to pr workflow

* Fixed github actions

* Fixed node version in CI

* Fixed ssl errors

* Fixed existing processes at port 4000

* Added command to free port 4000

* Fixed errors

* Fix port issues

* Updated few packages

* Updated node version to latest

* Added coderabbitai changes

* Fixed vulnerabilities

* Update node-version to latest LTS version

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
adithyanotfound and coderabbitai[bot] authored Dec 26, 2024
1 parent 4b5c74f commit 5c5b29a
Show file tree
Hide file tree
Showing 72 changed files with 8,007 additions and 6,494 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-tsdoc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

// List of files to skip
const filesToSkip = [
Expand Down
170 changes: 144 additions & 26 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 2
persist-crendetials: true
persist-credentials: true

- name: Set up Node.js
uses: actions/setup-node@v4
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 2
persist-crendetials: true
persist-credentials: true


- name: Get Changed Unauthorized files
Expand Down Expand Up @@ -154,7 +154,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 2
persist-crendetials: true
persist-credentials: true

- name: Get changed files
id: changed-files
Expand Down Expand Up @@ -255,29 +255,6 @@ jobs:

- name: Run the tests
run: npm run test

- name: Start the development server
run: |
npm run dev &
echo "Development server started..."
- name: Check server status
run: |
if curl -f 'http://localhost:4000' | jq -e '. == {"talawa-version":"v1","status":"healthy"}' > /dev/null; then
echo "Server is up and healthy"
else
echo "Server is down"
exit 1
fi
- name: Check Apollo Server status
run: |
if curl -f 'http://localhost:4000/graphql?query=%7B__typename%7D' \
-H 'Apollo-Require-Preflight: true' | jq -e '. == {"data":{"__typename":"Query"}}' > /dev/null; then
echo "Apollo Server is up"
else
echo "Server is down"
exit 1
fi

- name: Import Sample Data
run: npm run import:sample-data
Expand All @@ -296,6 +273,147 @@ jobs:
path: './coverage/lcov.info'
min_coverage: 95.0

Test-Builds:
name: Test Development and Production Builds
runs-on: ubuntu-latest
needs: Test-Application
strategy:
matrix:
node-version: [22.x]
services:
mongo:
image: mongo:4.4
options: >-
--health-cmd mongo
--health-interval 10s
--health-timeout 5s
--health-retries 10
ports:
- 27017:27017
redis:
image: redis:6.0
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 10
ports:
- 6379:6379
env:
MONGO_DB_URL: mongodb://localhost:27017/talawa-test-db
REDIS_HOST: localhost
REDIS_PORT: 6379
LAST_RESORT_SUPERADMIN_EMAIL: "abc@gmail.com"
COLORIZE_LOGS: "true"
LOG_LEVEL: "info"

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

- name: Generate Secrets
run: |
echo "Generating secrets..."
{
echo "ACCESS_TOKEN_SECRET=$(openssl rand -hex 32)"
echo "REFRESH_TOKEN_SECRET=$(openssl rand -hex 32)"
echo "SERVER_PORT=4000"
} >> "$GITHUB_ENV"
- name: Check MongoDB and Redis Availability
run: |
echo "Checking MongoDB and Redis availability..."
for i in {1..10}; do
if nc -z localhost 27017 && nc -z localhost 6379; then
echo "MongoDB and Redis are available!"
break
fi
echo "Waiting for MongoDB and Redis..."
sleep 5
done
- name: Test Development Build
run: |
echo "Testing development build..."
NODE_ENV=development npm run dev &
DEV_PID=$!
sleep 10
if curl -f http://localhost:4000 | jq -e '. == {"talawa-version":"v1","status":"healthy"}' > /dev/null; then
echo "Development server is up and healthy"
else
echo "Development server is down"
kill $DEV_PID
exit 1
fi
- name: Check Apollo Server for development
run: |
echo "Checking Apollo server for development..."
if curl -f 'http://localhost:4000/graphql?query=%7B__typename%7D' \
-H 'Apollo-Require-Preflight: true' | jq -e '. == {"data":{"__typename":"Query"}}' > /dev/null; then
echo "Apollo server is up and healthy"
else
echo "Apollo server is down"
exit 1
fi
echo "Stopping development server..."
kill -9 $DEV_PID || echo "Process already stopped."
- name: Generate SSL Certificates for Production
run: |
echo "Generating SSL certificates for production..."
npm run generate:ssl-private-key
- name: Build Production Build
run: |
echo "Building production build..."
NODE_ENV=production npm run build
- name: Test Production Build
run: |
echo "Testing production build..."
lsof -ti:4000 | xargs -r kill -9 || echo "No process was using port 4000."
NODE_ENV=production SERVER_PORT=4001 npm run prod &
PROD_PID=$!
echo "Waiting for server to stabilize..."
sleep 10
if curl -f -k https://localhost:4001 | jq -e '. == {"talawa-version":"v1","status":"healthy"}' > /dev/null; then
echo "Production server is up and healthy."
else
echo "Production server is down. Cleaning up..."
kill -9 $PROD_PID || echo "Process already stopped."
exit 1
fi
- name: Check Apollo Server for Production
run: |
echo "Checking Apollo server for production..."
if curl -f -k 'https://localhost:4001/graphql?query=%7B__typename%7D' \
-H 'Apollo-Require-Preflight: true' | jq -e '. == {"data":{"__typename":"Query"}}' > /dev/null; then
echo "Apollo server is up and healthy"
else
echo "Apollo server is down"
exit 1
fi
echo "Stopping production server..."
kill -9 $PROD_PID || echo "Process already stopped."
- name: Clean Up Resources
if: always()
run: |
echo "Cleaning up background processes and temporary files..."
pkill -f "npm run dev" || echo "No dev process found."
pkill -f "npm run prod" || echo "No prod process found."
rm -f key.pem cert.pem
JSDocs:
if: ${{ github.actor != 'dependabot[bot]' }}
name: 'JSDocs comments and pipeline'
Expand Down
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v22.7.0
v22.12.0
Empty file added certs/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions fix-imports/fix-imports-graphql.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs/promises';
import path from 'path';

const directoryPath = './build';
const oldImport = `import { express as voyagerMiddleware } from "graphql-voyager/middleware";`;
const newImport = `import { express as voyagerMiddleware } from "graphql-voyager/middleware/index.js";`;

async function replaceImports(dir) {
try {
const files = await fs.readdir(dir);

for (const file of files) {
const filePath = path.join(dir, file);
const stat = await fs.stat(filePath);

if (stat.isDirectory()) {
await replaceImports(filePath);
} else if (path.extname(file) === '.js') {
let content = await fs.readFile(filePath, 'utf8');

if (content.includes(oldImport)) {
content = content.replace(oldImport, newImport);
await fs.writeFile(filePath, content, 'utf8');
console.log(`Updated graphql import in ${filePath}`);
}
}
}
} catch (error) {
console.error('Error:', error);
}
}

replaceImports(directoryPath);
44 changes: 44 additions & 0 deletions fix-imports/fix-imports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { readdirSync, statSync, readFileSync, writeFileSync, existsSync } from 'fs';
import { join, extname, dirname, resolve } from 'path';

const directory = './build'; // Adjust this if the build output folder changes

function fixImports(dir) {
readdirSync(dir).forEach((file) => {
const filePath = join(dir, file);

if (statSync(filePath).isDirectory()) {
fixImports(filePath);
} else if (filePath.endsWith('.js')) {
let content = readFileSync(filePath, 'utf-8');

// Fix missing extensions in import paths and add assert for JSON imports
content = content.replace(
/from\s+['"](\..*?)['"]/g,
(match, path) => {
const resolvedPath = resolve(dirname(filePath), path);

// Handle directories and missing extensions
if (existsSync(resolvedPath) && statSync(resolvedPath).isDirectory()) {
return `from '${path}/index.js'`;
} else if (!extname(path) && existsSync(`${resolvedPath}.js`)) {
return `from '${path}.js'`;
}

// Add assert for JSON files
if (path.endsWith('.json')) {
return `from '${path}' with { type: 'json' }`;
}

// If the file or directory doesn't exist, leave the import unchanged
return match;
}
);

writeFileSync(filePath, content, 'utf-8');
}
});
}

fixImports(directory);
console.log('Added index.js and .js extention to imports');
Loading

0 comments on commit 5c5b29a

Please sign in to comment.