Skip to content

Commit

Permalink
chore(ci): rebuild TS types from OpenAPI specs
Browse files Browse the repository at this point in the history
Signed-off-by: John Cowen <[email protected]>
  • Loading branch information
johncowen committed Feb 14, 2025
1 parent ed4ddae commit 49e7ce5
Show file tree
Hide file tree
Showing 4 changed files with 672 additions and 58 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/on-openapi-change.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: 'On OpenAPI Change'

# Ensures that only one workflow is run per branch at a time.
concurrency:
cancel-in-progress: true
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }}

on:
workflow_dispatch:
repository_dispatch:
types:
- rebuild_types
#
pull_request:
types:
- opened
- reopened
- synchronize

permissions:
contents: read # for checking out the repository (e.g. actions/checkout)
# pull-requests: write # for reading, and creating pull requests (via peter-evans/create-pull-request)

jobs:
rebuild-types:
timeout-minutes: 10
runs-on: ubuntu-latest
env:
SOURCE_REPOSITORY: kumahq/kuma
# SHA: ${{ github.event.workflow_run.head_sha || inputs.sha }}
# BRANCH: master #${{ github.event.workflow_run.head_branch || inputs.branch }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
with:
node-version-file: '.nvmrc'
- uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
id: node-modules-cache
with:
path: |
**/node_modules
/home/runner/.cache/Cypress
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
- run: |
make -C packages/kuma-http-api build
make -C packages/kuma-gui lint/script
# - uses: actions/create-github-app-token@67e27a7eb7db372a1c61a7f9bdab8699e9ee57f7 # v1.11.3
# id: github-app-token
# with:
# app-id: ${{ secrets.APP_ID }}
# private-key: ${{ secrets.APP_PRIVATE_KEY }}
# # Access to kuma needs to be explicitly included here so that a pull request can be opened in that repository.
# owner: ${{ github.repository_owner }}
# repositories: "kuma-gui"

# - uses: peter-evans/create-pull-request@67ccf781d68cd99b580ae25a5c18a1cc84ffff1f # v7.0.6
# with:
# # Note: This token can be a GITHUB_TOKEN if the created PR doesn’t
# # need to trigger workflows `on: push` or `on: pull_request`.
# # However, we definitely need to trigger workflows (e.g. to run test
# # workflows on the PR). Instead, we should use a personal access
# # token (PAT). See
# # https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#triggering-further-workflow-runs
# # for a more detailed explanation.
# token: ${{ steps.github-app-token.outputs.token }}
# commit-message: |
# chore(types): rebuild @kumahq/kuma-http-api from ${{ env.OLD_SHA }} to ${{ env.SHA }}
#
# Rebuilds ${{ github.repository }}:@kumahq/kuma-http-api to:
# [${{ env.BRANCH }}@${{ env.SHA }}](https://github.com/${{ env.SOURCE_REPOSITORY }}/tree/${{ env.SHA }})
# committer: GitHub <[email protected]>
# author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
# team-reviewers: kuma-maintainers
# signoff: true
# branch: chore/update-types-${{ env.BRANCH }}
# delete-branch: true
# title: 'chore(types): rebuild @kumahq/kuma-http-api from ${{ env.OLD_SHA }} to ${{ env.SHA }}'
# labels: ci/skip-e2e-test,${{ env.BRANCH }}
# body: |
# Bumps ${{ github.repository }} to version [${{ env.BRANCH }}@${{ env.SHA }}](https://github.com/${{ github.repository }}/tree/${{ env.SHA }})
# draft: false

3 changes: 2 additions & 1 deletion packages/config/src/mk/install.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: .install
.install: check/node
@npm install
@cd $(NPM_WORKSPACE_ROOT) \
&& npm install

.PHONY: .install/sync
.install/sync:
Expand Down
67 changes: 57 additions & 10 deletions packages/kuma-http-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,72 @@ lint: lint/js ## Dev: Run lint checks
## When amending the specs also see `make generate/oas-for-ts` in the main kuma
## repository to regenerate the specs before running `make build`
KUMAHQ_HTTP_API := $(NPM_WORKSPACE_ROOT)/$(shell cat $(NPM_WORKSPACE_ROOT)/package-lock.json | jq -r '.packages | to_entries[] | select(.value.name == "@kumahq/kuma-http-api") | .key')

CLONE :=
ifndef CI
CLONE += types/clone
endif

bump: VERSION ?= ""
bump:
@node -e \
"console.log(JSON.stringify(((json) => { json.version = '$(VERSION)' || json.version; return json; })(require('$(KUMAHQ_HTTP_API)/package.json')), null, 2))" \
> $(KUMAHQ_HTTP_API)/package-bumped.json
@rm $(KUMAHQ_HTTP_API)/package.json
@mv \
$(KUMAHQ_HTTP_API)/package-bumped.json \
$(KUMAHQ_HTTP_API)/package.json


.PHONY: build
build: types/fetch install ## Regenerate the TS types from kumahq/kuma OpenAPI specs
build: REPO ?= kumahq/kuma
build: OPENAPI_PATH ?= /docs/generated/openapi.yaml
build: LOCAL_SPEC = $(KUMAHQ_HTTP_API)/repo/$(OPENAPI_PATH)
build: REMOTE_SPEC = https://raw.githubusercontent.com/$(REPO)/$(SHA)$(OPENAPI_PATH)
build: SHA ?= $(shell git ls-remote https://github.com/$(REPO).git HEAD | awk '{ print $$1 }')
build: SHORTSHA ?= $(shell echo "$(SHA)" | cut -c 1-7)
build: TAG ?= $(shell git -c 'versionsort.suffix=-' ls-remote --exit-code --refs --sort='version:refname' --tags https://github.com/$(REPO).git '*.*.*' | tail --lines=1 | awk '{ print $$2 }' | awk -F '/' '{ print $$3 }')
build: install $(CLONE) ## Regenerate the TS types from kumahq/kuma OpenAPI specs
@npx openapi-typescript \
$(KUMAHQ_HTTP_API)/kuma/docs/generated/openapi.yaml \
$(if $($CLONE),$(LOCAL_SPEC),$(REMOTE_SPEC)) \
-o $(KUMAHQ_HTTP_API)/index.d.ts
@$(MAKE) bump VERSION="$(TAG)+$(SHORTSHA)"
@npm install --package-lock-only
@git diff

types/fetch:
@echo "Fetching latest kumahq/kuma@master..."
# if SHA is master we clone, or if its there already pull
# otherwise, shallow clone the specific SHA
# TBH we should check the SHA is equal in the second case and maybe rm -rf
# ./kuma first but the use case is so unlikely, if anyone wants please go
# ahead and add
.PHONY: types/clone
types/clone: REPO ?= kumahq/kuma
types/clone: SHA ?= $(shell git ls-remote https://github.com/$(REPO).git HEAD | awk '{ print $$1 }')
types/clone:
@echo "Fetching $(REPO)@$(SHA) ..."
@cd $(KUMAHQ_HTTP_API) \
&& \
if [ -d "./kuma/.git" ]; then \
cd ./kuma \
&& git pull; \
if [ -d "./repo/.git" ]; then \
if [ "$(SHA)" == "master" ]; then \
cd ./repo \
&& git pull; \
fi; \
else \
git clone https://github.com/kumahq/kuma.git \
--single-branch --branch=master \
--depth 1; \
if [ "$(SHA)" == "master" ]; then \
git clone \
--depth 1 \
https://github.com/$(REPO).git; \
else \
mkdir -p ./repo \
&& cd ./repo \
&& git init -b master \
&& git remote add origin https://github.com/$(REPO).git \
&& git fetch --depth 1 origin $(SHA) \
&& git checkout FETCH_HEAD; \
fi; \
fi

.PHONY: types/clean
types/clean:
@echo "Removing cloned kuma repository at..."
@echo "$(KUMAHQ_HTTP_API)/kuma..."
Expand Down
Loading

0 comments on commit 49e7ce5

Please sign in to comment.