Skip to content

Commit

Permalink
Support creating TestMo "resource" files (#32)
Browse files Browse the repository at this point in the history
* support creation and use of testmo resource files

* pass testmo command as list of strings

* redefine testmo_args as list

* still use shell=True

* skip shell=True, but ignore ruff S603

* ruff style updates

* Keep shell=True after all

* and skip the ruff error

* properly reference thread_resources

* include some useful debugging output

and don't capture_output from subprocess so that it ended up in stdout/stderr for debugging.

* block quality check flag on debugging print statement

* add more debugging statements

* minor fixes to debug

* more debug statements

* try quoting attribute values for add-field

* only double quotes

* set testmo token when calling add-field

* remove some debugging lines and minimize subprocess call

* fix quality check fail

* fix quality check fail

* remove token input
  • Loading branch information
derekk-nm authored Oct 1, 2024
1 parent 74adf41 commit 909ee01
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
34 changes: 34 additions & 0 deletions actions/testmo-create-resources/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
name: create testmo resources
description: create a Testmo resource .json file

inputs:
resources_file:
description: file name where the resources will be stored
required: true
resources_json:
description: stringified JSON associative array of resource fields to add. Each field's
key is the name of the field, and its value is the value of the field.
default: ''
required: false

runs:
using: composite
steps:
- name: add actions path to PATH
shell: bash
run: echo "${{ github.action_path }}" >> $GITHUB_PATH

- name: compose Testmo resources file
run: |
echo "composing Testmo resources file '$RESOURCES_FILE'"
compose_testmo_resources_file.py \
--resources_json "$RESOURCES_JSON" \
--destination "$RESOURCES_FILE"
echo "resources file contents:"
cat "$RESOURCES_FILE"
shell: bash
env:
RESOURCES_FILE: ${{ inputs.resources_file }}
RESOURCES_JSON: ${{ inputs.resources_json }}
46 changes: 46 additions & 0 deletions actions/testmo-create-resources/compose_testmo_resources_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#! /usr/bin/env python

# Script to create a TestMo "resources" file containing a specified
# set of fields. The fields are passed in as JSON with the key being the name
# of the field, and it's value being the value of the field.
# Warning; only "string" type fields are supported here.
# see these references for details about the use of TestMo resources files:
# https://docs.testmo.com/docs/automation/reference#submitting-threads-for-runs
# https://docs.testmo.com/docs/automation/reference#adding-fields-links-and-artifacts

import argparse
import json
import subprocess

if __name__ == "__main__":

# command line arguments
args_parser = argparse.ArgumentParser()

args_parser.add_argument(
"-j",
"--resources_json",
help="string version of JSON identifying resource fields",
type=str)

args_parser.add_argument(
"-d",
"--destination",
help="absolute path for where to generate the file,",
type=str)

args = args_parser.parse_args()
fields = json.loads(args.resources_json)

for field in fields:
testmo_args = [
"--resources", f"{args.destination}",
"--name", f"{field}",
"--type", "string",
"--value", f"{fields[field]}"
]
testmo_command = ["npx", "testmo", "automation:resources:add-field"]
full_command = testmo_command + testmo_args

# run the command and raise any exceptions
subprocess.run(full_command, check=True)
17 changes: 14 additions & 3 deletions actions/testmo-run-submit-thread/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
results:
description: directory of JUnit '*.xml' formatted result files
required: true
thread_resources:
description: JSON file with TestMo resources to attach to this thread
default: ''
required: false
step_status:
description: status of reported step
required: true
Expand Down Expand Up @@ -48,14 +52,21 @@ runs:
# submit results
SUCCESS=0
if [ ${REPORT} -eq 1 ]; then
if [[ ${REPORT} -eq 1 ]]; then
echo "submitting results to TESTMO run ..."
# not checking testmo_url and token as this should be
# called between "create" and "complete"
## not checking testmo_url and token as this should be
## called between "create" and "complete"
if [[ -f "${{ inputs.thread_resources }}" ]]; then
thread_resources=${{ inputs.thread_resources }}
else
echo '{}' > thread_resources.json
thread_resources=thread_resources.json
fi
npx testmo automation:run:submit-thread \
--instance ${TESTMO_URL} \
--run-id ${TESTMO_RUN_ID} \
--results ${RESULTS} \
--thread-resources ${thread_resources} \
-- step-status.sh "${{ inputs.step_status }}" || SUCCESS=$?
fi
echo "status=${SUCCESS}" >> "$GITHUB_OUTPUT"
Expand Down
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ignore = [
"ISC001",
"TCH002",
"PLW1514", # allow Path.open without encoding
"S603"# https://github.com/astral-sh/ruff/issues/4045
]
select = [
# Rules reference: https://docs.astral.sh/ruff/rules/
Expand Down

0 comments on commit 909ee01

Please sign in to comment.