Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-commit demo #148

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "monthly"
versioning-strategy: increase
Expand Down
27 changes: 19 additions & 8 deletions .github/workflows/authzed-node.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
name: authzed-node-ci
on:

Check warning on line 3 in .github/workflows/authzed-node.yaml

View workflow job for this annotation

GitHub Actions / Lint

3:1 [truthy] truthy value should be one of [false, true]
push:
branches:
- "main"
Expand Down Expand Up @@ -29,14 +30,14 @@
- js-dist/package.json
- buf.gen.yaml
test:
name: Lint and Test
name: Run Tests
runs-on: "buildjet-2vcpu-ubuntu-2204"
strategy:
matrix:
node-version: [18, 20, 21]
needs: "paths-filter"
if: |
needs.paths-filter.outputs.codechange == 'true' || github.event_name == 'release'

Check failure on line 40 in .github/workflows/authzed-node.yaml

View workflow job for this annotation

GitHub Actions / Lint

40:81 [line-length] line too long (87 > 80 characters)
steps:
- uses: actions/checkout@v4
- uses: "authzed/action-spicedb@v1"
Expand All @@ -45,16 +46,26 @@
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- uses: bahmutov/npm-install@v1
with:
install-command: yarn --frozen-lockfile --silent
working-directory: ./
- name: Run lint
run: CI=true yarn lint
working-directory: ./
cache: "yarn"
- name: "Yarn install"
run: yarn install --frozen-lockfile
- name: Run Yarn tests
run: CI=true yarn only-run-tests
working-directory: ./
lint:
name: Lint
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
- uses: "actions/setup-python@v5"
# NOTE: this is needed for installing and then running tsc,
# which is invoked locally by pre-commit
- uses: "actions/setup-node@v4"
with:
cache: "yarn"
- name: "Yarn install"
run: yarn install --frozen-lockfile
- uses: "pre-commit/[email protected]"
publish-npm:
name: Publish to NPM
needs:
Expand Down Expand Up @@ -85,7 +96,7 @@
node-version: [18, 20, 21]
needs: "paths-filter"
if: |
needs.paths-filter.outputs.codechange == 'true' || github.event_name == 'release'

Check failure on line 99 in .github/workflows/authzed-node.yaml

View workflow job for this annotation

GitHub Actions / Lint

99:81 [line-length] line too long (87 > 80 characters)
steps:
- uses: actions/checkout@v4
- uses: "authzed/action-spicedb@v1"
Expand Down
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
repos:
- repo: "https://github.com/adrienverge/yamllint"
rev: "v1.35.1"
hooks:
- id: "yamllint"
- repo: "https://github.com/biomejs/pre-commit"
rev: "v0.4.0"
hooks:
- id: "biome-check"
additional_dependencies: ["@biomejs/[email protected]"]
- repo: "local"
hooks:
- id: "typescript-check"
name: "Check types"
entry: "yarn run tsc --noEmit"
language: "system"
types_or: ["ts"]
# We do this to ensure that type changes in the modified
# files don't change things elsewhere.
pass_filenames: false
22 changes: 22 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": ["src/authzedapi", "dist"]
},
"linter": {
"rules": {
"complexity": {
"noForEach": "off"
}
}
},
"formatter": {
"indentWidth": 2,
"indentStyle": "space"
},
"json": {
"formatter": {
"enabled": false
}
}
}
18 changes: 13 additions & 5 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#!/usr/bin/env -S buf generate buf.build/authzed/api:v1.35.0 --template
#!/usr/bin/env -S buf generate
# To regenerate:
# npm install -g grpc-tools
# ./buf.gen.yaml
version: "v1beta1"
---
version: "v2"
inputs:
- module: "buf.build/authzed/api:v1.35.0"
plugins:
- name: "ts"
- local: "./node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts"
out: "src/authzedapi"
opt: generate_dependencies,long_type_string,client_grpc1
path: ./node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts
# protobuf-ts expects this to be set to "all"
# https://github.com/timostamm/protobuf-ts/issues/165#issuecomment-931492639
strategy: "all"
opt:
- "generate_dependencies"
- "long_type_string"
- "client_grpc1"
40 changes: 0 additions & 40 deletions eslint.config.mjs

This file was deleted.

34 changes: 17 additions & 17 deletions examples/v1/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v1 } from '@authzed/authzed-node';
import { v1 } from "@authzed/authzed-node";
// set up it on localhost like this:
// const client = v1.NewClient('mytokenhere', 'localhost:50051', 1);
const client = v1.NewClient('mytokenhere');
const client = v1.NewClient("mytokenhere");

const writeRequest = v1.WriteSchemaRequest.create({
schema: `definition test/user {}
Expand All @@ -15,7 +15,7 @@ const writeRequest = v1.WriteSchemaRequest.create({

// Write a schema.
await new Promise((resolve, reject) => {
client.writeSchema(writeRequest, function (err, response) {
client.writeSchema(writeRequest, (err, response) => {
if (err) reject(err);
resolve(response);
});
Expand All @@ -27,14 +27,14 @@ const writeRelationshipRequest = v1.WriteRelationshipsRequest.create({
v1.RelationshipUpdate.create({
relationship: v1.Relationship.create({
resource: v1.ObjectReference.create({
objectType: 'test/document',
objectId: 'somedocument',
objectType: "test/document",
objectId: "somedocument",
}),
relation: 'viewer',
relation: "viewer",
subject: v1.SubjectReference.create({
object: v1.ObjectReference.create({
objectType: 'test/user',
objectId: 'fred',
objectType: "test/user",
objectId: "fred",
}),
}),
}),
Expand All @@ -44,7 +44,7 @@ const writeRelationshipRequest = v1.WriteRelationshipsRequest.create({
});

await new Promise((resolve, reject) => {
client.writeRelationships(writeRelationshipRequest, function (err, response) {
client.writeRelationships(writeRelationshipRequest, (err, response) => {
if (err) reject(err);
resolve(response);
});
Expand All @@ -53,32 +53,32 @@ await new Promise((resolve, reject) => {
// Check a permission.
const checkPermissionRequest = v1.CheckPermissionRequest.create({
resource: v1.ObjectReference.create({
objectType: 'test/document',
objectId: 'somedocument',
objectType: "test/document",
objectId: "somedocument",
}),
permission: 'view',
permission: "view",
subject: v1.SubjectReference.create({
object: v1.ObjectReference.create({
objectType: 'test/user',
objectId: 'fred',
objectType: "test/user",
objectId: "fred",
}),
}),
consistency: v1.Consistency.create({
requirement: {
oneofKind: 'fullyConsistent',
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
}),
});

const checkResult = await new Promise((resolve, reject) => {
client.checkPermission(checkPermissionRequest, function (err, response) {
client.checkPermission(checkPermissionRequest, (err, response) => {
if (err) reject(err);
resolve(response);
});
});

console.log(
checkResult.permissionship ===
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION,
);
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"test": "./scripts/run-and-test.sh",
"only-run-tests": "ts-node node_modules/jasmine/bin/jasmine --config=jasmine.json",
"buf": "./buf.gen.yaml",
"lint": "./node_modules/.bin/eslint src",
"lint": "biome check .",
"fix": "biome check --write .",
"build": "tsc",
"prepublish": "yarn build",
"build-js-client": "tsc --target es2018 --declaration false --outDir js-dist"
Expand All @@ -33,14 +34,10 @@
"google-protobuf": "^3.21.4"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.9.0",
"@biomejs/biome": "1.8.3",
"@protobuf-ts/plugin": "^2.9.4",
"@types/jasmine": "^5.1.4",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.1.0",
"eslint": "^9.9.0",
"grpc-tools": "^1.11.2",
"jasmine": "^5.2.0",
"ts-node": "^10.9.2",
Expand Down
8 changes: 4 additions & 4 deletions src/__utils__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Generates a random token with a prefix to support
* unique, idempotent local testing.
* @param prefix
* @returns
* @param prefix
* @returns
*/
export function generateTestToken(prefix: string): string {
return `${prefix}-${Date.now().toString()}`
}
return `${prefix}-${Date.now().toString()}`;
}
22 changes: 14 additions & 8 deletions src/full-promises.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { generateTestToken } from "./__utils__/helpers";
import { ClientSecurity } from "./util";
import * as v1 from "./v1";
import { Consistency } from "./v1";
import { generateTestToken } from './__utils__/helpers'

describe("a check following a write of schema and relationships", () => {
it("should succeed", async () => {
// Write the schema.
const token = generateTestToken('full-promises')
const { promises: v1client } = v1.NewClient(token, "localhost:50051", ClientSecurity.INSECURE_LOCALHOST_ALLOWED);
const token = generateTestToken("full-promises");
Dismissed Show dismissed Hide dismissed
const { promises: v1client } = v1.NewClient(
token,
"localhost:50051",
ClientSecurity.INSECURE_LOCALHOST_ALLOWED,
);

const writeSchemaRequest = v1.WriteSchemaRequest.create({
schema: `
Expand All @@ -20,7 +24,7 @@ describe("a check following a write of schema and relationships", () => {
`,
});

await v1client.writeSchema(writeSchemaRequest)
await v1client.writeSchema(writeSchemaRequest);
// Create the relationship between the resource and the user.
const resource = v1.ObjectReference.create({
objectType: "test/resource",
Expand Down Expand Up @@ -53,7 +57,7 @@ describe("a check following a write of schema and relationships", () => {
updates: [update],
});

const response = await v1client.writeRelationships(writeRequest)
const response = await v1client.writeRelationships(writeRequest);
expect(response).toBeTruthy();

const checkPermissionRequest = v1.CheckPermissionRequest.create({
Expand All @@ -65,12 +69,14 @@ describe("a check following a write of schema and relationships", () => {
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
})
}),
});

const permissionResponse = await v1client.checkPermission(checkPermissionRequest)
const permissionResponse = await v1client.checkPermission(
checkPermissionRequest,
);
expect(permissionResponse?.permissionship).toBe(
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION,
);
});
});
Loading
Loading