Skip to content

Commit

Permalink
dev CI: openapi flows(6)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 committed Nov 1, 2024
1 parent 5544ded commit 2a39f46
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 115 deletions.
62 changes: 0 additions & 62 deletions .github/workflows/flow-openapi-commenter.yml

This file was deleted.

148 changes: 95 additions & 53 deletions .github/workflows/flow-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ on:
pull_request:
paths:
- 'flows/**'
repository_dispatch:
types: [flows_openapi_update]

permissions:
contents: read
issues: write
pull-requests: write
actions: read

jobs:
flow-openapi:
Expand All @@ -32,82 +37,119 @@ jobs:
- name: Install redoc-cli
run: npm install -g redoc-cli

- name: Checkout Visionatrix
uses: actions/checkout@v4
- name: Install, check, and generate OpenAPI specs and HTML docs for flows
run: |
mkdir -p flows_openapi
mkdir -p flows_docs
for file in flows/*; do
echo "Processing $file"
VIX_MODE=SERVER VIX_SERVER_FULL_MODELS=0 python3 -m visionatrix install-flow --file="$file"
flow_name=$(basename "$file")
openapi_file="flows_openapi/$flow_name"
VIX_MODE=SERVER VIX_SERVER_FULL_MODELS=0 python3 -m visionatrix openapi --only-flows --installed --file="$openapi_file"
html_file="flows_docs/${flow_name%.json}.html"
npx redoc-cli bundle "$openapi_file" -o "$html_file"
done
- name: Upload OpenAPI specs artifact
uses: actions/upload-artifact@v3
with:
name: OpenAPI Specs
path: flows_openapi/

- name: Upload HTML documentation artifact
uses: actions/upload-artifact@v3
with:
path: src_visionatrix
repository: Visionatrix/Visionatrix
name: OpenAPI Docs
path: flows_docs/

- name: Install PostgreSQL
- name: Dispatch event to main repository
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
env:
GITHUB_TOKEN: ${{ secrets.PAT_FOR_DISPATCH }}
run: |
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql postgresql-contrib
sudo pg_ctlcluster 14 main start
sudo -u postgres psql -c "CREATE USER vix_user WITH PASSWORD 'vix_password';"
sudo -u postgres psql -c "CREATE DATABASE vix_db OWNER vix_user;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE vix_db TO vix_user;"
curl -X POST -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/${{ github.repository }}/dispatches \
-d '{"event_type": "flows_openapi_update", "client_payload": {"pr_number": ${{ github.event.pull_request.number }}, "repository": "${{ github.repository }}", "run_id": "${{ github.run_id }}"}}'
- name: Set DATABASE_URI environment variable
run: echo "DATABASE_URI=postgresql+psycopg://vix_user:vix_password@localhost:5432/vix_db" >> $GITHUB_ENV
handle-dispatch:
if: github.event_name == 'repository_dispatch'
runs-on: ubuntu-22.04

- name: Install Visionatrix
run: |
python3 -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
python3 -m pip install "src_visionatrix/.[pgsql]"
python3 -m visionatrix install
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get list of changed flow files
id: changed_files
run: |
# Add the base repository as a new remote
git remote add base https://github.com/${{ github.event.pull_request.base.repo.full_name }}.git
# Fetch the base branch from the base repository
git fetch base ${{ github.event.pull_request.base.ref }} --depth=1
# Get the list of changed files between the PR and the base branch
git diff --name-only --diff-filter=AM base/${{ github.event.pull_request.base.ref }}...HEAD > changed_files_am.txt
# Filter for changes in 'flows/' directory
grep '^flows/' changed_files_am.txt > flows_changed_files_am.txt || true
- name: Show changed flow files
run: cat flows_changed_files_am.txt

- name: Check if any flows were changed
id: check_flows
run: |
if [ -s flows_changed_files_am.txt ]; then
echo "flows_changed=true" >> $GITHUB_OUTPUT
else
echo "flows_changed=false" >> $GITHUB_OUTPUT
fi
- name: Install Python and Node.js
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install, check, and generate OpenAPI specs and HTML docs for flows
if: steps.check_flows.outputs.flows_changed == 'true'
- uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install redoc-cli
run: npm install -g redoc-cli

- name: Install and generate OpenAPI specs
run: |
mkdir -p flows_openapi
mkdir -p flows_docs
while read file; do
echo "Processing $file"
# Install and check the flow
VIX_MODE=SERVER VIX_SERVER_FULL_MODELS=0 python3 -m visionatrix install-flow --file="$file"
for file in flows/*; do
flow_name=$(basename "$file")
openapi_file="flows_openapi/$flow_name"
VIX_MODE=SERVER VIX_SERVER_FULL_MODELS=0 python3 -m visionatrix openapi --only-flows --installed --file="$openapi_file"
# Generate HTML documentation using Redoc
html_file="flows_docs/${flow_name%.json}.html"
npx redoc-cli bundle "$openapi_file" -o "$html_file"
rm -f "vix_flows/$flow_name"
done < flows_changed_files_am.txt
done
- name: Upload OpenAPI specs artifact (JSON files)
if: steps.check_flows.outputs.flows_changed == 'true'
- name: Upload OpenAPI specs artifact
uses: actions/upload-artifact@v3
with:
name: OpenAPI Specs
path: flows_openapi/

- name: Upload HTML documentation artifact
if: steps.check_flows.outputs.flows_changed == 'true'
uses: actions/upload-artifact@v3
with:
name: OpenAPI Docs
path: flows_docs/

- name: List artifacts and extract URLs
id: list_artifacts
uses: actions/github-script@v6
with:
script: |
const run_id = context.runId;
const { data: artifacts } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run_id
});
const openapiArtifact = artifacts.artifacts.find(artifact => artifact.name === 'OpenAPI Specs');
const docsArtifact = artifacts.artifacts.find(artifact => artifact.name === 'OpenAPI Docs');
core.setOutput('openapi_url', openapiArtifact ? openapiArtifact.archive_download_url : '');
core.setOutput('docs_url', docsArtifact ? docsArtifact.archive_download_url : '');
- name: Comment on PR with artifact links and instructions
uses: thollander/actions-comment-pull-request@v3
with:
message: |
✅ **OpenAPI Specifications and Documentation Generated**
**Instructions:**
- **Reviewers:**
- [OpenAPI Spec](${{ steps.list_artifacts.outputs.openapi_url }})
- [HTML Documentation](${{ steps.list_artifacts.outputs.docs_url }})
- **Contributors:**
- Download the **OpenAPI Specs** artifact from the [OpenAPI Spec](${{ steps.list_artifacts.outputs.openapi_url }})
- Add the JSON files to the `flows_openapi/` directory in your PR
- Commit and push the changes to update your PR
pr_number: ${{ github.event.client_payload.pr_number }}

0 comments on commit 2a39f46

Please sign in to comment.