Skip to content

Commit

Permalink
Move ndc rest schema to ndc-rest monorepo (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Oct 12, 2024
1 parent ebf060b commit aa7e580
Show file tree
Hide file tree
Showing 99 changed files with 30,692 additions and 234 deletions.
10 changes: 10 additions & 0 deletions .github/scripts/plugin-manifest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

export CLI_VERSION=$GITHUB_REF_NAME
export MACOS_AMD64_SHA256=$(sha256sum "_output/ndc-rest-schema-darwin-amd64" | awk '{ print $1 }')
export MACOS_ARM64_SHA256=$(sha256sum "_output/ndc-rest-schema-darwin-arm64" | awk '{ print $1 }')
export LINUX_AMD64_SHA256=$(sha256sum "_output/ndc-rest-schema-linux-amd64" | awk '{ print $1 }')
export LINUX_ARM64_SHA256=$(sha256sum "_output/ndc-rest-schema-linux-arm64" | awk '{ print $1 }')
export WINDOWS_AMD64_SHA256=$(sha256sum "_output/ndc-rest-schema-windows-amd64.exe" | awk '{ print $1 }')

envsubst < .github/scripts/plugin-manifest.yaml > release/manifest.yaml
41 changes: 41 additions & 0 deletions .github/scripts/plugin-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: ndc-rest
version: "${CLI_VERSION}"
shortDescription: "CLI plugin for Hasura REST data connector"
homepage: https://github.com/hasura/ndc-rest
hidden: true
platforms:
- selector: darwin-arm64
uri: "https://github.com/hasura/ndc-rest/releases/download/${CLI_VERSION}/ndc-rest-schema-darwin-arm64"
sha256: "${MACOS_ARM64_SHA256}"
bin: "ndc-rest-schema"
files:
- from: "./ndc-rest-schema-darwin-arm64"
to: "ndc-rest-schema"
- selector: linux-arm64
uri: "https://github.com/hasura/ndc-rest/releases/download/${CLI_VERSION}/ndc-rest-schema-linux-arm64"
sha256: "${LINUX_ARM64_SHA256}"
bin: "ndc-rest-schema"
files:
- from: "./ndc-rest-schema-linux-arm64"
to: "ndc-rest-schema"
- selector: darwin-amd64
uri: "https://github.com/hasura/ndc-rest/releases/download/${CLI_VERSION}/ndc-rest-schema-darwin-amd64"
sha256: "${MACOS_AMD64_SHA256}"
bin: "ndc-rest-schema"
files:
- from: "./ndc-rest-schema-darwin-amd64"
to: "ndc-rest-schema"
- selector: windows-amd64
uri: "https://github.com/hasura/ndc-rest/releases/download/${CLI_VERSION}/ndc-rest-schema-windows-amd64.exe"
sha256: "${WINDOWS_AMD64_SHA256}"
bin: "ndc-rest-schema.exe"
files:
- from: "./ndc-rest-schema-windows-amd64.exe"
to: "ndc-rest-schema.exe"
- selector: linux-amd64
uri: "https://github.com/hasura/ndc-rest/releases/download/${CLI_VERSION}/ndc-rest-schema-linux-amd64"
sha256: "${LINUX_AMD64_SHA256}"
bin: "ndc-rest-schema"
files:
- from: "./ndc-rest-schema-linux-amd64"
to: "ndc-rest-schema"
46 changes: 46 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Lint

on:
push:
paths:
- "**.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yaml"

env:
GO_VERSION: 1.23

jobs:
detect-modules:
runs-on: ubuntu-latest
outputs:
modules: ${{ steps.set-modules.outputs.modules }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- id: set-modules
run: echo "modules=$(go list -m -json | jq -s '.' | jq -c '[.[].Dir]')" >> $GITHUB_OUTPUT

golangci-lint:
needs: detect-modules
runs-on: ubuntu-latest
strategy:
matrix:
modules: ${{ fromJSON(needs.detect-modules.outputs.modules) }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Format
run: |
diff -u <(echo -n) <(gofmt -d -s .)
cd ndc-rest-schema && diff -u <(echo -n) <(gofmt -d -s .)
- name: golangci-lint ${{ matrix.modules }}
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=5m
working-directory: ${{ matrix.modules }}
33 changes: 26 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

release-image:
name: Release ndc-rest image
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
needs: [tests]
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -51,20 +51,39 @@ jobs:
tags: ${{ steps.docker-metadata.outputs.tags }}
labels: ${{ steps.docker-metadata.outputs.labels }}

- name: Build and push
uses: docker/build-push-action@v6
build-cli-and-manifests:
name: Build the CLI binaries and manifests
runs-on: ubuntu-latest
needs: [release-image]
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
push: true
file: Dockerfile.alpine
tags: ${{ steps.docker-metadata.outputs.tags }}-alpine
labels: ${{ steps.docker-metadata.outputs.labels }}
go-version: "1.23"
- name: Build the CLI
run: |
VERSION="$GITHUB_REF_NAME" make ci-build-cli
mkdir release
mv _output/* release
- name: Get version from tag
id: get-version
run: |
echo "tagged_version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
shell: bash

- name: Build connector definition
run: |
./scripts/build-manifest.sh
env:
VERSION: ${{ steps.get-version.outputs.tagged_version }}

- uses: actions/upload-artifact@v4
with:
path: release/*
if-no-files-found: error

- name: create a draft release
uses: ncipollo/release-action@v1
with:
Expand Down
26 changes: 5 additions & 21 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
name: Unit tests
name: Tests

on:
workflow_call:
pull_request:
push:
paths:
- "**.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yaml"

jobs:
test-go:
name: Run Go lint and unit tests
name: Run unit tests
runs-on: ubuntu-latest
permissions:
pull-requests: write
Expand All @@ -21,22 +15,12 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22"
- name: Format
run: diff -u <(echo -n) <(gofmt -d -s .)
- name: Vet
run: go vet ./...
- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
only-new-issues: true
skip-cache: true
go-version: ${{ env.GO_VERSION }}
- name: Run Go unit tests
run: |
go test -v -coverpkg=./... -race -timeout 3m -coverprofile=coverage.out.tmp ./...
cat coverage.out.tmp | grep -v "main.go" > coverage.out
- name: Run Go unit tests
- name: Run integration tests
run: |
./scripts/test.sh
- name: Go coverage format
Expand All @@ -57,7 +41,7 @@ jobs:
hide_complexity: true
indicators: true
output: both
thresholds: "50 70"
thresholds: "40 70"
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ github.event_name == 'pull_request' }}
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ tmp/
# vendor/

# Go workspace files
go.work
# go.work
go.work.sum

# Release directory
release/
release/
_output/
44 changes: 44 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
linters:
enable-all: true
disable:
- err113
- lll
- gocognit
- gocritic
- nestif
- execinquery
- exportloopref
- gomnd
- funlen
- godot
- gofumpt
- gomoddirectives
- depguard
- gosec
- revive
- cyclop
- wsl
- wrapcheck
- varnamelen
- nlreturn
- exhaustive
- exhaustruct
- gocyclo
- prealloc
- ireturn
- gochecknoglobals
- gocyclo
- godox
- stylecheck
- nilnil
- maintidx
- mnd
- tagliatelle
- goconst
- noctx
- unparam
- forbidigo

issues:
exclude-files:
- ".*_test\\.go$"
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# build context at repo root: docker build -f Dockerfile .
FROM golang:1.22 AS builder
FROM golang:1.23 AS builder

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -v -o ndc-cli .
RUN CGO_ENABLED=0 go build -v -o ndc-cli ./server

# stage 2: production image
FROM gcr.io/distroless/static-debian12:nonroot
Expand Down
24 changes: 0 additions & 24 deletions Dockerfile.alpine

This file was deleted.

37 changes: 36 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
VERSION ?= $(shell date +"%Y%m%d")
OUTPUT_DIR := _output

.PHONY: format
format:
gofmt -w -s .
cd ndc-rest-schema && gofmt -w -s .

.PHONY: test
test:
go test -v -race -timeout 3m ./...
cd ndc-rest-schema && go test -v -race -timeout 3m ./...

# Install golangci-lint tool to run lint locally
# https://golangci-lint.run/usage/install
.PHONY: lint
lint:
golangci-lint run
golangci-lint run --fix
cd ndc-rest-schema && golangci-lint run --fix

# clean the output directory
.PHONY: clean
clean:
rm -rf "$(OUTPUT_DIR)"

.PHONY: go-tidy
go-tidy:
go mod tidy
cd ndc-rest-schema && go mod tidy

.PHONY: build-jsonschema
build-jsonschema:
cd ./ndc-rest-schema/jsonschema && go run .

# build the ndc-rest-schema for all given platform/arch
.PHONY: build-cli
build-cli:
go build -o _output/ndc-rest-schema ./ndc-rest-schema

.PHONY: ci-build-cli
ci-build-cli: export CGO_ENABLED=0
ci-build-cli: clean
cd ./ndc-rest-schema && \
go get github.com/mitchellh/gox && \
go run github.com/mitchellh/gox -ldflags '-X github.com/hasura/ndc-rest/ndc-rest-schema/version.BuildVersion=$(VERSION) -s -w -extldflags "-static"' \
-osarch="linux/amd64 darwin/amd64 windows/amd64 darwin/arm64" \
-output="../$(OUTPUT_DIR)/ndc-rest-schema-{{.OS}}-{{.Arch}}" \
.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ go run . serve --configuration ./rest/testdata/jsonplaceholder

![REST connector](./assets/rest_connector.png)

REST connector uses the [NDC REST extension](https://github.com/hasura/ndc-rest-schema#ndc-rest-schema-extension) that includes request information.
REST connector uses the [NDC REST extension](https://github.com/hasura/ndc-rest/ndc-rest-schema#ndc-rest-schema-extension) that includes request information.
The connector has request context to transform the NDC request body to REST request and versa.

### Configuration
Expand All @@ -37,7 +37,7 @@ files:
spec: ndc
```
The config of each element follows the [config schema](https://github.com/hasura/ndc-rest-schema/blob/main/config.example.yaml) of `ndc-rest-schema`.
The config of each element follows the [config schema](https://github.com/hasura/ndc-rest/ndc-rest-schema/blob/main/config.example.yaml) of `ndc-rest-schema`.

> [!IMPORTANT]
> Conflicted object and scalar types will be ignored. Only the type of the first file is kept in the schema.
Expand All @@ -51,7 +51,7 @@ The config of each element follows the [config schema](https://github.com/hasura
The connector can convert OpenAPI to REST NDC schema in runtime. However, it's more flexible and performance-wise to pre-convert them, for example, change better function or procedure names.

```sh
go install github.com/hasura/ndc-rest-schema@latest
go install github.com/hasura/ndc-rest/ndc-rest-schema@latest
ndc-rest-schema convert -f ./rest/testdata/jsonplaceholder/swagger.json -o ./rest/testdata/jsonplaceholder/schema.json --spec openapi2
```

Expand Down Expand Up @@ -84,7 +84,7 @@ settings:

### Authentication

The current version supports API key and Auth token authentication schemes. The configuration is inspired from `securitySchemes` [with env variables](https://github.com/hasura/ndc-rest-schema#authentication)
The current version supports API key and Auth token authentication schemes. The configuration is inspired from `securitySchemes` [with env variables](https://github.com/hasura/ndc-rest/ndc-rest-schema#authentication)

See [this example](rest/testdata/auth/schema.yaml) for more context.

Expand Down
Loading

0 comments on commit aa7e580

Please sign in to comment.