From d8d25789b0608f7c7566be0c68970d167ae80709 Mon Sep 17 00:00:00 2001 From: Katy Baulch <46493669+katybaulch@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:36:14 +0000 Subject: [PATCH] Pdct 851/fail the auto tagging build during a pr if no semver boxes ticked (#2) --- action.yml | 70 ++++++++++++++++++++++------------------ auto-tag.sh | 9 ++++-- funcs.sh | 7 +++- tests/test_auto_tag.bats | 18 ++++++++++- 4 files changed, 69 insertions(+), 35 deletions(-) diff --git a/action.yml b/action.yml index 222df2f..cb84bdf 100644 --- a/action.yml +++ b/action.yml @@ -24,17 +24,38 @@ runs: using: "composite" steps: - - name: Debug + - name: Set GitHub Path + shell: bash + env: + GITHUB_ACTION_PATH: ${{ github.action_path }} + run: | + echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH + + - uses: actions/checkout@v4 + - uses: fregante/setup-git-user@v2 + + - name: Determine new tag version from PR event + if: ${{ inputs.pr_body != '' && inputs.pr_number != '' }} shell: sh + id: determine_next_tag_from_pr_event + # User controlled input needs to be santitised beforehand e.g., by adding an + # intermediate env var to prevent the workflow being exposed to a critical + # command injection attack + env: + PR_BODY: "${{ inputs.pr_body }}" + PR_NUMBER: "${{ inputs.pr_number }}" run: | - echo ${{ inputs['PR_BODY'] }} - echo ${{ inputs['PR_NUMBER'] }} - echo ${{ inputs.PR_BODY }} - echo ${{ inputs.PR_NUMBER }} - echo ${{ inputs.PR_BODY == '' && inputs.PR_NUMBER == ''}} + echo "Determining whether user has selected an auto-tag option..." + script_output=$("${{ github.action_path }}/auto-tag.sh" "${PR_BODY}" "${PR_NUMBER}") + if $? != 0; then + exit 1 + fi + new_tag=$(echo "${script_output}" | tail -n 1) + echo "New tag: ${new_tag}" + echo "new_tag=${new_tag}" >> "$GITHUB_OUTPUT" - uses: actions/github-script@v7 - if: ${{ inputs.PR_BODY == '' && inputs.PR_NUMBER == '' }} + if: ${{ (inputs.pr_body == '' && inputs.pr_number == '') }} id: get_pr_data with: script: | @@ -46,16 +67,13 @@ runs: }) ).data[0]; - - uses: actions/checkout@v4 - - uses: fregante/setup-git-user@v2 - - - name: Determine new tag version from PR event - if: ${{ inputs.PR_BODY != '' && inputs.PR_NUMBER != ''}} + - name: Determine new tag version + if: ${{ (inputs.pr_body == '' && inputs.pr_number == '') && steps.get_pr_data.outputs.result && (fromJSON(steps.get_pr_data.outputs.result).number && fromJSON(steps.get_pr_data.outputs.result).body) }} shell: sh - id: determine_next_tag_from_pr_event + id: determine_next_tag_retrospectively run: | - echo "Checking whether tag version can be determined from PR body..." - script_output=$("${{ github.action_path }}/auto-tag.sh" "${{ inputs.PR_BODY }}" "${{ inputs.PR_NUMBER }}") + echo "Attempting to auto-tag merge commit" + script_output=$("${{ github.action_path }}/auto-tag.sh" "${{ fromJson(steps.get_pr_data.outputs.result).body }}" "${{ fromJson(steps.get_pr_data.outputs.result).number }}") if $? != 0; then exit 1 fi @@ -63,22 +81,12 @@ runs: echo "New tag: ${new_tag}" echo "new_tag=${new_tag}" >> "$GITHUB_OUTPUT" - - name: Determine new tag version - if: ${{ inputs.PR_BODY == '' && inputs.PR_NUMBER == '' }} - shell: sh + - name: Set new_tag + shell: bash id: determine_next_tag - # User controlled input needs to be santitised beforehand e.g., by adding an - # intermediate env var to prevent the workflow being exposed to a critical - # command injection attack - env: - PR_BODY_FROM_JSON: "${{ fromJson(steps.get_pr_data.outputs.result).body }}" - PR_NUMBER_FROM_JSON: "${{ fromJson(steps.get_pr_data.outputs.result).number }}" run: | - echo "Attempting to auto-tag merge commit" - script_output=$("${{ github.action_path }}/auto-tag.sh" "${PR_BODY_FROM_JSON}" "${PR_NUMBER_FROM_JSON}") - if $? != 0; then - exit 1 + if [[ "${{ (inputs.pr_body != '' && inputs.pr_number != '') }}" ]]; then + echo "new_tag=${{ steps.determine_next_tag_from_pr_event.outputs.new_tag }}" >> "$GITHUB_OUTPUT" + elif [[ "${{ (inputs.pr_body == '' && inputs.pr_number == '') }}" ]]; then + echo "new_tag=${{ steps.determine_next_tag_retrospectively.outputs.new_tag }}" >> "$GITHUB_OUTPUT" fi - new_tag=$(echo "${script_output}" | tail -n 1) - echo "New tag: ${new_tag}" - echo "new_tag=${new_tag}" >> "$GITHUB_OUTPUT" diff --git a/auto-tag.sh b/auto-tag.sh index a9b3137..c1541f6 100755 --- a/auto-tag.sh +++ b/auto-tag.sh @@ -15,6 +15,7 @@ echo "Latest tag: ${latest_tag}" pr_body="$1" # Get selected versioning checkboxes. +is_skip=$(is_skip_selected "${pr_body}") is_patch=$(is_patch_selected "${pr_body}") is_minor=$(is_minor_selected "${pr_body}") is_major=$(is_major_selected "${pr_body}") @@ -54,9 +55,13 @@ else fi # If multiple have been checked or none have been checked, don't auto tag. -if { [[ ${is_minor} == false ]] && [[ ${is_patch} == false ]] && [[ ${is_major} == false ]]; }; then +if { [[ ${is_minor} == false ]] && [[ ${is_patch} == false ]] && [[ ${is_major} == false ]] && [[ ${is_skip} == false ]]; }; then echo "No tag information found in body of pull request #${pr_number}. Auto-tagging failed..." exit 1 fi -echo "${new_tag}" +if [[ ${is_skip} == true ]]; then + echo "Skip" +else + echo "${new_tag}" +fi diff --git a/funcs.sh b/funcs.sh index 6511691..978a347 100755 --- a/funcs.sh +++ b/funcs.sh @@ -106,8 +106,13 @@ is_major_selected() { is_selected "$1" "${phrase}" } +is_skip_selected() { + local phrase="Skip auto-tagging" + is_selected "$1" "${phrase}" +} + get_latest_tag() { git fetch --prune --unshallow --tags --force # This is needed - without it no tags are found. latest_tag=$(git tag --list 'v*' --sort=-v:refname | head -n1) echo "${latest_tag}" -} +} \ No newline at end of file diff --git a/tests/test_auto_tag.bats b/tests/test_auto_tag.bats index eef9191..d99ce91 100644 --- a/tests/test_auto_tag.bats +++ b/tests/test_auto_tag.bats @@ -112,6 +112,14 @@ load '/opt/bats-test-helpers/lox-bats-mock/stub.bash' } +@test "is_skip_selected returns correctly when Skip checkbox checked" { + source /code/funcs.sh + run is_skip_selected "[x] Skip auto-tagging" + [ "$status" -eq 0 ] + [ "$output" == "true" ] + +} + # ------ @test "is_patch_selected returns false when Patch checkbox not found" { @@ -138,6 +146,14 @@ load '/opt/bats-test-helpers/lox-bats-mock/stub.bash' } +@test "is_skip_selected returns false when Skip checkbox not found" { + source /code/funcs.sh + run is_skip_selected "[x] cucumber" + [ "$status" -eq 0 ] + [ "$output" == "false" ] + +} + # ------ @test "is_selected returns correctly when checkbox checked correctly" { @@ -170,4 +186,4 @@ load '/opt/bats-test-helpers/lox-bats-mock/stub.bash' [ "$status" -eq 0 ] [ "$output" == "false" ] -} \ No newline at end of file +}