Skip to content

Commit af40635

Browse files
authored
feat: add support for replace forward slashes with hyphens (#412)
* feat: add support for replace forward slashes with hypens * Update test.yml * Update test.yml
1 parent c209967 commit af40635

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

.github/workflows/test.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,33 @@ jobs:
101101
echo "Is Default: ${{ steps.branch-names-strip_branch_prefix.outputs.is_default }}"
102102
echo "Is Tag: ${{ steps.branch-names-strip_branch_prefix.outputs.is_tag }}"
103103
echo "Current tag: ${{ steps.branch-names-strip_branch_prefix.outputs.tag }}"
104+
105+
- name: Run test replace_slashes_with_hyphens
106+
id: branch-names-replace-slashes
107+
uses: ./
108+
with:
109+
replace_slashes_with_hyphens: 'true'
110+
- name: Show output with replaced slashes
111+
run: |
112+
echo "Default Branch: ${{ steps.branch-names-replace-slashes.outputs.default_branch }}"
113+
echo "Current Branch: ${{ steps.branch-names-replace-slashes.outputs.current_branch }}"
114+
echo "Base Ref: ${{ steps.branch-names-replace-slashes.outputs.base_ref_branch }}"
115+
echo "Head Ref: ${{ steps.branch-names-replace-slashes.outputs.head_ref_branch }}"
116+
echo "Ref: ${{ steps.branch-names-replace-slashes.outputs.ref_branch }}"
117+
echo "Is Default: ${{ steps.branch-names-replace-slashes.outputs.is_default }}"
118+
echo "Is Tag: ${{ steps.branch-names-replace-slashes.outputs.is_tag }}"
119+
echo "Current tag: ${{ steps.branch-names-replace-slashes.outputs.tag }}"
120+
- name: Test slash replacement in branch names
121+
if: contains(github.event_name, 'pull_request')
122+
shell: bash
123+
run: |
124+
head_ref="${{ steps.branch-names-replace-slashes.outputs.head_ref_branch }}"
125+
if [[ "$head_ref" == *"/"* ]]; then
126+
echo "Slash found in head_ref_branch after replacement: $head_ref"
127+
exit 1
128+
fi
129+
current_branch="${{ steps.branch-names-replace-slashes.outputs.current_branch }}"
130+
if [[ "$current_branch" == *"/"* ]]; then
131+
echo "Slash found in current_branch after replacement: $current_branch"
132+
exit 1
133+
fi

action.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ inputs:
1010
description: 'The prefix that should be stripped from the branch e.g `release/` -> with a branch `release/1.0` -> returns `1.0`'
1111
default: ''
1212
required: false
13+
replace_slashes_with_hyphens:
14+
description: 'Replace forward slashes with hyphens in branch names'
15+
default: 'false'
16+
required: false
1317

1418
outputs:
1519
is_default:
@@ -48,6 +52,7 @@ runs:
4852
GITHUB_EVENT_BASE_REF: ${{ github.event.base_ref }}
4953
INPUTS_STRIP_TAG_PREFIX: ${{ inputs.strip_tag_prefix }}
5054
INPUTS_STRIP_BRANCH_PREFIX: ${{ inputs.strip_branch_prefix }}
55+
INPUTS_REPLACE_SLASHES: ${{ inputs.replace_slashes_with_hyphens }}
5156
run: |
5257
# "Set branch names..."
5358
if [[ "$GITHUB_REF" != "refs/tags/"* ]]; then
@@ -64,13 +69,25 @@ runs:
6469
REF_BRANCH=${REF_BRANCH/$INPUTS_STRIP_BRANCH_PREFIX/}
6570
HEAD_REF=${HEAD_REF/$INPUTS_STRIP_BRANCH_PREFIX/}
6671
72+
# Replace slashes with hyphens if enabled
73+
if [[ "$INPUTS_REPLACE_SLASHES" == "true" ]]; then
74+
BASE_REF=${BASE_REF//\//-}
75+
HEAD_REF=${HEAD_REF//\//-}
76+
REF_BRANCH=${REF_BRANCH//\//-}
77+
fi
78+
6779
echo "base_ref_branch=$(eval printf "%s" "$BASE_REF")" >> "$GITHUB_OUTPUT"
6880
echo "head_ref_branch=$(eval printf "%s" "$HEAD_REF")" >> "$GITHUB_OUTPUT"
6981
echo "ref_branch=$(eval printf "%s" "$REF_BRANCH")" >> "$GITHUB_OUTPUT"
7082
else
7183
BASE_REF=$(printf "%q" "$GITHUB_EVENT_BASE_REF")
7284
BASE_REF=${BASE_REF/refs\/heads\/$INPUTS_STRIP_TAG_PREFIX/}
7385
86+
# Replace slashes with hyphens if enabled
87+
if [[ "$INPUTS_REPLACE_SLASHES" == "true" ]]; then
88+
BASE_REF=${BASE_REF//\//-}
89+
fi
90+
7491
echo "base_ref_branch=$(eval printf "%s" "$BASE_REF")" >> "$GITHUB_OUTPUT"
7592
fi
7693
shell: bash
@@ -112,12 +129,18 @@ runs:
112129
env:
113130
GITHUB_REF: ${{ github.ref }}
114131
INPUTS_STRIP_TAG_PREFIX: ${{ inputs.strip_tag_prefix }}
132+
INPUTS_REPLACE_SLASHES: ${{ inputs.replace_slashes_with_hyphens }}
115133
run: |
116134
# "Set the tag name..."
117135
if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then
118136
REF=$(printf "%q" "$GITHUB_REF")
119137
TAG="${REF/refs\/tags\/$INPUTS_STRIP_TAG_PREFIX/}"
120138
139+
# Replace slashes with hyphens if enabled
140+
if [[ "$INPUTS_REPLACE_SLASHES" == "true" ]]; then
141+
TAG=${TAG//\//-}
142+
fi
143+
121144
echo "tag=$(eval printf "%s" "$TAG")" >> "$GITHUB_OUTPUT"
122145
echo "is_tag=true" >> "$GITHUB_OUTPUT"
123146
else

0 commit comments

Comments
 (0)