Skip to content

Commit

Permalink
feat!: refactor action to be CLI based (asyncapi#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurtu-gal authored Jan 8, 2024
1 parent ae6207b commit 2c68a25
Show file tree
Hide file tree
Showing 22 changed files with 1,158 additions and 24,934 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

95 changes: 0 additions & 95 deletions .eslintrc

This file was deleted.

191 changes: 191 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
name: PR testing of CLI action

on:
pull_request:
types: [ opened, synchronize, reopened, ready_for_review ]

jobs:
test-defaults:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
uses: ./
with:
filepath: test/asyncapi.yml
- name: Assert GitHub Action
run: |
echo "Listing all files"
ls -R
echo "Asserting GitHub Action"
if [ -f "./output/asyncapi.md" ]; then
echo "Files exist"
else
echo "Files do not exist:- ./output/asyncapi.md"
echo "Action failed"
exit 1
fi
test-validate-success:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
uses: ./
with:
filepath: test/asyncapi.yml
command: validate

test-custom-command:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
uses: ./
with:
# Custom command to generate models
# Note: You can use command itself to generate models, but this is just an example for testing custom commands
custom_command: "generate models typescript ./test/asyncapi.yml -o ./output"
- name: Assert GitHub Action
run: |
echo "Listing all files"
ls -R
echo "Asserting GitHub Action"
if [ -f "./output/AnonymousSchema_1.ts" ]; then
echo "Models have been generated"
else
echo "Models have not been generated"
echo "Action failed"
exit 1
fi
test-custom-output:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
uses: ./
with:
filepath: test/asyncapi.yml
output: custom-output
- name: Assert GitHub Action
run: |
echo "Listing all files"
ls -R
echo "Asserting GitHub Action"
if [ -f "./custom-output/asyncapi.md" ]; then
echo "Files exist"
else
echo "Files do not exist:- ./custom-output/asyncapi.md"
echo "Action failed"
exit 1
fi
test-file-not-found:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
id: test
uses: ./
with:
filepath: non_existent_file.yml
continue-on-error: true
- name: Check for failure
run: |
if [ "${{ steps.test.outcome }}" == "success" ]; then
echo "Test Failure: non_existent_file.yml should throw an error but did not"
exit 1
else
echo "Test Success: non_existent_file.yml threw an error as expected"
fi
test-invalid-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
id: test
uses: ./
with:
filepath: test/asyncapi.yml
command: generate # No template or language specified
template: '' # Empty string
continue-on-error: true
- name: Check for failure
run: |
if [ "${{ steps.test.outcome }}" == "success" ]; then
echo "Test Failure: generate command should throw an error as no template or language specified but did not"
exit 1
else
echo "Test Success: generate command threw an error as expected"
fi
test-optimize:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
uses: ./
with:
filepath: test/unoptimized.yml
command: optimize
parameters: '-o new-file --no-tty'
- name: Assert GitHub Action
run: |
echo "Listing all files"
ls -R
echo "Asserting GitHub Action"
if [ -f "./test/unoptimized_optimized.yml" ]; then
echo "The specified file has been optimized"
else
echo "The specified file has not been optimized"
echo "Action failed"
exit 1
fi
test-bundle:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Make output directory
run: mkdir -p ./output/bundle
- name: Test GitHub Action
uses: ./
with:
custom_command: 'bundle ./test/bundle/asyncapi.yaml ./test/bundle/features.yaml --base ./test/bundle/asyncapi.yaml --reference-into-components -o ./output/bundle/asyncapi.yaml'
- name: Assert GitHub Action
run: |
echo "Listing all files"
ls -R
echo "Asserting GitHub Action"
if [ -f "./output/bundle/asyncapi.yaml" ]; then
echo "The specified files have been bundled"
else
echo "The specified files have not been bundled"
echo "Action failed"
exit 1
fi
test-convert:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test GitHub Action
uses: ./
with:
command: convert
filepath: test/asyncapi.yml
output: output/convert/asyncapi.yaml
- name: Assert GitHub Action
run: |
echo "Listing all files"
ls -R
echo "Asserting GitHub Action"
if [ -f "./output/convert/asyncapi.yaml" ]; then
echo "The specified file has been converted"
else
echo "The specified file has not been converted"
echo "Action failed"
exit 1
fi
1 change: 0 additions & 1 deletion .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ plugins:
- preset: conventionalcommits
- - "@semantic-release/release-notes-generator"
- preset: conventionalcommits
- "@semantic-release/npm"
- "@semantic-release/github"
2 changes: 2 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The dockerfile has been excluded from the SonarCloud as this container is meant to be run on GitHub Actions, hence running with security violation is ok
sonar.exclusions=Dockerfile
2 changes: 1 addition & 1 deletion CODEOWNER
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

# The default owners are automatically added as reviewers when you open a pull request unless different owners are specified in the file.

* @derberg @magicmatatjahu @asyncapi-bot-eve
* @derberg @magicmatatjahu @asyncapi-bot-eve @Shurtu-gal
22 changes: 15 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
FROM node:16 as builder
FROM node:18-alpine

COPY ./ /app
WORKDIR /app
# Create a non-root user
RUN addgroup -S myuser && adduser -S myuser -G myuser

RUN npm install
RUN apk add --no-cache bash>5.1.16 git>2.42.0 chromium

FROM node:16-alpine
# Environment variables for Puppeteer for PDF generation by HTML Template
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

COPY --from=builder /app ./
# Installing latest released npm package
RUN npm install --ignore-scripts -g @asyncapi/cli

ENTRYPOINT [ "node", "/lib/index.js" ]
COPY entrypoint.sh /entrypoint.sh

# Make the bash file executable
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
DEFAULT_VERSION = 'latest'
DEFAULT_COMMAND = 'generate'
TEST_FILEPATH = 'test/asyncapi.yml'
DEFAULT_TEMPLATE = '@asyncapi/[email protected]'
DEFAULT_LANGUAGE = ''
DEFAULT_OUTPUT = 'output'
DEFAULT_PARAMETERS = ''
DEFAULT_CUSTOM_COMMANDS = ''
CUSTOM_COMMANDS = 'validate test/asyncapi.yml'

# Add env variables to the shell
export GITHUB_WORKSPACE = $(shell pwd)

run:
@bash ./entrypoint.sh $(DEFAULT_VERSION) $(DEFAULT_COMMAND) $(TEST_FILEPATH) $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) $(DEFAULT_PARAMETERS) $(DEFAULT_CUSTOM_COMMANDS)

test: test-default test-validate-success test-validate-fail test-custom-output test-custom-commands test-optimize test-bundle test-convert

# Test cases

# Tests the default configuration without any inputs
test-default:
@bash ./entrypoint.sh $(DEFAULT_VERSION) $(DEFAULT_COMMAND) $(TEST_FILEPATH) $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) $(DEFAULT_PARAMETERS) $(DEFAULT_CUSTOM_COMMANDS)

# Tests the validate command with a valid specification
test-validate-success:
@bash ./entrypoint.sh $(DEFAULT_VERSION) 'validate' $(TEST_FILEPATH) $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) $(DEFAULT_PARAMETERS) $(DEFAULT_CUSTOM_COMMANDS)

# Tests the validate command with an invalid specification
test-validate-fail:
@bash ./entrypoint.sh $(DEFAULT_VERSION) 'validate' './test/specification-invalid.yml' $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) $(DEFAULT_PARAMETERS) $(DEFAULT_CUSTOM_COMMANDS)

# Tests if the generator can output to a custom directory
test-custom-output:
@bash ./entrypoint.sh $(DEFAULT_VERSION) $(DEFAULT_COMMAND) $(TEST_FILEPATH) $(DEFAULT_TEMPLATE) 'typescript' './output/custom-output' $(DEFAULT_PARAMETERS) $(DEFAULT_CUSTOM_COMMANDS)

# Tests if the action prefers custom commands over the default command
test-custom-commands:
@bash ./entrypoint.sh $(DEFAULT_VERSION) $(DEFAULT_COMMAND) $(TEST_FILEPATH) $(DEFAULT_TEMPLATE) 'typescript' './output/custom-output' $(DEFAULT_PARAMETERS) $(CUSTOM_COMMANDS)

# Tests if the action fails when the input is invalid (e.g. invalid template as is the case here)
fail-test:
@bash ./entrypoint.sh $(DEFAULT_VERSION) $(DEFAULT_COMMAND) $(TEST_FILEPATH) '' $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) $(DEFAULT_PARAMETERS) $(DEFAULT_CUSTOM_COMMANDS)

# Tests if the action optimizes the specification
test-optimize:
@bash ./entrypoint.sh $(DEFAULT_VERSION) 'optimize' 'test/unoptimized.yml' $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) '-o new-file --no-tty' $(DEFAULT_CUSTOM_COMMANDS)

# Tests if the action can bundle the specification with custom commands
BUNDLE_COMMAND='bundle ./test/bundle/asyncapi.yaml ./test/bundle/features.yaml --base ./test/bundle/asyncapi.yaml --reference-into-components -o ./output/bundle/asyncapi.yaml'
test-bundle:
mkdir -p ./output/bundle
@bash ./entrypoint.sh $(DEFAULT_VERSION) 'bundle' 'test/bundle/asyncapi.yaml' $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) $(DEFAULT_OUTPUT) '-o output/bundle/asyncapi.yaml' $(BUNDLE_COMMAND)

# Tests if the action can convert the specification with custom commands
test-convert:
@bash ./entrypoint.sh $(DEFAULT_VERSION) 'convert' 'test/asyncapi.yml' $(DEFAULT_TEMPLATE) $(DEFAULT_LANGUAGE) 'output/convert/asyncapi.yaml' '' $(DEFAULT_CUSTOM_COMMANDS)
Loading

0 comments on commit 2c68a25

Please sign in to comment.