Skip to content

chore: refresh examples + docs + add security scanning #71

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

Merged
merged 24 commits into from
Jun 2, 2023
Merged
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
33 changes: 33 additions & 0 deletions .github/workflows/ci-aqua-security-trivy-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: ci-aqua-security-trivy-tests
on:
push:
branches:
- master
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
branches:
- master
schedule:
- cron: "0 * * * *"
jobs:
build:
name: trivy-tests
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
45 changes: 33 additions & 12 deletions .github/workflows/ci-dgraph-js-http.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

name: ci-dgraph-js-http-tests

on:
push:
branches:
Expand All @@ -13,27 +12,49 @@ on:
- ready_for_review
branches:
- master

jobs:
build:

runs-on: ubuntu-20.04

strategy:
fail-fast: false
matrix:
node-version: [14.x, 16.x, 18.x, 19.x]
node-version: [16.x, 18.x, 19.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
- name: Checkout dgraph-js-http repo
uses: actions/checkout@v3
with:
path: dgraph-js-http
repository: dgraph-io/dgraph-js-http
ref: ${{ github.ref }}
- name: Checkout dgraph repo
uses: actions/checkout@v3
with:
path: dgraph
repository: dgraph-io/dgraph
ref: main
- name: Get Go Version
run: |
#!/bin/bash
cd dgraph
GOVERSION=$({ [ -f .go-version ] && cat .go-version; })
echo "GOVERSION=$GOVERSION" >> $GITHUB_ENV
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GOVERSION }}
- name: Build dgraph binary
run: cd dgraph && make docker-image # also builds dgraph binary
- name: Move dgraph binary to gopath
run: cd dgraph && mv dgraph/dgraph ~/go/bin/dgraph
- name: Setup node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install Dependencies
run: bash ./scripts/install_dgraph.sh
- name: run tests
cache-dependency-path: dgraph-js-http/package-lock.json
- name: Run dgraph-js-http tests
working-directory: dgraph-js-http
run: |
npm ci --legacy-peer-deps
bash ./scripts/build.sh
bash ./scripts/run-tests.sh
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ coverage/
Thumbs.db

# Dgraph

data/**
dgraph-local-data/

# npm test cruft
t/
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ and understand how to run and work with Dgraph.

[docs.dgraph.io]: https://docs.dgraph.io

**Use [Discuss Issues](https://discuss.dgraph.io/c/issues/35) for reporting issues about this repository.**

## Table of contents

- [Install](#install)
Expand Down Expand Up @@ -47,7 +45,7 @@ yarn add dgraph-js-http
or npm:

```sh
npm install dgraph-js-http --save
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasn't been needed in modern versions of npm for a long time, see https://stackoverflow.com/questions/19578796/what-is-the-save-option-for-npm-install

npm install dgraph-js-http
```

You will also need a Promise polyfill for
Expand All @@ -62,9 +60,9 @@ use a different version of this client.

| Dgraph version | dgraph-js-http version |
| :------------: | :--------------------: |
| >= 21.03.0 | >= _21.3.0_ |
| >= 20.03.0 | >= _20.3.0_ |
| >= 1.1 | >= _1.1.0_ |
| 21.3.X | *21.3.0* |
| 22.0.X | *21.3.0* |
| 23.0.X | *23.0.0* |

## Quickstart

Expand Down Expand Up @@ -418,8 +416,8 @@ npm run build

### Running tests

Make sure you have a Dgraph server running on localhost before you run this task.
The script `run-tests.sh` spins up a local cluster and runs the npm tests.

```sh
npm test
bash scripts/run-tests.sh
```
22 changes: 13 additions & 9 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,46 @@ client for Dgraph.

## Running

### Start Dgraph server
### Start dgraph alpha

You will need to install [Dgraph v1.0.6 or above][releases] and run it.
You will need to install [Dgraph v21.3.2 or above][releases] and run it.

[releases]: https://github.com/dgraph-io/dgraph/releases

You can run the commands below to start a clean Dgraph server every time, for
testing and exploration.

First, create two separate directories for `dgraph zero` and `dgraph server`.
First, create two separate directories for `dgraph zero` and `dgraph alpha`.

```sh
mkdir -p dgraphdata/zero dgraphdata/data
mkdir -p local-dgraph-data/zero local-dgraph-data/data
```

Then start `dgraph zero`:

```sh
cd dgraphdata/zero
cd local-dgraph-data/zero
rm -r zw; dgraph zero
```

Finally, start the `dgraph server`:
Finally, start the `dgraph alpha`:

```sh
cd dgraphdata/data
rm -r p w; dgraph server --lru_mb=1024 --zero localhost:5080
cd local-dgraph-data/data
rm -r p w; dgraph alpha --zero localhost:5080 -o 100
```

Notice that in the command above, we shifted the ports by 100 from the default ports of 7080 for
internal traffic, 8080 for http, and 9080 for GRPC, which means the alpha server is binding to
the port 7180 for internal traffic, 8180 for http, and 9180 for GRPC.

For more configuration options, and other details, refer to
[docs.dgraph.io](https://docs.dgraph.io)

## Install dependencies

```sh
yarn install
npm install
```

## Run the sample code
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple",
"dependencies": {
"dgraph-js-http": "^0.1.0"
"dgraph-js-http": "^21.3.1"
}
}
20 changes: 0 additions & 20 deletions scripts/build.sh

This file was deleted.

41 changes: 0 additions & 41 deletions scripts/functions.sh

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/install_dgraph.sh

This file was deleted.

99 changes: 99 additions & 0 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

sleepTime=5

function wait-for-healthy() {
printf 'wait-for-healthy: waiting for %s to return 200 OK\n' "$1"
tries=0
until curl -sL -w "%{http_code}\\n" "$1" -o /dev/null | grep -q 200; do
tries=$tries+1
if [[ $tries -gt 300 ]]; then
printf "wait-for-healthy: Took longer than 1 minute to be healthy.\n"
printf "wait-for-healthy: Waiting stopped.\n"
return 1
fi
sleep 0.2
done
printf "wait-for-healthy: done.\n"
}

function errorCheck {
EXIT_CODE=$1
ERROR_MESSAGE=$2

if [[ EXIT_CODE -ne 0 ]]; then
echo $ERROR_MESSAGE
stopCluster
exit $EXIT_CODE
fi
return 0
}

function stopCluster {
echo "shutting down dgraph alpha and zero..."
kill -9 $(pgrep -f "dgraph zero") > /dev/null # kill dgraph zero
kill -9 $(pgrep -f "dgraph alpha") > /dev/null # kill dgraph alpha

if pgrep -x dgraph > /dev/null
then
echo "sleeping for 5 seconds so dgraph can shutdown"
sleep 5
fi

echo "cluster teardown complete"
return 0
}

function startAlpha {
echo -e "starting dgraph alpha..."
head -c 1024 /dev/random > $SRCDIR/dgraph-local-data/acl-secret.txt
dgraph alpha -p $SRCDIR/dgraph-local-data/p \
-w $SRCDIR/dgraph-local-data/w \
--bindall \
--my localhost:7080 \
--acl "access-ttl=1h; refresh-ttl=1d; secret-file=$SRCDIR/dgraph-local-data/acl-secret.txt" \
> $SRCDIR/dgraph-local-data/alpha.log 2>&1 &

# wait for alpha to be healthy
ALPHA_HTTP_ADDR="localhost:8080"
wait-for-healthy $ALPHA_HTTP_ADDR/health
errorCheck $? "dgraph alpha could not come up"
sleep $sleepTime
return 0
}

function startZero {
echo -e "starting dgraph zero..."
dgraph zero --my localhost:5080 --bindall \
-w $SRCDIR/dgraph-local-data/wz > $SRCDIR/dgraph-local-data/zero.log 2>&1 &

# wait for zero to be healthy
ZERO_HTTP_ADDR="localhost:6080"
wait-for-healthy $ZERO_HTTP_ADDR/health
errorCheck $? "dgraph zero could not come up"
sleep $sleepTime
}

function init {
echo -e "initializing..."
rm -rf $SRCDIR/dgraph-local-data
mkdir $SRCDIR/dgraph-local-data
}

# find parent directory of test script
readonly _SRCDIR=$(readlink -f ${BASH_SOURCE[0]%/*})
SRCDIR=$(dirname $_SRCDIR)

init
startZero
startAlpha
sleep 10 # need time to create Groot user

npm run build

npm test
errorCheck $? "dgraph-js-http client tests FAILED"

stopCluster
rm -rf $SRCDIR/local-dgraph-data
exit 0
2 changes: 1 addition & 1 deletion tests/integration/versionDetect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("clientStub version detection", () => {
legacyApi: USE_LEGACY_API,
});
return expect(stub.detectApiVersion()).resolves.toMatch(
/^v[0-9]+(.[0-9]+){2}.*/,
/^v[0-9]+(.[0-9]+){2}.*|^[a-z0-9]*$/,
);
});
});