Skip to content

Commit 4cbdb89

Browse files
committed
refactor: implode template
1 parent d88eb84 commit 4cbdb89

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

github/workflows/auto-update.yml

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
name: Auto update generated client
2+
on:
3+
workflow_dispatch:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
7+
# Allows external webhook trigger
8+
repository_dispatch:
9+
types:
10+
- webhook
11+
env:
12+
library_last_version: 0.0.0
13+
template_last_version: 0.0.0
14+
template_current_version: 0.0.0
15+
document_last_version: 0.0.0
16+
document_current_version: 0.0.0
17+
major_template_last_version: 0
18+
minor_template_last_version: 0
19+
patch_template_last_version: 0
20+
major_template_current_version: 0
21+
minor_template_current_version: 0
22+
patch_template_current_version: 0
23+
major_version_change: false
24+
minor_version_change: false
25+
patch_version_change: false
26+
commit_message: ""
27+
jobs:
28+
get-template-version:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v2
32+
# Find all relevant semver versions needed to figure out how to update the client
33+
- name: Fetch the last AsyncAPI document version that generated the client
34+
run: echo "document_last_version=$(cat ./configs.json | jq -r '.document_last_version')" >> $GITHUB_ENV
35+
- name: Fetch the last template version that generated the client
36+
run: echo "template_last_version=$(cat ./configs.json | jq -r '.template_last_version')" >> $GITHUB_ENV
37+
- name: Fetch the last library version
38+
run: echo "library_last_version=$(cat ./package.json | jq -r '.version')" >> $GITHUB_ENV
39+
- name: Fetch current template version
40+
run: echo "template_current_version=$(curl -sL https://api.github.com/repos/asyncapi/ts-nats-template/releases/latest | jq -r '.tag_name' | sed 's/v//')" >> $GITHUB_ENV
41+
- name: Get all the application AsyncAPI documents
42+
uses: actions/checkout@v2
43+
with:
44+
repository: GamingAPI/definitions
45+
path: "./definitions"
46+
- name: Get the version of the API
47+
run: echo "document_current_version=$(cat ./definitions/bundled/rust.asyncapi.json | jq -r '.info.version' | sed 's/v//')" >> $GITHUB_ENV
48+
49+
# Figure out how the template version have changed
50+
- run: semver_template_last_version=( ${template_last_version//./ } ) && major_template_last_version=${semver_template_last_version[0]} && echo "major_template_last_version=$major_template_last_version" >> $GITHUB_ENV
51+
- run: semver_template_last_version=( ${template_last_version//./ } ) && minor_template_last_version=${semver_template_last_version[1]} && echo "minor_template_last_version=$minor_template_last_version" >> $GITHUB_ENV
52+
- run: semver_template_last_version=( ${template_last_version//./ } ) && patch_template_last_version=${semver_template_last_version[2]} && echo "patch_template_last_version=$patch_template_last_version" >> $GITHUB_ENV
53+
- run: semver_template_current_version=( ${template_current_version//./ } ) && major_template_current_version=${semver_template_current_version[0]} && echo "major_template_current_version=$major_template_current_version" >> $GITHUB_ENV
54+
- run: semver_template_current_version=( ${template_current_version//./ } ) && minor_template_current_version=${semver_template_current_version[1]} && echo "minor_template_current_version=$minor_template_current_version" >> $GITHUB_ENV
55+
- run: semver_template_current_version=( ${template_current_version//./ } ) && patch_template_current_version=${semver_template_current_version[2]} && echo "patch_template_current_version=$patch_template_current_version" >> $GITHUB_ENV
56+
- if: ${{ env.major_template_current_version > env.major_template_last_version }}
57+
run: echo "major_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}Template have changed to a new major version." >> $GITHUB_ENV
58+
- if: ${{ env.minor_template_current_version > env.minor_template_last_version }}
59+
run: echo "minor_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}Template have changed to a new minor version." >> $GITHUB_ENV
60+
- if: ${{ env.patch_template_current_version > env.patch_template_last_version }}
61+
run: echo "patch_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}Template have changed to a new patch version." >> $GITHUB_ENV
62+
63+
# Figure out how the AsyncAPI document have changed
64+
- run: semver_document_last_version=( ${document_last_version//./ } ) && major_document_last_version=${semver_document_last_version[0]} && echo "major_document_last_version=$major_document_last_version" >> $GITHUB_ENV
65+
- run: semver_document_last_version=( ${document_last_version//./ } ) && minor_document_last_version=${semver_document_last_version[1]} && echo "minor_document_last_version=$minor_document_last_version" >> $GITHUB_ENV
66+
- run: semver_document_last_version=( ${document_last_version//./ } ) && patch_document_last_version=${semver_document_last_version[2]} && echo "patch_document_last_version=$patch_document_last_version" >> $GITHUB_ENV
67+
- run: semver_document_current_version=( ${document_current_version//./ } ) && major_document_current_version=${semver_document_current_version[0]} && echo "major_document_current_version=$major_document_current_version" >> $GITHUB_ENV
68+
- run: semver_document_current_version=( ${document_current_version//./ } ) && minor_document_current_version=${semver_document_current_version[1]} && echo "minor_document_current_version=$minor_document_current_version" >> $GITHUB_ENV
69+
- run: semver_document_current_version=( ${document_current_version//./ } ) && patch_document_current_version=${semver_document_current_version[2]} && echo "patch_document_current_version=$patch_document_current_version" >> $GITHUB_ENV
70+
- if: ${{ env.major_document_current_version > env.major_document_last_version }}
71+
run: echo "major_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}AsyncAPI document have changed to a new major version. " >> $GITHUB_ENV
72+
- if: ${{ env.minor_document_current_version > env.minor_document_last_version && env.major_document_current_version < env.major_document_last_version }}
73+
run: echo "minor_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}AsyncAPI document have changed to a new minor version. " >> $GITHUB_ENV
74+
- if: ${{ env.patch_document_current_version > env.patch_document_last_version && env.minor_document_current_version < env.minor_document_last_version && env.major_document_current_version < env.major_document_last_version }}
75+
run: echo "patch_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}AsyncAPI document have changed to a new patch template version. " >> $GITHUB_ENV
76+
77+
# Make changes to the client code if needed
78+
- name: Remove previous files to ensure clean slate
79+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
80+
run: find . -not \( -name configs.json -or -name .gitignore -or -name custom_package.json -or -iwholename *.github* -or -iwholename *./definitions* -or -iwholename *.git* -or -name . \) -exec echo {} +
81+
- name: Generating client from the AsyncAPI document
82+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
83+
uses: docker://asyncapi/github-action-for-generator:2.0.0
84+
with:
85+
template: '@asyncapi/ts-nats-template'
86+
filepath: ./definitions/rust.asyncapi.json
87+
output: ./
88+
- name: Write new config file to ensure we keep the new state for next time
89+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
90+
run: |
91+
contents="$(jq ".template_last_version = \"$template_current_version\" | .document_last_version = \"$document_current_version\"" configs.json)" && echo "${contents}" > configs.json
92+
- name: Write old version to package.json file as it was cleared by the generator
93+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
94+
run: contents="$(jq ".version = \"$library_last_version\"" package.json)" && echo "${contents}" > package.json
95+
- name: Merge custom package file with template generated
96+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
97+
run: jq -s '.[0] * .[1]' ./package.json ./custom_package.json > ./package_tmp.json
98+
- name: Remove old package file
99+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
100+
run: rm ./package.json
101+
- name: Take new package file in use
102+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
103+
run: mv ./package_tmp.json ./package.json
104+
105+
# Clean up from code generation
106+
- name: Remove excess files
107+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
108+
run: rm -rf ./definitions
109+
110+
# Build the library
111+
- name: Install dependencies
112+
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
113+
run: npm i
114+
115+
# Follow conventional commit for PR's to ensure accurate updates
116+
- name: Create pull request for major version if needed
117+
if: ${{env.major_version_change == 'true'}}
118+
uses: peter-evans/create-pull-request@v3
119+
with:
120+
title: "feat!: major version change"
121+
committer: GamingEventapiBot <gamingeventapi@gmail.com>
122+
author: GamingEventapiBot <gamingeventapi@gmail.com>
123+
delete-branch: true
124+
commit-message: ${{env.commit_message}}. Library require a new major version change.
125+
body: ${{env.commit_message}}. Library require a new major version change.
126+
- name: Create pull request for minor version if needed
127+
if: ${{env.minor_version_change == 'true'}}
128+
uses: peter-evans/create-pull-request@v3
129+
with:
130+
title: "feat: minor version change"
131+
committer: GamingEventapiBot <gamingeventapi@gmail.com>
132+
author: GamingEventapiBot <gamingeventapi@gmail.com>
133+
delete-branch: true
134+
commit-message: ${{env.commit_message}}. Library require a new minor version change.
135+
body: ${{env.commit_message}}. Library require a new minor version change.
136+
- name: Create pull request for patch version if needed
137+
if: ${{env.patch_version_change == 'true'}}
138+
uses: peter-evans/create-pull-request@v3
139+
with:
140+
title: "fix: patch version change"
141+
committer: GamingEventapiBot <gamingeventapi@gmail.com>
142+
author: GamingEventapiBot <gamingeventapi@gmail.com>
143+
delete-branch: true
144+
commit-message: ${{env.commit_message}}. Library require a new patch version change.
145+
body: ${{env.commit_message}}. Library require a new patch version change.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Pull request integration testing
2+
3+
on:
4+
pull_request
5+
6+
jobs:
7+
test:
8+
name: 'Run integration tests'
9+
runs-on: ubuntu-latest
10+
services:
11+
nats:
12+
image: nats
13+
ports:
14+
- 4222:4222
15+
steps:
16+
- name: Checkout repo
17+
uses: actions/checkout@v2
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: 14
22+
- name: Install dependencies
23+
run: npm ci
24+
- name: Test the generated client
25+
run: npm run test:integration
26+
- name: Dump docker logs on failure
27+
if: failure()
28+
uses: jwalton/gh-docker-logs@v1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Pull request testing
2+
3+
on:
4+
pull_request
5+
6+
jobs:
7+
release:
8+
name: 'Run linter and tests'
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repo
12+
uses: actions/checkout@v2
13+
- name: Setup Node.js
14+
uses: actions/setup-node@v1
15+
with:
16+
node-version: 13
17+
- name: Install dependencies
18+
run: npm ci
19+
- name: Should be able to build the library
20+
run: npm run build

github/workflows/release.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
release:
10+
name: 'Release NPM, GitHub'
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repo
14+
uses: actions/checkout@v2
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: 13
19+
- name: Install dependencies
20+
run: npm ci
21+
- name: Build library
22+
run: npm run build
23+
- name: Get version from package.json before release step
24+
id: initversion
25+
run: echo "::set-output name=version::$(npm run get-version --silent)"
26+
- name: Release to NPM and GitHub
27+
id: release
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
30+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
31+
GIT_AUTHOR_NAME: GamingEventapiBot
32+
GIT_AUTHOR_EMAIL: gamingeventapi@gmail.com
33+
GIT_COMMITTER_NAME: GamingEventapiBot
34+
GIT_COMMITTER_EMAIL: gamingeventapi@gmail.com
35+
run: npm run release
36+
- name: Get version from package.json after release step
37+
id: extractver
38+
run: echo "::set-output name=version::$(npm run get-version --silent)"
39+
- name: Create Pull Request with updated package files
40+
if: steps.initversion.outputs.version != steps.extractver.outputs.version
41+
uses: peter-evans/create-pull-request@v2.4.4
42+
with:
43+
token: ${{ secrets.GH_TOKEN }}
44+
commit-message: 'chore(release): ${{ steps.extractver.outputs.version }}'
45+
committer: GamingEventapiBot <gamingeventapi@gmail.com>
46+
author: GamingEventapiBot <gamingeventapi@gmail.com>
47+
title: 'chore(release): ${{ steps.extractver.outputs.version }}'
48+
body: 'Version bump in package.json and package-lock.json for release [${{ steps.extractver.outputs.version }}](https://github.com/${{github.repository}}/releases/tag/v${{ steps.extractver.outputs.version }})'
49+
branch: version-bump/${{ steps.extractver.outputs.version }}

0 commit comments

Comments
 (0)