Skip to content

Commit

Permalink
Added new GitHub action (regenerate-library.yml)
Browse files Browse the repository at this point in the history
The new action auto-regenerates the Python library upon creation of a new release.
  • Loading branch information
TKIPisalegacycipher committed Apr 6, 2023
1 parent c8b6b13 commit ebf3f97
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/python-publish.yml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/regenerate-library.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Regenerate Python Library
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set release name as environment variable
run: echo "RELEASE_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Delete folder
run: |
rm -rf meraki
- name: Regenerate Python Library
run: |
python generator/generate_library.py -g true -v $RELEASE_NAME
- name: Commit changes
run: |
git config --global user.name "GitHub Action on behalf of TKIPisalegacycipher"
git add .
git commit -m "Auto-update library to release version $RELEASE_NAME."
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.ACTION_RUNNER_GITHUB_TOKEN }}
28 changes: 19 additions & 9 deletions generator/generate_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ID as inputs, a specific dashboard org's OpenAPI spec.
=== USAGE ===
python[3] generate_library.py [-o <org_id>] [-k <api_key>] [-v <version_number>]
python[3] generate_library.py [-o <org_id>] [-k <api_key>] [-v <version_number>] [-g <is_called_from_github_action>]
API key can, and is recommended to, be set as an environment variable named MERAKI_DASHBOARD_API_KEY.
"""

Expand Down Expand Up @@ -123,7 +123,7 @@ def parse_params(operation, parameters, param_filters=[]):
return ret


def generate_library(spec, version_number):
def generate_library(spec, version_number, is_github_action):
# Supported scopes list will include organizations, networks, devices, and all product types.
supported_scopes = ['organizations', 'networks', 'devices', 'appliance', 'camera', 'cellularGateway', 'insight',
'sm', 'switch', 'wireless', 'sensor', 'administered', 'licensing', 'secureConnect']
Expand All @@ -136,6 +136,12 @@ def generate_library(spec, version_number):

batchable_action_summaries = [action['summary'] for action in spec['x-batchable-actions']]

# Set template_dir if a GitHub action is invoking it
if is_github_action:
template_dir = 'generator/'
else:
template_dir = ''

# Check paths and create sub-directories if needed
subdirs = ['meraki', 'meraki/api', 'meraki/api/batch', 'meraki/aio', 'meraki/aio/api', 'meraki/api/batch']
for dir in subdirs:
Expand Down Expand Up @@ -178,7 +184,7 @@ def generate_library(spec, version_number):
section = scopes[scope]

with open(f'meraki/api/{scope}.py', 'w', encoding='utf-8', newline='') as output:
with open('class_template.jinja2', encoding='utf-8', newline='') as fp:
with open(f'{template_dir}class_template.jinja2', encoding='utf-8', newline='') as fp:
class_template = fp.read()
template = jinja_env.from_string(class_template)
output.write(
Expand All @@ -189,7 +195,7 @@ def generate_library(spec, version_number):

# Generate Asyncio API libraries
async_output = open(f'meraki/aio/api/{scope}.py', 'w', encoding='utf-8', newline='')
with open('async_class_template.jinja2', encoding='utf-8', newline='') as fp:
with open(f'{template_dir}async_class_template.jinja2', encoding='utf-8', newline='') as fp:
class_template = fp.read()
template = jinja_env.from_string(class_template)
async_output.write(
Expand All @@ -200,7 +206,7 @@ def generate_library(spec, version_number):

# Generate Action Batch API libraries
batch_output = open(f'meraki/api/batch/{scope}.py', 'w', encoding='utf-8', newline='')
with open('batch_class_template.jinja2', encoding='utf-8', newline='') as fp:
with open(f'{template_dir}batch_class_template.jinja2', encoding='utf-8', newline='') as fp:
class_template = fp.read()
template = jinja_env.from_string(class_template)
batch_output.write(
Expand Down Expand Up @@ -301,7 +307,7 @@ def generate_library(spec, version_number):
call_line = 'return self._session.delete(metadata, resource)'

# Add function to files
with open('function_template.jinja2', encoding='utf-8', newline='') as fp:
with open(f'{template_dir}function_template.jinja2', encoding='utf-8', newline='') as fp:
function_template = fp.read()
template = jinja_env.from_string(function_template)
output.write(
Expand Down Expand Up @@ -424,7 +430,7 @@ def generate_library(spec, version_number):
call_line = 'return action'

# Add function to files
with open('batch_function_template.jinja2', encoding='utf-8', newline='') as fp:
with open(f'{template_dir}batch_function_template.jinja2', encoding='utf-8', newline='') as fp:
function_template = fp.read()
template = jinja_env.from_string(function_template)
batch_output.write(
Expand Down Expand Up @@ -461,9 +467,10 @@ def main(inputs):
api_key = os.environ.get('MERAKI_DASHBOARD_API_KEY')
org_id = None
version_number = 'custom'
is_github_action = False

try:
opts, args = getopt.getopt(inputs, 'ho:k:v:')
opts, args = getopt.getopt(inputs, 'ho:k:v:g:')
except getopt.GetoptError:
print_help()
sys.exit(2)
Expand All @@ -477,6 +484,9 @@ def main(inputs):
api_key = arg
elif opt == '-v':
version_number = arg
elif opt == '-g':
if arg.lower() == 'true':
is_github_action = True

# Retrieve latest OpenAPI specification
if org_id:
Expand All @@ -494,7 +504,7 @@ def main(inputs):
else:
spec = requests.get('https://api.meraki.com/api/v1/openapiSpec').json()

generate_library(spec, version_number)
generate_library(spec, version_number, is_github_action)


if __name__ == '__main__':
Expand Down

0 comments on commit ebf3f97

Please sign in to comment.