Skip to content

Commit

Permalink
Initial commit! 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozzey committed Oct 6, 2022
0 parents commit 58cab42
Show file tree
Hide file tree
Showing 45 changed files with 11,384 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Airbrake config
AIRBRAKE_HOST=https://my-errbit-instance.com
AIRBRAKE_KEY=longvaluefullofnumbersandlettersinlowercase
ENVIRONMENT=dev

# Database config
POSTGRES_USER=myuser
POSTGRES_PASSWORD=password12345
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_DB=sroc_charge
POSTGRES_DB_TEST=sroc_charge_test

# Server config
PORT=3000

# Test config
LOG_IN_TEST=false
16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# See the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
versioning-strategy: lockfile-only
- package-ecosystem: "github-actions"
# Workflow files stored in the default location of `.github/workflows`
directory: "/"
schedule:
interval: "daily"
106 changes: 106 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: CI

on: push

jobs:
build:
# You must use a Linux environment when using service containers or container jobs
runs-on: ubuntu-latest
env:
ADMIN_CLIENT_ID: shortvaluefullofnumbersandlettersinlowercase
AIRBRAKE_HOST: https://my-errbit-instance.com
AIRBRAKE_KEY: longvaluefullofnumbersandlettersinlowercase
PORT: 3000
# These need to be duplicated in services section for postgres. Unfortunately, there is not a way to reuse them
POSTGRES_USER: water_user
POSTGRES_PASSWORD: password
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_DB: wabs_test
POSTGRES_DB_TEST: wabs_test
ENVIRONMENT: dev

# Service containers to run with `runner-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres:12-alpine
# Provide the password for postgres
env:
POSTGRES_USER: water_user
POSTGRES_PASSWORD: password
POSTGRES_DB: wabs_test
# Maps tcp port 5432 on service container to the host
ports:
- 5432:5432
# Set health checks to wait until postgres has started. You must have this so the runner knows to wait till
# postgres is up and running before proceeding
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of sonarcloud analysis

# Before we do anything, check we haven't accidentally left any `describe.only()` or `it.only(` statements in the
# tests
#
# Reworking of https://stackoverflow.com/a/21788642/6117745
- name: Temporary tag check
run: |
! grep -R 'describe.only(\|it.only(' test
# Our projects use .nvmrc files to specify the node version to use. We can read and then output it as the result
# this step. Subsequent steps can then access the value
- name: Read Node version
run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
# Give the step an ID to make it easier to refer to
id: nvm

# Gets the version to use by referring to the previous step
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: "${{ steps.nvm.outputs.NVMRC }}"

# Speeds up workflows by reading the node modules from cache. Obviously you need to run it at least once, and the
# cache will be updated should the package-lock.json file change
- name: Cache Node modules
uses: actions/cache@v3
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see https://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci

# Run linting first. No point running the tests if there is a linting issue
- name: Run lint check
run: |
npm run lint
# This includes an extra run step. The sonarcloud analysis will be run in a docker container with the current
# folder mounted as `/github/workspace`. The problem is when the lcov.info file is generated it will reference the
# code in the current folder. So to enable sonarcloud to matchup code coverage with the files we use sed to update
# the references in lcov.info
# https://community.sonarsource.com/t/code-coverage-doesnt-work-with-github-action/16747/6
- name: Run unit tests
run: |
npm test
sed -i 's/\/home\/runner\/work\/water-abstraction-system\/water-abstraction-system\//\/github\/workspace\//g' coverage/lcov.info
- name: Analyze with SonarCloud
if: github.actor != 'dependabot[bot]'
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This is provided automatically by GitHub
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # This needs to be set in your repo; settings -> secrets
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.

# IDE's and text editors #
# Ruby mine
/.idea
# Symbol tags (e.g. generated by Atom Editor)
/tags

# Logs, temp and OS specific files
/log/*
!/log/.keep
/tmp/*
!/tmp/.keep
*~
.DS_Store
.svn
.*.swp
___*
pkg/
chromedriver.log
/.byebug_history
*.log

# Dependency directories
node_modules/

# Dotenv https://github.com/motdotla/dotenv
# Ignore the .env file as this may contain actual credentials. The .env.example
# file gets committed which acts as documentation on what environment
# variables will need to be set up
.env
.env.local
.env_docker

# Unit test coverage output. We keep references to the individual files in
# case any devs still have ones generated before the switch to outputting to
# coverage/
coverage/
coverage.html
lcov.info
20 changes: 20 additions & 0 deletions .labrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

module.exports = {
verbose: true,
coverage: true,
// lcov reporter required for SonarCloud
reporter: ['console', 'html', 'lcov'],
output: ['stdout', 'coverage/coverage.html', 'coverage/lcov.info'],
// @aws-sdk/s3 exposes global variables which cause errors during test if we don't ignore them. lab expects the list
// of globals to ignore to be a single comma-delimited string; for ease of management we define them in an array then
// join them.
globals: [
'__extends', '__assign', '__rest', '__decorate', '__param', '__metadata', '__awaiter', '__generator',
'__exportStar', '__createBinding', '__values', '__read', '__spread', '__spreadArrays', '__spreadArray', '__await',
'__asyncGenerator', '__asyncDelegator', '__asyncValues', '__makeTemplateObject', '__importStar', '__importDefault',
'__classPrivateFieldGet', '__classPrivateFieldSet',
// We also ignore globals exposed by global-agent:
'GLOBAL_AGENT','ROARR'
].join(',')
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
74 changes: 74 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
The Open Government Licence (OGL) Version 3

Copyright (c) 2022 Defra

You are encouraged to use and re-use the Information that is available under this licence freely and flexibly, with only a few conditions.

Using Information under this licence

Use of copyright and database right material expressly made available under this licence (the 'Information') indicates your acceptance of the terms and conditions below.

The Licensor grants you a worldwide, royalty-free, perpetual, non-exclusive licence to use the Information subject to the conditions below.

This licence does not affect your freedom under fair dealing or fair use or any other copyright or database right exceptions and limitations.

You are free to:

copy, publish, distribute and transmit the Information;
adapt the Information;
exploit the Information commercially and non-commercially for example, by combining it with other Information, or by including it in your own product or application.
You must (where you do any of the above):

acknowledge the source of the Information in your product or application by including or linking to any attribution statement specified by the Information Provider(s) and, where possible, provide a link to this licence;
If the Information Provider does not provide a specific attribution statement, you must use the following:
Contains public sector information licensed under the Open Government Licence v3.0.

If you are using Information from several Information Providers and listing multiple attributions is not practical in your product or application, you may include a URI or hyperlink to a resource that contains the required attribution statements.

These are important conditions of this licence and if you fail to comply with them the rights granted to you under this licence, or any similar licence granted by the Licensor, will end automatically.

Exemptions

This licence does not cover:

personal data in the Information;
Information that has not been accessed by way of publication or disclosure under information access legislation (including the Freedom of Information Acts for the UK and Scotland) by or with the consent of the Information Provider;
departmental or public sector organisation logos, crests and the Royal Arms except where they form an integral part of a document or dataset;
military insignia;
third party rights the Information Provider is not authorised to license;
other intellectual property rights, including patents, trade marks, and design rights; and
identity documents such as the British Passport
Non-endorsement

This licence does not grant you any right to use the Information in a way that suggests any official status or that the Information Provider and/or Licensor endorse you or your use of the Information.

No warranty

The Information is licensed 'as is' and the Information Provider and/or Licensor excludes all representations, warranties, obligations and liabilities in relation to the Information to the maximum extent permitted by law.

The Information Provider and/or Licensor are not liable for any errors or omissions in the Information and shall not be liable for any loss, injury or damage of any kind caused by its use. The Information Provider does not guarantee the continued supply of the Information.

Governing Law

This licence is governed by the laws of the jurisdiction in which the Information Provider has its principal place of business, unless otherwise specified by the Information Provider.

Definitions

In this licence, the terms below have the following meanings:

'Information' means information protected by copyright or by database right (for example, literary and artistic works, content, data and source code) offered for use under the terms of this licence.
'Information Provider' means the person or organisation providing the Information under this licence.
'Licensor' means any Information Provider which has the authority to offer Information under the terms of this licence or the Keeper of Public Records, who has the authority to offer Information subject to Crown copyright and Crown database rights and Information subject to copyright and database right that has been assigned to or acquired by the Crown, under the terms of this licence.
'Use' means doing any act which is restricted by copyright or database right, whether in the original medium or in any other medium, and includes without limitation distributing, copying, adapting, modifying as may be technically necessary to use it in a different mode or format.
'You', 'you' and 'your' means the natural or legal person, or body of persons corporate or incorporate, acquiring rights in the Information (whether the Information is obtained directly from the Licensor or otherwise) under this licence.
About the Open Government Licence

The National Archives has developed this licence as a tool to enable Information Providers in the public sector to license the use and re-use of their Information under a common open licence. The National Archives invites public sector bodies owning their own copyright and database rights to permit the use of their Information under this licence.

The Keeper of the Public Records has authority to license Information subject to copyright and database right owned by the Crown. The extent of the offer to license this Information under the terms of this licence is set out in the UK Government Licensing Framework.

This is version 3.0 of the Open Government Licence. The National Archives may, from time to time, issue new versions of the Open Government Licence. If you are already using Information under a previous version of the Open Government Licence, the terms of that licence will continue to apply.

These terms are compatible with the Creative Commons Attribution License 4.0 and the Open Data Commons Attribution License, both of which license copyright and database rights. This means that when the Information is adapted and licensed under either of those licences, you automatically satisfy the conditions of the OGL when you comply with the other licence. The OGLv3.0 is Open Definition compliant.

Further context, best practice and guidance can be found in the UK Government Licensing Framework section on The National Archives website.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Water Abstraction System

![Build Status](https://github.com/DEFRA/water-abstraction-system/workflows/CI/badge.svg?branch=main)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_water-abstraction-system&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=DEFRA_water-abstraction-system)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_water-abstraction-system&metric=coverage)](https://sonarcloud.io/dashboard?id=DEFRA_water-abstraction-system)
[![Known Vulnerabilities](https://snyk.io/test/github/DEFRA/water-abstraction-system/badge.svg)](https://snyk.io/test/github/DEFRA/water-abstraction-system)
[![Licence](https://img.shields.io/badge/Licence-OGLv3-blue.svg)](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3)

This API provides an interface for calculating charges, queuing transactions and generating transaction files used to produce invoices.

## Prerequisites

Make sure you already have:

- [Node.js v16.*](https://nodejs.org/en/)
- [PostgreSQL v12](https://www.postgresql.org/)

## Installation

First clone the repository and then drop into your new local repo:

```bash
git clone https://github.com/DEFRA/water-abstraction-system.git && cd water-abstraction-system
```

Our preference is to run the database and API within Docker, so [install Docker](https://docs.docker.com/get-docker/) if you don't already have it.

## Configuration

Any configuration is expected to be driven by environment variables when the service is run in production as per [12 factor app](https://12factor.net/config).

However when running locally in development mode or in test it makes use of the [Dotenv](https://github.com/motdotla/dotenv) package. This is a shim that will load values stored in a `.env` file into the environment which the service will then pick up as though they were there all along.

Check out [.env.example](/.env.example) for details of the required things you'll need in your `.env` file.

Refer to the [config files](config) for details of all the configuration used.

## Contributing to this project

If you have an idea you'd like to contribute please log an issue.

All contributions should be submitted via a pull request.

## License

THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:

<http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3>

The following attribution statement MUST be cited in your products and applications when using this information.

> Contains public sector information licensed under the Open Government license v3
### About the license

The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.

It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.
21 changes: 21 additions & 0 deletions app/controllers/health/airbrake.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

class AirbrakeController {
static async index (req, _h) {
// First section tests connecting to Airbrake through a manual notification
req.server.app.airbrake.notify({
message: 'Airbrake manual health check',
error: new Error('Airbrake manual health check error'),
session: {
req: {
id: req.info.id
}
}
})

// Second section throws an error and checks that we automatically capture it and then connect to Airbrake
throw new Error('Airbrake automatic health check error')
}
}

module.exports = AirbrakeController
13 changes: 13 additions & 0 deletions app/controllers/health/database.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const DatabaseHealthCheckService = require('../../services/database_health_check.service.js')

class DatabaseController {
static async index (_req, h) {
const result = await DatabaseHealthCheckService.go()

return h.response(result).code(200)
}
}

module.exports = DatabaseController
9 changes: 9 additions & 0 deletions app/controllers/root.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

class RootController {
static async index (_req, _h) {
return { status: 'alive' }
}
}

module.exports = RootController
Loading

0 comments on commit 58cab42

Please sign in to comment.