Skip to content

Commit

Permalink
support creation and use of testmo resource files
Browse files Browse the repository at this point in the history
  • Loading branch information
derekk-nm committed Sep 25, 2024
1 parent 3b77d67 commit 2698d99
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
24 changes: 24 additions & 0 deletions actions/testmo-create-resources/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 it's 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 ${{ inputs.resources_file }} ..."
compose_testmo_resources_file.py \
--resources_json '${{ inputs.resources_json }}' \
--destination "${{ inputs.resources_file }}"
shell: bash
57 changes: 57 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,57 @@
#! /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 pathlib
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="filepath relative to REPO root to generate the file,"
" default 'testmo_resources.json'.",
default='testmo_resources.json',
type=str)

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

for field in fields:
testmo_args = {
"resources": args.destination,
"name": field,
"type": "string",
"value": fields[field]
}
testmo_command = "npx testmo automation:resources:add-field " + \
" ".join([f"--{k} {v}"
for k, v in testmo_args.items()])

# run the command and raise any exceptions
result = subprocess.run(
testmo_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
cwd=pathlib.Path.cwd(),
shell=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.resources_file }}" ]]; then
thread_resources=${{ inputs.resources_file }}
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

0 comments on commit 2698d99

Please sign in to comment.