Skip to content

Commit

Permalink
Merge pull request #32 from rainforestapp/RF-27572/pp/escape-strings-…
Browse files Browse the repository at this point in the history
…take-2

[RF-27572] Escape strings in `branch`, `description`, and `release`
  • Loading branch information
magni- authored Mar 17, 2023
2 parents c21865a + 8a9da08 commit 1a134f3
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 134 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ jobs:
shell: bash
steps:
- uses: actions/checkout@v3
- name: Run ShellCheck
uses: ludeeus/[email protected]
with:
scandir: './steps'
- name: Install faketime
run: |
sudo apt-get update
Expand Down Expand Up @@ -92,6 +96,21 @@ jobs:
with:
actual: ${{ steps.test_branch.outputs.command }}
expected: run --skip-update --token "test_token" --run-group 42 --junit-file results/rainforest/junit.xml --save-run-id .rainforest_run_id --branch "feature-branch" --description "foo bar" --release "1.0"
- name: Quoted parameters command
id: test_quoted_parameters
uses: ./
with:
token: test_token
run_group_id: 42
description: something"exit 1
branch: some name"exit 2
release: some sha"exit 3
dry_run: true
- name: Quoted parameters test
uses: ./.github/actions/test
with:
actual: ${{ steps.test_quoted_parameters.outputs.command }}
expected: run --skip-update --token "test_token" --run-group 42 --junit-file results/rainforest/junit.xml --save-run-id .rainforest_run_id --branch "some name\"exit 2" --description "something\"exit 1" --release "some sha\"exit 3"
- name: Missing token command
id: test_missing_token
uses: ./
Expand Down
152 changes: 18 additions & 134 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ runs:
- name: Set Action Version
shell: bash
run: |
echo "version=3.2.2" >> $GITHUB_ENV
echo "RF_ACTION_VERSION=3.2.3" >> $GITHUB_ENV
- name: Check for reruns
uses: pat-s/always-upload-cache@v3
if: (! inputs.dry_run)
Expand All @@ -102,142 +102,26 @@ runs:
path: .rainforest_run_id
restore-keys: |
rainforest-run-${{ github.run_id }}-${{ inputs.cache_key }}-
- name: Validate Parameters
shell: bash
id: validate
run: |
# Show Action Version
echo "Using Rainforest GitHub Action v${{ env.version }}"
# Ensure results directory is there
mkdir -p results/rainforest
echo "::add-mask::${{ inputs.token }}"
# Define error helper
error () {
echo "::error ::${1}"
echo "error=${1}" >> $GITHUB_OUTPUT
exit 1
}
# Validate token
if [ -z "${{ inputs.token }}" ] ; then
error "Token not set"
fi
# Validate run_group_id
if ! echo "${{ inputs.run_group_id }}" | grep -Eq '^[0-9]+$' ; then
error "run_group_id not a positive integer (${{ inputs.run_group_id }})"
fi
# Check for rerun
if [ -s .rainforest_run_id ] ; then
RAINFOREST_RUN_ID=$(cat .rainforest_run_id)
rm .rainforest_run_id
echo "Rerunning Run ${RAINFOREST_RUN_ID}"
RUN_COMMAND="rerun \"${RAINFOREST_RUN_ID}\" --skip-update --token \"${{ inputs.token }}\" --junit-file results/rainforest/junit.xml --save-run-id .rainforest_run_id"
else
RUN_COMMAND="run --skip-update --token \"${{ inputs.token }}\" --run-group ${{ inputs.run_group_id }} --junit-file results/rainforest/junit.xml --save-run-id .rainforest_run_id"
fi
# Validate conflict
if [ -n "${{ inputs.conflict }}" ] ; then
case "${{ inputs.conflict }}" in
cancel) ;&
cancel-all)
RUN_COMMAND="${RUN_COMMAND} --conflict ${{ inputs.conflict }}"
;;
*)
error "${{ inputs.conflict }} not in (cancel cancel-all)"
;;
esac
fi
# Set custom_url, or validate and set environment_id
if [ -z "${RAINFOREST_RUN_ID}" ] && [ -n "${{ inputs.custom_url }}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --custom-url \"${{ inputs.custom_url }}\""
if [ -n "${{ inputs.environment_id }}" ] ; then
echo "::warning title=Environment ID Ignored::You've set values for the mutually exclusive custom_url and environment_id parameters. Unset one of these to fix this warning."
fi
elif [ -z "${RAINFOREST_RUN_ID}" ] && [ -n "${{ inputs.environment_id }}" ] ; then
if echo "${{ inputs.environment_id }}" | grep -Eq '^[0-9]+$' ; then
RUN_COMMAND="${RUN_COMMAND} --environment-id ${{ inputs.environment_id }}"
else
error "environment_id not a positive integer (${{ inputs.environment_id }})"
fi
fi
# Validate execution_method / crowd
if [ -z "${RAINFOREST_RUN_ID}" ] ; then
if [ -n "${{ inputs.execution_method }}" ] ; then
case "${{ inputs.execution_method }}" in
automation) ;&
crowd) ;&
automation_and_crowd) ;&
on_premise)
RUN_COMMAND="${RUN_COMMAND} --execution-method ${{ inputs.execution_method }}"
;;
*)
error "${{ inputs.execution_method }} not in (automation crowd automation_and_crowd on_premise)"
;;
esac
fi
if [ -n "${{ inputs.crowd }}" ] ; then
if [ -n "${{ inputs.execution_method }}" ] ; then
error "Error: execution_method and crowd are mutually exclusive"
fi
case "${{ inputs.crowd }}" in
default) ;&
automation) ;&
automation_and_crowd) ;&
on_premise_crowd)
RUN_COMMAND="${RUN_COMMAND} --crowd ${{ inputs.crowd }}"
;;
*)
error "${{ inputs.crowd }} not in (default automation automation_and_crowd on_premise_crowd)"
;;
esac
fi
fi
# Validate automation_max_retries
if [ -n "${{ inputs.automation_max_retries }}" ] ; then
if echo "${{ inputs.automation_max_retries }}" | grep -Eq '^[0-9]+$' ; then
RUN_COMMAND="${RUN_COMMAND} --automation-max-retries ${{ inputs.automation_max_retries }}"
else
error "automation_max_retries not a positive integer (${{ inputs.automation_max_retries }})"
fi
fi
# Set branch
if [ -n "${{ inputs.branch }}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --branch \"${{ inputs.branch }}\""
fi
# Set description
if [ -z "${RAINFOREST_RUN_ID}" ] && [ -n "${{ inputs.description }}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --description \"${{ inputs.description }}\""
elif [ -z "${RAINFOREST_RUN_ID}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --description \"${GITHUB_REPOSITORY} - ${GITHUB_REF_NAME} ${GITHUB_JOB} $(date -u +'%FT%TZ')\""
fi
# Set release
if [ -n "${{ inputs.release }}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --release \"${{ inputs.release }}\""
elif [ -z "${RAINFOREST_RUN_ID}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --release \"${GITHUB_SHA}\""
fi
# Set background
if [ -n "${{ inputs.background }}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --background"
fi
echo "command=${RUN_COMMAND}" >> $GITHUB_OUTPUT
env:
RF_DESCRIPTION: ${{ inputs.description }}
RF_RUN_GROUP_ID: ${{ inputs.run_group_id }}
RF_ENVIRONMENT_ID: ${{ inputs.environment_id }}
RF_CUSTOM_URL: ${{ inputs.custom_url }}
RF_CONFLICT: ${{ inputs.conflict }}
RF_EXECUTION_METHOD: ${{ inputs.execution_method }}
RF_CROWD: ${{ inputs.crowd }}
RF_RELEASE: ${{ inputs.release }}
RF_AUTOMATION_MAX_RETRIES: ${{ inputs.automation_max_retries }}
RF_BRANCH: ${{ inputs.branch }}
RF_TOKEN: ${{ inputs.token }}
RF_BACKGROUND: ${{ inputs.background }}
RF_DRY_RUN: ${{ inputs.dry_run }}
RF_CACHE_KEY: ${{ inputs.cache_key }}
run: ${GITHUB_ACTION_PATH}/steps/validate.sh

- name: Run Rainforest
uses: docker://gcr.io/rf-public-images/rainforest-cli:latest
Expand Down
137 changes: 137 additions & 0 deletions steps/validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/bin/bash

set -euo pipefail

# Show Action Version
echo "Using Rainforest GitHub Action v${RF_ACTION_VERSION}"

# Ensure results directory is there
mkdir -p results/rainforest

echo "::add-mask::${RF_TOKEN}"

# Define error helper
error () {
echo "::error ::${1}"
echo "error=${1}" >> "$GITHUB_OUTPUT"
exit 1
}

# Validate token
if [ -z "${RF_TOKEN}" ] ; then
error "Token not set"
fi

# Validate run_group_id
if ! echo "${RF_RUN_GROUP_ID}" | grep -Eq '^[0-9]+$' ; then
error "run_group_id not a positive integer (${RF_RUN_GROUP_ID})"
fi

# Check for rerun
if [ -s .rainforest_run_id ] ; then
RAINFOREST_RUN_ID=$(cat .rainforest_run_id)
rm .rainforest_run_id
echo "Rerunning Run ${RAINFOREST_RUN_ID}"

RUN_COMMAND="rerun \"${RAINFOREST_RUN_ID}\" --skip-update --token \"${RF_TOKEN}\" --junit-file results/rainforest/junit.xml --save-run-id .rainforest_run_id"
else
RAINFOREST_RUN_ID=""
RUN_COMMAND="run --skip-update --token \"${RF_TOKEN}\" --run-group ${RF_RUN_GROUP_ID} --junit-file results/rainforest/junit.xml --save-run-id .rainforest_run_id"
fi

# Validate conflict
if [ -n "${RF_CONFLICT}" ] ; then
case "${RF_CONFLICT}" in
cancel) ;&
cancel-all)
RUN_COMMAND="${RUN_COMMAND} --conflict ${RF_CONFLICT}"
;;
*)
error "${RF_CONFLICT} not in (cancel cancel-all)"
;;
esac
fi

# Set custom_url, or validate and set environment_id
if [ -z "${RAINFOREST_RUN_ID}" ] && [ -n "${RF_CUSTOM_URL}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --custom-url \"${RF_CUSTOM_URL}\""
if [ -n "${RF_ENVIRONMENT_ID}" ] ; then
echo "::warning title=Environment ID Ignored::You've set values for the mutually exclusive custom_url and environment_id parameters. Unset one of these to fix this warning."
fi
elif [ -z "${RAINFOREST_RUN_ID}" ] && [ -n "${RF_ENVIRONMENT_ID}" ] ; then
if echo "${RF_ENVIRONMENT_ID}" | grep -Eq '^[0-9]+$' ; then
RUN_COMMAND="${RUN_COMMAND} --environment-id ${RF_ENVIRONMENT_ID}"
else
error "environment_id not a positive integer (${RF_ENVIRONMENT_ID})"
fi
fi

# Validate execution_method / crowd
if [ -z "${RAINFOREST_RUN_ID}" ] ; then
if [ -n "${RF_EXECUTION_METHOD}" ] ; then
case "${RF_EXECUTION_METHOD}" in
automation) ;&
crowd) ;&
automation_and_crowd) ;&
on_premise)
RUN_COMMAND="${RUN_COMMAND} --execution-method ${RF_EXECUTION_METHOD}"
;;
*)
error "${RF_EXECUTION_METHOD} not in (automation crowd automation_and_crowd on_premise)"
;;
esac
fi

if [ -n "${RF_CROWD}" ] ; then
if [ -n "${RF_EXECUTION_METHOD}" ] ; then
error "Error: execution_method and crowd are mutually exclusive"
fi

case "${RF_CROWD}" in
default) ;&
automation) ;&
automation_and_crowd) ;&
on_premise_crowd)
RUN_COMMAND="${RUN_COMMAND} --crowd ${RF_CROWD}"
;;
*)
error "${RF_CROWD} not in (default automation automation_and_crowd on_premise_crowd)"
;;
esac
fi
fi

# Validate automation_max_retries
if [ -n "${RF_AUTOMATION_MAX_RETRIES}" ] ; then
if echo "${RF_AUTOMATION_MAX_RETRIES}" | grep -Eq '^[0-9]+$' ; then
RUN_COMMAND="${RUN_COMMAND} --automation-max-retries ${RF_AUTOMATION_MAX_RETRIES}"
else
error "automation_max_retries not a positive integer (${RF_AUTOMATION_MAX_RETRIES})"
fi
fi

# Set branch
if [ -n "${RF_BRANCH}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --branch \"${RF_BRANCH//\"/\\\"}\""
fi

# Set description
if [ -z "${RAINFOREST_RUN_ID}" ] && [ -n "${RF_DESCRIPTION}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --description \"${RF_DESCRIPTION//\"/\\\"}\""
elif [ -z "${RAINFOREST_RUN_ID}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --description \"${GITHUB_REPOSITORY} - ${GITHUB_REF_NAME} ${GITHUB_JOB} $(date -u +'%FT%TZ')\""
fi

# Set release
if [ -n "${RF_RELEASE}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --release \"${RF_RELEASE//\"/\\\"}\""
elif [ -z "${RAINFOREST_RUN_ID}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --release \"${GITHUB_SHA}\""
fi

# Set background
if [ -n "${RF_BACKGROUND}" ] ; then
RUN_COMMAND="${RUN_COMMAND} --background"
fi

echo "command=${RUN_COMMAND}" >> "$GITHUB_OUTPUT"

0 comments on commit 1a134f3

Please sign in to comment.