Skip to content

Commit

Permalink
merge dev to main (v2.0.2) (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored May 1, 2024
2 parents 3291c6e + bb775ef commit 96390fd
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 156 deletions.
95 changes: 60 additions & 35 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -1,67 +1,92 @@
name: Security - CodeQL
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: 'CodeQL'

on:
push:
branches: ['main', 'dev', 'release/*']
pull_request:
branches:
- main
- dev
- release/*
branches: ['main', 'dev', 'release/*']
schedule:
- cron: '0 0 * * 1'

permissions:
contents: read
- cron: '25 0 * * 6'

jobs:
analyze:
name: Analyze (${{ matrix.language }})
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners (GitHub.com only)
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
# required for all workflows
security-events: write

# required to fetch internal or private CodeQL packs
packages: read

# only required for workflows in private repositories
actions: read
contents: read
security-events: write
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: ['javascript', 'typescript']
# CodeQL supports [ $supported-codeql-languages ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

include:
- language: javascript-typescript
build-mode: none
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Harden Runner
uses: step-security/harden-runner@v2.6.1
with:
egress-policy: audit

- name: Checkout repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2.22.12
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2.22.12
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh
- if: matrix.build-mode == 'manual'
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2.22.12
uses: github/codeql-action/analyze@v3
with:
category: '/language:${{matrix.language}}'
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ The following diagram gives a high-level architecture overview of ZenStack.
- [SvelteKit](https://zenstack.dev/docs/reference/server-adapters/sveltekit)
- [Fastify](https://zenstack.dev/docs/reference/server-adapters/fastify)
- [ExpressJS](https://zenstack.dev/docs/reference/server-adapters/express)
- [NestJS](https://zenstack.dev/docs/reference/server-adapters/nestjs)
- 🙋🏻 [Request for an adapter](https://discord.gg/Ykhr738dUe)

### Prisma schema extensions
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "2.0.1",
"version": "2.0.2",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.zenstack"
version = "2.0.1"
version = "2.0.2"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetbrains",
"version": "2.0.1",
"version": "2.0.2",
"displayName": "ZenStack JetBrains IDE Plugin",
"description": "ZenStack JetBrains IDE plugin",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "2.0.1",
"version": "2.0.2",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "2.0.1",
"version": "2.0.2",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "2.0.1",
"version": "2.0.2",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "2.0.1",
"version": "2.0.2",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "2.0.1",
"version": "2.0.2",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "2.0.1",
"version": "2.0.2",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "2.0.1",
"version": "2.0.2",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "Build scalable web apps with minimum code by defining authorization and validation rules inside the data schema that closer to the database",
"version": "2.0.1",
"version": "2.0.2",
"author": {
"name": "ZenStack Team"
},
Expand Down
99 changes: 70 additions & 29 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getAttributeArg,
getAuthModel,
getDataModels,
getLiteral,
isDelegateModel,
type PluginOptions,
} from '@zenstackhq/sdk';
Expand All @@ -14,12 +15,14 @@ import {
ReferenceExpr,
isArrayExpr,
isDataModel,
isGeneratorDecl,
isReferenceExpr,
type Model,
} from '@zenstackhq/sdk/ast';
import { getDMMF, getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import { getDMMF, getPrismaClientImportSpec, getPrismaVersion, type DMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import {
FunctionDeclarationStructure,
InterfaceDeclaration,
Expand All @@ -42,6 +45,8 @@ import { generateAuthType } from './auth-type-generator';
// information of delegate models and their sub models
type DelegateInfo = [DataModel, DataModel[]][];

const LOGICAL_CLIENT_GENERATION_PATH = './.logical-prisma-client';

export class EnhancerGenerator {
constructor(
private readonly model: Model,
Expand All @@ -60,7 +65,7 @@ export class EnhancerGenerator {
// schema contains delegate models, need to generate a logical prisma schema
const result = await this.generateLogicalPrisma();

logicalPrismaClientDir = './.logical-prisma-client';
logicalPrismaClientDir = LOGICAL_CLIENT_GENERATION_PATH;
dmmf = result.dmmf;

// create a reexport of the logical prisma client
Expand Down Expand Up @@ -190,40 +195,76 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara

private async generateLogicalPrisma() {
const prismaGenerator = new PrismaSchemaGenerator(this.model);
const prismaClientOutDir = './.logical-prisma-client';
const logicalPrismaFile = path.join(this.outDir, 'logical.prisma');
await prismaGenerator.generate({
provider: '@internal', // doesn't matter
schemaPath: this.options.schemaPath,
output: logicalPrismaFile,
overrideClientGenerationPath: prismaClientOutDir,
mode: 'logical',
});

// generate the prisma client
const generateCmd = `prisma generate --schema "${logicalPrismaFile}" --no-engine`;
// dir of the zmodel file
const zmodelDir = path.dirname(this.options.schemaPath);

// generate a temp logical prisma schema in zmodel's dir
const logicalPrismaFile = path.join(zmodelDir, `logical-${Date.now()}.prisma`);

// calculate a relative output path to output the logical prisma client into enhancer's output dir
const prismaClientOutDir = path.join(path.relative(zmodelDir, this.outDir), LOGICAL_CLIENT_GENERATION_PATH);
try {
// run 'prisma generate'
await execPackage(generateCmd, { stdio: 'ignore' });
} catch {
await trackPrismaSchemaError(logicalPrismaFile);
await prismaGenerator.generate({
provider: '@internal', // doesn't matter
schemaPath: this.options.schemaPath,
output: logicalPrismaFile,
overrideClientGenerationPath: prismaClientOutDir,
mode: 'logical',
});

// generate the prisma client

// only run prisma client generator for the logical schema
const prismaClientGeneratorName = this.getPrismaClientGeneratorName(this.model);
let generateCmd = `prisma generate --schema "${logicalPrismaFile}" --generator=${prismaClientGeneratorName}`;

const prismaVersion = getPrismaVersion();
if (!prismaVersion || semver.gte(prismaVersion, '5.2.0')) {
// add --no-engine to reduce generation size if the prisma version supports
generateCmd += ' --no-engine';
}

try {
// run 'prisma generate' again with output to the console
await execPackage(generateCmd);
// run 'prisma generate'
await execPackage(generateCmd, { stdio: 'ignore' });
} catch {
// noop
await trackPrismaSchemaError(logicalPrismaFile);
try {
// run 'prisma generate' again with output to the console
await execPackage(generateCmd);
} catch {
// noop
}
throw new PluginError(name, `Failed to run "prisma generate" on logical schema: ${logicalPrismaFile}`);
}
throw new PluginError(name, `Failed to run "prisma generate" on logical schema: ${logicalPrismaFile}`);
}

// make a bunch of typing fixes to the generated prisma client
await this.processClientTypes(path.join(this.outDir, prismaClientOutDir));
// make a bunch of typing fixes to the generated prisma client
await this.processClientTypes(path.join(this.outDir, LOGICAL_CLIENT_GENERATION_PATH));

return {
prismaSchema: logicalPrismaFile,
// load the dmmf of the logical prisma schema
dmmf: await getDMMF({ datamodel: fs.readFileSync(logicalPrismaFile, { encoding: 'utf-8' }) }),
};
} finally {
if (fs.existsSync(logicalPrismaFile)) {
fs.rmSync(logicalPrismaFile);
}
}
}

return {
prismaSchema: logicalPrismaFile,
// load the dmmf of the logical prisma schema
dmmf: await getDMMF({ datamodel: fs.readFileSync(logicalPrismaFile, { encoding: 'utf-8' }) }),
};
private getPrismaClientGeneratorName(model: Model) {
for (const generator of model.declarations.filter(isGeneratorDecl)) {
if (
generator.fields.some(
(f) => f.name === 'provider' && getLiteral<string>(f.value) === 'prisma-client-js'
)
) {
return generator.name;
}
}
throw new PluginError(name, `Cannot find prisma-client-js generator in the schema`);
}

private async processClientTypes(prismaClientDir: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/plugins/enhancer/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginError, createProject, resolvePath, type PluginFunction } from '@zenstackhq/sdk';
import { PluginError, RUNTIME_PACKAGE, createProject, resolvePath, type PluginFunction } from '@zenstackhq/sdk';
import path from 'path';
import { getDefaultOutputFolder } from '../plugin-utils';
import { EnhancerGenerator } from './enhance';
Expand Down Expand Up @@ -31,7 +31,7 @@ const run: PluginFunction = async (model, options, _dmmf, globalOptions) => {
// resolve it relative to the schema path
prismaClientPath = path.relative(path.dirname(options.schemaPath), prismaClientPathAbs);
} else {
prismaClientPath = `.zenstack/models`;
prismaClientPath = `${RUNTIME_PACKAGE}/models`;
}
}

Expand Down
Loading

0 comments on commit 96390fd

Please sign in to comment.