Skip to content
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
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": [
"airbnb-base",
"plugin:jest/recommended",
"plugin:jest-formatting/recommended",
"plugin:jsdoc/recommended",
"plugin:node/recommended",
"prettier",
"plugin:security/recommended"
],
"plugins": [
"jest",
"jest-formatting",
"jsdoc",
"node",
"prettier",
"security"
],
"rules": {
"no-console": "off"
}
}
64 changes: 64 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Build

on:
push:

jobs:
Git:
name: Git
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Commit Lint
uses: wagoid/commitlint-github-action@v4
- name: Secrets Scan
uses: max/secret-scan@master
with:
exclude_path: 'secret-scan-exclude.txt'
- name: Shell Lint
uses: luizm/action-sh-checker@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Outstanding Todos
run: |
echo Checking for outstanding todos...
TODO_COUNT=$(grep -r '[\/\/#] [Tt][Oo][Dd][Oo]' . | wc -l)
if [[ $TODO_COUNT -gt 0 ]] ; then
echo "$TODO_COUNT" todos found...
grep -r '[\/\/#] [Tt][Oo][Dd][Oo]' .
exit 1
fi
echo No todos found...



Node:
name: Node
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [ 14.x, 16.x ]

steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Setup Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
# cache: npm # Currently broken
- name: Install Dependencies
run: npm ci
- name: ESLint
run: npm run lint
- name: Unit Tests
run: npm test
- name: Test Coverage
run: npm run test:coverage
- name: Audit Dependencies
run: npx better-npm-audit audit --level=high
- name: Outdated Dependencies
run: npx check-outdated --columns package,current,latest,wanted
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pids
*.seed
*.pid.lock

# Test data
test/bin/data/gl-code-quality-report.json

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

Expand Down Expand Up @@ -102,3 +105,9 @@ dist

# TernJS port file
.tern-port

# Intellij
.idea

# macOS
.DS_Store
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v1.3.0
hooks:
- id: conventional-pre-commit
stages: [ commit-msg ]
args: []
161 changes: 161 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,163 @@
# gl-code-quality-openapi-validator

[![Build](https://github.com/J-R-Oliver/gl-code-quality-openapi-validator/actions/workflows/build.yml/badge.svg)](https://github.com/J-R-Oliver/gl-code-quality-openapi-validator/actions/workflows/build.yml)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/)

<table>
<tr>
<td>
GitLab Code Quality generator for IBM's OpenAPI Validator.
</td>
</tr>
</table>

## Intro

`gl-code-quality-openapi-validator` converts the JSON file output of
IBM's [OpenAPI Validator](https://github.com/IBM/openapi-validator)
to a [GitLab Code Quality](https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html) report JSON file.
This file can then be exposed from a GitLab CI/CD job as an artifact and used to display code quality changes directly
on the merge request.

## Contents

- [Installation](#installation)
- [Options](#options)
- [Local Development](#local-development)
- [Testing](#testing)
- [Conventional Commits](#conventional-commits)
- [GitHub Actions](#github-actions)

## Installation

To install this project you will need to have [Node.js](https://nodejs.org/en/) (`>=14.0.0`)
installed. `gl-code-quality-openapi-validator` has been tested on the current LTS versions, `14.0.X` and `16.0.X`.

To install `gl-code-quality-openapi-validator` using `npm` execute:

```shell
npm install -g gl-code-quality-openapi-validator
```

The `-g` option installs `gl-code-quality-openapi-validator` globally so that it can be run from anywhere in the file
system.

To execute `gl-code-quality-openapi-validator` after installing globally run:

```shell
gl-code-quality-openapi-validator
```

`gl-code-quality-openapi-validator` is designed to work
with the JSON file output of [IBM's OpenAPI Validator](https://github.com/IBM/openapi-validator). Instructions for
installation and usage can be found [here](https://github.com/IBM/openapi-validator#installation).

The following command runs `lint-openapi` against `openapi-spec.yml` and redirects the JSON output to
`openapi-validator-report.json`. `gl-code-quality-openapi-validator` is then able to convert this file into a GitLab
Code Quality report.

```shell
lint-openapi --json openapi-spec.yml >> openapi-validator-report.json
gl-code-quality-openapi-validator -s openapi-spec.yml
```

## Options

`gl-code-quality-openapi-validator` has a handful of options. These options can be used to override the defaults that
have been provided. For example:

```shell
gl-code-quality-openapi-validator -s ./specification/openapi-specification.yml -i /reporting/openapi-validator-report.json -o ./code-quality/gl-code-quality-report.json
```

### Command Line Options

The following command line options are available for configuration:

| Option | Default | Description |
|--------------------------------------|---------------------------------|------------------------------------------------------|
| -V, --version | 1.0.0 | Output the version number |
| -s, --specification \<specification> | ./openapi-specification.yml | Filepath for the OpenAPI specification |
| -i, --input \<input> | ./openapi-validator-report.json | Filepath for the OpenAPI Validator JSON report input |
| -o, --output \<output> | ./gl-code-quality-report.json | Filepath for the GitLab Code Quality report output |
| -h, --help | | display help for command |

## Local Development

### Prerequisites

To install, run and modify this project you will need to have:

- [Node.js](https://nodejs.org/en/)
- [Git](https://git-scm.com)

### Installation

To start, please `fork` and `clone` the repository to your local machine. You are able to run the service directly on
the command line or with your IDE of choice.

### Running

#### Command Line

To run the service from the command line first you need to install the dependencies, install the project locally, and
then execute `gl-code-quality-openapi-validator`.

```shell
npm install
npm install --global
gl-code-quality-openapi-validator
```

## Testing

All tests have been written using [Jest](https://jestjs.io). To run the tests execute:

```shell
npm test
```

Code coverage is also measured by `Jest` and set to a minimum of 95%. To run tests with coverage execute:

```shell
npm run test:coverage
```

Code coverage reports can be found in the `/coverage/` directory.

## Conventional Commits

This project uses the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification for commit
messages. The specification provides a simple rule set for creating commit messages, documenting features, fixes, and
breaking changes in commit messages.

A [pre-commit](https://pre-commit.com) [configuration file](.pre-commit-config.yaml) has been provided to automate
commit linting. Ensure that *pre-commit* has been [installed](https://www.conventionalcommits.org/en/v1.0.0/) and
execute...

```shell
pre-commit install
````

...to add a commit [Git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to your local machine.

An automated pipeline job has been [configured](.github/workflows/build.yml) to lint commit messages on a push.

## GitHub Actions

CI/CD pipelines has been created using [GitHub Actions](https://github.com/features/actions) to automated tasks such as
linting, testing, and releasing.

### Build Workflow

The [build](./.github/workflows/build.yml) workflow handles integration tasks. This workflow consists of two jobs, `Git`
and `Node`, that run in parallel.

#### Git

This job automates tasks relating to repository linting and enforcing best practices.

#### Node

This job automates `Node.js` specific tasks.
48 changes: 48 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node
const { program } = require("commander");
const { readFileSync, writeFileSync } = require("fs");
const { convertValidatorReport } = require("../lib/generator");
const packageJson = require("../package.json");

/**
* Commander action callback.
*
* @param {object} commanderOptions Object containing commander options
* @param {string} commanderOptions.input Filepath for the OpenAPI Validator JSON report input
* @param {string} commanderOptions.specification Filepath for the OpenAPI specification
* @param {string} commanderOptions.output Filepath for the GitLab Code Quality report output
*/
const action = ({ input, specification, output }) => {
console.log(`Reading OpenAPI Validator JSON report from ${input}`);
const buffer = readFileSync(input);
const validatorReport = JSON.parse(buffer);

console.log("Converting report...");
const gitlabReport = convertValidatorReport(validatorReport, specification);

console.log(`Writing GitLab Code Quality report to ${output}`);
const gitlabReportJson = JSON.stringify(gitlabReport, null, 2);
writeFileSync(output, gitlabReportJson);
};

program
.name("gl-code-quality-openapi-validator")
.description("GitLab Code Quality generator for IBM's OpenAPI Validator.")
.version(packageJson.version)
.option(
"-s, --specification <specification>",
"filepath for the OpenAPI specification",
"./openapi-specification.yml"
)
.option(
"-i, --input <input>",
"filepath for the OpenAPI Validator JSON report input",
"./openapi-validator-report.json"
)
.option(
"-o, --output <output>",
"filepath for the GitLab Code Quality report output",
"./gl-code-quality-report.json"
)
.action(action)
.parse();
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
testEnvironment: "node",
coverageThreshold: {
global: {
branches: 95,
functions: 95,
lines: 95,
statements: 95,
},
},
};
Loading