Skip to content

Commit

Permalink
Only let single matrix argument in action
Browse files Browse the repository at this point in the history
  • Loading branch information
druzsan committed Feb 7, 2024
1 parent d5cbdfe commit 0aa9ffa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 47 deletions.
27 changes: 14 additions & 13 deletions .github/workflows/issue-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ jobs:
- id: setup-matrix
uses: druzsan/setup-matrix@feature/use-python-dockerfile
with:
include: |
- os: linux
cpu: amd64
run-on: ubuntu-latest
shell: bash -e
- os: macos
cpu: amd64
run-on: macos-latest
shell: bash -e
- os: windows
cpu: amd64
run-on: windows-latest
shell: msys2
matrix: |
include:
- os: linux
cpu: amd64
run-on: ubuntu-latest
shell: bash -e
- os: macos
cpu: amd64
run-on: macos-latest
shell: bash -e
- os: windows
cpu: amd64
run-on: windows-latest
shell: msys2
# Setup python and print version
build:
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
matrix: |
fruit: [apple, pear]
animal: [cat, dog]
include: |
- color: green
- color: pink
animal: cat
- fruit: apple
shape: circle
- fruit: banana
- fruit: banana
animal: cat
include:
- color: green
- color: pink
animal: cat
- fruit: apple
shape: circle
- fruit: banana
- fruit: banana
animal: cat
# Setup python and print version
setup-python:
needs: setup-matrix
Expand Down
10 changes: 2 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ branding:
inputs:
matrix:
default: ''
description: Base matrix
include:
default: ''
description: Extra matrix combinations to include
exclude:
default: ''
description: Matrix combinations to exclude from matrix
description: Strategy matrix exactly as it would be given to a job.
outputs:
matrix:
description: matrix in JSON format
description: Strategy matrix in JSON format, ready to be used in other jobs.
runs:
using: docker
image: Dockerfile
42 changes: 25 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import json
import os
from typing import Any

import yaml

Expand Down Expand Up @@ -72,35 +73,42 @@ def parse_include_exclude(input_include_exclude: str) -> list:
return include_exclude


def parse_matrix(input_matrix: str, input_include: str, input_exclude: str) -> dict:
matrix = parse_base_matrix(input_matrix)
include = parse_include_exclude(input_include)
exclude = parse_include_exclude(input_exclude)
def assert_valid_extra(extra: Any) -> None: ...

if not matrix and not include and not exclude:
raise RuntimeError(
"At least one of 'matrix', 'include' or 'exclude' arguments should be set."

def assert_valid_matrix(matrix: Any) -> None:
if not isinstance(matrix, dict):
raise TypeError(
f"Matrix must be an YAML object (Python dict), but Python "
f"{type(matrix)} received."
)
for variable, values in matrix.items():
if not isinstance(variable, str):
raise TypeError(
f"Matrix variables must be strings, but {type(variable)} received."
)
if variable in ("include", "exclude"):
assert_valid_extra(values)


def parse_matrix(input_matrix: str) -> dict:
matrix = yaml.load(input_matrix, Loader=yaml.loader.BaseLoader)
print(matrix)

if matrix is None:
raise RuntimeError("Strategy matrix must define at least one combination.")

if include:
matrix["include"] = include
if exclude:
matrix["exclude"] = exclude
return matrix


if __name__ == "__main__":
matrix = parse_matrix(
os.environ["INPUT_MATRIX"],
os.environ["INPUT_INCLUDE"],
os.environ["INPUT_EXCLUDE"],
)
matrix = parse_matrix(os.environ["INPUT_MATRIX"])

print(yaml.dump({"matrix": matrix}))

output_matrix = json.dumps(matrix)

# output("matrix", output_matrix)
# setenv("MATRIX", output_matrix)
output("matrix", "{}")
output("matrix", "{'include':{}}")
setenv("MATRIX", "{}")

0 comments on commit 0aa9ffa

Please sign in to comment.