Skip to content

Commit

Permalink
Include CI/CD workflows #53
Browse files Browse the repository at this point in the history
Signed-off-by: Fran Mulero <fmulero@vmware.com>
  • Loading branch information
fmulero authored Oct 21, 2022
2 parents 9f1aa5e + 03241d2 commit 9e33ca8
Show file tree
Hide file tree
Showing 16 changed files with 2,383 additions and 6,880 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/cd-pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: '[CI/CD] Publish Package to npmjs'
on:
release:
types:
- created
permissions:
contents: read
jobs:
publish:
name: Publish
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
scope: '@bitnami'
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-
- name: Clean install
run: npm ci
- name: Publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
36 changes: 36 additions & 0 deletions .github/workflows/ci-pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: '[CI/CD] CI Pipeline'
on: # Test pull requests and any push to main.
push:
branches:
- main
pull_request:
branches:
- main
permissions: # Force read permissions
contents: read
jobs:
verify:
name: Verify
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16.x'
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-
- id: npm-ci
name: Clean install
run: npm ci
- name: Linter
run: npm run lint
- name: Tests
# Run tests if linter fails
if: always() && steps.npm-ci.outcome == 'success'
run: npm run test-ci
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Any type of contribution is welcome; from new features, bug fixes or documentati
## Technical requirements

- When adding new feature or modifiying old behaviours a test must be implemented/changed.
- All tests should pass without failures.
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
# Copyright 2021-2021 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
#
FROM bitnami/node:12-prod
LABEL maintainer "Bitnami <containers@bitnami.com>"
FROM bitnami/node:16

LABEL org.opencontainers.image.authors="https://bitnami.com/contact" \
org.opencontainers.image.description="Readme Generator For Helm" \
org.opencontainers.image.source="https://github.com/bitnami-labs/readme-generator-for-helm" \
org.opencontainers.image.title="readme-generator-for-helm" \
org.opencontainers.image.vendor="VMware, Inc."

COPY . /app
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The number of `#` characters needed for the section titles is dynamically calcul

## Requirements

The project has been developed and tested with node version `12.21.0`.
The project has been developed and tested with node version `16.x`.

## Install

Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/* eslint-disable import/no-dynamic-require */

const fs = require('fs');
const pjson = require('./package.json')
const pjson = require('./package.json');

const { createValuesObject, parseMetadataComments } = require('./lib/parser');
const { checkKeys } = require('./lib/checker');
Expand Down Expand Up @@ -39,7 +39,7 @@ function runReadmeGenerator(options) {
const versionFlag = options.version;

if (versionFlag) {
console.log("Version:", pjson.version);
console.log('Version:', pjson.version); // eslint-disable-line no-console
} else {
if (!readmeFilePath && !schemaFilePath) {
throw new Error('Nothing to do. Please provide the --readme or --schema options.');
Expand All @@ -52,6 +52,7 @@ function runReadmeGenerator(options) {
const parsedMetadata = getParsedMetadata(options);

if (readmeFilePath) {
/* eslint no-param-reassign: ["error", { "props": false }] */
parsedMetadata.sections.forEach((section) => {
section.parameters = buildParamsToRenderList(section.parameters, config);
});
Expand Down
16 changes: 11 additions & 5 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ const { cloneDeep } = require('lodash');
* Sets the proper value for the provided parameter taking into account its modifiers.
* IMPORTANT: the last modifier takes precedence when it changes the default value
*/
function applyModifiers(param, config, realValue) {
// Hack for nullable parameters. If there are several modifiers and nullable is the last one, we just want to change the type and not the value.
/* eslint no-param-reassign: ["error", { "props": false }] */
function applyModifiers(param, config) {
// Hack for nullable parameters.
// If there are several modifiers and nullable is the last one,
// we just want to change the type and not the value.
// This case is used to change the type on the schema only, maintaining the original value
if (!(param.modifiers.findIndex((m) => m === config.modifiers.nullable) + 1 === param.modifiers.length)) {
const index = (param.modifiers.findIndex((m) => m === config.modifiers.nullable)) + 1;
if (!(index === param.modifiers.length)) {
param.modifiers.forEach((modifier) => {
switch (modifier) {
case `${config.modifiers.array}`:
Expand Down Expand Up @@ -57,7 +61,8 @@ function combineMetadataAndValues(valuesObject, valuesMetadata) {
// Set the value from actual object
param.value = valuesObject[paramIndex].value;
param.type = valuesObject[paramIndex].type;
// TODO(miguelaeh): Hack to avoid render parameters with dots in keys into the schema. Must be removed once fixed
// TODO(miguelaeh): Hack to avoid render parameters with dots in keys into the schema.
// Must be removed once fixed
param.schema = valuesObject[paramIndex].schema;
}
}
Expand Down Expand Up @@ -85,7 +90,8 @@ function combineMetadataAndValues(valuesObject, valuesMetadata) {
function buildParamsToRenderList(parametersList, config) {
let returnList = cloneDeep(parametersList);
for (const param of returnList) {
// Modify values following modifiers, except for nullable parameters that must preserve its value
// Modify values following modifiers, except for nullable parameters
// that must preserve its value
if (param.modifiers) {
applyModifiers(param, config);
}
Expand Down
28 changes: 14 additions & 14 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
*/

class Metadata {
constructor() {
/** Parsed metadata comments **/
// List of available sections
this.sections = [];
// All parameters across sections
this.parameters = [];
}
constructor() {
/* Parsed metadata comments */
// List of available sections
this.sections = [];
// All parameters across sections
this.parameters = [];
}

addSection(section) {
this.sections.push(section);
}
addSection(section) {
this.sections.push(section);
}

addParameter(parameter) {
this.parameters.push(parameter);
}
addParameter(parameter) {
this.parameters.push(parameter);
}
}

module.exports = Metadata;
6 changes: 3 additions & 3 deletions lib/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Parameter {
constructor(name) {
/** Parameter information **/
/* Parameter information */
// The parameter path using dot notation
this.name = name;
// The parameter description
Expand All @@ -15,13 +15,13 @@ class Parameter {
// The parameter type
this.type = '';

/** Extra metadata about the parameter **/
/* Extra metadata about the parameter */
// The modifiers applied to the parameter as an array of strings
this.modifiers = [];
// The section the parameter belongs to
this.section = '';

/** Properties to manage tool behaviour for this parameter **/
/* Properties to manage tool behaviour for this parameter */
// Skips the check of the parameter
this.validate = true;
// Whether to render the paramter into the README
Expand Down
18 changes: 11 additions & 7 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ function parseMetadataComments(valuesFilePath, config) {
const skipRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.skip}\\s*(.*)$`);
const extraRegex = new RegExp(`^\\s*${config.comments.format}\\s*${config.tags.extra}\\s*([^\\s]+)\\s*(\\[.*?\\])?\\s*(.*)$`);

let currentSection = null; // We assume there will always be a section before any parameter. At least one section is required
// We assume there will always be a section before any parameter. At least one section is required
let currentSection = null;
let descriptionParsing = false;
lines.forEach((line) => {
// Parse param line
const paramMatch = line.match(paramRegex);
if (paramMatch && paramMatch.length > 0) {
const param = new Parameter(paramMatch[1]);
const modifiers = paramMatch[2] ? paramMatch[2].split('[')[1].split(']')[0] : '';
param.modifiers = modifiers.split(',').filter((m) => m).map((m) => m.trim());
param.modifiers = modifiers.split(',').filter((m) => m).map((m) => m.trim());
param.description = paramMatch[3];
if (currentSection) {
param.section = currentSection.name;
Expand All @@ -68,8 +69,9 @@ function parseMetadataComments(valuesFilePath, config) {

// Parse section description content line between start and end
const descriptionContentMatch = line.match(descriptionContentRegex);
if (currentSection && descriptionParsing && descriptionContentMatch && descriptionContentMatch.length > 0) {
currentSection.addDescriptionLine(descriptionContentMatch[1])
if (currentSection && descriptionParsing
&& descriptionContentMatch && descriptionContentMatch.length > 0) {
currentSection.addDescriptionLine(descriptionContentMatch[1]);
}

// Parse section description start line
Expand Down Expand Up @@ -123,11 +125,13 @@ function createValuesObject(valuesFilePath) {
for (let valuePath in dottedFormatProperties) {
if (Object.prototype.hasOwnProperty.call(dottedFormatProperties, valuePath)) {
let value = _.get(valuesJSON, valuePath);
// TODO(miguelaeh): Variable to avoid render in the schema parameters with dots in the keys.
// the ocurrences of this variable inside this function must be deleted after fixing it.
// TODO(miguelaeh):
// Variable to avoid render in the schema parameters with dots in the keys.
// the ocurrences of this variable inside this function must be deleted after fixing it.
let renderInSchema = true;
if (value === undefined) {
// If the value is not found, give a try to our function for complex keys like 'annotations.prometheus.io/scrape'
// If the value is not found,
// give a try to our function for complex keys like 'annotations.prometheus.io/scrape'
value = _.get(valuesJSON, utils.getArrayPath(valuesJSON, valuePath));
renderInSchema = false;
}
Expand Down
26 changes: 15 additions & 11 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ function createMarkdownTable(objArray) {
/*
* Returns the section rendered
*/
function renderSection({name, description, parameters}, lineNumberSigns) {
function renderSection({ name, description, parameters }, lineNumberSigns) {
let sectionTable = '';
sectionTable += '\r\n';
sectionTable += `${lineNumberSigns} ${name}\r\n\n`; // section header
if (description != '') {
if (description !== '') {
sectionTable += `${description}\r\n\n`; // section description
}
if (parameters.length > 0) {
Expand Down Expand Up @@ -103,7 +103,7 @@ function insertReadmeTable(readmeFilePath, sections, config) {
lines.slice(paramsSectionLimits[0] + 1, paramsSectionLimits[1]).reverse().forEach((line, i) => {
if (!lastTableLikeLineFound && line && !line.match(endParamsSectionRegExp)) {
lastTableLikeLineFound = true;
paramsSectionLimits[1] = paramsSectionLimits[1] - lastNonTableMatchInLine;
paramsSectionLimits[1] -= lastNonTableMatchInLine;
console.log(`INFO: Last parameter table line found at: ${line}`);
} else if (!lastTableLikeLineFound) {
lastNonTableMatchInLine = i;
Expand Down Expand Up @@ -145,19 +145,20 @@ function generateSchema(value, tree, properties, ignoreDefault = false) {
description: value.description,
};
if (!ignoreDefault) {
if (value.value == 'null') {
if (value.value === 'null') {
value.value = null;
}
properties[tree[i]].default = value.value;
}
if (value.nullable) {
properties[tree[i]].nullable = true;
}
if (value.type == 'array') {
// The last element of the tree is an array. It is a plain or empty array since if not, the tree would have more elements
if (value.value == null || value.value.length === 0) {
if (value.type === 'array') {
// The last element of the tree is an array.
// It is a plain or empty array since if not, the tree would have more elements
if (value.value === null || value.value.length === 0) {
// When it is an empty array
properties[tree[i]].items = {}; //TODO: how do we know the type in empty arrays?
properties[tree[i]].items = {}; // TODO: how do we know the type in empty arrays?
} else {
properties[tree[i]].items = { type: (typeof value.value[0]) };
}
Expand Down Expand Up @@ -254,7 +255,7 @@ function renderOpenAPISchema(schemaFilePath, parametersList, config) {
paramsList.forEach((p) => {
if (p.modifiers.length > 0) {
p.modifiers.forEach((m) => {
switch(m) {
switch (m) {
case `${config.modifiers.array}`:
p.type = 'array';
break;
Expand All @@ -263,7 +264,9 @@ function renderOpenAPISchema(schemaFilePath, parametersList, config) {
break;
case `${config.modifiers.string}`:
p.type = 'string';
break;
break;
default:
break;
}
});
}
Expand All @@ -278,7 +281,8 @@ function renderOpenAPISchema(schemaFilePath, parametersList, config) {
// b: "something"
// "a.b" must be in the schema, while "a" is not an actual entry (Rendered in the README only)
paramsList = paramsList.filter((p) => !utils.containsModifier(p, config.modifiers.object));
// Filter the parameters without a value. When there is a modifier a fake parameter is added into the list due to the metadata
// Filter the parameters without a value.
// When there is a modifier a fake parameter is added into the list due to the metadata
paramsList = paramsList.filter((p) => p.value !== undefined);
// Render only parameter with schema=true
paramsList = paramsList.filter((p) => p.schema);
Expand Down
Loading

0 comments on commit 9e33ca8

Please sign in to comment.