From 64489247aa833d990c6c325989b5ade4412a441d Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 4 Jul 2024 20:15:10 +0200 Subject: [PATCH 1/2] actions: run in development mode for debug purposes --- actions/default-app.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actions/default-app.ini b/actions/default-app.ini index 4f30f27..e4577a5 100644 --- a/actions/default-app.ini +++ b/actions/default-app.ini @@ -1,4 +1,4 @@ -RUN_MODE = prod +RUN_MODE = dev WORK_PATH = forgejo-work-path [server] From 05a341e20141bc04ee916bc330a8462cfdd3a088 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 4 Jul 2024 20:15:32 +0200 Subject: [PATCH 2/2] actions: add an example for workflow-dispatch /repos/{owner}/{repo}/actions/workflows/{workflowname}/dispatches * fails if the required inputs are not provided * sets defaults to inputs that are not provided The type of inputs are only used for building the web UI interface, not for validation. All values are strings. --- actions/actions.sh | 6 ++ .../.forgejo/workflows/test.yml | 57 +++++++++++++++++ actions/example-workflow-dispatch/run.sh | 62 +++++++++++++++++++ actions/example-workflow-dispatch/setup.sh | 1 + 4 files changed, 126 insertions(+) create mode 100644 actions/example-workflow-dispatch/.forgejo/workflows/test.yml create mode 100755 actions/example-workflow-dispatch/run.sh create mode 100755 actions/example-workflow-dispatch/setup.sh diff --git a/actions/actions.sh b/actions/actions.sh index 4d1f2ef..d06027f 100755 --- a/actions/actions.sh +++ b/actions/actions.sh @@ -118,5 +118,11 @@ function test_actions() { run actions_verify_example $example done fi + + if dpkg --compare-versions $version ge 8.0 ; then + for example in workflow-dispatch ; do + run actions_verify_example $example + done + fi done } diff --git a/actions/example-workflow-dispatch/.forgejo/workflows/test.yml b/actions/example-workflow-dispatch/.forgejo/workflows/test.yml new file mode 100644 index 0000000..186fe56 --- /dev/null +++ b/actions/example-workflow-dispatch/.forgejo/workflows/test.yml @@ -0,0 +1,57 @@ +on: + workflow_dispatch: + inputs: + logLevel: + description: 'Log Level' + required: true + default: 'warning' + type: choice + options: + - info + - warning + - debug + tags: + description: 'Test scenario tags' + required: false + type: boolean + boolean_default_true: + description: 'Test scenario tags' + required: true + type: boolean + default: true + boolean_default_false: + description: 'Test scenario tags' + required: false + type: boolean + default: false + number1_default: + description: 'Number w. default' + default: '100' + type: number + number2: + description: 'Number w/o. default' + type: number + string1_default: + description: 'String w. default' + default: 'Hello world' + type: string + string2: + description: 'String w/o. default' + required: true + type: string + +jobs: + test: + runs-on: docker + container: + image: code.forgejo.org/oci/debian:bookworm + options: "--volume /srv/example:/srv/example" + + steps: + - name: save and display context + run: | + d=/srv/example/workflow-dispatch/contexts/$GITHUB_EVENT_NAME + mkdir -p $d + tee $d/github <<'EOF' + ${{ toJSON(github) }} + EOF diff --git a/actions/example-workflow-dispatch/run.sh b/actions/example-workflow-dispatch/run.sh new file mode 100755 index 0000000..ed55070 --- /dev/null +++ b/actions/example-workflow-dispatch/run.sh @@ -0,0 +1,62 @@ +TMPDIR=$(mktemp -d) + +trap "rm -fr $TMPDIR" EXIT + +source $EXAMPLE_DIR/../../lib/lib.sh + +export d=/srv/example/tag +context=/srv/example/workflow-dispatch/contexts/workflow_dispatch/github +api=$url/api/v1 +repo=root/example-$example + +function context_wait() { + if ! forgejo.sh retry test -f $context ; then + cat "$FORGEJO_RUNNER_LOGS" + false + fi +} + +function verify_required() { + local actual=$(forgejo-curl.sh api_json -w '%{http_code}' --data '{"ref":"main","inputs":{}}' $api/repos/$repo/actions/workflows/test.yml/dispatches) + local expected=400 + if test "$actual" != $expected ; then + log_info "dispatch is expected to fail with status $expected because of string2 is a required value but got status $actual instead" + return 1 + fi +} + +function verify_inputs() { + local inputs='{"string2":"value2"}' + forgejo-curl.sh api_json --data '{"ref":"main","inputs":'$inputs'}' $api/repos/$repo/actions/workflows/test.yml/dispatches + cat > $TMPDIR/expected <<'EOF' + { + "boolean_default_false": "false", + "boolean_default_true": "true", + "logLevel": "warning", + "number1_default": "100", + "string1_default": "Hello world", + "string2": "value2" + } +EOF +} + +function run_tests() { + verify_required + + npm --silent install json-diff + verify_inputs + context_wait + node_modules/.bin/json-diff <(jq .event.inputs < $context) $TMPDIR/expected +} + +function main() { + mkdir -p $d + + forgejo-test-helper.sh push_workflow actions/example-$example $url root example-$example setup-forgejo $token + + run_tests + + test "workflow_dispatch" = "$(jq -r .event_name < $context)" +} + +main diff --git a/actions/example-workflow-dispatch/setup.sh b/actions/example-workflow-dispatch/setup.sh new file mode 100755 index 0000000..402b08c --- /dev/null +++ b/actions/example-workflow-dispatch/setup.sh @@ -0,0 +1 @@ +mkdir -p /srv/example/workflow-dispatch