Skip to content

Commit

Permalink
refactor env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dogfrogfog committed Nov 16, 2023
1 parent dcd7822 commit 7f7c93d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
10 changes: 5 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ SANITY_API_READ_TOKEN=
NEXT_PUBLIC_SANITY_PROJECT_ID=
NEXT_PUBLIC_SANITY_DATASET=

// roll-out feature
HOST=

REPO_TYPE=
TEAM_GITHUB_REPO_ID=
TEAM_GITHUB_REPO_PRODUCTION_BRANCH=
REPO_TYPE=
PROJECT_NAME=

SANITY_ORGANIZATION_ID=
VERCEL_FR_TEAM_ID=

SANITY_PERSONAL_AUTH_TOKEN=
VERCEL_PERSONAL_AUTH_TOKEN=

PROJECT_NAME=mvp-next-sanity
GITHUB_PERSONAL_ACCESS_TOKEN=
# token to access "/api/roll-out" | "/api/roll-out/deploy" endpoints
ROLL_OUT_API_TOKEN=
4 changes: 2 additions & 2 deletions .github/workflows/import-dataset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
-d '{
"type":"document",
"name":"Sanity Studio",
"url": "https://${{ inputs.vercel-project-name }}.vercel.app/api/sanity-deploy",
"url": "https://${{ inputs.vercel-project-name }}.vercel.app/api/roll-out/deploy",
"httpMethod":"POST",
"apiVersion":"v2021-03-25",
"includeDrafts":false,
Expand All @@ -109,7 +109,7 @@ jobs:
"on": ["create", "update", "delete"]
},
"headers": {
"Authorization": "Bearer ${{ secrets.VERCEL_PERSONAL_AUTH_TOKEN }}"
"Authorization": "Bearer ${{ secrets.ROLL_OUT_API_TOKEN }}"
}
}'
- name: Curl command to triger vercel initial deployment
Expand Down
57 changes: 32 additions & 25 deletions app/api/roll-out/route.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
import { createSanityProject, createVercelProject, triggerGithubWorkflow } from '@/lib/services';
import { headers } from 'next/headers';

import { isValidEmail } from '@/lib/email';

export async function POST(request: Request) {
if (headers().get('authorization') !== `Bearer ${process.env.ROLL_OUT_API_TOKEN}`) {
return new Response('Invalid roll-out token', { status: 401 });
}

const { email } = await request.json();
const username = email
.split('@')[0]
.toLowerCase()
.replace(/[^a-z0-9]/g, '') // prevent forbidden symbols
.slice(0, 90); // prevent project name from being too long

const sanityProjectId = await createSanityProject(username);
const sanityDatasetName = process.env.NEXT_PUBLIC_SANITY_DATASET || 'production';

if (sanityProjectId) {
const projectData = await createVercelProject({
projectNamePrifix: username,
sanityProjectId: sanityProjectId,
sanityDatasetName,
});

if (projectData) {
await triggerGithubWorkflow({
sanityProjectId,

if (email && isValidEmail(email)) {
const username = email
.split('@')[0]
.toLowerCase()
.replace(/[^a-z0-9]/g, '') // prevent forbidden symbols
.slice(0, 90); // prevent project name from being too long

const sanityProjectId = await createSanityProject(username);
const sanityDatasetName = process.env.NEXT_PUBLIC_SANITY_DATASET || 'production';

if (sanityProjectId) {
const projectData = await createVercelProject({
projectNamePrifix: username,
sanityProjectId: sanityProjectId,
sanityDatasetName,
vercelProjectId: projectData.projectId,
vercelProjectName: projectData.projectName,
vercelDeploymentUrl: projectData.deploymentUrl,
email,
});

return Response.json({ ok: true, status: 200, statusText: 'All steps were successful 🎉' });
if (projectData) {
await triggerGithubWorkflow({
sanityProjectId,
sanityDatasetName,
vercelProjectId: projectData.projectId,
vercelProjectName: projectData.projectName,
vercelDeploymentUrl: projectData.deploymentUrl,
email,
});

return Response.json({ ok: true, status: 200, statusText: 'All steps were successful 🎉' });
}
}

return Response.json({ status: '503', statusText: 'One of the steps was not successful😿' });
}

return Response.json({ status: '503', statusText: 'One of the steps was not successful😿' });
return Response.json({ status: '400', statusText: 'Email is not valid' });
}
5 changes: 5 additions & 0 deletions lib/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function isValidEmail(email: string) {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

return emailRegex.test(email);
}
6 changes: 3 additions & 3 deletions lib/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ export async function createVercelProject({
},
{
key: 'TEAM_GITHUB_REPO_ID',
value: '684968839',
value: process.env.TEAM_GITHUB_REPO_ID,
},
{
key: 'TEAM_GITHUB_REPO_PRODUCTION_BRANCH',
value: 'auto-roll-out',
value: process.env.TEAM_GITHUB_REPO_PRODUCTION_BRANCH,
},
{
key: 'REPO_TYPE',
value: 'github',
value: process.env.REPO_TYPE,
},
{
key: 'VERCEL_PERSONAL_AUTH_TOKEN',
Expand Down

0 comments on commit 7f7c93d

Please sign in to comment.