# Contributing to Annams

1. [Technologies Involved](#technologies-involved)
2. [Setting up the development environment](#setting-up-the-development-environment)
3. [Running in development](#running-in-development)
4. [Testing](#testing)

We work via standard fork + merge request to `master` model.

## Technologies Involved
1. Node.js - *the runtime*
  - Express application framework
  - Knex query builder library
  - Mocha test framework
  - Chai assertion library
  - Sinon mocking library
2. Yarn - *dependency mangement*
3. Docker - *local development/deployment*
4. Wiremock - *consumer driven contract testing*
5. Kubernetes - *deployment*
  - MiniKube for testing deployments

## Provisioning
You'll need a Docker daemon installed and running on your host machine, and you can get it from this link: https://store.docker.com/search?type=edition&offering=community.

Confirm that Docker, Git, Node 8 and Yarn are installed on your machine. You can verify this via:

```bash
docker version;
# > Client: ...
git --version
# > git version ...
node -v
# > v....
yarn -v
# > ...
```

## Development
### Dependency Installation
To get started, install the NPM dependencies. We use Yarn to manage the dependencies:

```bash
yarn install
```

### External Services Startup
Next, start the services in the background:

```bash
npm run services start
```

Verify they are up by running:

```bash
docker ps | grep annams_dev
```

> To stop the services, run `npm run services stop`

The available services are:

- MySQL (exposed on port 13306 on host - [localhost:13306](localhost:13306))
- Prometheus (exposed on port 19090 on host [localhost:19090](http://localhost:19090))
- Prometheus PushGateway (exposed on port 19091 on host [localhost:19091](http://localhost:19091))
- Redis (exposed on port 16379 on host [localhost:16379](localhost:16379))
- Wiremock (exposed on port 18081 on host localhost:18081](localhost:18081))
- Zipkin (exposed on port 19411 on host [localhost:19411](http://localhost:19411))

> The ports have been configured with a pre-pended `1` to avoid confusion between running in production/running in development.

### Application Startup
You should then be able to run the following command to get started developing locally:

```bash
npm start
```

To test the production build, run:

```bash
npm start-prd
```

OR

```bash
ENV=production npm start
```

## Testing

### Code Quality
ESLint is used for the linter. Run it with:

```bash
npm run test-lint;
```

To fix auto-correctable errors, run:

```bash
npm run fix-lint;
```

For pipeline purposes, run:

```bash
ENV=developmnt npm run build \
  && ANNAMS_DEV_IMAGE="zephinzer/annams:development-latest" docker-compose -f ./provisioning/deployments/docker/test/docker-compose.yml run test-lint;
```

### Depndency Vulnerability
NSP is used to check for known security vulnerabilities in dependencies. Run it with:

```bash
npm run test-sec;
```

For pipeline purposes, run:

```bash
ENV=developmnt npm run build \
  && ANNAMS_DEV_IMAGE="zephinzer/annams:development-latest" docker-compose -f ./provisioning/deployments/docker/test/docker-compose.yml run test-sec;
```

### Functional Testing
Mocha is used as the test framework with tests stored in [`./tests`](../tests) relative to the project root.

`WIP`

For pipeline purposes, run:

```bash
ENV=developmnt npm run build \
  && ANNAMS_DEV_IMAGE="zephinzer/annams:development-latest" docker-compose -f ./provisioning/deployments/docker/test/docker-compose.yml run test-unit;
```

### Consumer-Driven-Contract Testing
Wiremock is used to mock Annams. This feature is still a WIP but you can check out the documentation on how to do this at [the README](../mock/README.md).

In short, run `npm run build-mock` to create the Wiremock image, then run `npm run start-mock` to start the container. Visit http://localhost:8080. To start the recorder, go to http://localhost:8080/__admin/recorder.

[Read more on Wiremock](http://wiremock.org).

## Continuous Integration
We use Travis to automatically run tests on every push to any branch. 

### Image Build Process
#### Dependencies Image Building
##### Building for Development
```sh
ENV=development npm run build-deps -- latest
```
##### Building for Production
```sh
npm run build-deps -- latest
```
#### Application Image Building
##### Building for Development
```sh
ENV=development DEPENDENCY_VERSION=latest npm run build;
```
##### Building for Production
```sh
DEPENDENCY_VERSION=latest npm run build;
```

## Release
Releases are done on GitHub, NPM and Docker Hub.

`WIP`