Skip to content

Commit

Permalink
create action - in mypy we trust
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlopez-rod committed Oct 6, 2023
1 parent 64ed7d9 commit 5377baa
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "GHA (square-num-m)",
"image": "ghcr.io/jmlopez-rod/m-devcontainer-py311:latest",
"runArgs": ["--rm", "--network=host", "--name=square-num-devcontainer"],
"postStartCommand": "git config --global --add safe.directory ${containerWorkspaceFolder} && ./install_m.sh",
"containerEnv": {
"GITHUB_TOKEN": "${localEnv:GITHUB_TOKEN}",
"CONTAINER_WORKSPACE": "${containerWorkspaceFolder}"
},
"mounts": [
"source=opt_pnpm,target=/opt/pnpm,type=volume",
"source=opt_venv,target=/opt/venv,type=volume",
"source=m-vscode-server-extensions,target=/root/.vscode-server/extensions,type=volume",
"source=m-vscode-server-extensions-insiders,target=/root/.vscode-server-insiders/extensions,type=volume"
],
"customizations": {
"vscode": {
"settings": {
"[json][jsonc][markdown][mdx][yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"editor.renderWhitespace": "all",
"files.associations": {
"Makefile.*": "makefile"
},
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.py$",
"cmd": "isort ${file}"
}
]
},
"editor.minimap.enabled": false,
"editor.rulers": [80, 120],
"python.defaultInterpreterPath": "/opt/venv/m/bin/python",
"python.pythonPath": "/workspaces/gha-square-num-m/src",
"python.languageServer": "Pylance",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": ["--rc-file=.pylintrc"],
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": ["--config=.flake8"],
"python.linting.mypyEnabled": true,
"python.linting.mypyArgs": ["--config-file=mypy.ini"]
},
"extensions": [
"GitHub.copilot",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.flake8",
"EditorConfig.EditorConfig",
"unifiedjs.vscode-mdx",
"emeraldwalk.RunOnSave",
"streetsidesoftware.code-spell-checker"
]
}
},
"shutdownAction": "stopContainer"
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
m/.m
.mypy_cache
__pycache__
Empty file added .npmrc
Empty file.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mypy:
mypy src

action:
m github actions src/pkg/actions.py
25 changes: 25 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# AUTOGENERATED FILE - Do not update by hand!
# Edit src/pkg/actions.py
# then run `m github actions [python_file]` to update

name: Square Number
description: Square the given number

inputs:
num:
description: the number to square
required: true

outputs:
squared-num:
description: the squared number
value: ${{ steps.square.outputs.squared-num }}

runs:
using: composite
steps:
- id: square
shell: bash
env:
INPUT_NUM: ${{ inputs.num }}
run: PYTHONPATH="$GITHUB_ACTION_PATH/src" python -m pkg.main
9 changes: 9 additions & 0 deletions install_m.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -euxo pipefail

branch=github-actions
pip install "git+https://github.com/jmlopez-rod/m.git@$branch"

/opt/m_helpers/_init.sh

echo 'close this terminal and open another one then run `pnpm install`'
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "square-num-gha",
"version": "1.0.0",
"description": "",
"private": true,
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"prettier": "2.8.8"
}
}
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added src/pkg/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions src/pkg/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from m.github.actions import Action

from .inputs import GithubInputs
from .main import main_step

actions = Action(
name='Square Number',
description='Square the given number',
file_path='action.yaml',
inputs=GithubInputs,
steps=[
main_step(step_id='square', args=GithubInputs(num='inputs.num')),
]
)
7 changes: 7 additions & 0 deletions src/pkg/inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from m.github.actions import InArg, KebabModel


class GithubInputs(KebabModel):
"""Inputs from the Github Action."""

num: str = InArg(help='the number to square')
44 changes: 44 additions & 0 deletions src/pkg/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from m.core import Good, Res
from m.github.actions import KebabModel, OutArg, RunStep, run_action

from .inputs import GithubInputs


class Outputs(KebabModel):
"""Outputs from the Github Action."""

squared_num: str = OutArg(help='the squared number', export=True)


def main(inputs: GithubInputs) -> Res[Outputs]:
"""Square the given number.
Args:
inputs: The inputs to the step.
Returns:
The outputs of the step or an issue.
"""
print('Squaring the number')
num = int(inputs.num)
result = num * num
return Good(Outputs(squared_num=str(result)))


def main_step(
step_id: str,
args: GithubInputs
) -> RunStep[GithubInputs, Outputs]:
"""Create a step to square the given number.
Args:
step_id: The id of the step.
inputs: The inputs to the step.
Returns:
A step to use in the action.
"""
return RunStep[GithubInputs, Outputs](id=step_id, run=main, args=args)

if __name__ == '__main__':
run_action(main)

0 comments on commit 5377baa

Please sign in to comment.