diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml index fa92e0b..7e15b08 100644 --- a/.github/workflows/build-dev.yml +++ b/.github/workflows/build-dev.yml @@ -1,4 +1,4 @@ -name: Build xyzuan-api-v2 Development Build +name: Deploy xyzuan-api-v2 Development Build on: push: @@ -6,9 +6,52 @@ on: - dev jobs: - build: + test: + name: Run Treaty Tests runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Initialize Bun Environtment + uses: oven-sh/setup-bun@v2 + + - name: Declaring some Global Environtment vars + run: | + echo "PORT=3121" >> $GITHUB_ENV + echo "DOMAIN=localhost" >> $GITHUB_ENV + echo "NODE_ENV=dev" >> $GITHUB_ENV + echo "PASSWORD_PEPPER=${{ secrets.PASSWORD_PEPPER }}" >> $GITHUB_ENV + echo "DATABASE_URL=${{ secrets.DATABASE_URL_DEV }}" >> $GITHUB_ENV + echo "GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }}" >> $GITHUB_ENV + echo "GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "GITHUB_CLIENT_ID=${{ secrets.GH_CLIENT_ID }}" >> $GITHUB_ENV + echo "GITHUB_CLIENT_SECRET=${{ secrets.GH_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "LINKEDIN_CLIENT_ID=${{ secrets.LINKEDIN_CLIENT_ID }}" >> $GITHUB_ENV + echo "LINKEDIN_CLIENT_SECRET=${{ secrets.LINKEDIN_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "TELEGRAM_TOKEN=${{ secrets.TELEGRAM_TOKEN }}" >> $GITHUB_ENV + echo "TELEGRAM_CHAT_ID=${{ secrets.TELEGRAM_CHAT_ID }}" >> $GITHUB_ENV + echo "CLOUDINARY_CLOUD_NAME=${{ secrets.CLOUDINARY_CLOUD_NAME }}" >> $GITHUB_ENV + echo "CLOUDINARY_API_KEY=${{ secrets.CLOUDINARY_API_KEY }}" >> $GITHUB_ENV + echo "CLOUDINARY_API_SECRET=${{ secrets.CLOUDINARY_API_SECRET }}" >> $GITHUB_ENV + - name: Run Treaty Tests + id: treaty_test + run: | + bun install + bunx prisma generate + bun test + echo "::set-output name=result::success" + + - name: Mark as failed if tests fail + if: failure() + run: echo "::set-output name=result::failure" + + build: + name: Build & Push to GHCR Registry + runs-on: ubuntu-latest + needs: test + if: success() steps: - name: Checkout code uses: actions/checkout@v3 @@ -23,7 +66,6 @@ jobs: echo "ENV=dev" >> $GITHUB_ENV echo "PORT=3121" >> $GITHUB_ENV echo "BASE_URL=https://api.xyzuan.my.id/dev" >> $GITHUB_ENV - echo "CT_NAME=xyzuan-api-v2-dev" >> $GITHUB_ENV echo "DOMAIN=xyzuan.my.id" >> $GITHUB_ENV echo "REGISTRY=ghcr.io/xyzuan" >> $GITHUB_ENV echo "IMAGE_NAME=xyzuan_api_v2" >> $GITHUB_ENV @@ -68,18 +110,36 @@ jobs: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }} docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{env.SHA}} - - name: Serving the Images in Eden Server realms. + serve: + name: Serve Images in Server + runs-on: ubuntu-latest + needs: build + if: success() + steps: + - name: Authenticating to Eden Server realms + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.SERVER_PRIVATE_KEY }} + + - name: Declaring some Global Environtment vars + run: | + echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV + echo "PORT=3121" >> $GITHUB_ENV + echo "CT_NAME=xyzuan-api-v2-dev" >> $GITHUB_ENV + echo "REGISTRY=ghcr.io/xyzuan" >> $GITHUB_ENV + echo "IMAGE_NAME=xyzuan_api_v2" >> $GITHUB_ENV + + - name: Serve the Images run: | ssh-keyscan -t rsa ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts ssh ${{ github.actor }}@${{ secrets.SERVER_HOST }} << 'EOF' - echo 'Authenticating to GHCR realms...' echo "${{ secrets.GH_TOKEN }}" | docker login -u "${{ github.actor }}" ghcr.io --password-stdin echo 'Pulling the latest image from GHCR...' docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }} - echo 'Force Blowing the exist Docker Container...' + echo 'Force Blowing the existing Docker Container...' docker rm -f ${{ env.CT_NAME }} echo 'Running the Docker Container...' diff --git a/.github/workflows/build-prod.yml b/.github/workflows/build-prod.yml index 9ae968e..edd3e69 100644 --- a/.github/workflows/build-prod.yml +++ b/.github/workflows/build-prod.yml @@ -1,4 +1,4 @@ -name: Build xyzuan-api-v2 Production Build +name: Deploy xyzuan-api-v2 Production Build on: push: @@ -6,9 +6,52 @@ on: - main jobs: - build: + test: + name: Run Treaty Tests runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Initialize Bun Environtment + uses: oven-sh/setup-bun@v2 + + - name: Declaring some Global Environtment vars + run: | + echo "PORT=3121" >> $GITHUB_ENV + echo "DOMAIN=localhost" >> $GITHUB_ENV + echo "NODE_ENV=dev" >> $GITHUB_ENV + echo "PASSWORD_PEPPER=${{ secrets.PASSWORD_PEPPER }}" >> $GITHUB_ENV + echo "DATABASE_URL=${{ secrets.DATABASE_URL }}" >> $GITHUB_ENV + echo "GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }}" >> $GITHUB_ENV + echo "GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "GITHUB_CLIENT_ID=${{ secrets.GH_CLIENT_ID }}" >> $GITHUB_ENV + echo "GITHUB_CLIENT_SECRET=${{ secrets.GH_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "LINKEDIN_CLIENT_ID=${{ secrets.LINKEDIN_CLIENT_ID }}" >> $GITHUB_ENV + echo "LINKEDIN_CLIENT_SECRET=${{ secrets.LINKEDIN_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "TELEGRAM_TOKEN=${{ secrets.TELEGRAM_TOKEN }}" >> $GITHUB_ENV + echo "TELEGRAM_CHAT_ID=${{ secrets.TELEGRAM_CHAT_ID }}" >> $GITHUB_ENV + echo "CLOUDINARY_CLOUD_NAME=${{ secrets.CLOUDINARY_CLOUD_NAME }}" >> $GITHUB_ENV + echo "CLOUDINARY_API_KEY=${{ secrets.CLOUDINARY_API_KEY }}" >> $GITHUB_ENV + echo "CLOUDINARY_API_SECRET=${{ secrets.CLOUDINARY_API_SECRET }}" >> $GITHUB_ENV + + - name: Run Treaty Tests + id: treaty_test + run: | + bun install + bunx prisma generate + bun test + echo "::set-output name=result::success" + + - name: Mark as failed if tests fail + if: failure() + run: echo "::set-output name=result::failure" + build: + name: Build & Push to GHCR Registry + runs-on: ubuntu-latest + needs: test + if: success() steps: - name: Checkout code uses: actions/checkout@v3 @@ -23,7 +66,6 @@ jobs: echo "ENV=production" >> $GITHUB_ENV echo "PORT=3131" >> $GITHUB_ENV echo "BASE_URL=https://api.xyzuan.my.id" >> $GITHUB_ENV - echo "CT_NAME=xyzuan-api-v2" >> $GITHUB_ENV echo "DOMAIN=xyzuan.my.id" >> $GITHUB_ENV echo "REGISTRY=ghcr.io/xyzuan" >> $GITHUB_ENV echo "IMAGE_NAME=xyzuan_api_v2" >> $GITHUB_ENV @@ -68,18 +110,36 @@ jobs: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }} docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{env.SHA}} - - name: Serving the Images in Eden Server realms. + serve: + name: Serve Images in Server + runs-on: ubuntu-latest + needs: build + if: success() + steps: + - name: Authenticating to Eden Server realms + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.SERVER_PRIVATE_KEY }} + + - name: Declaring some Global Environtment vars + run: | + echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV + echo "PORT=3131" >> $GITHUB_ENV + echo "CT_NAME=xyzuan-api-v2" >> $GITHUB_ENV + echo "REGISTRY=ghcr.io/xyzuan" >> $GITHUB_ENV + echo "IMAGE_NAME=xyzuan_api_v2" >> $GITHUB_ENV + + - name: Serve the Images run: | ssh-keyscan -t rsa ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts ssh ${{ github.actor }}@${{ secrets.SERVER_HOST }} << 'EOF' - echo 'Authenticating to GHCR realms...' echo "${{ secrets.GH_TOKEN }}" | docker login -u "${{ github.actor }}" ghcr.io --password-stdin echo 'Pulling the latest image from GHCR...' docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }} - echo 'Force Blowing the exist Docker Container...' + echo 'Force Blowing the existing Docker Container...' docker rm -f ${{ env.CT_NAME }} echo 'Running the Docker Container...' diff --git a/.github/workflows/pr-test.yml b/.github/workflows/pr-test.yml new file mode 100644 index 0000000..051d6a3 --- /dev/null +++ b/.github/workflows/pr-test.yml @@ -0,0 +1,48 @@ +name: Test Pull Request Build + +on: + pull_request: + branches: + - main + +jobs: + test: + name: Run Treaty Tests + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Initialize Bun Environment + uses: oven-sh/setup-bun@v2 + + - name: Declaring some Global Environment vars + run: | + echo "PORT=3121" >> $GITHUB_ENV + echo "DOMAIN=localhost" >> $GITHUB_ENV + echo "NODE_ENV=dev" >> $GITHUB_ENV + echo "PASSWORD_PEPPER=${{ secrets.PASSWORD_PEPPER }}" >> $GITHUB_ENV + echo "DATABASE_URL=${{ secrets.DATABASE_URL_DEV }}" >> $GITHUB_ENV + echo "GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }}" >> $GITHUB_ENV + echo "GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "GITHUB_CLIENT_ID=${{ secrets.GH_CLIENT_ID }}" >> $GITHUB_ENV + echo "GITHUB_CLIENT_SECRET=${{ secrets.GH_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "LINKEDIN_CLIENT_ID=${{ secrets.LINKEDIN_CLIENT_ID }}" >> $GITHUB_ENV + echo "LINKEDIN_CLIENT_SECRET=${{ secrets.LINKEDIN_CLIENT_SECRET }}" >> $GITHUB_ENV + echo "TELEGRAM_TOKEN=${{ secrets.TELEGRAM_TOKEN }}" >> $GITHUB_ENV + echo "TELEGRAM_CHAT_ID=${{ secrets.TELEGRAM_CHAT_ID }}" >> $GITHUB_ENV + echo "CLOUDINARY_CLOUD_NAME=${{ secrets.CLOUDINARY_CLOUD_NAME }}" >> $GITHUB_ENV + echo "CLOUDINARY_API_KEY=${{ secrets.CLOUDINARY_API_KEY }}" >> $GITHUB_ENV + echo "CLOUDINARY_API_SECRET=${{ secrets.CLOUDINARY_API_SECRET }}" >> $GITHUB_ENV + + - name: Run Treaty Tests + id: treaty_test + run: | + bun install + bunx prisma generate + bun test + echo "::set-output name=result::success" + + - name: Mark as failed if tests fail + if: failure() + run: echo "::set-output name=result::failure" diff --git a/bun.lockb b/bun.lockb index 920e108..31c0358 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index d8a5c88..3d6693a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "dependencies": { "@bogeychan/elysia-logger": "^0.1.2", "@elysiajs/cors": "^1.1.1", + "@elysiajs/eden": "^1.1.3", "@elysiajs/swagger": "^1.1.1", "@lucia-auth/adapter-prisma": "^4.0.1", "@prisma/client": "5.19.1", diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 21cec97..276502d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,7 +7,7 @@ generator client { provider = "prisma-client-js" engineType = "binary" - binaryTargets = ["debian-openssl-3.0.x"] + binaryTargets = ["debian-openssl-3.0.x", "debian-openssl-1.1.x"] } datasource db { diff --git a/src/index.ts b/src/index.ts index a5b2591..1e60bd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import cors from "@elysiajs/cors"; import { docs } from "@libs/swagger"; import apiRoutes from "./api"; -const api = baseElysia() +export const api = baseElysia() .use( cors({ origin: ["xyzuan.my.id", "localhost:3000", "localhost"], diff --git a/test/blog.test.ts b/test/blog.test.ts new file mode 100644 index 0000000..09e2874 --- /dev/null +++ b/test/blog.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "bun:test"; +import { api } from "../src"; + +describe("Blog Modules", () => { + it("Return a all blog list", async () => { + const response = await api + .handle(new Request("http://localhost:3121/v2/blog")) + .then(async (res) => await res.json()); + + expect(response).toHaveProperty("data"); + expect(Array.isArray(response.data)).toBe(true); + + const blog = response.data[0]; + expect(blog).toHaveProperty("id"); + expect(blog).toHaveProperty("title"); + expect(blog).toHaveProperty("content"); + expect(blog).toHaveProperty("commentsCount"); + expect(blog).toHaveProperty("reactionsCount"); + + expect(blog.commentsCount).toBeGreaterThanOrEqual(0); + expect(blog.reactionsCount).toBeGreaterThanOrEqual(0); + }); +});