Skip to content

Commit

Permalink
Pdct 851/fail the auto tagging build during a pr if no semver boxes t…
Browse files Browse the repository at this point in the history
…icked (#2)
  • Loading branch information
katybaulch authored Mar 27, 2024
1 parent 6fef43b commit d8d2578
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
70 changes: 39 additions & 31 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -46,39 +67,26 @@ 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
new_tag=$(echo "${script_output}" | tail -n 1)
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"
9 changes: 7 additions & 2 deletions auto-tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion funcs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
}
18 changes: 17 additions & 1 deletion tests/test_auto_tag.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down Expand Up @@ -170,4 +186,4 @@ load '/opt/bats-test-helpers/lox-bats-mock/stub.bash'
[ "$status" -eq 0 ]
[ "$output" == "false" ]

}
}

0 comments on commit d8d2578

Please sign in to comment.